Skip to content

Commit

Permalink
added ref
Browse files Browse the repository at this point in the history
  • Loading branch information
hsk committed Feb 10, 2015
1 parent 34b0185 commit 84f80a0
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 5 deletions.
7 changes: 6 additions & 1 deletion scala/mincaml2js.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ object parse extends RegexParsers {
def sub: Parser[E] =
"-." ~> app ^^ { a => EPre("-", a) } |
"-" ~> app ^^ { a => EPre("-", a) } |
"!" ~> app ^^ { a => EBin(a,".", EVar("ref")) } |
app

def app: Parser[E] =
Expand All @@ -199,6 +200,9 @@ object parse extends RegexParsers {
}
case a ~ b ~ None => b.foldLeft(a) { case (a, b) => EGet(a, EStr(b)) }
} |
simple_exp ~ (":=" ~> let) ^^ {
case a ~ b => EPut(a, EStr("ref"), b)
} |
simple_exp

def apply(str: String) = {
Expand Down Expand Up @@ -354,6 +358,7 @@ object cnv {
oc.printf("function print_int(n) { console._stdout.write(\"\"+n);}\n")
oc.printf("var print_string = print_int;\n");
oc.printf("function makeArray(n,v) { var a = []; for(var i = 0; i < n; i++) a[i] = v; return a; }\n")
oc.printf("function ref(n) { return {ref:n}; }\n")
oc.printf("var abs_float = Math.abs;\n")
oc.printf("var sqrt = Math.sqrt;\n")
oc.printf("var sin = Math.sin;\n")
Expand Down Expand Up @@ -382,7 +387,7 @@ object main extends App {
object test extends App {

val tests = List(
"record", "string", "as", "list1", "match", "begin", "print", "sum-tail", "gcd", "sum", "fib", "ack", "even-odd",
"ref", "record", "string", "as", "list1", "match", "begin", "print", "sum-tail", "gcd", "sum", "fib", "ack", "even-odd",
"adder", "funcomp", "cls-rec", "cls-bug", "cls-bug2",
"shuffle", "spill", "spill2", "spill3", "join-stack", "join-stack2", "join-stack3",
"join-reg", "join-reg2", "non-tail-if", "non-tail-if2",
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ clean:: nobackup

SOURCES = syntax.ml parser.mly lexer.mll to_if.ml emit.ml main.ml

TESTS = record string as list1 match begin print sum-tail gcd sum fib ack even-odd \
TESTS = ref record string as list1 match begin print sum-tail gcd sum fib ack even-odd \
adder funcomp cls-rec cls-bug cls-bug2 \
shuffle spill spill2 spill3 join-stack join-stack2 join-stack3 \
join-reg join-reg2 non-tail-if non-tail-if2 \
Expand Down
1 change: 1 addition & 0 deletions src/emit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ let f oc ast =
Printf.fprintf oc "function print_int(n) { console._stdout.write(\"\"+n);}\n";
Printf.fprintf oc "var print_string = print_int;\n";
Printf.fprintf oc "function makeArray(n,v) { var a = []; for(var i = 0; i < n; i++) a[i] = v; return a; }\n";
Printf.fprintf oc "function ref(n) { return {ref:n}; }\n";
Printf.fprintf oc "var abs_float = Math.abs;\n";
Printf.fprintf oc "var sqrt = Math.sqrt;\n";
Printf.fprintf oc "var sin = Math.sin;\n";
Expand Down
2 changes: 2 additions & 0 deletions src/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ rule token = parse
| "Array.create" { ARRAY_CREATE }
| '.' { DOT }
| "<-" { LESS_MINUS }
| ":=" { COLON_EQUAL }
| '!' { EXCLAM }
| ';' { SEMICOLON }
| eof { EOF }
| '"' ([^ '"' '\\'] | '\\' _)* '"' { let s = Lexing.lexeme lexbuf in STRING(String.sub s 1 ((String.length s)-2))}
Expand Down
5 changes: 4 additions & 1 deletion src/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ open Syntax
%token TYPE OF SEMISEMI AST
%token LBRACK RBRACK CONS AT AS
%token MUTABLE LBRACE RBRACE COLON
%token EXCLAM COLON_EQUAL
%token EOF

%right prec_let
%right SEMICOLON CONS AT AS
%right prec_if
%right LESS_MINUS
%right LESS_MINUS COLON_EQUAL
%left COMMA
%left EQUAL LESS_GREATER LESS GREATER LESS_EQUAL GREATER_EQUAL
%left PLUS MINUS PLUS_DOT MINUS_DOT
Expand Down Expand Up @@ -92,6 +93,7 @@ exp:
| Float(f) -> Float(-.f)
| e -> Pre("-", e)
}
| EXCLAM exp %prec prec_unary_minus { Bin($2,".",Var "ref") }
| exp CONS exp { CApp("Cons", Tuple[$1; $3]) }
| exp AT exp { App(Var "concat", [$1; $3]) }
| exp AS IDENT { Bin($1, "as", Var $3) }
Expand Down Expand Up @@ -133,6 +135,7 @@ exp:
| LET LBRACE fields RBRACE EQUAL exp IN exp { Match($6, [Rec $3, None, $8]) }
| simple_exp DOT LPAREN exp RPAREN LESS_MINUS exp { Put($1, $4, $7) }
| simple_exp DOT IDENT LESS_MINUS exp { Put($1, Str $3, $5) }
| simple_exp COLON_EQUAL exp { Put($1, Str "ref", $3) }
| exp SEMICOLON exp { Let(Syntax.gentmp (), $1, $3) }
| ARRAY_CREATE simple_exp simple_exp %prec prec_app { Array($2, $3) }
| error
Expand Down
2 changes: 1 addition & 1 deletion src2/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RESULT = mincaml
TESTS = record string as list1 match begin print sum-tail gcd sum fib ack even-odd \
TESTS = ref record string as list1 match begin print sum-tail gcd sum fib ack even-odd \
adder funcomp cls-rec cls-bug cls-bug2 \
shuffle spill spill2 spill3 join-stack join-stack2 join-stack3 \
join-reg join-reg2 non-tail-if non-tail-if2 \
Expand Down
4 changes: 4 additions & 0 deletions src2/parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ and sub i = i |> (
| Float(f) -> Float (-. f)
| a -> Pre("-", a)
)) <|>
((str "!" >> app) >>> (fun a -> Bin(a, ".", Var "ref") )) <|>
app
)
and app i = i |> (
Expand Down Expand Up @@ -222,6 +223,9 @@ and dot i = i |> (
end
| ((a, b), None) -> List.fold_left (fun a b -> Get(a, Str b) ) a b
)) <|>
(simple_exp <~> (str ":=" >> _let) >>> (
fun (a,b) -> Put(a, Str "ref", b)
)) <|>
simple_exp
)
and start i = i |> (exp << skip)
Expand Down
2 changes: 1 addition & 1 deletion src3/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: clean all default
RESULT = mincaml
TESTS = record string as list1 match begin print sum-tail gcd sum fib ack even-odd \
TESTS = ref record string as list1 match begin print sum-tail gcd sum fib ack even-odd \
adder funcomp cls-rec cls-bug cls-bug2 \
shuffle spill spill2 spill3 join-stack join-stack2 join-stack3 \
join-reg join-reg2 non-tail-if non-tail-if2 \
Expand Down
1 change: 1 addition & 0 deletions src3/emit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ let f oc ast =
Printf.fprintf oc "function print_int(n) { console._stdout.write(\"\"+n);}\n";
Printf.fprintf oc "var print_string = print_int;\n";
Printf.fprintf oc "function makeArray(n,v) { var a = []; for(var i = 0; i < n; i++) a[i] = v; return a; }\n";
Printf.fprintf oc "function ref(n) { return {ref:n}; }\n";
Printf.fprintf oc "var abs_float = Math.abs;\n";
Printf.fprintf oc "var sqrt = Math.sqrt;\n";
Printf.fprintf oc "var sin = Math.sin;\n";
Expand Down
8 changes: 8 additions & 0 deletions test/ref.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let a = ref 5 in
let b = !a in
print_int b;
a := b + 1;
print_int(!a);
a := !a + 1;
print_int(!a);
print_newline()

0 comments on commit 84f80a0

Please sign in to comment.