浏览代码

Refactor the compiler and standard library a little

Matt Coles 9 年之前
父节点
当前提交
ccd68d0ee3
共有 4 个文件被更改,包括 13 次插入18 次删除
  1. 1 2
      .gitignore
  2. 12 16
      compiler.js
  3. 0 0
      libjs/stdlib.js
  4. 0 0
      libjs/str.js

+ 1 - 2
.gitignore

@@ -1,2 +1 @@
1
-myFirstProgram.mc
2
-output.js
1
+*.mc.js

+ 12 - 16
compiler.js

@@ -312,7 +312,7 @@ var generator = function (node) {
312 312
   switch (node.type) {
313 313
     case 'Prog':
314 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 316
       return program.join('\n')
317 317
       break
318 318
     case 'Statement':
@@ -322,13 +322,10 @@ var generator = function (node) {
322 322
       if (!node.callee.name.match('(def|if|repeat)')) {
323 323
         if (node.callee.name.match('include')) {
324 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 329
         } else {
333 330
           return (generator(node.callee) + '(' + node.args.map(generator).join(', ') + ')')
334 331
         }
@@ -379,15 +376,14 @@ var generator = function (node) {
379 376
 
380 377
 }
381 378
 
382
-
383
-// const myInput = '(assign twelve 12) (assign myvar (add twelve (subtract 6 2))) (log myvar)'
384 379
 const fileNameIn = process.argv[2]
385 380
 const fileNameOut = fileNameIn + '.js'
386 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 389
 fs.writeFileSync(fileNameOut, output)

lib/stdlib.js → libjs/stdlib.js


lib/str.js → libjs/str.js