Skip to content

Commit

Permalink
Add Array and require to cli
Browse files Browse the repository at this point in the history
Added `Array` class and `require` function to the node cli implementation
  • Loading branch information
danman113 authored Jun 4, 2021
1 parent 309f6a2 commit f765c11
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,46 @@ const getChar = () => {
return cache.shift()
}

const makeCLIEnvironment = () => {
class LoxArray {
init(_vars, args) {
this.internalArray = Array(args[0] || 0).fill(null)
}
at(_vars, args) {
return this.internalArray[args[0]] ?? null
}
get(_vars, args) {
return this.internalArray[args[0]] ?? null
}
set(_vars, args) {
// TODO: Throw error when set is incorrect
if (args[0] > this.internalArray.length) return null
this.internalArray[args[0]] = args[1]
return args[1]
}
length() {
return this.internalArray.length ?? null
}
}

const makeCLIEnvironment = (printfn, debug) => {
const evalFile = (_vars, args) => {
const code = fs.readFileSync(args[0], 'utf8')
const newEnv = makeCLIEnvironment(printfn, debug)
let lastLine
try {
lastLine = run(code, newEnv, printfn, debug)
} catch (e) {
console.log('Error importing', args[0], ':', e)
}
return newEnv
}
const env = new Environment()
env.setBuiltin('readFile', (_vars, args) => fs.readFileSync(args[0], 'utf8'))
env.setBuiltin('evalFile', (...args) => evalFile(...args) && null)
env.setBuiltin('require', (...args) => evalFile(...args).map.get('exports'))
env.setBuiltin('exit', (_vars, args) => process.exit(args[0] || 0))
env.setBuiltin('chr', (_vars, args) => String.fromCharCode(args[0]))
env.setBuiltinClass('Array', LoxArray)
env.setBuiltin('getc', () => {
const val = getChar()
return val ? val.charCodeAt(0) : -1
Expand All @@ -57,7 +92,7 @@ const runPrompt = () => {
prompt: prompt,
historySize: +options.history
})
const env = makeCLIEnvironment()
const env = makeCLIEnvironment(console.log, options.debug)

lineReader.on('line', line => {
let code = line
Expand All @@ -76,7 +111,7 @@ const runPrompt = () => {
const runFile = filename => {
try {
const file = fs.readFileSync(filename, 'utf8')
const env = makeCLIEnvironment()
const env = makeCLIEnvironment(console.log, options.debug)
try {
run(file, env, console.log, options.debug)
} catch (e) {
Expand Down

0 comments on commit f765c11

Please sign in to comment.