From d0d6aae1e56428f597f69f5c9cfb261afe671f5d Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Fri, 23 Feb 2024 16:46:10 -0700 Subject: initial parser --- src/index.ts | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/index.ts (limited to 'src/index.ts') 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); -- cgit v1.2.3-70-g09d2