operator ? to sign an argument as not-required
int sqr(int x) => x ** 2; sqr(2); // 4 sqr(); // Exception int sqrt(int x?) => x ** 2; sqr(); // NaN
This commit is contained in:
parent
c9f4a2cd12
commit
aec283f0b4
20
README.md
20
README.md
|
|
@ -378,16 +378,7 @@ Future plans
|
||||||
int data.getFriendsCount() => this.friends.length;
|
int data.getFriendsCount() => this.friends.length;
|
||||||
|
|
||||||
- operator `?.`. status: done
|
- operator `?.`. status: done
|
||||||
- Negate array accessor ( getter )
|
- operator `?` to sign an argument as not-required. status: done
|
||||||
|
|
||||||
arr[-1]; // last element
|
|
||||||
|
|
||||||
only for static negate index, for other cases you can use `%` unary prefix:
|
|
||||||
|
|
||||||
int index = -10;
|
|
||||||
arr[%index] = 34; // arr[index %% arr.length];
|
|
||||||
|
|
||||||
- operator `?` to sign an argument as not-required
|
|
||||||
|
|
||||||
int sqr(int x) => x ** 2;
|
int sqr(int x) => x ** 2;
|
||||||
|
|
||||||
|
|
@ -397,6 +388,15 @@ Future plans
|
||||||
int sqrt(int x?) => x ** 2;
|
int sqrt(int x?) => x ** 2;
|
||||||
sqr(); // NaN
|
sqr(); // NaN
|
||||||
|
|
||||||
|
- Negate array accessor ( getter )
|
||||||
|
|
||||||
|
arr[-1]; // last element
|
||||||
|
|
||||||
|
only for static negate index, for other cases you can use `%` unary prefix:
|
||||||
|
|
||||||
|
int index = -10;
|
||||||
|
arr[%index] = 34; // arr[index %% arr.length];
|
||||||
|
|
||||||
- static typing
|
- static typing
|
||||||
- `@use` expressions
|
- `@use` expressions
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -421,7 +421,7 @@ Cola.AST_Namedarg = Cola.DEFNODE("Namedarg", "start end name value", {
|
||||||
}
|
}
|
||||||
}, Cola.AST_SymbolRef);
|
}, Cola.AST_SymbolRef);
|
||||||
|
|
||||||
Cola.AST_ArgDef = Cola.DEFNODE("ArgDef", "start end name type argtype defval", {
|
Cola.AST_ArgDef = Cola.DEFNODE("ArgDef", "start end name type argtype defval required", {
|
||||||
$iscola: true,
|
$iscola: true,
|
||||||
$documentation: "A function argument expression",
|
$documentation: "A function argument expression",
|
||||||
$propdoc: {
|
$propdoc: {
|
||||||
|
|
|
||||||
15
lib/parse.js
15
lib/parse.js
|
|
@ -1310,14 +1310,22 @@ Cola.Parser.prototype.for_in = function (init) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Cola.Parser.prototype.as_funcarg = function(splatedexist) {
|
Cola.Parser.prototype.as_funcarg = function(splatedexist) {
|
||||||
var name = this.as_symbol(Cola.AST_SymbolFunarg), type = name, argtype = 'positional', defval = new Cola.AST_Noop();
|
var name = this.as_symbol(Cola.AST_SymbolFunarg), type = name, argtype = 'positional', defval = new Cola.AST_Noop, required = true;
|
||||||
|
|
||||||
if(this.is("name")) name = this.as_symbol(Cola.AST_SymbolFunarg);
|
if(this.is("name")) name = this.as_symbol(Cola.AST_SymbolFunarg);
|
||||||
if(this.is("punc", "...")){
|
|
||||||
|
if(this.is("operator", "?")){
|
||||||
|
required = false;
|
||||||
|
this.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this.is("punc", "...") && required){
|
||||||
if(splatedexist) this.token_error(this.S, "Unexpected token: splated argument can be only one.");
|
if(splatedexist) this.token_error(this.S, "Unexpected token: splated argument can be only one.");
|
||||||
if(type != name && type.name != "Array") this.token_error(this.S, "Unexpected token: splated argument must have `Array` type.");
|
if(type != name && type.name != "Array") this.token_error(this.S, "Unexpected token: splated argument must have `Array` type.");
|
||||||
|
required = false;
|
||||||
this.next();
|
this.next();
|
||||||
argtype = "splated";
|
argtype = "splated";
|
||||||
} else if(this.is("operator", "=")){
|
} else if(this.is("operator", "=") && required){
|
||||||
this.next();
|
this.next();
|
||||||
defval = this.expression(false);
|
defval = this.expression(false);
|
||||||
} else if(this.is("punc",":")){
|
} else if(this.is("punc",":")){
|
||||||
|
|
@ -1331,6 +1339,7 @@ Cola.Parser.prototype.as_funcarg = function(splatedexist) {
|
||||||
return new Cola.AST_ArgDef({
|
return new Cola.AST_ArgDef({
|
||||||
name : name,
|
name : name,
|
||||||
type : name.type = (argtype == "splated" ? "Array" : type == name ? "dynamic" : type.name),
|
type : name.type = (argtype == "splated" ? "Array" : type == name ? "dynamic" : type.name),
|
||||||
|
required : required,
|
||||||
argtype : argtype,
|
argtype : argtype,
|
||||||
defval : defval,
|
defval : defval,
|
||||||
start : type.start,
|
start : type.start,
|
||||||
|
|
|
||||||
10
lib/std.js
10
lib/std.js
|
|
@ -121,12 +121,18 @@ Cola._ColaRuntime$$func_set_named_args = function _ColaRuntime$$func_set_named_a
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Cola._ColaRuntime$$func_set_named_args.i = 10;
|
Cola._ColaRuntime$$func_set_named_args.i = 10;
|
||||||
Cola._ColaRuntime$$arguments_def = { i : 11 };
|
|
||||||
|
Cola._ColaRuntime$$error = function _ColaRuntime$$error(_error) {
|
||||||
|
throw new Error(_error);
|
||||||
|
};
|
||||||
|
Cola._ColaRuntime$$error.i = 11;
|
||||||
|
|
||||||
|
Cola._ColaRuntime$$arguments_def = { i : 12 };
|
||||||
|
|
||||||
Cola.$_cola =
|
Cola.$_cola =
|
||||||
Cola._ColaRuntime$$is + Cola._ColaRuntime$$isnt + Cola._ColaRuntime$$modulo + Cola._ColaRuntime$$isset +
|
Cola._ColaRuntime$$is + Cola._ColaRuntime$$isnt + Cola._ColaRuntime$$modulo + Cola._ColaRuntime$$isset +
|
||||||
Cola._ColaRuntime$$isntset + Cola._ColaRuntime$$clone + Cola._ColaRuntime$$array_last + Cola._ColaRuntime$$array_range +
|
Cola._ColaRuntime$$isntset + Cola._ColaRuntime$$clone + Cola._ColaRuntime$$array_last + Cola._ColaRuntime$$array_range +
|
||||||
Cola._ColaRuntime$$array_asplice + Cola._ColaRuntime$$func_named_args + Cola._ColaRuntime$$func_set_named_args +
|
Cola._ColaRuntime$$array_asplice + Cola._ColaRuntime$$func_named_args + Cola._ColaRuntime$$func_set_named_args + Cola._ColaRuntime$$error +
|
||||||
"var arguments;";
|
"var arguments;";
|
||||||
|
|
||||||
Cola.Compressor.StdFuncs = {
|
Cola.Compressor.StdFuncs = {
|
||||||
|
|
|
||||||
|
|
@ -1044,7 +1044,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
|
||||||
|
|
||||||
node.argnames.forEach(function(val, i){
|
node.argnames.forEach(function(val, i){
|
||||||
if(val.argtype == "positional"){
|
if(val.argtype == "positional"){
|
||||||
if(val.defval instanceof Cola.AST_Noop && onfront) pos++, node.argnames[i] = val.name;
|
if(val.defval instanceof Cola.AST_Noop && onfront && !val.required) pos++, node.argnames[i] = val.name;
|
||||||
else {
|
else {
|
||||||
onfront = false;
|
onfront = false;
|
||||||
delQueue.push(i);
|
delQueue.push(i);
|
||||||
|
|
@ -1073,7 +1073,7 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
|
||||||
|
|
||||||
posed.forEach(function(val, i){
|
posed.forEach(function(val, i){
|
||||||
var pos = val.pos; val = val.val;
|
var pos = val.pos; val = val.val;
|
||||||
|
console.log(val);
|
||||||
if(val.argtype == "splated"){
|
if(val.argtype == "splated"){
|
||||||
aftersplated = 0;
|
aftersplated = 0;
|
||||||
props.definitions.push(new Cola.AST_VarDef({
|
props.definitions.push(new Cola.AST_VarDef({
|
||||||
|
|
@ -1086,12 +1086,20 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
||||||
if(val.defval instanceof Cola.AST_Noop) props.definitions.push(Cola.Constructions.PosedVarDef(val.name, val.type, pos, aftersplated++));
|
if(val.defval instanceof Cola.AST_Noop && !val.required)
|
||||||
else props.definitions.push(Cola.Constructions.PosedWithDefsVarDef(val.name, val.type, val.defval, pos, aftersplated++));
|
props.definitions.push(Cola.Constructions.PosedVarDef(val.name, val.type, pos, aftersplated++));
|
||||||
|
else if(val.defval instanceof Cola.AST_Noop && val.required){
|
||||||
|
_ColaRuntime$$hash[Cola._ColaRuntime$$error.i] = true;
|
||||||
|
props.definitions.push(Cola.Constructions.PosedWithDefsVarDef(val.name, val.type, new Cola.AST_Call({
|
||||||
|
args : [new Cola.AST_String({ value : "Argument `" + val.name.name + "` is required!" })],
|
||||||
|
expression : new Cola.AST_SymbolRef({ name : '_ColaRuntime$$error' })
|
||||||
|
}), pos, aftersplated++));
|
||||||
|
} else
|
||||||
|
props.definitions.push(Cola.Constructions.PosedWithDefsVarDef(val.name, val.type, val.defval, pos, aftersplated++));
|
||||||
});
|
});
|
||||||
|
|
||||||
named.forEach(function(val, i){
|
named.forEach(function(val, i){
|
||||||
if(val.defval instanceof Cola.AST_Noop) props.definitions.push(new Cola.AST_VarDef({
|
if(val.defval instanceof Cola.AST_Noop && !val.required) props.definitions.push(new Cola.AST_VarDef({
|
||||||
type : val.type,
|
type : val.type,
|
||||||
name : new Cola.AST_SymbolVar(val.name),
|
name : new Cola.AST_SymbolVar(val.name),
|
||||||
value : new Cola.AST_Dot({
|
value : new Cola.AST_Dot({
|
||||||
|
|
@ -1099,7 +1107,14 @@ Cola.AST_Toplevel.prototype.toJavaScript = function(options){
|
||||||
property : val.name.name
|
property : val.name.name
|
||||||
})
|
})
|
||||||
}));
|
}));
|
||||||
else props.definitions.push(Cola.Constructions.NamedVarDef(val.name, val.type, val.defval, val.name.name));
|
else if(val.defval instanceof Cola.AST_Noop && val.required){
|
||||||
|
_ColaRuntime$$hash[Cola._ColaRuntime$$error.i] = true;
|
||||||
|
props.definitions.push(Cola.Constructions.NamedVarDef(val.name, val.type, new Cola.AST_Call({
|
||||||
|
args : [new Cola.AST_String({ value : "Argument `" + val.name.name + "` is required!" })],
|
||||||
|
expression : new Cola.AST_SymbolRef({ name : '_ColaRuntime$$error' })
|
||||||
|
}), val.name.name));
|
||||||
|
} else
|
||||||
|
props.definitions.push(Cola.Constructions.NamedVarDef(val.name, val.type, val.defval, val.name.name));
|
||||||
});
|
});
|
||||||
|
|
||||||
if(delQueue.length != 0 || named.length != 0) node.body.unshift(new Cola.AST_Var(props));
|
if(delQueue.length != 0 || named.length != 0) node.body.unshift(new Cola.AST_Var(props));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user