Skip to content
Snippets Groups Projects
Commit 99de03c3 authored by Cesar Roux Dit Buisson's avatar Cesar Roux Dit Buisson
Browse files

Add calc instructions to stdlibjs

We can now run the calc example from the ./run.sh script. Note how when generating the tree we must take into account annotation names used in the ml code.
parent 597c892d
No related branches found
No related tags found
No related merge requests found
......@@ -19,9 +19,13 @@ var to_string = function (x) { return String(x) }
var parse = function (source) {
var ast = require('esprima').parse(source).body[0].expression;
// Esprima does it's little thing. To be handled by auto generated code we need to
// reshape the tree structure to match the labels and expected content.
function transform (tree) {
if (tree === undefined) {
} else {
// Javascript style instructions are well handled with no additional creation.
switch (tree.operator) {
case '+':
tree.type = "Add"; tree.operator = undefined; break;
......@@ -34,14 +38,40 @@ var parse = function (source) {
default: break;
}
switch (tree.type) {
case "Literal":
tree.type = "Const"; break;
default: break;
if (tree.type === "Literal") {
tree.type = "Const";
}
// tree.left and tree.right from parser interpretation
if (tree.left !== undefined) tree.left = transform(tree.left);
if (tree.right !== undefined) tree.right = transform(tree.right);
// Esprima sees these standalone type instructions as Identifiers
if (tree.type === "Identifier") {
switch (tree.name) {
case "Emp":
tree.type = "Emp"; break;
default: console.log("Unknown ident"); break;
}
}
// Esprima generates a function type structure that needs reshaping to the
// automatically generated structure, respecting label names!
if (tree.type === "CallExpression") {
switch (tree.callee.name) {
case "Push":
tree.type = "Push";
tree.value = transform(tree.arguments[0]);
tree.stack = transform(tree.arguments[1]);
break;
case "Pop":
tree.type = "Pop";
tree.stack = transform(tree.arguments[0]);
break;
default: console.log("Unknown callexpr"); break;
}
}
return tree;
}
}
......
......@@ -37,5 +37,5 @@ and print_sexpr sexpr = match sexpr with
let f =
(*let bli = Stack.C(1, Stack.N) in
let blii = (Stack.push 2 bli) in*)
let source = parse "Pop(Emp)" in
let source = parse "Pop(Push(1, Push(5, Emp))) - 7" in
print ((print_expr source) + " = " + to_string (eval_ source))
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