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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions _build/_digests
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"Rule: ocaml: ml & cmi -> cmx & o (%=src/StackMachine )": "{m\222\"T\136\011\169\235\134\028t\253\186\207H"
"Resource: /home/konstantin/eltech_compilers/eltech_compilers/src/X86.ml": "\223q\241\169\152\199\1553\172\250@Z\226\011\127s"
"Rule: ocaml: ml -> cmo & cmi (%=src/rc )": "\155\135YJNg^\196@\179+\215kG\247q"
"Resource: /home/konstantin/eltech_compilers/eltech_compilers/src/StackMachine.ml": "\187\160]\214\031ab\006'\234\138'\024\241\192\250"
"Rule: ocaml dependencies ml (%=src/rc )": "\171SM$\002/S\029\154\174\225\215\185\195Q\166"
"Rule: ocaml: ml -> cmo & cmi (%=src/X86 )": "\183\012G|5\151z\150&\015\168\236}\192\134g"
"Rule: ocaml: cmo* -> byte (%=src/rc )": "c1|\229\1951\140\192\131M\225\233\232\238\001\014"
"Rule: ocaml: ml -> cmo & cmi (%=src/Interpret )": "\204\002trh#e\181:>\213\233\187w\241\019"
"Rule: ocaml dependencies ml (%=src/Driver )": "\127\218Gt\030\192\176Q\165\164\026\218O\179\172}"
"Rule: ocaml: ml & cmi -> cmx & o (%=src/X86 )": "0756{n\1622\207\178\144\196\198\141>\213"
"Resource: /home/konstantin/eltech_compilers/eltech_compilers/src/Driver.ml": "N\152\191q\216\185=Q\128EC\129-)4\127"
"Rule: ocaml dependencies ml (%=src/Language )": "\243J\030\003\201\176D\184\139[\241\220\005zx\161"
"Rule: ocaml: ml & cmi -> cmx & o (%=src/Driver )": "L\238\239;\194\186\024\006[\144\030\181\161Q\193\233"
"Rule: ocaml dependencies ml (%=src/Interpret )": "g\244\137u\185#_\227\1801\155c\188\209\021\238"
"Rule: ocaml: ml -> cmo & cmi (%=src/StackMachine )": "\000\160\137#\171\199$ES?\196\158T\r\212\206"
"Rule: ocaml: ml & cmi -> cmx & o (%=src/Interpret )": "E\229\016i\248q\b@\136\188\022\173\220\254\137\012"
"Resource: /home/konstantin/eltech_compilers/eltech_compilers/src/Interpret.ml": "\228\238\207\144Jd\235\021>\1971\138\198\212\031+"
"Resource: /home/konstantin/eltech_compilers/eltech_compilers/src/Language.ml": "j\197\170\150Oy\234\135\219/\196\187:pV\224"
"Rule: ocaml: cmx* & o* -> native (%=src/rc )": "\030\214@uR\240m\148[\182\233 \172y\150x"
"Rule: ocaml: ml -> cmo & cmi (%=src/Language )": "&Nb\001\159\1512\012\197\205=\014\026\024\1484"
"Rule: ocaml: ml & cmi -> cmx & o (%=src/Language )": "\160\165\029\216\012\135\004\2007\139\128\244$]\251\238"
"Rule: ocaml: ml -> cmo & cmi (%=src/Driver )": "\241\138-\188&w\191\212\201\174\246\253=C2\161"
"Resource: /home/konstantin/eltech_compilers/eltech_compilers/src/rc.ml": "\1925m\003\2304M\252\t\212\022\151\002\167\151\245"
"Rule: ocaml: ml & cmi -> cmx & o (%=src/rc )": "\174\196:A\134\242Z \220\245\213\0126\2209\222"
"Rule: ocaml dependencies ml (%=src/X86 )": "\228\181\175\184e\253\128\218N\255G!\r\161yO"
"Rule: ocaml dependencies ml (%=src/StackMachine )": "\188\182iI\007\225\181\213J>\139O\141\205\004n"
33 changes: 33 additions & 0 deletions _build/_log
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
### Starting build.
# Target: src/rc.ml.depends, tags: { debug, extension:ml, file:src/rc.ml, ocaml, ocamldep, package(ostap.syntax), quiet, rectypes, syntax(camlp5o), traverse }
ocamlfind ocamldep -syntax camlp5o -package ostap.syntax -modules src/rc.ml > src/rc.ml.depends # cached
# Target: src/Driver.ml.depends, tags: { debug, extension:ml, file:src/Driver.ml, ocaml, ocamldep, package(ostap.syntax), quiet, rectypes, syntax(camlp5o), traverse }
ocamlfind ocamldep -syntax camlp5o -package ostap.syntax -modules src/Driver.ml > src/Driver.ml.depends
# Target: src/Interpret.ml.depends, tags: { debug, extension:ml, file:src/Interpret.ml, ocaml, ocamldep, package(ostap.syntax), quiet, rectypes, syntax(camlp5o), traverse }
ocamlfind ocamldep -syntax camlp5o -package ostap.syntax -modules src/Interpret.ml > src/Interpret.ml.depends
# Target: src/Language.ml.depends, tags: { debug, extension:ml, file:src/Language.ml, ocaml, ocamldep, package(ostap.syntax), quiet, rectypes, syntax(camlp5o), traverse }
ocamlfind ocamldep -syntax camlp5o -package ostap.syntax -modules src/Language.ml > src/Language.ml.depends
# Target: src/Language.cmo, tags: { byte, compile, debug, extension:cmo, extension:ml, file:src/Language.cmo, file:src/Language.ml, implem, ocaml, package(ostap.syntax), quiet, rectypes, syntax(camlp5o), traverse }
ocamlfind ocamlc -c -g -g -rectypes -syntax camlp5o -package ostap.syntax -I src -o src/Language.cmo src/Language.ml
# Target: src/StackMachine.ml.depends, tags: { debug, extension:ml, file:src/StackMachine.ml, ocaml, ocamldep, package(ostap.syntax), quiet, rectypes, syntax(camlp5o), traverse }
ocamlfind ocamldep -syntax camlp5o -package ostap.syntax -modules src/StackMachine.ml > src/StackMachine.ml.depends
# Target: src/Interpret.cmo, tags: { byte, compile, debug, extension:cmo, extension:ml, file:src/Interpret.cmo, file:src/Interpret.ml, implem, ocaml, package(ostap.syntax), quiet, rectypes, syntax(camlp5o), traverse }
ocamlfind ocamlc -c -g -g -rectypes -syntax camlp5o -package ostap.syntax -I src -o src/Interpret.cmo src/Interpret.ml
+ ocamlfind ocamlc -c -g -g -rectypes -syntax camlp5o -package ostap.syntax -I src -o src/Interpret.cmo src/Interpret.ml
File "src/Interpret.ml", line 11, characters 6-950:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
Binop ("", _, _)
File "src/Interpret.ml", line 45, characters 12-23:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
[]
File "src/Interpret.ml", line 41, characters 6-636:
Warning 8: this pattern-matching is not exhaustive.
Here is an example of a value that is not matched:
Repeat (_, _)
File "src/Interpret.ml", line 69, characters 0-1067:
Error: Multiple definition of the module name Expr.
Names must be unique in a given structure or signature.
Command exited with code 2.
# Compilation unsuccessful.
1 change: 1 addition & 0 deletions _build/ocamlc.where
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/usr/lib/ocaml
Binary file added _build/src/Driver.cmi
Binary file not shown.
Binary file added _build/src/Driver.cmo
Binary file not shown.
Binary file added _build/src/Driver.cmx
Binary file not shown.
51 changes: 51 additions & 0 deletions _build/src/Driver.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
open Language
open Expr
open Stmt
open Ostap

