Skip to content

Instantly share code, notes, and snippets.

@cdiggins
Created July 23, 2018 18:40
Show Gist options
  • Save cdiggins/c776e8a797422d55c0770edaf2b7e776 to your computer and use it in GitHub Desktop.
Save cdiggins/c776e8a797422d55c0770edaf2b7e776 to your computer and use it in GitHub Desktop.
An example Arithmetic evaluator in JavaScript written using the Myna parsing library:
"use strict";
function EvalArithmetic(exprNode)
{
switch (exprNode.rule.name)
{
case "expr":
{
return EvalArithmetic(exprNode.children[0]);
}
case "sum":
{
let v = EvalArithmetic(exprNode.children[0]);
for (let i=1; i < exprNode.children.length; ++i) {
let child = exprNode.children[i];
switch (child.rule.name) {
case("addExpr"): v += EvalArithmetic(child); break;
case("subExpr"): v -= EvalArithmetic(child); break;
default: throw "Unexpected expression " + child.rule.name;
}
}
return v;
}
case "product":
{
let v = EvalArithmetic(exprNode.children[0]);
for (let i=1; i < exprNode.children.length; ++i) {
let child = exprNode.children[i];
switch (child.rule.name) {
case("mulExpr"): v *= EvalArithmetic(child); break;
case("divExpr"): v /= EvalArithmetic(child); break;
default: throw "Unexpected expression " + child.rule.name;
}
}
return v;
}
case "prefixExpr":
{
let v = EvalArithmetic(exprNode.children[exprNode.children.length-1]);
for (let i=exprNode.children.length-2; i >= 0; --i)
if (exprNode.children[i].allText == "-")
v = -v;
return v;
}
case "parenExpr" : return EvalArithmetic(exprNode.children[0]);
case "number": return Number(exprNode.allText);
case "addExpr": return EvalArithmetic(exprNode.children[0]);
case "subExpr": return EvalArithmetic(exprNode.children[0]);
case "mulExpr": return EvalArithmetic(exprNode.children[0]);
case "divExpr": return EvalArithmetic(exprNode.children[0]);
default: throw "Unrecognized expression " + exprNode.rule.name;
}
}
function CreateEvaluator(myna) {
return function (expr) {
let ast = myna.parsers.arithmetic(expr);
return EvalArithmetic(ast);
}
}
// Export the function for use with Node.js
if (typeof module === "object" && module.exports)
module.exports = CreateEvaluator;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment