Binding toplevel main functions to onload event.

This commit is contained in:
Onoshko Dan 2014-04-24 00:05:34 +07:00
parent f88a7e12d9
commit 3ce0b42fd7
4 changed files with 34 additions and 5 deletions

View File

@ -212,7 +212,7 @@ ColaScript is a language that compiles in JavaScript. This language is similar t
console.log('Hello World!'); console.log('Hello World!');
} }
- binding toplevel `main` functions to onload event - binding toplevel `main` functions to onload event, status: done
// lib.cola // lib.cola
@ -326,4 +326,4 @@ ColaScript is a language that compiles in JavaScript. This language is similar t
### Statistic ### Statistic
- 34 feature ( without classes ) - 34 feature ( without classes )
- 22 done - 23 done

View File

@ -27,6 +27,9 @@ main(){
y[] = 321; y[] = 321;
console.log('original:',x,'cloned:',y, y[]); console.log('original:',x,'cloned:',y, y[]);
y[0..1] = [1..10];
console.log(y[0..4]);
y.forEach((val) => console.log(val)); y.forEach((val) => console.log(val));
console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}"); console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}");

View File

@ -93,6 +93,9 @@
y[] = 321; y[] = 321;
console.log('original:',x,'cloned:',y, y[]); console.log('original:',x,'cloned:',y, y[]);
y[0..1] = [1..10];
console.log(y[0..4]);
y.forEach((val) => console.log(val)); y.forEach((val) => console.log(val));
console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}"); console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}");
@ -146,7 +149,7 @@ main();</textarea>
try { try {
// 1. compile // 1. compile
ast = Cola.parse(source, { is_js : isjs.checked }); 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); ast.print(stream);
translationArea.value = stream.toString(); translationArea.value = stream.toString();

View File

@ -37,15 +37,38 @@
"use strict"; "use strict";
!this.Cola && (this.Cola = {}); !this.Cola && (this.Cola = {});
Cola.AST_Toplevel.prototype.toJavaScript = function(){ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
if(this.language == 'js') return this; if(this.language == 'js') return this;
this.language = 'js'; 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, var $_cola_ast = Cola.parse(Cola.$_cola, { is_js : true}), $_cola_hash = {}, _this,
tt = new Cola.TreeTransformer(function(node, descend){ tt = new Cola.TreeTransformer(function(node, descend){
var newNode, props = {}, parent = this.parent(); var newNode, props = {}, parent = this.parent();
node = node.clone(); 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 == '**'){ if(node instanceof Cola.AST_Binary && node.operator == '**'){
props = { props = {
args : [node.left, node.right], args : [node.left, node.right],
@ -56,7 +79,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){
property : 'pow', property : 'pow',
//start : props.start, //start : props.start,
//end : new Cola.AST_Token({ nlb : false, type : 'name', value : 'pow' }), //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); node = new Cola.AST_Call(props);