diff --git a/lib/compiler.ml b/lib/compiler.ml new file mode 100644 index 0000000..6c86643 --- /dev/null +++ b/lib/compiler.ml @@ -0,0 +1,43 @@ +open Ppx_compare_lib.Builtin +open Sexplib.Std + +type ocaml_type = + | Unspecified + | Int + | Float + | Bool + [@@deriving compare, sexp] + +type ocaml_expr = + | None_ + | Int of int + | Bool of bool + | Symbol of { + name: string; + type_: ocaml_type; + } + | Let of { + rec_: bool; + name: string; + body: ocaml_expr list; + in_: ocaml_expr; + } + | Fun of { + params: ocaml_expr list; + body: ocaml_expr list; + } + [@@deriving compare, sexp] + +let compile node = + match node with + | Parser.Bool (value) -> Bool value + | Parser.Func (params, body) -> 0 + | Parser.NumberInt (value) -> Int value + (* | Parser.NumberFloat (_) *) + (* | Parser.String_ (_) -> true *) + | _ -> failwith "compile unreachable code" +let%test_unit "compile" = + ([%test_eq: ocaml_expr] + (compile (Parser.NumberInt 2)) + (Int 2)) + diff --git a/lib/dune b/lib/dune index af777f5..9cff243 100644 --- a/lib/dune +++ b/lib/dune @@ -7,5 +7,5 @@ ppx_assert ppx_compare ppx_sexp_conv)) - (modules Tokenizer Parser Evaluator) + (modules Tokenizer Parser Evaluator Compiler) (libraries str sexplib))