diff options
author | Alon Zakai <alonzakai@gmail.com> | 2011-12-03 11:54:08 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2011-12-03 11:54:08 -0800 |
commit | 4bf23981037b66af07ddf9ee9b089a9f2b62b2a1 (patch) | |
tree | 738138a0078db1c3e6c6d3a9b421d99bfccaa946 /src/compiler.js | |
parent | 1fbe97bff634539433074c8156a82c2dee67399a (diff) |
refactoring towards supporting node.js
Diffstat (limited to 'src/compiler.js')
-rw-r--r-- | src/compiler.js | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/src/compiler.js b/src/compiler.js index 32c1120e..ea55d95e 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -8,11 +8,30 @@ try { } catch(e) {} var arguments_ = []; -var globalScope = this; -var ENVIRONMENT_IS_SHELL = typeof window === 'undefined'; +var ENVIRONMENT_IS_NODE = typeof process === 'object'; +var ENVIRONMENT_IS_WEB = typeof window === 'object'; +var ENVIRONMENT_IS_SHELL = !ENVIRONMENT_IS_WEB && !ENVIRONMENT_IS_NODE; -if (ENVIRONMENT_IS_SHELL) { +if (ENVIRONMENT_IS_NODE) { + // Expose functionality in the same simple way that the shells work + print = function(x) { + process.stdout.write(x + '\n'); + }; + printErr = function(x) { + process.stderr.write(x + '\n'); + }; + + var nodeFS = require('fs'); + + read = function(filename) { + if (filename[0] != '/') filename = __dirname.split('/').slice(0, -1).join('/') + '/src/' + filename; + return nodeFS.readFileSync(filename).toString(); + }; + + arguments_ = process.argv.slice(2); + +} else if (ENVIRONMENT_IS_SHELL) { // Polyfill over SpiderMonkey/V8 differences if (!this['read']) { read = function(f) { snarf(f) }; @@ -23,8 +42,8 @@ if (ENVIRONMENT_IS_SHELL) { } else { arguments_ = arguments; } -} else { - // We are on the web. + +} else if (ENVIRONMENT_IS_WEB) { printErr = function(x) { console.log(x); }; @@ -39,11 +58,17 @@ if (ENVIRONMENT_IS_SHELL) { if (this['arguments']) { arguments_ = arguments; } +} else { + throw 'Unknown runtime environment. Where are we?'; +} + +function globalEval(x) { + eval.call(null, x); } if (!this['load']) { load = function(f) { - eval.call(globalScope, read(f)); + globalEval(read(f)); }; } @@ -61,12 +86,10 @@ var ll_file = arguments_[1]; if (settings_file) { var settings = JSON.parse(read(settings_file)); for (setting in settings) { - this[setting] = settings[setting]; + eval(setting + ' = ' + JSON.stringify(settings[setting])); } } -var CONSTANTS = { 'QUANTUM_SIZE': QUANTUM_SIZE }; - if (CORRECT_SIGNS >= 2) { CORRECT_SIGNS_LINES = set(CORRECT_SIGNS_LINES); // for fast checking } @@ -114,7 +137,8 @@ load('parseTools.js'); load('intertyper.js'); load('analyzer.js'); load('jsifier.js'); -eval.call(globalScope, processMacros(preprocess(read('runtime.js')))); +globalEval(processMacros(preprocess(read('runtime.js')))); +Runtime.QUANTUM_SIZE = QUANTUM_SIZE; //=============================== // Main |