let parse filename =
let s = Util.read filename in
Util.parse
(object
inherit Matcher.t s
inherit Util.Lexers.ident ["read"; "write"; "skip"; "if"; "then"; "else"; "fi"; "while"; "do"; "od"] s
inherit Util.Lexers.decimal s
inherit Util.Lexers.skip [
Matcher.Skip.whitespaces " \t\n";
Matcher.Skip.lineComment "--";
Matcher.Skip.nestedComment "(*" "*)"
] s
end)
(ostap (!(Stmt.parse) -EOF))

let main =
try
let interpret = Sys.argv.(1) = "-i" in
let stack = Sys.argv.(1) = "-s" in
let to_compile = not (interpret || stack) in
let infile = Sys.argv.(if not to_compile then 2 else 1) in
match parse infile with
| `Ok prog ->
if to_compile
then
let basename = Filename.chop_suffix infile ".expr" in
ignore @@ X86.build prog basename
else
let rec read acc =
try
let r = read_int () in
Printf.printf "> ";
read (acc @ [r])
with End_of_file -> acc
in
let input = read [] in
let output =
if interpret
then Interpret.Program.eval prog input
else StackMachine.Interpret.run (StackMachine.Compile.Program.compile prog) input
in
List.iter (fun i -> Printf.printf "%d\n" i) output
| `Fail er -> Printf.eprintf "Syntax error: %s\n" er
with Invalid_argument _ ->
Printf.printf "Usage: rc [-i] <input file.expr>\n"

