From 540c902e62f3756bf61e625e781b7fbf409cb18c Mon Sep 17 00:00:00 2001 From: Jay Lee Date: Fri, 5 Jul 2024 15:09:10 +0900 Subject: [PATCH] :sparkles: SEQ parsing --- .ocamlformat-ignore | 1 + lib/lexer.mll | 4 ++-- lib/parser.mly | 15 ++++++++++++--- samples/simple.ml | 2 +- 4 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 .ocamlformat-ignore diff --git a/.ocamlformat-ignore b/.ocamlformat-ignore new file mode 100644 index 0000000..781fb73 --- /dev/null +++ b/.ocamlformat-ignore @@ -0,0 +1 @@ +samples/* diff --git a/lib/lexer.mll b/lib/lexer.mll index e77a46f..d6d0beb 100644 --- a/lib/lexer.mll +++ b/lib/lexer.mll @@ -62,8 +62,8 @@ rule read = | '[' { LBRACK } | ']' { RBRACK } | ',' { COMMA } - | ';' { SEMICOLON } - | ";;" { COMPEND } + | ';' { SEMI } + | ";;" { SEMISEMI } | eof { EOF } | _ { raise (SyntaxError ("Unexpected char: " ^ lexeme lexbuf)) } diff --git a/lib/parser.mly b/lib/parser.mly index 367b7fb..2dd7366 100644 --- a/lib/parser.mly +++ b/lib/parser.mly @@ -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 @@ -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: @@ -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 diff --git a/samples/simple.ml b/samples/simple.ml index 179386d..7958474 100644 --- a/samples/simple.ml +++ b/samples/simple.ml @@ -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 ()]