Skip to content

Commit

Permalink
commit forgotten new file
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-trunov committed Jun 21, 2024
1 parent 7dec1b3 commit 0175fdf
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/interpreter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { evalConstantExpression } from "./constEval";
import { CompilerContext } from "./context";
import { TactConstEvalError, TactParseError } from "./errors";
import { parseExpression } from "./grammar/grammar";
import { Value } from "./types/types";

export type EvalResult =
| { kind: "ok"; value: Value }
| { kind: "error"; message: string };

export function parseAndEvalExpression(sourceCode: string): EvalResult {
try {
const ast = parseExpression(sourceCode);
const constEvalResult = evalConstantExpression(
ast,
new CompilerContext(),
);
return { kind: "ok", value: constEvalResult };
} catch (error) {
if (
error instanceof TactParseError ||
error instanceof TactConstEvalError
)
return { kind: "error", message: error.message };
throw error;
}
}

0 comments on commit 0175fdf

Please sign in to comment.