1 change: 1 addition & 0 deletions _build/src/Driver.ml.depends
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/Driver.ml: Array Expr Filename Interpret Language List Matcher Ostap Printf StackMachine Stmt Sys Util X86
Binary file added _build/src/Driver.o
Binary file not shown.
Binary file added _build/src/Interpret.cmx
Binary file not shown.
127 changes: 127 additions & 0 deletions _build/src/Interpret.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
open Language

(* Interpreter for expressions *)
module Expr =
struct

open Expr

let rec eval expr st =
let eval' e = eval e st in
match expr with
| Var x -> st x
| Const z -> z
| Binop ("+", x, y) -> eval' x + eval' y
| Binop ("-", x, y) -> eval' x - eval' y
| Binop ("*", x, y) -> eval' x * eval' y
| Binop ("/", x, y) -> eval' x / eval' y
| Binop ("%", x, y) -> (eval' x) mod (eval' y)
| Binop ("<", x, y) -> if (eval' x) < (eval' y) then 1 else 0
| Binop ("<=", x, y) -> if (eval' x) <= (eval' y) then 1 else 0
| Binop (">", x, y) -> if (eval' x) > (eval' y) then 1 else 0
| Binop (">=", x, y) -> if (eval' x) >= (eval' y) then 1 else 0
| Binop ("==", x, y) -> if (eval' x) == (eval' y) then 1 else 0
| Binop ("!=", x, y) -> if (eval' x) <> (eval' y) then 1 else 0
| Binop ("&&", x, y) -> if ((eval' x) <> 0) && ((eval' y) <> 0) then 1 else 0
| Binop ("!!", x, y) -> if ((eval' x) <> 0) || ((eval' y) <> 0) then 1 else 0


end

(* Interpreter for statements *)
module Stmt =
struct

open Stmt

(* State update primitive *)
let update st x v = fun y -> if y = x then v else st y

