function calculate()

in packages/sqrl/src/compile/SqrlTruthTable.ts [72:102]


function calculate(ast: Ast, values) {
  if (ast.type === "feature") {
    invariant(
      typeof values[ast.value] === "boolean",
      "Expected feature to be set"
    );
    return values[ast.value];
  } else if (ast.type === "not") {
    return !calculate(ast.expr, values);
  } else if (ast.type === "boolean_expr") {
    if (ast.operator === "and") {
      return calculate(ast.left, values) && calculate(ast.right, values);
    } else if (ast.operator === "or") {
      return calculate(ast.left, values) || calculate(ast.right, values);
    } else {
      throw new Error("Unknown ast operator during calculate step");
    }
  } else if (ast.type === "binary_expr") {
    const name = binaryFeatureName(ast as ValidBinaryExprAst);
    invariant(typeof values[name] === "boolean", "Expected value to be set");
    if (ast.operator === "=") {
      return values[name];
    } else if (ast.operator === "!=") {
      return !values[name];
    } else {
      throw new Error("Unknown binary_expr operator");
    }
  } else {
    throw new Error("Unexpected ast type during truth table calculate step");
  }
}