Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
d9ba8df
Add travis ci support
Feb 24, 2017
14efe6c
Add travis ci support
Feb 24, 2017
7ffce14
.
Feb 24, 2017
94f90d7
.
Feb 24, 2017
593e5c1
.
Feb 24, 2017
cc58188
.
Feb 24, 2017
9bafe27
Create README.md
SammyVimes Feb 24, 2017
e58923e
Update README.md
SammyVimes Feb 24, 2017
09ddccf
add c++ helpers for reading and writing
Feb 24, 2017
790c7a6
fix asm
Feb 26, 2017
208c066
Test now output OK in case of OK, not only code 0
Feb 26, 2017
0ebf988
better comments
Feb 26, 2017
843bcd3
fin
Feb 26, 2017
e8fc5f8
Merge branch 'master' into master
SammyVimes Feb 27, 2017
a20ee58
Update .travis.yml
SammyVimes Feb 28, 2017
8d06995
remove redundant code
Mar 15, 2017
96a80ce
new proj paths
Mar 21, 2017
a910582
Fix travis build
Mar 21, 2017
e486a89
Merge branch 'master' of https://github.com/SammyVimes/eltech_compilers
Mar 21, 2017
ed35700
Update .travis.yml
SammyVimes Mar 21, 2017
4ba4bf4
merge from remote
Apr 24, 2017
f226c2c
fix language and interpreter
Apr 24, 2017
e91a8f0
fix stackmachine
Apr 24, 2017
99975aa
semi-working X86 compiler
Apr 24, 2017
694ed40
fix registers variable names
Apr 24, 2017
ee55a18
x86 fix load op
Apr 24, 2017
3294ad5
008 works
Apr 24, 2017
94abe92
deep and simple test pass
Apr 25, 2017
50c4fb6
use ebx too
Apr 25, 2017
f75e0c8
add travis ci build
Apr 25, 2017
8f3ff71
make script executable
Apr 25, 2017
2ed0d3a
tokenize new operators
May 9, 2017
bc049b4
if\while in Interpreter
May 9, 2017
f2dc85f
x86 if and while
May 9, 2017
60886a1
Update from upstream & fix makefile
May 9, 2017
e1ec561
add repeat until
May 17, 2017
055c83b
add tests with repeat
May 17, 2017
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
5 changes: 0 additions & 5 deletions .gitignore

This file was deleted.

37 changes: 37 additions & 0 deletions .travis-opam.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
echo -en "travis_fold:start:prepare.ci\r"
# If a fork of these scripts is specified, use that GitHub user instead
fork_user=${FORK_USER:-ocaml}

# If a branch of these scripts is specified, use that branch instead of 'master'
fork_branch=${FORK_BRANCH:-master}

### Bootstrap

set -uex

get() {
wget https://raw.githubusercontent.com/${fork_user}/ocaml-ci-scripts/${fork_branch}/$@
}

TMP_BUILD=$(mktemp -d 2>/dev/null || mktemp -d -t 'citmpdir')
cd ${TMP_BUILD}

get .travis-ocaml.sh
get yorick.mli
get yorick.ml
get ci_opam.ml

sh .travis-ocaml.sh
export OPAMYES=1
eval $(opam config env)

# This could be removed with some OPAM variable plumbing into build commands
opam install ocamlfind

ocamlc.opt yorick.mli
ocamlfind ocamlc -c yorick.ml

ocamlfind ocamlc -o ci-opam -package unix -linkpkg yorick.cmo ci_opam.ml
cd -

echo -en "travis_fold:end:prepare.ci\r"
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: c
sudo: required
script:
- bash -ex .travis-opam.sh
- ./installBuildRun.sh
env:
- OCAML_VERSION=4.04
os:
- linux
14 changes: 14 additions & 0 deletions installBuildRun.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
opam pin add GT https://github.com/Kakadu/GT.git -n -y
opam pin add ostap https://github.com/dboulytchev/ostap.git -n -y
opam install camlp5 -y
opam install GT ostap ocamlfind -y
eval `opam config env`
sudo apt-get install gcc-multilib -y

make

cd regression
make

cd deep-expressions
make
4 changes: 2 additions & 2 deletions regression/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
TESTS=test001 test002 test012 test013 test003 test004 test005 test006 test007 test008 test009 test010 test011 test014 test015 test016 test017 test018
TESTS=test001 test002 test012 test013 test003 test004 test005 test006 test007 test008 test009 test010 test014 test015 test016 test017 test018 test019 test020 test021 test022 test023

# test019 test020 test021 test022 test023 test024 test025 test026
# test011 test024 test025 test026
# test027 test028 test029 test030

