|
|
|
|
|
|
312
|
switch (node.type) {
|
312
|
switch (node.type) {
|
|
313
|
case 'Prog':
|
313
|
case 'Prog':
|
|
314
|
let program = node.body.map(generator)
|
314
|
let program = node.body.map(generator)
|
|
315
|
- program.unshift('var _ = require("./lib/stdlib.js")(this)')
|
|
|
|
|
|
315
|
+ program.unshift('var _ = require("./libjs/stdlib.js")(this)')
|
|
316
|
return program.join('\n')
|
316
|
return program.join('\n')
|
|
317
|
break
|
317
|
break
|
|
318
|
case 'Statement':
|
318
|
case 'Statement':
|
|
|
|
|
|
|
322
|
if (!node.callee.name.match('(def|if|repeat)')) {
|
322
|
if (!node.callee.name.match('(def|if|repeat)')) {
|
|
323
|
if (node.callee.name.match('include')) {
|
323
|
if (node.callee.name.match('include')) {
|
|
324
|
// Include is a special function and we will write the generation ourselves
|
324
|
// Include is a special function and we will write the generation ourselves
|
|
325
|
- if (node.args.length > 1) {
|
|
|
|
326
|
- console.error("Compiler Error: (include) may only take 1 argument!")
|
|
|
|
327
|
- process.exit(1)
|
|
|
|
328
|
- } else {
|
|
|
|
329
|
- let lib = './lib/' + node.args[0].value + '.js'
|
|
|
|
330
|
- return ('var _' + node.args[0].value + ' = require("' + lib + '")(this)')
|
|
|
|
331
|
- }
|
|
|
|
|
|
325
|
+ return node.args.map((arg) => {
|
|
|
|
326
|
+ let lib = './libjs/' + arg.value + '.js'
|
|
|
|
327
|
+ return ('var _' + arg.value + ' = require("' + lib + '")(this)')
|
|
|
|
328
|
+ }).join("\n")
|
|
332
|
} else {
|
329
|
} else {
|
|
333
|
return (generator(node.callee) + '(' + node.args.map(generator).join(', ') + ')')
|
330
|
return (generator(node.callee) + '(' + node.args.map(generator).join(', ') + ')')
|
|
334
|
}
|
331
|
}
|
|
|
|
|
|
|
379
|
|
376
|
|
|
380
|
}
|
377
|
}
|
|
381
|
|
378
|
|
|
382
|
-
|
|
|
|
383
|
-// const myInput = '(assign twelve 12) (assign myvar (add twelve (subtract 6 2))) (log myvar)'
|
|
|
|
384
|
const fileNameIn = process.argv[2]
|
379
|
const fileNameIn = process.argv[2]
|
|
385
|
const fileNameOut = fileNameIn + '.js'
|
380
|
const fileNameOut = fileNameIn + '.js'
|
|
386
|
const myInput = fs.readFileSync(process.argv[2], { encoding: 'utf-8' })
|
381
|
const myInput = fs.readFileSync(process.argv[2], { encoding: 'utf-8' })
|
|
387
|
-const preProcessedInput = preprocess(myInput)
|
|
|
|
388
|
-const myTokens = tokenizer(preProcessedInput)
|
|
|
|
389
|
-const parsedTree = parser(myTokens)
|
|
|
|
390
|
-const transformedTree = transformer(parsedTree)
|
|
|
|
391
|
-//console.log(JSON.stringify(transformedTree,null,2))
|
|
|
|
392
|
-const output = generator(transformedTree)
|
|
|
|
|
|
382
|
+
|
|
|
|
383
|
+const preProcessedInput = preprocess(myInput) // Run the preprocessor to evaluate any `source's
|
|
|
|
384
|
+const myTokens = tokenizer(preProcessedInput) // Convert our input into individual tokens
|
|
|
|
385
|
+const parsedTree = parser(myTokens) // Convert these tokens into a syntax tree
|
|
|
|
386
|
+const transformedTree = transformer(parsedTree) // Now put the tree into an easily traversable format for our generator
|
|
|
|
387
|
+const output = generator(transformedTree) // Generate the final JS code
|
|
|
|
388
|
+
|
|
393
|
fs.writeFileSync(fileNameOut, output)
|
389
|
fs.writeFileSync(fileNameOut, output)
|