Commits

Steve Losh committed 5313f59

Add name mangling, a few preds, and clean up a bit.

Comments (0)

Files changed (4)

alienscript.jison

 true  { return 'TRUE'; }
 false  { return 'FALSE'; }
 
-[-+/*_<>=a-zA-Z.]+  { return 'SYMBOL'; }
+[-+/*_<>=a-zA-Z.?]+  { return 'SYMBOL'; }
 
 <<EOF>>    { return 'EOF'; }
 
     return "Symbol(\"" + this.name + "\")";
 };
 Symbol.prototype.__alienscript_tag = "S";
+function makeSymbol(name) {
+    return new Symbol(name);
+}
 
 function Keyword(name) {
     this.name = name;
 //
 // Primitives are built-in "functions" that are transformed directly into a JS
 // AST.  They differ from special forms in that they receive their arguments
-// pre-grown into ASTs.
+// already-grown into ASTs.
 var primitives = {
     ".": function(elements) {
         var obj = elements[0];
             arguments: args
         };
     },
-    "symbol": function(elements) {
-        var name = elements[0].name;
-        return {
-            type: "NewExpression",
-            callee: { type: "Identifier", name: "Symbol" },
-            arguments: [{ type: "Literal", value: name }]
-        };
-    },
     "list": function(elements) {
         return { type: "ArrayExpression", elements: elements };
     },
 //
 // grow() is the main entry point here, and will take an arbitrary ASS and
 // transform it into an AST.
+var mangleName = function(name) {
+    return name.replace(/[?]/g, "__QMARK");
+};
 var growSpecialForm = function(head, tail) {
     return special_forms[head.name](tail);
 };
     } else if (isNull(sexp) || isString(sexp) || isBoolean(sexp)) {
         return { type: 'Literal', value: sexp };
     } else if (isSymbol(sexp)) {
-        return { type: 'Identifier', name: sexp.name };
+        return { type: 'Identifier', name: mangleName(sexp.name) };
     } else if (util.isArray(sexp)) {
         var head = sexp[0];
         var tail = slice(sexp, 1);
 // Main -----------------------------------------------------------------------
 var context = function() {
     var ctx = vm.createContext();
-    ctx['console'] = console;
-    ctx['macros'] = macros;
-    ctx['Symbol'] = Symbol;
+    var insert = function(name, data) {
+        ctx[mangleName(name)] = data;
+    };
+
+    insert('console', console);
+    insert('macros', macros);
+    insert('symbol', makeSymbol);
+
+    insert('number?', isNumber);
+    insert('null?', isNull);
+    insert('string?', isString);
+    insert('boolean?', isBoolean);
+    insert('symbol?', isSymbol);
+
     return ctx;
 };
 var compile = function(filename, ctx, verbose) {
 };
 (function() {
     var ctx = context();
-    compile('stdlib.alien', ctx, true);
+    compile('stdlib.alien', ctx, false);
     compile(process.argv[2], ctx, false);
 })();
 
-(defmacro defn (name args body)
-  [(symbol def) name [(symbol fn) args body]])
-
-(defmacro when (cond body)
-  [(symbol if) cond body null])
+(. console log "pass")
 #!/bin/bash
 
-echo main.js sample.alien alienscript.jison | peat 'jison alienscript.jison -o alienparse.js && cat sample.alien | pygmentize -l clojure && echo && node main.js sample.alien | pygmentize -l js'
+echo main.js stdlib.alien sample.alien alienscript.jison | peat 'jison alienscript.jison -o alienparse.js && cat sample.alien | pygmentize -l clojure && echo && node main.js sample.alien | pygmentize -l js'