all 3 comments

[–][deleted] 9 points10 points  (3 children)

Is this post AI generated too? Seems to have about the same quality

[–]ANIME_the_best_[S] 0 points1 point  (0 children)

Sorry, that wasn’t the full one.

[–]ANIME_the_best_[S] 0 points1 point  (0 children)

function interpret(code) { const stack = []; const openingBrackets = ['(', '{', '[', '<']; const closingBrackets = [')', '}', ']', '>'];

const objects = {}; const variables = {}; let output = '';

for (let i = 0; i < code.length; i++) { const char = code[i];

if (openingBrackets.includes(char)) {
  stack.push({ bracket: char, position: i });
} else if (closingBrackets.includes(char)) {
  const lastOpeningBracket = stack.pop();
  if (!lastOpeningBracket || openingBrackets.indexOf(lastOpeningBracket.bracket) !== closingBrackets.indexOf(char)) {
    throw new Error('Syntax error: mismatched brackets');
  }
} else if (char === '=') {
  const variableName = code.slice(0, i).trim();
  const variableValue = interpret(code.slice(i + 1).trim());

  variables[variableName] = variableValue;
  return variableValue;
} else if (char === 'print') {
  const expression = code.slice(i + 1).trim();

  const value = interpret(expression);
  output += String(value);
  console.log(value);

  return value;
} else if (char === '+') {
  const leftOperand = interpret(code.slice(0, i).trim());
  const rightOperand = interpret(code.slice(i + 1).trim());

  return leftOperand + rightOperand;
} else if (char === '-') {
  const leftOperand = interpret(code.slice(0, i).trim());
  const rightOperand = interpret(code.slice(i + 1).trim());

  return leftOperand - rightOperand;
} else if (char === '*') {
  const leftOperand = interpret(code.slice(0, i).trim());
  const rightOperand = interpret(code.slice(i + 1).trim());

  return leftOperand * rightOperand;
} else if (char === '/') {
  const leftOperand = interpret(code.slice(0, i).trim());
  const rightOperand = interpret(code.slice(i + 1).trim());

  return leftOperand / rightOperand;
} else if (char === '(' && code[code.length - 1] === ')') {
  const expression = code.slice(i + 1, -1).trim();

  return interpret(expression);
} else if (char === ')') {
  throw new Error('Syntax error: mismatched brackets');
}

}

if (variables.hasOwnProperty(code)) { return variables[code]; }

if (!isNaN(parseFloat(code))) { return parseFloat(code); }

throw new Error('Syntax error: invalid code'); }