Home > Software design >  How can I loop through an object (for a simple math interpreter, in Javascript)
How can I loop through an object (for a simple math interpreter, in Javascript)

Time:01-07

I've been working on a simple math interpreter and I'm stuck on a problem.
I can't figure out how to loop through the object in the interpreter. My attemps either don't work at all, or cause infinite loops until Javascript reaches it's max amount of memory.

The result of the Parser would look something like this for a simple 2 2 3:

{
    "operator": " ",
    "left": {
        "operator": " ",     
        "left": {
            "type": "NUMBER",
            "value": 2       
        },
        "right": {
            "type": "NUMBER",
            "value": 2       
        }
    },
    "right": {
        "type": "NUMBER",    
        "value": 3
    }
}

Here's one of the attempts I've made.

interpret(node) {
    node.left = this.parseNode(node.left);
    node.right = this.parseNode(node.right);

    return this.parseNum(node.left, node.right, node.operator);
}

parseNode(node) {
    let left = node.left;
    let right = node.right;

    while (left != null) {
        left = this.destructure(left);
    }
    while (right != null) {
        right = this.destructure(right);
    }

    if (left == null && right == null) {
        return { ...node };
    } else {
        return {
            type: "NUMBER",
            value: this.parseNum(left, right, node.operator),
        };
    }
}

The parse number (parseNum) function is pretty simple, so I don't think I need to share it. All it does is take the operator and add/multiply/subtract/divide the first two items based on that.

Any help would be appreciated, thanks.

CodePudding user response:

What you want is to recursively reduce each node into a single value by inspecting it and...

  • If the node is already a value node, just return it
  • Otherwise, create a new value node by reducing both the left and right nodes to a single value node and evaluating the result with the operator

const root = {"operator":" ","left":{"operator":" ","left":{"type":"NUMBER","value":2},"right":{"type":"NUMBER","value":2}},"right":{"type":"NUMBER","value":3}}

// Operator functions
const operators = {
  " ": (l, r) => l   r,
}

// Expression evaluation
const evaluate = ({ value: l }, { value: r }, operator) =>
  operators[operator](l, r)
  
const isValueNode = node => "value" in node
  
// Reduce a node to a _value_ node
const reducer = (node) => {
  // Already a value node? Just return it
  if (isValueNode(node)) return node
  
  return {
    type: "NUMBER", // no idea what this is for ¯\_(ツ)_/¯
    value: evaluate(
      reducer(node.left),  // recursively reduce the _left_ node
      reducer(node.right), // recursively reduce the _right_ node
      node.operator
    )
  }
}

console.log(reducer(root))

  •  Tags:  
  • Related