Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
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. 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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda