Comparing version 0.3.0 to 0.4.0
@@ -0,11 +1,14 @@ | ||
import { Interpreter, RuntimeError } from './interpreter.js'; | ||
import { Token } from './token.js'; | ||
import './ast.js'; | ||
import './token-type.js'; | ||
declare function add(a: number, b: number): number; | ||
declare function runFile(path: string): Promise<void>; | ||
declare function runPrompt(): Promise<void>; | ||
declare function runFile(interpreter: Interpreter, path: string): Promise<void>; | ||
declare function runPrompt(interpreter: Interpreter): Promise<void>; | ||
declare function error(line: number, message: string): void; | ||
declare function errorOnToken(token: Token, message: string): void; | ||
declare function runtimeError(error: RuntimeError): void; | ||
declare function main(): Promise<void>; | ||
export { add, error, errorOnToken, main, runFile, runPrompt }; | ||
export { add, error, errorOnToken, main, runFile, runPrompt, runtimeError }; |
import * as fs from "node:fs/promises"; | ||
import { createInterface } from "node:readline"; | ||
import { visitExpr } from "./ast.js"; | ||
import { astPrinter } from "./ast-printer.js"; | ||
import { Interpreter } from "./interpreter.js"; | ||
import { parse } from "./parser.js"; | ||
@@ -10,10 +9,16 @@ import { Scanner } from "./scanner.js"; | ||
} | ||
async function runFile(path) { | ||
async function runFile(interpreter, path) { | ||
const contents = await fs.readFile(path, "utf-8"); | ||
run(contents); | ||
run(interpreter, contents); | ||
if (hadError) { | ||
process.exit(65); | ||
} | ||
if (hadRuntimeError) { | ||
process.exit(70); | ||
} | ||
} | ||
async function runPrompt() { | ||
async function runPrompt(interpreter) { | ||
process.stdout.write("> "); | ||
for await (const line of createInterface({ input: process.stdin })) { | ||
run(line); | ||
run(interpreter, line); | ||
hadError = false; | ||
@@ -24,2 +29,3 @@ process.stdout.write("> "); | ||
let hadError = false; | ||
let hadRuntimeError = false; | ||
function error(line, message) { | ||
@@ -35,6 +41,11 @@ report(line, "", message); | ||
} | ||
function runtimeError(error2) { | ||
console.error(`${error2.message} | ||
[line ${error2.token.line}]`); | ||
hadRuntimeError = true; | ||
} | ||
function report(line, where, message) { | ||
console.error(`[line ${line}] Error${where}: ${message}`); | ||
} | ||
function run(contents) { | ||
function run(interpreter, contents) { | ||
const scanner = new Scanner(contents); | ||
@@ -46,3 +57,3 @@ const tokens = scanner.scanTokens(); | ||
} | ||
console.log(visitExpr(expr, astPrinter)); | ||
interpreter.interpret(expr); | ||
} | ||
@@ -54,10 +65,9 @@ async function main() { | ||
process.exit(64); | ||
} else if (args.length == 1) { | ||
await runFile(args[0]); | ||
} | ||
const interpreter = new Interpreter(); | ||
if (args.length == 1) { | ||
await runFile(interpreter, args[0]); | ||
} else { | ||
await runPrompt(); | ||
await runPrompt(interpreter); | ||
} | ||
if (hadError) { | ||
process.exit(65); | ||
} | ||
} | ||
@@ -70,4 +80,5 @@ export { | ||
runFile, | ||
runPrompt | ||
runPrompt, | ||
runtimeError | ||
}; | ||
//# sourceMappingURL=main.js.map |
{ | ||
"name": "gravlax", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A Lox interpreter with tasty TypeScript seasoning", | ||
@@ -83,3 +83,3 @@ "repository": { | ||
}, | ||
"packageManager": "pnpm@8.14.0", | ||
"packageManager": "pnpm@8.14.1", | ||
"engines": { | ||
@@ -86,0 +86,0 @@ "node": ">=18" |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
51852
30
633