Skip to content

Commit

Permalink
Initial implementation of SyntaxMacroExpander
Browse files Browse the repository at this point in the history
  • Loading branch information
vipentti committed Dec 3, 2023
1 parent 9d53e0e commit 7b138d8
Show file tree
Hide file tree
Showing 16 changed files with 1,635 additions and 1,261 deletions.
8 changes: 1 addition & 7 deletions src/Visp.Compiler/Lexer.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,7 @@ rule token (args: LexArgs) (skip: bool) = parse
| ':' { COLON }
| ident {
let text = lexeme lexbuf
match tryGetKeyword text with
| Some (tok) -> tok
| None ->
if text.EndsWith('!') then
MACRO_NAME (text.TrimEnd('!'))
else
SYMBOL text
symbolOrKeyword text
}
| ',' { COMMA }
| _ { unexpected_char "token" lexbuf }
Expand Down
18 changes: 15 additions & 3 deletions src/Visp.Compiler/Parser.fsy
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let parse_error_rich = Some(fun (ctxt: ParseErrorContext<_>) ->
%}

%start start
%start raw_expr

%token EOF
%token LPAREN RPAREN
Expand Down Expand Up @@ -102,12 +103,15 @@ let parse_error_rich = Some(fun (ctxt: ParseErrorContext<_>) ->
%nonassoc EOF

%type <ParsedFile> start
%type <SynExpr> raw_expr

%%

// start of actual code
start: prog { $1 }

raw_expr: expr { $1 }

prog:
| file_fragments
{ ParsedFile($1) }
Expand Down Expand Up @@ -257,8 +261,12 @@ parens_expr:
| BANG_VEC expr_list { SynExpr.FsVec($2, lhs parseState)}

macro_call_expr:
| macro_call { SynExpr.SyntaxMacroCall($1) }

macro_call:
| macro_name macro_body_list
{ SynExpr.SyntaxMacroCall($1, $2, lhs parseState) }
{ let mNameBody = SynMacroBody.Symbol($1)
SynMacroCall($1, mNameBody :: $2, lhs parseState) }

syntax_macro_expr:
| syntax_macro { SynExpr.SyntaxMacro($1)}
Expand All @@ -273,11 +281,15 @@ rev_macro_cases:
| rev_macro_cases macro_case_start { $2 :: $1 }

macro_case_start:
| LPAREN macro_pat macro_body RPAREN
| LPAREN macro_case_pat macro_body RPAREN
{ SynMacroCase($2, $3, lhs parseState) }
| LBRACKET macro_pat macro_body RBRACKET
| LBRACKET macro_case_pat macro_body RBRACKET
{ SynMacroCase($2, $3, lhs parseState) }

macro_case_pat:
| LPAREN macro_pat_list RPAREN { $2 }
| LBRACKET macro_pat_list RBRACKET { $2 }

macro_pat_list: rev_macro_pat_list { List.rev $1 }
rev_macro_pat_list:
| macro_pat { [$1] }
Expand Down
Loading

0 comments on commit 7b138d8

Please sign in to comment.