File tree 6 files changed +23
-16
lines changed
6 files changed +23
-16
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ Library llama_man
19
19
Path: lib_src
20
20
CompiledObject: best
21
21
Modules: Gcc_types,
22
+ Gcc_internals,
22
23
Gcc_compiler
23
24
if flag(strict)
24
25
NativeOpt: -w @a-4 -warn-error -a
Original file line number Diff line number Diff line change @@ -3,10 +3,11 @@ open Sexplib.Std
3
3
4
4
open Gcc_types
5
5
6
+
6
7
let () =
7
8
try
8
9
let sexp = Sexp. input_sexp stdin in
9
- let expr = expr_of_sexp sexp in
10
+ let (globals, expr) = program_of_sexp sexp in
10
11
let open Gcc_compiler in
11
- compile expr |> assemble |> print_endline
12
+ compile ~globals expr |> assemble |> print_endline
12
13
with e -> sexp_of_exn e |> Sexp. to_string |> print_endline
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ open Core_kernel.Std
3
3
open Gcc_types
4
4
5
5
type env = {
6
- vars : var list ;
6
+ vars : ident list ;
7
7
parent : env option ;
8
8
}
9
9
@@ -22,15 +22,12 @@ let rec lookup {vars; parent} v =
22
22
let (e, i) = lookup env v in
23
23
(succ e, i)
24
24
25
+
25
26
type state = {
26
27
functions : code list ;
27
- env : env ;
28
+ env : env
28
29
}
29
30
30
- let initial_state = {
31
- functions = [] ;
32
- env = {vars = [" world" ; " undocumented" ]; parent = None }
33
- }
34
31
35
32
let add_fn label code ret {functions; env} =
36
33
let wrapped_code = LABEL label :: code @ [ret] in
@@ -104,7 +101,12 @@ and compile_func id formals expr {functions; env} =
104
101
let code, {functions = fns1; _} = compile_expr expr e_state in
105
102
add_fn id code RTN {functions = fns1; env= env}
106
103
107
- let compile expr =
104
+ let compile ?(globals =[] ) expr =
105
+ let initial_state = {
106
+ functions = [] ;
107
+ env = {vars = [" world" ; " undocumented" ]; parent = None }
108
+ } in
109
+
108
110
let (code, state) = compile_expr expr initial_state in
109
111
code @ [RTN ] @ List. concat state.functions
110
112
Original file line number Diff line number Diff line change
1
+ open Gcc_types
2
+
3
+ let scope ~name ~var expr = Call (Fn ([name], expr), [var])
Original file line number Diff line number Diff line change @@ -36,7 +36,7 @@ with sexp_of
36
36
37
37
type code = instruction list
38
38
39
- type var = string with sexp
39
+ type ident = string with sexp
40
40
41
41
type expr =
42
42
| Const of int
@@ -45,14 +45,16 @@ type expr =
45
45
| Mul of expr * expr
46
46
| Div of expr * expr
47
47
| Cons of expr * expr
48
- | Fn of var list * expr
48
+ | Fn of ident list * expr
49
49
| Call of expr * expr list
50
- | Letrec of (var * expr ) list * expr
50
+ | Letrec of (ident * expr ) list * expr
51
51
| If of expr * expr * expr
52
52
| Eq of expr * expr
53
53
| Gt of expr * expr
54
54
| Gte of expr * expr
55
- | Var of var
55
+ | Var of ident
56
56
| Car of expr
57
57
| Cdr of expr
58
58
with sexp
59
+
60
+ type program = ((ident * expr ) list ) * expr with sexp
Original file line number Diff line number Diff line change @@ -3,9 +3,7 @@ open Core_kernel.Std
3
3
open OUnit2
4
4
5
5
open Gcc_types
6
-
7
-
8
- let scope name var expr = Call (Fn ([name], expr), [var])
6
+ open Gcc_internals
9
7
10
8
11
9
let test_gcc ~ast ~path =
You can’t perform that action at this time.
0 commit comments