Skip to content

Commit e984cc3

Browse files
committed
Getting ready for globals
1 parent 9c72b70 commit e984cc3

File tree

6 files changed

+23
-16
lines changed

6 files changed

+23
-16
lines changed

_oasis

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Library llama_man
1919
Path: lib_src
2020
CompiledObject: best
2121
Modules: Gcc_types,
22+
Gcc_internals,
2223
Gcc_compiler
2324
if flag(strict)
2425
NativeOpt: -w @a-4 -warn-error -a

bin_src/gcc.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ open Sexplib.Std
33

44
open Gcc_types
55

6+
67
let () =
78
try
89
let sexp = Sexp.input_sexp stdin in
9-
let expr = expr_of_sexp sexp in
10+
let (globals, expr) = program_of_sexp sexp in
1011
let open Gcc_compiler in
11-
compile expr |> assemble |> print_endline
12+
compile ~globals expr |> assemble |> print_endline
1213
with e -> sexp_of_exn e |> Sexp.to_string |> print_endline

lib_src/gcc_compiler.ml

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ open Core_kernel.Std
33
open Gcc_types
44

55
type env = {
6-
vars : var list;
6+
vars : ident list;
77
parent : env option;
88
}
99

@@ -22,15 +22,12 @@ let rec lookup {vars; parent} v =
2222
let (e, i) = lookup env v in
2323
(succ e, i)
2424

25+
2526
type state = {
2627
functions : code list;
27-
env : env;
28+
env : env
2829
}
2930

30-
let initial_state = {
31-
functions = [];
32-
env = {vars = ["world"; "undocumented"]; parent = None}
33-
}
3431

3532
let add_fn label code ret {functions; env} =
3633
let wrapped_code = LABEL label :: code @ [ret] in
@@ -104,7 +101,12 @@ and compile_func id formals expr {functions; env} =
104101
let code, {functions = fns1; _} = compile_expr expr e_state in
105102
add_fn id code RTN {functions = fns1; env=env}
106103

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+
108110
let (code, state) = compile_expr expr initial_state in
109111
code @ [RTN] @ List.concat state.functions
110112

lib_src/gcc_internals.ml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
open Gcc_types
2+
3+
let scope ~name ~var expr = Call (Fn ([name], expr), [var])

lib_src/gcc_types.ml

+6-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ with sexp_of
3636

3737
type code = instruction list
3838

39-
type var = string with sexp
39+
type ident = string with sexp
4040

4141
type expr =
4242
| Const of int
@@ -45,14 +45,16 @@ type expr =
4545
| Mul of expr * expr
4646
| Div of expr * expr
4747
| Cons of expr * expr
48-
| Fn of var list * expr
48+
| Fn of ident list * expr
4949
| Call of expr * expr list
50-
| Letrec of (var * expr) list * expr
50+
| Letrec of (ident * expr) list * expr
5151
| If of expr * expr * expr
5252
| Eq of expr * expr
5353
| Gt of expr * expr
5454
| Gte of expr * expr
55-
| Var of var
55+
| Var of ident
5656
| Car of expr
5757
| Cdr of expr
5858
with sexp
59+
60+
type program = ((ident * expr) list) * expr with sexp

lib_test/gcc_test.ml

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ open Core_kernel.Std
33
open OUnit2
44

55
open Gcc_types
6-
7-
8-
let scope name var expr = Call (Fn ([name], expr), [var])
6+
open Gcc_internals
97

108

119
let test_gcc ~ast ~path =

0 commit comments

Comments
 (0)