add if/else

This commit is contained in:
tali 2023-11-23 23:18:49 -05:00
parent 466b224e65
commit dc3a0368b3
4 changed files with 15 additions and 7 deletions

View File

@ -1,6 +1,6 @@
let () =
try
let modl = Spice.parse "fun main(x) { val y = x + 2 IO.print(y * x) }" in
let modl = Spice.parse "fun main(x) { val y = x * 2 val z = if(y > x) y else 1 }" in
Format.fprintf Format.std_formatter "%a\n" Spice.Syn.pp_modl modl
with Spice.Error.Error err ->
Format.fprintf Format.err_formatter "error: %a\n" Spice.Error.pp err

View File

@ -9,6 +9,8 @@ let _ = begin
Hashtbl.add keywords "val" Kw_val;
Hashtbl.add keywords "fun" Kw_fun;
Hashtbl.add keywords "obj" Kw_obj;
Hashtbl.add keywords "if" Kw_if;
Hashtbl.add keywords "else" Kw_else;
end
let is_digit ch = ch >= '0' && ch <= '9'

View File

@ -14,6 +14,8 @@
%token Kw_val "val"
%token Kw_fun "fun"
%token Kw_obj "obj"
%token Kw_if "if"
%token Kw_else "else"
%token EOF
%start <Syn.modl> modl
@ -21,10 +23,6 @@
%{
open Syn
let maybe_call prefix = function
| None -> Path prefix
| Some args -> Call (prefix, args)
let infix_base e0 = (e0, [])
let infix_app (e0, ops) op e2 = (e0, (op, e2) :: ops)
@ -46,8 +44,10 @@ item:
exp:
| e = infixexp { resolve_fixity e }
| "if"; "("; e1 = exp; ")"; e2 = exp; "else"; e3 = exp { If (e1, e2, e3) }
| "fun"; p = params; e = exp { Fun (p, e) }
| "obj"; b = block { Obj b }
| b = block { Scope b }
infixexp:
| e0 = singleexp { infix_base e0 }
@ -55,8 +55,8 @@ infixexp:
singleexp:
| i = Int { Literal (Int i) }
| p = path; a = option(args) { maybe_call p a }
| b = block { Scope b }
| p = path { Path p }
| f = path; a = args { Call (f, a) }
path:
| x = Name { Var x }

View File

@ -34,6 +34,7 @@ and exp =
| Path of path
| Call of path * exp list
| Binop of binop * exp * exp
| If of exp * exp * exp
| Fun of params * exp
| Obj of block
| Scope of block
@ -83,6 +84,11 @@ let rec pp_exp ppf = function
| Path (Var x) -> pf ppf "{\"var\":%S}" x
| Path (Ele (e, x)) -> pf ppf "{\"ele\":%a,\"field\":%S}" pp_exp e x
| Call (fn, args) -> pf ppf "{\"call\":%a}" (pp_list pp_exp) (Path fn :: args)
| If (ec, et, ee) ->
pf ppf "{\"if\":%a,\"then\":%a,\"else\":%a}"
pp_exp ec
pp_exp et
pp_exp ee
| Binop (op, e1, e2) ->
pf
ppf