Skip to content

Commit 803f42c

Browse files
committed
Stetskevich Roman 2381 Driver.ml
1 parent 84f595a commit 803f42c

2 files changed

Lines changed: 70 additions & 10 deletions

File tree

‎Driver.ml‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ ostap (
88
expr : expr "+" expr
99
)
1010

11-
ostap (
12-
expr: x:mull "+" y:expr {Add (x,y)}
13-
| mull;
14-
mull: x:prim "*" y:mull {Mul (x,y)}
15-
| prim;
16-
prim: n:DECIMAL {Const n}
17-
| e:IDENT {Var e}
18-
| -"(" expr -")"
19-
(* | "(" e:expr ")" {e} *)
20-
)
11+
let rec parse s =
12+
expr id
13+
[|
14+
left , [ostap ("+"), (fun x y -> `Add (x, y)); ostap ("-"), (fun x y -> `Sub (x, y))];
15+
left , [ostap ("*"), (fun x y -> `Mul (x, y)); ostap ("/"), (fun x y -> `Div (x, y))]
16+
|]
17+
primary
18+
s
19+
and ostap (primary: n: DECIMAL {Const n}
20+
| e:IDENT {Var e}
21+
| -"(" parse -")")
2122

2223
ostap (
2324
simp: x:IDENT ":=" e:expr {Assign (x, e)}

‎Driver.ml~‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
open Language
2+
open Expr
3+
open Stmt
4+
open Ostap
5+
open GT
6+
7+
ostap (
8+
expr : expr "+" expr
9+
)
10+
11+
ostap (
12+
expr: x:mull "+" y:expr {Add (x,y)}
13+
| mull;
14+
mull: x:prim "*" y:mull {Mul (x,y)}
15+
| prim;
16+
prim: n:DECIMAL {Const n}
17+
| e:IDENT {Var e}
18+
| -"(" expr -")"
19+
(* | "(" e:expr ")" {e} *)
20+
)
21+
22+
ostap (
23+
simp: x:IDENT ":=" e:expr {Assign (x, e)}
24+
| %"read" "(" x:IDENT ")" {Read x}
25+
| %"write" "(" e:expr ")" {Write e}
26+
| %"skip" {Skip};
27+
28+
stmt: s:simp ";" d:stmt {Seq (s,d)}
29+
| simp
30+
)
31+
32+
let parse filename =
33+
let s = Util.read filename in
34+
Util.parse
35+
(object
36+
inherit Matcher.t s
37+
inherit Util.Lexers.ident ["read"; "write"; "skip"] s
38+
inherit Util.Lexers.decimal s
39+
inherit Util.Lexers.skip [
40+
Matcher.Skip.whitespaces " \t\n"
41+
] s
42+
end)
43+
(ostap (stmt -EOF))
44+
45+
let _ =
46+
match Sys.argv with
47+
| [|_; filename|] ->
48+
match parse filename with
49+
| `Ok stmt ->
50+
let basename = Filename.chop_suffix filename ".expr" in
51+
let text = X86.compile stmt in
52+
Printf.printf "%s\n" (show (Stmt.t) stmt);
53+
let asm = basename ^ ".s" in
54+
let ouch = open_out asm in
55+
Printf.fprintf ouch "%s\n" text;
56+
close_out ouch;
57+
let runtime = try Sys.getenv "RUNTIME" with _ -> "../runtime" in
58+
ignore @@ Sys.command (Printf.sprintf "gcc -m32 -o %s %s/runtime.o %s.s" basename runtime basename)
59+
| `Fail e -> Printf.eprintf "Parsing error: %s\n" e

0 commit comments

Comments
 (0)