Skip to content

Commit

Permalink
Add bitmaps support (Haskell backend).
Browse files Browse the repository at this point in the history
Also, mark some incorrect but unused syntax definitions as undefined.
  • Loading branch information
skvadrik committed Sep 21, 2024
1 parent da97854 commit 409dcab
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 20 deletions.
24 changes: 15 additions & 9 deletions bootstrap/src/default_syntax_haskell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const char* DEFAULT_SYNTAX_HASKELL =
"supported_api_styles = [\"free-form\"];\n"
"supported_code_models = [\"recursive-functions\"];\n"
"supported_targets = [\"code\", \"dot\"];\n"
"supported_features = [\"nested-ifs\", \"monadic\", \"tags\", \"captvars\"];\n"
"supported_features = [\"nested-ifs\", \"bitmaps\", \"monadic\", \"tags\", \"captvars\"];\n"
"\n"
"\n"
"// language-specific options ---------------------------------------------------\n"
Expand Down Expand Up @@ -161,13 +161,19 @@ const char* DEFAULT_SYNTAX_HASKELL =
"code:const_global = topindent name \" = \" init nl;\n"
"\n"
"code:array_local = <undefined>;\n"
"code:array_global = <undefined>;\n"
"code:array_elem = <undefined>;\n"
"\n"
"code:type_int = \"int\";\n"
"code:type_uint = \"uint\";\n"
"code:type_cond_enum = (storable_state? \"int\" : \"uint\");\n"
"code:type_yybm = <undefined>;\n"
"code:array_global =\n"
" topindent name \" :: Array Int \" type nl\n"
" topindent name \" = array (0, \" size \" - 1) $ Prelude.zip [0 .. \" size \" - 1] [\" nl indent\n"
" [row{0:-2}: topindent [elem{0:-2}: elem \", \"] [elem{-1}: elem \",\"] nl]\n"
" [row{-1}: topindent [elem{0:-2}: elem \", \"] [elem{-1}: elem \"]\"] nl];\n"
"\n"
"code:array_elem = array \" ! \" index;\n"
"\n"
"code:type_int = <undefined>;\n"
"code:type_uint = <undefined>;\n"
"code:type_cond_enum = <undefined>;\n"
"code:type_yybm = \"Word8\";\n"
"code:type_yytarget = <undefined>;\n"
"\n"
"code:assign = <undefined>;\n"
Expand Down Expand Up @@ -406,7 +412,7 @@ const char* DEFAULT_SYNTAX_HASKELL =
" ? YYCURSOR \" >= \" YYLIMIT // YYFILL check can only be used with EOF rule $\n"
" : YYLESSTHAN);\n"
"\n"
"code:yybm_filter = <undefined>;\n"
"code:yybm_filter = \"(\" yych \" .&. complement 0xFF) /= 0\";\n"
"\n"
"code:yybm_match = <undefined>;\n"
"code:yybm_match = \"(\" yybm \" ! (\" offset \" + fromIntegral \" yych \") .&. \" mask \") /= 0\";\n"
;
24 changes: 15 additions & 9 deletions include/syntax/haskell
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ supported_apis = ["record", "generic"];
supported_api_styles = ["free-form"];
supported_code_models = ["recursive-functions"];
supported_targets = ["code", "dot"];
supported_features = ["nested-ifs", "monadic", "tags", "captvars"];
supported_features = ["nested-ifs", "bitmaps", "monadic", "tags", "captvars"];


// language-specific options ---------------------------------------------------
Expand Down Expand Up @@ -159,13 +159,19 @@ code:const_local = topindent "let " name " = " init " in" nl;
code:const_global = topindent name " = " init nl;

code:array_local = <undefined>;
code:array_global = <undefined>;
code:array_elem = <undefined>;

code:type_int = "int";
code:type_uint = "uint";
code:type_cond_enum = (storable_state? "int" : "uint");
code:type_yybm = <undefined>;
code:array_global =
topindent name " :: Array Int " type nl
topindent name " = array (0, " size " - 1) $ Prelude.zip [0 .. " size " - 1] [" nl indent
[row{0:-2}: topindent [elem{0:-2}: elem ", "] [elem{-1}: elem ","] nl]
[row{-1}: topindent [elem{0:-2}: elem ", "] [elem{-1}: elem "]"] nl];

code:array_elem = array " ! " index;

code:type_int = <undefined>;
code:type_uint = <undefined>;
code:type_cond_enum = <undefined>;
code:type_yybm = "Word8";
code:type_yytarget = <undefined>;

code:assign = <undefined>;
Expand Down Expand Up @@ -404,6 +410,6 @@ code:yylessthan =
? YYCURSOR " >= " YYLIMIT // YYFILL check can only be used with EOF rule $
: YYLESSTHAN);

code:yybm_filter = <undefined>;
code:yybm_filter = "(" yych " .&. complement 0xFF) /= 0";

code:yybm_match = <undefined>;
code:yybm_match = "(" yybm " ! (" offset " + fromIntegral " yych ") .&. " mask ") /= 0";
86 changes: 86 additions & 0 deletions test/codegen/haskell/01_basic_b.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
-- Generated by re2hs
{-# LANGUAGE RecordWildCards #-}
-- re2hs $INPUT -o $OUTPUT -ib
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wno-unused-record-wildcards #-}

import Data.Array (Array, array, (!))
import Data.Bits ((.&.))
import Data.Word (Word8)
import Data.ByteString (ByteString, index)

data State = State {
_yyinput :: ByteString,
_yycursor :: Int
}


yy0 :: State -> Bool
yy0 State{..} =
let yych = index _yyinput _yycursor in
let __ = _yycursor + 1 in let _yycursor = __ in
if yych <= 0x30 then yy1 State{..}
else if yych <= 0x39 then yy2 State{..}
else yy1 State{..}

yy1 :: State -> Bool
yy1 State{..} =
False

yy2 :: State -> Bool
yy2 State{..} =
let yych = index _yyinput _yycursor in
if (yybm ! (0 + fromIntegral yych) .&. 128) /= 0 then
let __ = _yycursor + 1 in let _yycursor = __ in
yy2 State{..}
else
yy3 State{..}

yy3 :: State -> Bool
yy3 State{..} =
True

lexer :: State -> Bool
lexer State{..} =
yy0 State{..}

yybm :: Array Int Word8
yybm = array (0, 256 - 1) $ Prelude.zip [0 .. 256 - 1] [
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0]


main :: IO ()
main = case lexer State{_yyinput = "1234\0", _yycursor = 0} of
True -> return ()
False -> error "lexer failed!"
28 changes: 28 additions & 0 deletions test/codegen/haskell/01_basic_b.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- re2hs $INPUT -o $OUTPUT -ib
{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wno-unused-record-wildcards #-}

import Data.Array (Array, array, (!))
import Data.Bits ((.&.))
import Data.Word (Word8)
import Data.ByteString (ByteString, index)

data State = State {
_yyinput :: ByteString,
_yycursor :: Int
}

%{
re2c:define:YYFN = ["lexer;Bool", "State{..};State"];
re2c:yyfill:enable = 0;

number = [1-9][0-9]*;

number { True }
* { False }
%}

main :: IO ()
main = case lexer State{_yyinput = "1234\0", _yycursor = 0} of
True -> return ()
False -> error "lexer failed!"
1 change: 0 additions & 1 deletion test/codegen/haskell/unsupported_bitmaps.hs

This file was deleted.

1 change: 0 additions & 1 deletion test/codegen/haskell/unsupported_bitmaps.re

This file was deleted.

0 comments on commit 409dcab

Please sign in to comment.