-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·123 lines (112 loc) · 3.07 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#! /usr/bin/env node
const fs = require('fs')
const readline = require('readline')
const chalk = require('chalk')
const Environment = require('./environment')
const { run } = require('./index')
const { formatLoxError } = require('./errors')
let options = {
debug: false,
history: 30,
prompt: '>'
}
const printReturnValue = lastLine =>
JSON.stringify(
lastLine && typeof lastLine.toString === 'function' ? lastLine.toString() : lastLine
)
const printErrorMessage = (e, code) => {
if (options.debug) console.log(e)
const { oneLiner, preErrorSection, errorSection, postErrorSection } = formatLoxError(e, code)
console.error(oneLiner)
if (errorSection) {
console.error(preErrorSection + chalk.bgRed(errorSection) + postErrorSection)
}
}
let cache = []
const getChar = () => {
if (cache.length <= 0) {
const input = fs.readFileSync(0).toString()
input.split('').forEach(char => cache.push(char))
}
return cache.shift()
}
const makeCLIEnvironment = () => {
const env = new Environment()
env.setBuiltin('readFile', (_vars, args) => fs.readFileSync(args[0], 'utf8'))
env.setBuiltin('exit', (_vars, args) => process.exit(args[0] || 0))
env.setBuiltin('chr', (_vars, args) => String.fromCharCode(args[0]))
env.setBuiltin('getc', () => {
const val = getChar()
return val ? val.charCodeAt(0) : -1
})
return env
}
const runPrompt = () => {
const prompt = options.prompt + ' '
process.stdout.write(prompt)
const lineReader = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: true,
prompt: prompt,
historySize: +options.history
})
const env = makeCLIEnvironment()
lineReader.on('line', line => {
let code = line
if (!line.endsWith(';') && !line.endsWith('}')) code += ';'
// TODO: Support multi-line block statements
try {
const lastLine = run(code, env, console.log, options.debug)
console.log(printReturnValue(lastLine))
process.stdout.write(prompt)
} catch (e) {
printErrorMessage(e, code)
}
})
}
const runFile = filename => {
try {
const file = fs.readFileSync(filename, 'utf8')
const env = makeCLIEnvironment()
try {
run(file, env, console.log, options.debug)
} catch (e) {
printErrorMessage(e, file)
}
} catch (e) {
console.error(`YALI could not read the file ${filename}`)
console.error(e)
}
}
const optionRegex = /--(\w+)(?:=(.+))?/
const processOptions = args =>
args
.map(arg => {
const match = optionRegex.exec(arg)
if (match) {
const [, option, value] = match
if (!value) {
options[option] = !options[option]
} else {
options[option] = value
}
} else {
return arg
}
})
.filter(Boolean)
const main = argv => {
process.title = 'YALI'
const args = processOptions(argv.slice(2))
if (options.debug) console.log(options)
if (args.length > 1) {
console.error('Usage: yali [script]')
return 64
} else if (args.length === 1) {
runFile(args[0])
} else {
runPrompt()
}
}
main(process.argv)