简体   繁体   English

如何循环通过 object(对于简单的数学解释器,在 Javascript 中)

[英]How can I loop through an object (for a simple math interpreter, in Javascript)

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.我不知道如何在解释器中循环遍历 object。 My attemps either don't work at all, or cause infinite loops until Javascript reaches it's max amount of memory.我的尝试要么根本不起作用,要么导致无限循环,直到 Javascript 达到最大数量的 memory。

The result of the Parser would look something like this for a simple 2 + 2 + 3 :对于简单的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.解析数(parseNum)function 很简单,我觉得没必要分享。 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.任何帮助将不胜感激,谢谢。

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM