Skip to content

Commit

Permalink
✨ SEQ parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeta611 committed Jul 5, 2024
1 parent d4f91b0 commit 540c902
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions .ocamlformat-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
samples/*
4 changes: 2 additions & 2 deletions lib/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ rule read =
| '[' { LBRACK }
| ']' { RBRACK }
| ',' { COMMA }
| ';' { SEMICOLON }
| ";;" { COMPEND }
| ';' { SEMI }
| ";;" { SEMISEMI }
| eof { EOF }
| _ { raise (SyntaxError ("Unexpected char: " ^ lexeme lexbuf)) }

Expand Down
15 changes: 12 additions & 3 deletions lib/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ open Expr
%token AND OR
%token PLUS MINUS TIMES
%token LPAREN RPAREN LBRACK RBRACK
%token RARROW COMMA SEMICOLON COMPEND
%token RARROW COMMA SEMI SEMISEMI
%token EOF

%nonassoc RARROW
%nonassoc IN
%right SEMI
%nonassoc EFF
%nonassoc THEN /* below ELSE (if ... then ...) */
%nonassoc ELSE /* (if ... then ... else ...) */
%right OR
Expand All @@ -31,7 +35,7 @@ prog:
| prog = comp_lst; EOF { prog }
comp_lst:
| e = expr { Expr (hook_free_exn e) }
| c = comp_expr; COMPEND; tl = comp_lst { Comp (c, tl) } ;
| c = comp_expr; SEMISEMI; tl = comp_lst { Comp (c, tl) } ;
comp_expr:
| LET; name = var; param = var; EQ; body = expr { { name; param; body = hook_full body } }
expr:
Expand All @@ -44,12 +48,17 @@ expr:
| STT; stt = var; COMMA; set = var; EQ; init = expr; IN; body = expr
{ Ex (Stt { label = -1; stt; set; init = hook_free_exn init; body = hook_full body }) }
| EFF; e = expr { Ex (Eff (hook_free_exn e)) }
| VIEW; LBRACK; vss = separated_nonempty_list(SEMICOLON, expr); RBRACK { Ex (View (List.map hook_free_exn vss)) }
| VIEW; LBRACK; vss = separated_nonempty_list(COMMA, expr); RBRACK { Ex (View (List.map hook_free_exn vss)) }
| fn = atom; arg = atom { Ex (App { fn = hook_free_exn fn; arg = hook_free_exn arg }) }
| IF; pred = expr; THEN; con = expr; ELSE; alt = expr
{ Ex (Cond { pred = hook_free_exn pred; con = hook_free_exn con; alt = hook_free_exn alt }) }
| IF; pred = expr; THEN; con = expr
{ Ex (Cond { pred = hook_free_exn pred; con = hook_free_exn con; alt = Const Unit }) }
| e1 = expr; SEMI; e2 = expr
{ match hook_free e1, hook_free e2 with
| Some e1, Some e2 -> Ex (Seq (e1, e2))
| _, _ -> Ex (Seq (hook_full e1, hook_full e2))
}
bop_expr:
| atom { $1 }
| left = bop_expr; op = op; right = bop_expr
Expand Down
2 changes: 1 addition & 1 deletion samples/simple.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let C x =
stt s, setS = 42 in
# eff (setS (fun s -> 43));
eff (setS (fun s -> 43));
view [()]
;;
view [C ()]

0 comments on commit 540c902

Please sign in to comment.