summaryrefslogtreecommitdiff
path: root/src/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/index.ts')
-rw-r--r--src/index.ts63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/index.ts b/src/index.ts
new file mode 100644
index 0000000..29047f2
--- /dev/null
+++ b/src/index.ts
@@ -0,0 +1,63 @@
+import { args, type Args } from '@/args';
+import { join } from 'path';
+import { watch } from 'fs/promises';
+import { generateParser, GRAMMAR_FILE, GENERATED_PARSER } from '@/parser';
+import {
+ ConsoleTracingLogger,
+ type LogLevel,
+ type TracingLogger,
+} from '@/utils';
+import { evaluate } from '@/interpreter';
+
+const LOG_LEVELS: LogLevel[] = ['info', 'warn', 'error'];
+
+const devMode = async (logger: TracingLogger) => {
+ logger.info('Watching for changes in parser...');
+
+ const watcher = watch(import.meta.dir, { recursive: true });
+ for await (const event of watcher) {
+ if (event.filename?.endsWith(GRAMMAR_FILE)) {
+ const grammarFile = join(import.meta.dir, event.filename);
+ const outputFile = join(
+ import.meta.dir,
+ event.filename.replace(GRAMMAR_FILE, GENERATED_PARSER),
+ );
+ logger.info(
+ `Generating parser at Location=(${grammarFile}) to Source=(${outputFile})...`,
+ );
+ generateParser(grammarFile, outputFile);
+ }
+ }
+};
+
+const doRepl = async (prompt = '~> ') => {
+ process.stdout.write(prompt);
+
+ for await (const line of console) {
+ const result = await evaluate(line);
+ console.log(result);
+ break;
+ }
+
+ await doRepl(prompt);
+};
+
+export const main = async (args: Args) => {
+ if (args.devMode) {
+ LOG_LEVELS.push('debug');
+ }
+
+ const logger = new ConsoleTracingLogger('main', LOG_LEVELS);
+
+ if (args.devMode) {
+ logger.info('Starting in dev mode...');
+ await devMode(logger);
+ }
+
+ if (args.repl) {
+ logger.info('Starting REPL...');
+ logger.info('Welcome to the CPS interpreter!');
+ }
+};
+
+main(args);