New features added.

Operator `clone` and anonymous function definition `(){}`.
This commit is contained in:
Onoshko Dan 2014-04-20 14:31:03 +07:00
parent 9f024d3a3d
commit 7bdb095cee
8 changed files with 104 additions and 8 deletions

View File

@ -14,12 +14,14 @@ ColaScript is a language that compiles in JavaScript. This language is similar t
bool exist = SOME??;
bool exist2 = isset SOME;
- `clone`
- `clone`, status: done
a = [];
b = [];
Array b = clone a;
b[0] = 584; // a == []
if object have method `__clone__`, object will be copied with it.
### Binary

View File

@ -20,6 +20,17 @@ main(){
console.log(a = c ? b);
b = undefined;
Array x, y;
x = [123];
y = clone x;
y.push(321);
console.log('original:',x,'cloned:',y);
y.forEach((val){
console.log(val);
});
console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}");
console.log(`is:`, location.href is String, `; isnt:`, 123 isnt Number, ";");

View File

@ -383,7 +383,7 @@ Cola.AST_Accessor = Cola.DEFNODE("Accessor", null, {
$documentation: "A setter/getter function. The `name` property is always null."
}, Cola.AST_Lambda);
Cola.AST_Function = Cola.DEFNODE("Function", null, {
Cola.AST_Function = Cola.DEFNODE("Function", "type", {
$documentation: "A function expression"
}, Cola.AST_Lambda);

View File

@ -66,6 +66,17 @@
console.log(a = c ? b);
b = undefined;
Array x, y;
x = [123];
y = clone x;
y.push(321);
console.log('original:',x,'cloned:',y);
y.forEach((val){
console.log(val);
});
console.log("a is {{isset a ? 'set' : 'undefiend'}}, b is {{b?? ? 'set' : 'undefined'}}");
console.log(`is:`, location.href is String, `; isnt:`, 123 isnt Number, ";");

View File

@ -49,7 +49,7 @@
!window.Cola && (window.Cola = {});
Cola.KEYWORDS = 'break case catch const continue debugger default delete do else finally for function if in instanceof new return switch throw try typeof var void while with';
Cola.cKEYWORDS = Cola.KEYWORDS.replace(' void', '') + ' isset is isnt class singleton injector';
Cola.cKEYWORDS = Cola.KEYWORDS.replace(' void', '') + ' clone isset is isnt class singleton injector';
Cola.KEYWORDS_ATOM = 'false null true';
Cola.cKEYWORDS_ATOM = Cola.KEYWORDS_ATOM + ' on yes off no';
@ -123,6 +123,7 @@ Cola.OPERATORS = [
"&&",
"||",
// ColaScript
"clone",
"isset",
"is",
"isnt",
@ -134,7 +135,7 @@ Cola.OPERATORS = [
];
Cola.cOPERATORS = Cola.makePredicate(Cola.OPERATORS);
Cola.OPERATORS = Cola.OPERATORS.slice(0, Cola.OPERATORS.length - 7);
Cola.OPERATORS = Cola.OPERATORS.slice(0, Cola.OPERATORS.length - 8);
Cola.OPERATORS.push('void');
Cola.OPERATORS = Cola.makePredicate(Cola.OPERATORS);
@ -730,6 +731,7 @@ Cola.UNARY_PREFIX = Cola.makePredicate([
"+"
]);
Cola.cUNARY_PREFIX = Cola.makePredicate([
"clone",
"isset",
"typeof",
"delete",
@ -1214,7 +1216,7 @@ Cola.Parser.prototype.for_in = function (init) {
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)
@ -1491,6 +1493,24 @@ Cola.Parser.prototype.expr_atom = function(allow_calls) {
if (this.is("punc")) {
switch (start.value) {
case "(":
if(!this.is_js){
var _this = this, balance = 1, isfun = false;
this.dumpS();
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_Function);
}
this.next();
var ex = this.expression(true);
ex.start = start;

View File

@ -59,4 +59,41 @@ function $_cola_isntset(_object){
}
$_cola_isntset.i = 4;
$_cola = $_cola_is + $_cola_isnt + $_cola_modulo + $_cola_isset + $_cola_isntset;
function $_cola_clone(_item) {
if (_item === undefined || _item === null) return _item;
if (_item.__clone__ instanceof Function) return _item.__clone__();
var result, types = [ Number, String, Boolean ];
for (var i in types)
if(types.hasOwnProperty(i) && _item instanceof types[i])
return type( _item );
if (_item.__proto__ === Array.prototype) {
result = [];
_item.forEach(function(child, index, array) {
result[index] = $_cola_clone( child );
});
return result;
}
if (!(_item instanceof Object)) return _item;
if (_item.nodeType && _item.cloneNode instanceof Function) return _item.cloneNode( true );
if (!_item.prototype) {
if (_item instanceof Date) return new Date(_item);
if (_item instanceof Function) return _item;
result = {};
for (var i in _item) result[i] = $_cola_clone( _item[i] );
result.__proto__ = _item.__proto__;
return result;
}
return _item;
}
$_cola_clone.i = 5;
$_cola = $_cola_is + $_cola_isnt + $_cola_modulo + $_cola_isset + $_cola_isntset + $_cola_clone;

View File

@ -191,6 +191,22 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(){
node = new Cola.AST_Call(props);
} else
if(node instanceof Cola.AST_UnaryPrefix && node.operator == 'clone'){
$_cola_hash[$_cola_clone.i] = true;
props = {
args : [node.expression],
start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_clone' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' })
};
props.expression = new Cola.AST_SymbolRef({
name : '$_cola_clone',
start : props.start,
end : props.start
});
node = new Cola.AST_Call(props);
} else
if(node instanceof Cola.AST_StringTemplate){
newNode = new Cola.AST_Binary({
operator : '+',

View File

@ -304,9 +304,9 @@ Cola.Dictionary.prototype = {
Cola.clone = function (item) {
if (item === undefined || item === null) return item;
if (item.__clone__ instanceof Function) return item.__clone__();
var result, types = [ Number, String, Boolean ];
for (var i in types)
if(types.hasOwnProperty(i) && item instanceof types[i])
return type( item );
@ -327,7 +327,6 @@ Cola.clone = function (item) {
if (!item.prototype) {
if (item instanceof Date) return new Date(item);
if (item instanceof Function) return item;
if (item.clone instanceof Function) return item.clone();
result = {};
for (var i in item) result[i] = Cola.clone( item[i] );