From 3ce0b42fd7c9c78a3092bbdd9cf408b92dbb5a07 Mon Sep 17 00:00:00 2001 From: Onoshko Dan Date: Thu, 24 Apr 2014 00:05:34 +0700 Subject: [PATCH] Binding toplevel `main` functions to onload event. --- README.md | 4 ++-- demo.cola | 3 +++ lib/index.html | 5 ++++- lib/translate.js | 27 +++++++++++++++++++++++++-- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7d8b5643..3e18c4a4 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,7 @@ ColaScript is a language that compiles in JavaScript. This language is similar t console.log('Hello World!'); } -- binding toplevel `main` functions to onload event +- binding toplevel `main` functions to onload event, status: done // lib.cola @@ -326,4 +326,4 @@ ColaScript is a language that compiles in JavaScript. This language is similar t ### Statistic - 34 feature ( without classes ) -- 22 done +- 23 done diff --git a/demo.cola b/demo.cola index adf18902..8a8f01d9 100644 --- a/demo.cola +++ b/demo.cola @@ -27,6 +27,9 @@ main(){ y[] = 321; console.log('original:',x,'cloned:',y, y[]); + y[0..1] = [1..10]; + console.log(y[0..4]); + y.forEach((val) => console.log(val)); console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}"); diff --git a/lib/index.html b/lib/index.html index 08ae9c27..4dd1bdcb 100644 --- a/lib/index.html +++ b/lib/index.html @@ -93,6 +93,9 @@ y[] = 321; console.log('original:',x,'cloned:',y, y[]); + y[0..1] = [1..10]; + console.log(y[0..4]); + y.forEach((val) => console.log(val)); console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}"); @@ -146,7 +149,7 @@ main(); try { // 1. compile ast = Cola.parse(source, { is_js : isjs.checked }); - if(!isjs.checked) ast = ast.toJavaScript(); + if(!isjs.checked) ast = ast.toJavaScript({ main_binding : false }); ast.print(stream); translationArea.value = stream.toString(); diff --git a/lib/translate.js b/lib/translate.js index 4948b657..4b9f8998 100644 --- a/lib/translate.js +++ b/lib/translate.js @@ -37,15 +37,38 @@ "use strict"; !this.Cola && (this.Cola = {}); -Cola.AST_Toplevel.prototype.toJavaScript = function(){ +Cola.AST_Toplevel.prototype.toJavaScript = function(options){ if(this.language == 'js') return this; this.language = 'js'; + options = Cola.defaults(options, { + main_binding : true, + main_event : 'DOMContentLoaded' + }); + var $_cola_ast = Cola.parse(Cola.$_cola, { is_js : true}), $_cola_hash = {}, _this, tt = new Cola.TreeTransformer(function(node, descend){ var newNode, props = {}, parent = this.parent(); node = node.clone(); + if(options.main_binding && parent instanceof Cola.AST_Toplevel && node instanceof Cola.AST_Defun && node.name instanceof Cola.AST_SymbolDefun && node.name.name == "main"){ + props = { + args : [new Cola.AST_String({ value : options.main_event }), node, new Cola.AST_False()] + }; + props.expression = new Cola.AST_Dot({ + property : 'addEventListener', + //start : props.start, + //end : new Cola.AST_Token({ nlb : false, type : 'name', value : 'pow' }), + expression : new Cola.AST_SymbolRef({ name : 'window' }) + }); + + node = new Cola.AST_SimpleStatement({ + body : new Cola.AST_Call(props), + start : node.start, + end : node.left + }); + } else + if(node instanceof Cola.AST_Binary && node.operator == '**'){ props = { args : [node.left, node.right], @@ -56,7 +79,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){ property : 'pow', //start : props.start, //end : new Cola.AST_Token({ nlb : false, type : 'name', value : 'pow' }), - expression : new Cola.AST_SymbolRef({ name : 'Math', start : props.start, end : props.start }) + expression : new Cola.AST_SymbolRef({ name : 'Math' }) }); node = new Cola.AST_Call(props);