Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make compilation (--compile ...) works #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions lib/compiler.ml
Original file line number Diff line number Diff line change
@@ -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))

2 changes: 1 addition & 1 deletion lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
ppx_assert
ppx_compare
ppx_sexp_conv))
(modules Tokenizer Parser Evaluator)
(modules Tokenizer Parser Evaluator Compiler)
(libraries str sexplib))