Skip to content
Snippets Groups Projects
Commit 3634c98f authored by Paul Iannetta's avatar Paul Iannetta Committed by Thomas Wood
Browse files

add stuck function to stdlib

parent 1c6e4a6f
No related branches found
No related tags found
No related merge requests found
......@@ -44,6 +44,8 @@ let _ =
| Some f -> f
in
Printf.printf "sf: %s\n" sourcefile;
(*---------------------------------------------------*)
(* "reading and typing source file" *)
let (opt, inputfile) = process_implementation_file ppf sourcefile in
......@@ -55,5 +57,3 @@ let _ =
let (logged, unlogged, pre) = Js_of_ast.to_javascript typedtree1 in
file_put_contents outputfile unlogged
......@@ -12,6 +12,8 @@ var geq = function (a, b) { return a >= b }
var print = function (x) { console.log(x) }
var stuck = function (msg) { throw {type:'stuck', msg:msg} }
var to_string = function (x) { return String(x) }
var parse = function (source) {
......
......@@ -21,5 +21,6 @@ val ( >= ) : 'a -> 'a -> 'a
val print : 'a -> unit
val stuck : string -> 'a
val to_string : 'a -> string
val parse : 'a -> 'b
var eval_ = function (expr) {
return function () {
switch (expr.type) {
case "Const": var n = expr.value;
return n;
case "Add": var ls = expr.left, rs = expr.right;
return eval_(ls) + eval_(rs);
case "Sub": var ls = expr.left, rs = expr.right;
return eval_(ls) - eval_(rs);
case "Mul": var ls = expr.left, rs = expr.right;
return eval_(ls) * eval_(rs);
case "Div": var ls = expr.left, rs = expr.right;
return eval_(ls) / eval_(rs);
}
}();
};
var print_expr = function (expr) {
return function () {
switch (expr.type) {
case "Const": var n = expr.value;
return to_string(n);
case "Add": var ls = expr.left, rs = expr.right;
return "(" + print_expr(ls) + ")" + " + " + print_expr(rs);
case "Sub": var ls = expr.left, rs = expr.right;
return "(" + print_expr(ls) + ")" + " - " + print_expr(rs);
case "Mul": var ls = expr.left, rs = expr.right;
return "(" + print_expr(ls) + ")" + " * " + print_expr(rs);
case "Div": var ls = expr.left, rs = expr.right;
return "(" + print_expr(ls) + ")" + " / " + print_expr(rs);
}
}();
};
var f = function () {
var source = parse("((1972 / 29) / 2) + 8");
return print(print_expr(source) + " = " + to_string(eval_(source)));
}();
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment