Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
95 changes: 60 additions & 35 deletions Driver.ml
Original file line number Diff line number Diff line change
@@ -1,35 +1,60 @@
open Language
open Expr
open Stmt

let inc x = x+1

let ( ! ) x = Var x
let const n = Const n
let ( + ) x y = Add (x, y)
let ( * ) x y = Mul (x, y)

let read x = Read x
let write e = Write e
let (:=) x e = Assign (x, e)
let skip = Skip
let (|>) l r = Seq (l, r)

let parse filename =
read "x" |>
read "y" |>
read "z" |>
("z" := !"z" * (!"x" * !"y" + const 1)) |>
write !"z"

let _ =
match Sys.argv with
| [|_; filename|] ->
let basename = Filename.chop_suffix filename ".expr" in
let text = X86.compile (parse filename) in
let asm = basename ^ ".s" in
let ouch = open_out asm in
Printf.fprintf ouch "%s\n" text;
close_out ouch;
let runtime = try Sys.getenv "RUNTIME" with _ -> "../runtime" in
Sys.command (Printf.sprintf "gcc -m32 -o %s %s/runtime.o %s.s" basename runtime basename)
open Language
open Expr
open Stmt
open Ostap
open GT

ostap (
expr : expr "+" expr
)

let rec parse s =
expr id
[|
left , [ostap ("+"), (fun x y -> `Add (x, y)); ostap ("-"), (fun x y -> `Sub (x, y))];
left , [ostap ("*"), (fun x y -> `Mul (x, y)); ostap ("/"), (fun x y -> `Div (x, y))]
|]
primary
s
and ostap (primary: n: DECIMAL {Const n}
| e:IDENT {Var e}
| -"(" parse -")")

ostap (
simp: x:IDENT ":=" e:expr {Assign (x, e)}
| %"read" "(" x:IDENT ")" {Read x}
| %"write" "(" e:expr ")" {Write e}
| %"skip" {Skip};

stmt: s:simp ";" d:stmt {Seq (s,d)}
| simp
)

let parse filename =
let s = Util.read filename in
Util.parse
(object
inherit Matcher.t s
inherit Util.Lexers.ident ["read"; "write"; "skip"] s
inherit Util.Lexers.decimal s
inherit Util.Lexers.skip [
Matcher.Skip.whitespaces " \t\n"
] s
end)
(ostap (stmt -EOF))

let _ =
match Sys.argv with
| [|_; filename|] ->
match parse filename with
| `Ok stmt ->
let basename = Filename.chop_suffix filename ".expr" in
let text = X86.compile stmt in
Printf.printf "%s\n" (show (Stmt.t) stmt);
let asm = basename ^ ".s" in
let ouch = open_out asm in
Printf.fprintf ouch "%s\n" text;
close_out ouch;
let runtime = try Sys.getenv "RUNTIME" with _ -> "../runtime" in
ignore @@ Sys.command (Printf.sprintf "gcc -m32 -o %s %s/runtime.o %s.s" basename runtime basename)
| `Fail e -> Printf.eprintf "Parsing error: %s\n" e
59 changes: 59 additions & 0 deletions Driver.ml~
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
open Language
open Expr
open Stmt
open Ostap
open GT

ostap (
expr : expr "+" expr
)

ostap (
expr: x:mull "+" y:expr {Add (x,y)}
| mull;
mull: x:prim "*" y:mull {Mul (x,y)}
| prim;
prim: n:DECIMAL {Const n}
| e:IDENT {Var e}
| -"(" expr -")"
(* | "(" e:expr ")" {e} *)
)

ostap (
simp: x:IDENT ":=" e:expr {Assign (x, e)}
| %"read" "(" x:IDENT ")" {Read x}
| %"write" "(" e:expr ")" {Write e}
| %"skip" {Skip};

stmt: s:simp ";" d:stmt {Seq (s,d)}
| simp
)

let parse filename =
let s = Util.read filename in
Util.parse
(object
inherit Matcher.t s
inherit Util.Lexers.ident ["read"; "write"; "skip"] s
inherit Util.Lexers.decimal s
inherit Util.Lexers.skip [
Matcher.Skip.whitespaces " \t\n"
] s
end)
(ostap (stmt -EOF))

let _ =
match Sys.argv with
| [|_; filename|] ->
match parse filename with
| `Ok stmt ->
let basename = Filename.chop_suffix filename ".expr" in
let text = X86.compile stmt in
Printf.printf "%s\n" (show (Stmt.t) stmt);
let asm = basename ^ ".s" in
let ouch = open_out asm in
Printf.fprintf ouch "%s\n" text;
close_out ouch;
let runtime = try Sys.getenv "RUNTIME" with _ -> "../runtime" in
ignore @@ Sys.command (Printf.sprintf "gcc -m32 -o %s %s/runtime.o %s.s" basename runtime basename)
| `Fail e -> Printf.eprintf "Parsing error: %s\n" e
49 changes: 0 additions & 49 deletions Interpret.ml

