WHAT: this is a blog where i collect news about the usage of javascript a serious programming language on the client side and server side. WHY: because it seams to be the most ubiquitous language and has functions as first class objects, and closures and ....

Tuesday, July 1, 2008

Extending Javascript the easy way!!

Ometa by Alessandro Warth is a great environment, a must see!

Think of extending javascript with some little language extensions?
Writing a new / extending an existing compiler gives you a headache?
With OMeta/JS you can do it right in your javascript code:

Extend the Ometa Javascript parser (JSParser implemented in javascript) with a lets say say statement: Here it just does an alert();
Then switch the translator an execute the your new Javascript language right there:


eval(readFile('Compiled_JavaScript_Compiler'))

ometa EJSParser <: JSParser {
isKeyword :x = ?(x == 'say') | super(`isKeyword, x),
stmt = "say" expr:x sc -> ["call", ["get", "alert"], x]
| super("stmt")
}
EJSParser.matchAll("if (true) say 1+1", "stmt")

oldTranslateCode = translateCode

translateCode = function(s) {
var tree = EJSParser.matchAll(s, "topLevel")
var code = JSTranslator.match(tree, "trans")
return code
}

// evaluate the next statement in a different "do-it" than the above
say "hello world"

translateCode = oldTranslateCode



Some said Lively Kernel uses it. Well svg is nice. But why not stay with DOM nodes and be portable...

No comments: