// Select all [ctrl+a] and then click print it (ctrl+p) to evaluate. Scroll down to last line to view results when it finishes. eval(readFile('Compiled_JavaScript_Compiler')) [object Object] /* Starting on Coffee Script parser http://jashkenas.github.com/coffee-script/ */ // See JavaScript Compiler for details // http://www.tinlizzie.org/ometa-js/#JavaScript_Compiler ometa CoffeeJSParser <: JSParser { space = super(#space) | fromTo('#', '\n') , newspecial = (``=>'') :s -> [s, s], primExpr = primExpr:p ( "." "!" "name":m "(" listOf(#expr, ','):as ")" -> [`unop, "!", [`send, m, p].concat(as)] | "." "!" "name":f -> [`unop, "!", [`getp, [`string, f], p]] ) | super("primExpr"), expr = orExpr:e ( "if" expr:t "else" expr:f -> [`condExpr, e, t, f] | ":" expr:rhs -> [`set, e, rhs] | "+=" expr:rhs -> [`mset, e, "+", rhs] | "-=" expr:rhs -> [`mset, e, "-", rhs] | "*=" expr:rhs -> [`mset, e, "*", rhs] | "/=" expr:rhs -> [`mset, e, "/", rhs] | "%=" expr:rhs -> [`mset, e, "%", rhs] // x &&= y => if (x) x=y else x; => x? x=y : x; // x ||= y => if (x) x else (x=y); => x? x : x=y | "&&=" expr:rhs -> [`condExpr, e, [`set, e, rhs], e] | "||=" expr:rhs -> [`condExpr, e, e, [`set, e, rhs]] | empty -> e ), orExpr = orExpr:x "or" andExpr:y -> [`binop, "||", x, y] | super("orExpr"), andExpr = andExpr:x "and" eqExpr:y -> [`binop, "&&", x, y] | super("andExpr"), eqExpr = eqExpr:x ( "==" relExpr:y -> [`binop, "===", x, y] | "!=" relExpr:y -> [`binop, "!==", x, y] | "===" relExpr:y -> [`binop, "===", x, y] | "!==" relExpr:y -> [`binop, "!==", x, y] | "is" relExpr:y -> [`binop, "===", x, y] | "isnt" relExpr:y -> [`binop, "!==", x, y] ) | relExpr | super("eqExpr"), unary = "not" postfix:p -> [`unop, "!", p] | super("unary"), primExprHd = "on" -> [`get, 'true'] | "yes" -> [`get, 'true'] | "off" -> [`get, 'false'] | "no" -> [`get, 'false'] | listOf(`formal, ','):fs "=>" srcElems:body -> [`func, fs, body] | super("primExprHd"), json = "{" jsonBinding*:bs "}" -> [`json].concat(bs) | super("json"), stmt = "while" expr:c "then" stmt:s "." -> [`while, c, s] | expr:f "unless" expr:e -> [`condExpr, e, 'undefined', f] | super("stmt") } // From BangJS i = "typeof mind != 'undefined' && mind != null"; i = "c: getManager('asdf').employees.!isEmpty()" a = CoffeeJSParser.matchAll(i, "topLevel") b = JSTranslator.match(a, "trans") // a == [begin, [unop, !, [send, isEmpty, [getp, [string, employees], [call, [get, getManager], [string, asdf]]]]]] // b == (!getManager("asdf")["employees"].isEmpty()) // DONE CoffeeJSParser.keywords = JSParser.keywords || {}; // aliases for &&, ||, !, ===, !==, true, false CoffeeJSParser.keywords["and"]=true CoffeeJSParser.keywords["or"]=true CoffeeJSParser.keywords["not"]=true CoffeeJSParser.keywords["is"]=true CoffeeJSParser.keywords["isnt"]=true CoffeeJSParser.keywords["on"]=true CoffeeJSParser.keywords["off"]=true CoffeeJSParser.keywords["yes"]=true CoffeeJSParser.keywords["no"]=true // TODO: // Statemetns: Functions, ifs, unless, while, switch when, and so on. // fs "=>" body -> [`func, fs, body] // "if" expr:e "then" expr:t "else" expr:f -> [`condExpr, e, t, f] // let_the_wild_rumpus_begin() unless answer is no // expr:f "unless" expr:e -> [`condExpr, e, 'undefined', f] // while supply > demand then buy(). // "while" expr:c "then" stmt:s "." -> [`while, c, s] // "while" expr: e stmts:s "." // // End blocks on a period (".") instead of { curly braces }. // // sc = spacesNoNl ('\n' | &'}' | end) // | ";" // | ".", // // block = srcElems:ss "." -> ss // // etc. // TEST CASES multiline_test = 'moby_dick: \"Call me Ishmael. Some years ago -- never mind how long precisely -- having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world...\" ' expect = function(expected, got) { if (expected !== got && expected != got) throw("Expected: " + expected + ", got: " + got); return expected === got; } objtest = "y : false json: { max: 11, ida: 10, tim: 12 } song: ['do', 're', 'mi', 'fa', 'so'] ages: { max: 11 ida: 10 tim: 12 } y &&= 1 y ||= 2 y ||= 3 # Comment: function(y) {return y*y;} Comment: function y => y*y y " aa = CoffeeJSParser.matchAll(objtest, "topLevel") bb = JSTranslator.match(aa, "trans") ans = eval(bb) //if (ans !== 2) throw("Expected: " + 2 + ", got: " + ans); // 2 expect(2, ans); expect(ages.max, json.max); expect(ages.ida, json.ida); expect(ages.tim, json.tim); expect(Comment(2),4); test = "c : on and off or not yes and not no or 1 is 2 - not false" aa = CoffeeJSParser.matchAll(test, "topLevel") bb = JSTranslator.match(aa, "trans") //c=(((true && false) || ((!true) && (!false))) || ((1) === ((2) - (!false)))) ans = eval(bb); expect(c, true); test = "while c>0 then --c." aa = CoffeeJSParser.matchAll(test, "topLevel") bb = JSTranslator.match(aa, "trans")