This file was deleted.

34 changes: 0 additions & 34 deletions Language.ml

This file was deleted.

33 changes: 19 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
all: Driver.cmo
ocamlc -rectypes -o compiler -I `ocamlfind -query GT` -I `ocamlfind -query ostap` -I `ocamlfind -query re.str` re.cma re_emacs.cma re_str.cma GT.cma ostap.cmo Language.cmo Interpret.cmo StackMachine.cmo X86.cmo Driver.cmo
MKDIR ?= mkdir -vp
CP ?= cp

Language.cmo: Language.ml
ocamlc -rectypes -c -pp "camlp5o -I `ocamlfind -query GT.syntax.all` pa_gt.cmo -I `ocamlfind -query ostap` pa_ostap.cmo -L `ocamlfind -query GT.syntax.all`" -I `ocamlfind -query GT` -I `ocamlfind -query ostap` GT.cma $<
OB=ocamlbuild -cflag -g -no-hygiene -use-ocamlfind -plugin-tag "package(str)" -classic-display
ifdef OBV
OB += -verbose 6
endif

Interpret.cmo: Interpret.ml Language.cmo
ocamlc -rectypes -c -pp "camlp5o -I `ocamlfind -query GT.syntax.all` pa_gt.cmo -I `ocamlfind -query ostap` pa_ostap.cmo -L `ocamlfind -query GT.syntax.all`" -I `ocamlfind -query GT` -I `ocamlfind -query ostap` GT.cma $<
BYTE_TARGETS=src/rc.byte
NATIVE_TARGETS=src/rc.native

StackMachine.cmo: StackMachine.ml Language.cmo Interpret.cmo
ocamlc -rectypes -c -pp "camlp5o -I `ocamlfind -query GT.syntax.all` pa_gt.cmo -I `ocamlfind -query ostap` pa_ostap.cmo -L `ocamlfind -query GT.syntax.all`" -I `ocamlfind -query GT` -I `ocamlfind -query ostap` GT.cma $<
.PHONY: all clean runtime

X86.cmo: X86.ml StackMachine.cmo
ocamlc -rectypes -c -pp "camlp5o -I `ocamlfind -query GT.syntax.all` pa_gt.cmo -I `ocamlfind -query ostap` pa_ostap.cmo -L `ocamlfind -query GT.syntax.all`" -I `ocamlfind -query GT` -I `ocamlfind -query ostap` GT.cma $<
.DEFAULT_GOAL: all

Driver.cmo: Driver.ml StackMachine.cmo X86.cmo Interpret.cmo
ocamlc -rectypes -c -pp "camlp5o -I `ocamlfind -query GT.syntax.all` pa_gt.cmo -I `ocamlfind -query ostap` pa_ostap.cmo -L `ocamlfind -query GT.syntax.all`" -I `ocamlfind -query GT` -I `ocamlfind -query ostap` GT.cma Driver.ml
all: main runtime

clean:
rm -Rf *~ *.cmo compiler
runtime:
cd runtime && make all && cd ..

main:
$(OB) -Is src $(BYTE_TARGETS) $(NATIVE_TARGETS)

clean:
cd runtime && make clean && cd .. && $(RM) -r _build *.log *.native *.byte
25 changes: 25 additions & 0 deletions Makefile~
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
MKDIR ?= mkdir -vp
CP ?= cp

OB=ocamlbuild -cflag -g -no-hygiene -use-ocmalfind -plugin-tag "package(str)" -classic-display
ifdef OBV
OB += -verbose 6
endif

BYTE_TARGETS=src/rc.byte
NATIVE_TARGETS=src/rc.native

.PHONY: all clean runtime

.DEFAULT_GOAL: all

all: main runtime

runtime:
cd runtime && make all && cd ..

main:
$(OB) -Is src $(BYTE_TARGETS) $(NATIVE_TARGETS)

clean:
cd runtime && make clean && cd .. && $(RM) -r _build *.log *.native *.byte
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
Dependencies:

$ opam pin add GT https://github.com/Kakadu/GT.git -n -y
`$ opam pin add GT https://github.com/Kakadu/GT.git -n -y`

$ opam install camlp5 -y
`$ opam install camlp5 -y`

$ opam install GT ocamlfind -y
`$ opam install GT ocamlfind -y`

Build & use:

$ make
`$ make`

$ gcc -m32 -c runtime.c
`$ gcc -m32 -c runtime.c`

$ RUNTIME=. ./compiler file.expr
`$ RUNTIME=. ./compiler file.expr`

$ ./file
`$ ./file`

If make fails with an error which states that GT.Syntax.All cannot be found, make sure you initialized and configured opam:

$ opam init
`$ opam init`

$ eval \`opam config env\`
``$ eval `opam config env` ``

Ostap library:

`$ opam pin add ostap https://github.com/dboulytchev/ostap.git -n -y`

`$ opam install ostap -y`
Loading