Binding toplevel main functions to onload event.
This commit is contained in:
parent
f88a7e12d9
commit
3ce0b42fd7
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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'}}");
|
||||
|
|
|
|||
|
|
@ -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();</textarea>
|
|||
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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user