瀏覽代碼

Add ability to set a LIBJS_PATH when compiling

Matt Coles 9 年之前
父節點
當前提交
74769719a8
共有 2 個文件被更改,包括 8 次插入3 次删除
  1. 2 1
      README.md
  2. 6 2
      compiler.js

+ 2 - 1
README.md

@@ -11,7 +11,8 @@ In addition to the regular include, there is a preprocessing directive called ``
11 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 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 17
 Functions and variables are in different scopes, so variables can have the same names as functions - even builtins -
17 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,7 +359,7 @@ var generator = function (node) {
359 359
   switch (node.type) {
360 360
     case 'Prog':
361 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 363
       return program.join('\n')
364 364
       break
365 365
     case 'Statement':
@@ -370,7 +370,7 @@ var generator = function (node) {
370 370
         if (node.callee.name.match('include')) {
371 371
           // Include is a special function and we will write the generation ourselves
372 372
           return node.args.map((arg) => {
373
-            let lib = './libjs/' + arg.value + '.js'
373
+            let lib = libjsPath + '/' + arg.value + '.js'
374 374
             return ('var _' + arg.value + ' = require("' + lib + '")(this)')
375 375
           }).join("\n")
376 376
         } else {
@@ -430,10 +430,14 @@ var generator = function (node) {
430 430
 
431 431
 }
432 432
 
433
+let libjsPath = process.env['LIBJS_PATH']
433 434
 const fileNameIn = process.argv[2]
434 435
 const fileNameOut = fileNameIn + '.js'
435 436
 const myInput = fs.readFileSync(process.argv[2], { encoding: 'utf-8' })
436 437
 
438
+if (libjsPath === '') {
439
+  libjsPath = './libjs'
440
+}
437 441
 const preProcessedInput = preprocess(fileNameIn, myInput) // Run the preprocessor to evaluate any `source's
438 442
 const myTokens = tokenizer(preProcessedInput) // Convert our input into individual tokens
439 443
 const parsedTree = parser(myTokens) // Convert these tokens into a syntax tree