.PHONY: check $(TESTS)
Expand Down
2 changes: 1 addition & 1 deletion src/Driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let parse filename =
Util.parse
(object
inherit Matcher.t s
inherit Util.Lexers.ident ["read"; "write"; "skip"] s
inherit Util.Lexers.ident ["read"; "write"; "skip"; "if"; "fi"; "then"; "else"; "while"; "do"; "od"; "repeat"; "until"; "for"] s
inherit Util.Lexers.decimal s
inherit Util.Lexers.skip [
Matcher.Skip.whitespaces " \t\n";
Expand Down
101 changes: 52 additions & 49 deletions src/Interpret.ml
Original file line number Diff line number Diff line change
@@ -1,49 +1,52 @@
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
| Add (x, y) -> eval' x + eval' y
| Mul (x, y) -> eval' x * eval' y

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
open Language

(* Interpreter for expressions *)
module Expr =
struct

open Expr
open Language.BinOp

let rec eval expr st =
let eval' e = eval e st in
match expr with
| Var x -> st x
| Const z -> z
| Binop (op, x, y) -> (apply op) (eval' x) (eval' y)

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)
| Write e -> (st, input, output @ [Expr.eval e st])
| Seq (s1, s2) -> eval s1 conf |> eval s2
| Read x ->
let z :: input' = input in
(update st x z, input', output)
| If (e, s1, s2) -> if (Expr.eval e st) <> 0 then (eval s1 conf) else (eval s2 conf)
(*eval self again but with new conf (which is eval'ed body of while')*)
| While (e, s) -> if (Expr.eval e st) <> 0 then eval stmt (eval s conf) else conf

end

module Program =
struct

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

end
93 changes: 82 additions & 11 deletions src/Language.ml
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
(* AST for expressions *)
open Ostap
open Matcher

module Expr =
struct

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

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

orins:
l:andins suf:(("!!") andins)* {
List.fold_left (fun l (op, r) -> Binop (Token.repr op, l, r)) l suf
}
| andins;

andins:
l:cmp suf:(("&&") cmp)* {
List.fold_left (fun l (op, r) -> Binop (Token.repr op, l, r)) l suf
}
| cmp;

cmp:
l:add suf:(("<=" | "<" | ">=" | ">" | "==" | "!=") add)* {
List.fold_left (fun l (op, r) -> Binop (Token.repr op, l, r)) l suf
}
| add;

add:
l:mull suf:(("+" | "-") mull)* {
List.fold_left (fun l (op, r) -> Binop (Token.repr op, l, r)) l suf
}
| mull;

mull:
l:prim suf:(("*" | "/" | "%") prim)* {
List.fold_left (fun l (op, r) -> Binop (Token.repr op, l, r)) l suf
}
| prim;

prim:
n:DECIMAL {Const n}
| x:IDENT {Var x}
| -"(" parse -")"
)

end
Expand All @@ -29,20 +61,59 @@ module Stmt =
| 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};

| %"skip" {Skip}
| %"if" e:!(Expr.parse)
%"then" s1:!(parse)
%"else" s2:!(parse)
%"fi" {If (e, s1, s2)}
| %"if" e:!(Expr.parse)
%"then" s1:!(parse)
%"fi" {If (e, s1, Skip)}
| %"while" e:!(Expr.parse)
%"do" s:!(parse)
%"od" {While (e, s)}
| %"repeat" s:!(parse)
%"until" e:!(Expr.parse) {Seq (s, While (Binop ("==", e, Const 0), s))}
| %"for" i:!(parse) "," n:!(Expr.parse) "," b:!(parse)
%"do" a:!(parse)
%"od" {Seq (i, (While (n, Seq (a, b))))};
parse: s:simp ";" d:parse {Seq (s,d)} | simp
)

end


module BinOp =
struct

let apply op =
match op with
| "+" -> fun x y -> x + y
| "*" -> fun x y -> x * y
| "-" -> fun x y -> x - y
| "/" -> fun x y -> x / y
| "%" -> fun x y -> x mod y
| "<" -> fun x y -> if x < y then 1 else 0
| "<=" -> fun x y -> if x <= y then 1 else 0
| ">" -> fun x y -> if x > y then 1 else 0
| ">=" -> fun x y -> if x >= y then 1 else 0
| "==" -> fun x y -> if x = y then 1 else 0
| "!=" -> fun x y -> if x <> y then 1 else 0
| "&&" -> fun x y -> if (x <> 0) && (y <> 0) then 1 else 0
| "!!" -> fun x y -> if (x <> 0) || (y <> 0) then 1 else 0

end

module Program =
struct

Expand Down
Loading