let rec eval stmt ((st, input, output) as conf) =
match stmt with
| Skip -> conf
| Assign (x, e) -> (update st x (Expr.eval e st), input, output)
| Read x ->
let z :: input' = input in
(update st x z, input', output)
| Write e -> (st, input, output @ [Expr.eval e st])
| If (e, s1, s2) -> if (Expr.eval e st) <> 0 then (eval s1 conf)
else (eval s2 conf)
| While (e, s) -> if (Expr.eval e st) <> 0 then eval stmt (eval s conf)
else conf
| Seq (s1, s2) -> eval s1 conf |> eval s2

end

module Program =
struct

let eval p input =
let (_, _, output) =
Stmt.eval p ((fun _ -> failwith "undefined variable"), input, [])
in
output

end
open Language

(* Interpreter for expressions *)
module Expr =
struct

open Expr

let rec eval expr st =
let eval' e = eval e st in
match expr with
| Var x -> st x
| Const z -> z
| Binop ("+", x, y) -> eval' x + eval' y
| Binop ("-", x, y) -> eval' x - eval' y
| Binop ("*", x, y) -> eval' x * eval' y
| Binop ("/", x, y) -> eval' x / eval' y
| Binop ("%", x, y) -> (eval' x) mod (eval' y)
| Binop ("<", x, y) -> if (eval' x) < (eval' y) then 1 else 0
| Binop ("<=", x, y) -> if (eval' x) <= (eval' y) then 1 else 0
| Binop (">", x, y) -> if (eval' x) > (eval' y) then 1 else 0
| Binop (">=", x, y) -> if (eval' x) >= (eval' y) then 1 else 0
| Binop ("==", x, y) -> if (eval' x) == (eval' y) then 1 else 0
| Binop ("!=", x, y) -> if (eval' x) <> (eval' y) then 1 else 0
| Binop ("&&", x, y) -> if ((eval' x) <> 0) && ((eval' y) <> 0) then 1 else 0
| Binop ("!!", x, y) -> if ((eval' x) <> 0) || ((eval' y) <> 0) then 1 else 0


end

(* Interpreter for statements *)
module Stmt =
struct

open Stmt

(* State update primitive *)
let update st x v = fun y -> if y = x then v else st y

let rec eval stmt ((st, input, output) as conf) =
match stmt with
| Skip -> conf
| Assign (x, e) -> (update st x (Expr.eval e st), input, output)
| Read x ->
let z :: input' = input in
(update st x z, input', output)
| Write e -> (st, input, output @ [Expr.eval e st])
| Seq (s1, s2) -> eval s1 conf |> eval s2

end

module Program =
struct

let eval p input =
let (_, _, output) =
Stmt.eval p ((fun _ -> failwith "undefined variable"), input, [])
in
output

end

1 change: 1 addition & 0 deletions _build/src/Interpret.ml.depends
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/Interpret.ml: Expr Language Stmt
Binary file added _build/src/Interpret.o
Binary file not shown.
Binary file added _build/src/Language.cmi
Binary file not shown.
Binary file added _build/src/Language.cmo
Binary file not shown.
Binary file added _build/src/Language.cmx
Binary file not shown.
77 changes: 77 additions & 0 deletions _build/src/Language.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
(* AST for expressions *)
module Expr =
struct

type t =
| Var of string
| Const of int
| Binop of string * t * t

ostap(
parse: expr0;
expr0: h:expr1 t:(-"!!" expr1)*{
List.fold_left(fun e op ->Binop("!!", e, op)) h t};
expr1: h:expr2 t:(-"&&" expr2)*{
List.fold_left(fun e op ->Binop("&&", e, op)) h t};
expr2: h:expr3 t:(("==" | "!=" | "<=" | "<" | ">=" | ">")expr3)?{
match t with
| None -> h
| Some (op, y) -> Binop(Ostap.Matcher.Token.repr op, h, y)
};
expr3: h:expr4 t:(("+" | "-") expr4)*{
List.fold_left(fun e (op, y) -> Binop(Ostap.Matcher.Token.repr op, e, y)) h t};
expr4: h: prim t:(("*" | "/" | "%") prim)*{
List.fold_left(fun e (op, y) -> Binop(Ostap.Matcher.Token.repr op, e, y)) h t};
prim:
n:DECIMAL {Const n}
| e:IDENT {Var e}
| -"(" parse -")"
)
end

(* AST statements/commands *)
module Stmt =
struct

type t =
| Skip
| Assign of string * Expr.t
| Read of string
| Write of Expr.t
| Seq of t * t
| If of Expr.t * t * t
| While of Expr.t * t
| Repeat of t * Expr.t

let expr = Expr.parse

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

| %"if" e:!(Expr.parse) %"then" s:!(parse)
%"fi" {If (e, s, Skip)}

| %"if" e:!(Expr.parse) %"then" s1:!(parse)
%"else" s2:!(parse)
%"fi" {If (e, s1, s2)}

| %"while" e:!(Expr.parse) %"do" s:!(parse)
%"od" {While (e, s)};

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

end

module Program =
struct

type t = Stmt.t

let parse = Stmt.parse

end

1 change: 1 addition & 0 deletions _build/src/Language.ml.depends
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/Language.ml: List Ostap
Binary file added _build/src/Language.o
Binary file not shown.
Binary file added _build/src/StackMachine.cmx
Binary file not shown.
Loading