Procházet zdrojové kódy

Add ability to set a LIBJS_PATH when compiling

Matt Coles před 9 roky
rodič
revize
74769719a8
2 změnil soubory, kde provedl 8 přidání a 3 odebrání
  1. 2 1
      README.md
  2. 6 2
      compiler.js

+ 2 - 1
README.md

11
 to just directly insert the contents of `<filename>` into the file. -Instead of wasting the compilers energy checking for circular sources, you have two options, to not be so stupid or wait for the call stack to overflow.- Yay now the compiler protects you against circular sources, or sourcing the same file more than once by skipping these when it encounters them, this means that order is now important. Although it always was.
11
 to just directly insert the contents of `<filename>` into the file. -Instead of wasting the compilers energy checking for circular sources, you have two options, to not be so stupid or wait for the call stack to overflow.- Yay now the compiler protects you against circular sources, or sourcing the same file more than once by skipping these when it encounters them, this means that order is now important. Although it always was.
12
 
12
 
13
 The compiler runs like `node compiler.js file.mc` where `file.mc` is the file you wish to compile, and this will produce a
13
 The compiler runs like `node compiler.js file.mc` where `file.mc` is the file you wish to compile, and this will produce a
14
-`file.mc.js` which requires the `libjs` directory to be present in the same directory when running for now at least.
14
+`file.mc.js` which requires the `libjs` directory to be present in the same directory when running or for the environment variable
15
+`LIBJS_PATH` to have been set whilst compiling.
15
 
16
 
16
 Functions and variables are in different scopes, so variables can have the same names as functions - even builtins -
17
 Functions and variables are in different scopes, so variables can have the same names as functions - even builtins -
17
 thus making `(assign assign 5)` a totally okay thing to do. However note that builtins and defined functions are in the same scope no matter what and attempting to define a function with the same name as a builtin will not work properly.
18
 thus making `(assign assign 5)` a totally okay thing to do. However note that builtins and defined functions are in the same scope no matter what and attempting to define a function with the same name as a builtin will not work properly.

+ 6 - 2
compiler.js

359
   switch (node.type) {
359
   switch (node.type) {
360
     case 'Prog':
360
     case 'Prog':
361
       let program = node.body.map(generator)
361
       let program = node.body.map(generator)
362
-      program.unshift('var _ = require("./libjs/stdlib.js")(this)')
362
+      program.unshift('var _ = require("' + libjsPath + '/stdlib.js")(this)')
363
       return program.join('\n')
363
       return program.join('\n')
364
       break
364
       break
365
     case 'Statement':
365
     case 'Statement':
370
         if (node.callee.name.match('include')) {
370
         if (node.callee.name.match('include')) {
371
           // Include is a special function and we will write the generation ourselves
371
           // Include is a special function and we will write the generation ourselves
372
           return node.args.map((arg) => {
372
           return node.args.map((arg) => {
373
-            let lib = './libjs/' + arg.value + '.js'
373
+            let lib = libjsPath + '/' + arg.value + '.js'
374
             return ('var _' + arg.value + ' = require("' + lib + '")(this)')
374
             return ('var _' + arg.value + ' = require("' + lib + '")(this)')
375
           }).join("\n")
375
           }).join("\n")
376
         } else {
376
         } else {
430
 
430
 
431
 }
431
 }
432
 
432
 
433
+let libjsPath = process.env['LIBJS_PATH']
433
 const fileNameIn = process.argv[2]
434
 const fileNameIn = process.argv[2]
434
 const fileNameOut = fileNameIn + '.js'
435
 const fileNameOut = fileNameIn + '.js'
435
 const myInput = fs.readFileSync(process.argv[2], { encoding: 'utf-8' })
436
 const myInput = fs.readFileSync(process.argv[2], { encoding: 'utf-8' })
436
 
437
 
438
+if (libjsPath === '') {
439
+  libjsPath = './libjs'
440
+}
437
 const preProcessedInput = preprocess(fileNameIn, myInput) // Run the preprocessor to evaluate any `source's
441
 const preProcessedInput = preprocess(fileNameIn, myInput) // Run the preprocessor to evaluate any `source's
438
 const myTokens = tokenizer(preProcessedInput) // Convert our input into individual tokens
442
 const myTokens = tokenizer(preProcessedInput) // Convert our input into individual tokens
439
 const parsedTree = parser(myTokens) // Convert these tokens into a syntax tree
443
 const parsedTree = parser(myTokens) // Convert these tokens into a syntax tree