new declaration of functions added
This commit is contained in:
parent
bf3979f327
commit
2cff0f2f3e
|
|
@ -89,6 +89,9 @@ Cola.DEFNODE = function (type, props, methods, base) {
|
|||
};
|
||||
|
||||
Cola.AST_Token = Cola.DEFNODE("Token", "type value line col pos endpos nlb comments_before file", {
|
||||
clone: function() {
|
||||
return new this.CTOR(this);
|
||||
}
|
||||
}, null);
|
||||
|
||||
Cola.AST_Node = Cola.DEFNODE("Node", "start end", {
|
||||
|
|
@ -384,7 +387,7 @@ Cola.AST_Function = Cola.DEFNODE("Function", null, {
|
|||
$documentation: "A function expression"
|
||||
}, Cola.AST_Lambda);
|
||||
|
||||
Cola.AST_Defun = Cola.DEFNODE("Defun", null, {
|
||||
Cola.AST_Defun = Cola.DEFNODE("Defun", "type", {
|
||||
$documentation: "A function definition"
|
||||
}, Cola.AST_Lambda);
|
||||
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@
|
|||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<textarea id="source" onkeyup="compile()">function main(){
|
||||
<textarea id="source" onkeyup="compile()">main(){
|
||||
console.log(`
|
||||
|
||||
Hello!
|
||||
|
|
|
|||
61
lib/parse.js
61
lib/parse.js
|
|
@ -255,6 +255,7 @@ Cola.Tokenizer = function ($TEXT, filename, is_js, html5_comments) {
|
|||
regex_allowed : false,
|
||||
comments_before : []
|
||||
};
|
||||
this.dumps = {};
|
||||
|
||||
if(!is_js) this.S.string = {
|
||||
at : [ new Cola.Tokenizer.StringInfo() ],
|
||||
|
|
@ -298,6 +299,14 @@ Cola.Tokenizer.with_eof_error = function (eof_error, cont) {
|
|||
};
|
||||
};
|
||||
|
||||
Cola.Tokenizer.prototype.dumpS = function () {
|
||||
this.dumps = Cola.clone(this.S);
|
||||
};
|
||||
|
||||
Cola.Tokenizer.prototype.restoreS = function () {
|
||||
this.S = this.dumps;
|
||||
};
|
||||
|
||||
Cola.Tokenizer.prototype.peek = function (offset) { return this.S.text.charAt(this.S.pos + (offset ? offset : 0)); };
|
||||
|
||||
Cola.Tokenizer.prototype.next = function (signal_eof, in_string) {
|
||||
|
|
@ -817,6 +826,7 @@ Cola.Parser = function ($TEXT, options) {
|
|||
};
|
||||
this.S.input.context = function(){ return _this.tokenizer.context() };
|
||||
this.S.token = this.next();
|
||||
this.dumps = {};
|
||||
|
||||
if(this.is_js){
|
||||
this.UNARY_PREFIX = Cola.UNARY_PREFIX;
|
||||
|
|
@ -855,6 +865,24 @@ Cola.Parser.prototype.parse = function () {
|
|||
})();
|
||||
};
|
||||
|
||||
Cola.Parser.prototype.dumpS = function () {
|
||||
this.dumps = Cola.clone(this.S);
|
||||
this.tokenizer.dumpS();
|
||||
};
|
||||
|
||||
Cola.Parser.prototype.restoreS = function () {
|
||||
this.tokenizer.restoreS();
|
||||
this.S = this.dumps;
|
||||
};
|
||||
|
||||
Cola.Parser.prototype.next_until = function (until) {
|
||||
while(true){
|
||||
if(until(this.S.token)) break;
|
||||
this.next();
|
||||
}
|
||||
return this.S.token;
|
||||
}
|
||||
|
||||
Cola.Parser.prototype.is = function (type, value) {
|
||||
return Cola.is_token(this.S.token, type, value);
|
||||
};
|
||||
|
|
@ -961,7 +989,33 @@ Cola.Parser.prototype.statement = Cola.Parser.embed_tokens(function() {
|
|||
return this.simple_statement();
|
||||
|
||||
case "name":
|
||||
if(!this.is_js && Cola.is_token(this.peek(), "name")) return type = this.S.token.value, this.next(), tmp = this.var_(false, type), this.semicolon(), tmp;
|
||||
if(!this.is_js && Cola.is_token(this.peek(), "name")){
|
||||
type = this.S.token.value, this.next();
|
||||
if(Cola.is_token(this.peek(), "punc", "(")) return this.function_(Cola.AST_Defun, type);
|
||||
return tmp = this.var_(false, type), this.semicolon(), tmp;
|
||||
}
|
||||
|
||||
if(!this.is_js){
|
||||
var _this = this, balance = 1, isfun = false;
|
||||
this.dumpS();
|
||||
|
||||
this.next();
|
||||
if(this.is('punc', '(')){
|
||||
this.next();
|
||||
this.next_until(function(){
|
||||
if(_this.is('punc', '(')) balance++;
|
||||
else if(_this.is('punc', ')')) balance--;
|
||||
|
||||
return balance == 0 || _this.is('eof');
|
||||
});
|
||||
isfun = (this.next(), this.is('punc','{'));
|
||||
}
|
||||
|
||||
this.restoreS();
|
||||
|
||||
if(isfun) return this.function_(Cola.AST_Defun);
|
||||
}
|
||||
|
||||
return Cola.is_token(this.peek(), "punc", ":")
|
||||
? this.labeled_statement()
|
||||
: this.simple_statement();
|
||||
|
|
@ -1158,13 +1212,16 @@ Cola.Parser.prototype.for_in = function (init) {
|
|||
});
|
||||
};
|
||||
|
||||
Cola.Parser.prototype.function_ = function(ctor) {
|
||||
Cola.Parser.prototype.function_ = function(ctor, type) {
|
||||
!type && (type = "dynamic");
|
||||
|
||||
var in_statement = ctor === Cola.AST_Defun, _this = this;
|
||||
var name = this.is("name") ? this.as_symbol(in_statement ? Cola.AST_SymbolDefun : Cola.AST_SymbolLambda) : null;
|
||||
if (in_statement && !name)
|
||||
this.unexpected();
|
||||
this.expect("(");
|
||||
return new ctor({
|
||||
type: type,
|
||||
name: name,
|
||||
argnames: (function(first, a){
|
||||
while (!_this.is("punc", ")")) {
|
||||
|
|
|
|||
51
lib/utils.js
51
lib/utils.js
|
|
@ -300,4 +300,55 @@ Cola.Dictionary.prototype = {
|
|||
ret.push(f(this._values[i], i.substr(1)));
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
Cola.clone = function (item) {
|
||||
if (!item) { return item; } // null, undefined values check
|
||||
|
||||
var types = [ Number, String, Boolean ],
|
||||
result;
|
||||
|
||||
// normalizing primitives if someone did new String('aaa'), or new Number('444');
|
||||
types.forEach(function(type) {
|
||||
if (item instanceof type) {
|
||||
result = type( item );
|
||||
}
|
||||
});
|
||||
|
||||
if (typeof result == "undefined") {
|
||||
if (Object.prototype.toString.call( item ) === "[object Array]") {
|
||||
result = [];
|
||||
item.forEach(function(child, index, array) {
|
||||
result[index] = Cola.clone( child );
|
||||
});
|
||||
} else if (typeof item == "object") {
|
||||
// testing that this is DOM
|
||||
if (item.nodeType && typeof item.cloneNode == "function") {
|
||||
var result = item.cloneNode( true );
|
||||
} else if (!item.prototype) { // check that this is a literal
|
||||
if (item instanceof Date) {
|
||||
result = new Date(item);
|
||||
} else {
|
||||
// it is an object literal
|
||||
result = {};
|
||||
for (var i in item) {
|
||||
result[i] = Cola.clone( item[i] );
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// depending what you would like here,
|
||||
// just keep the reference, or create new object
|
||||
if (false && item.constructor) {
|
||||
// would not advice to do that, reason? Read below
|
||||
result = new item.constructor();
|
||||
} else {
|
||||
result = item;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result = item;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user