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 () = let () =
try 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 Format.fprintf Format.std_formatter "%a\n" Spice.Syn.pp_modl modl
with Spice.Error.Error err -> with Spice.Error.Error err ->
Format.fprintf Format.err_formatter "error: %a\n" Spice.Error.pp 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 "val" Kw_val;
Hashtbl.add keywords "fun" Kw_fun; Hashtbl.add keywords "fun" Kw_fun;
Hashtbl.add keywords "obj" Kw_obj; Hashtbl.add keywords "obj" Kw_obj;
Hashtbl.add keywords "if" Kw_if;
Hashtbl.add keywords "else" Kw_else;
end end
let is_digit ch = ch >= '0' && ch <= '9' let is_digit ch = ch >= '0' && ch <= '9'

View File

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

View File

@ -34,6 +34,7 @@ and exp =
| Path of path | Path of path
| Call of path * exp list | Call of path * exp list
| Binop of binop * exp * exp | Binop of binop * exp * exp
| If of exp * exp * exp
| Fun of params * exp | Fun of params * exp
| Obj of block | Obj of block
| Scope of block | Scope of block
@ -83,6 +84,11 @@ let rec pp_exp ppf = function
| Path (Var x) -> pf ppf "{\"var\":%S}" x | Path (Var x) -> pf ppf "{\"var\":%S}" x
| Path (Ele (e, x)) -> pf ppf "{\"ele\":%a,\"field\":%S}" pp_exp e 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) | 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) -> | Binop (op, e1, e2) ->
pf pf
ppf ppf