This commit is contained in:
gnembon committed 2021-04-23 01:20:42 -03:00
1 parent edca3fb131
commit bed9e2c60a
6 files changed
+56 -21

No files matched your search

+23 -2
View File
@@ -29,11 +29,12 @@ Here is the complete list of operators in `scarpet` including control flow opera
are not technically operators, but part of the language, even if they look like them: are not technically operators, but part of the language, even if they look like them:
* Match, Get `~ :` * Match, Get `~ :`
* Unary `+ - !` * Unary `+ - ! ...`
* Exponent `^` * Exponent `^`
* Multiplication `* / %` * Multiplication `* / %`
* Addition `+ -` * Addition `+ -`
* Comparison `== != > >= <= <` * Comparison `> >= <= <`
* Equality `== !=`
* Logical And`&&` * Logical And`&&`
* Logical Or `||` * Logical Or `||`
* Assignment `= += <>` * Assignment `= += <>`
@@ -234,3 +235,23 @@ flips boolean condition of the expression. Equivalent of `bool(expr)==false`
!l() => true !l() => true
!l(null) => false !l(null) => false
</pre> </pre>
### `Unpacking Operator ...`
Unpacks elements of a list of an iterator into a sequence of arguments in a function making so that `fun(...[1, 2, 3])` is
identical with `fun(1, 2, 3)`.
In function signatures it identifies a vararg parameter.
`fun(a, b, ... rest) -> [a, b, rest]; fun(1, 2, 3, 4)` => `[1, 2, [3, 4]]`
Effects of `...` can be surprisingly lasting. It is kept through the use of variables and function calls.
`fun(a, b, ... rest) -> [a, b, ... rest]; fun(1, 2, 3, 4)` => `[1, 2, 3, 4]`
`args() -> ... [1, 2, 3]; sum(a, b, c) -> a+b+c; sum(args())` => `6`
`a = ... [1, 2, 3]; sum(a, b, c) -> a+b+c; sum(a)` => `6`
Unpacking mechanics can be used for list and map constriction, not just for function calls.
`[...range(5), pi, ...range(5,-1,-1)]` => `[0, 1, 2, 3, 4, 3.14159265359, 5, 4, 3, 2, 1, 0]`
`{ ... map(range(5), _ -> _*_ )}` => `{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}`
+9 -9
View File
@@ -188,7 +188,7 @@ public class Expression
public void addLazyFunctionWithDelegation(String name, int numpar, public void addLazyFunctionWithDelegation(String name, int numpar,
QuinnFunction<Context, Integer, Expression, Tokenizer.Token, List<LazyValue>, LazyValue> lazyfun) QuinnFunction<Context, Integer, Expression, Tokenizer.Token, List<LazyValue>, LazyValue> lazyfun)
{ {
functions.put(name, new AbstractLazyFunction(numpar) functions.put(name, new AbstractLazyFunction(numpar, name)
{ {
@Override @Override
public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lv) public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lv)
@@ -208,7 +208,7 @@ public class Expression
public void addFunctionWithDelegation(String name, int numpar, public void addFunctionWithDelegation(String name, int numpar,
QuinnFunction<Context, Integer, Expression, Tokenizer.Token, List<Value>, Value> fun) QuinnFunction<Context, Integer, Expression, Tokenizer.Token, List<Value>, Value> fun)
{ {
functions.put(name, new AbstractLazyFunction(numpar) functions.put(name, new AbstractLazyFunction(numpar, name)
{ {
@Override @Override
public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lv) public LazyValue lazyEval(Context c, Integer type, Expression e, Tokenizer.Token t, List<LazyValue> lv)
@@ -286,7 +286,7 @@ public class Expression
public void addUnaryOperator(String surface, boolean leftAssoc, Function<Value, Value> fun) public void addUnaryOperator(String surface, boolean leftAssoc, Function<Value, Value> fun)
{ {
operators.put(surface+"u", new AbstractUnaryOperator(Operators.precedence.get("unary+-!"), leftAssoc) operators.put(surface+"u", new AbstractUnaryOperator(Operators.precedence.get("unary+-!..."), leftAssoc)
{ {
@Override @Override
public Value evalUnary(Value v1) public Value evalUnary(Value v1)
@@ -312,7 +312,7 @@ public class Expression
public void addUnaryFunction(String name, Function<Value, Value> fun) public void addUnaryFunction(String name, Function<Value, Value> fun)
{ {
functions.put(name, new AbstractFunction(1) functions.put(name, new AbstractFunction(1, name)
{ {
@Override @Override
public Value eval(List<Value> parameters) public Value eval(List<Value> parameters)
@@ -324,7 +324,7 @@ public class Expression
public void addBinaryFunction(String name, BiFunction<Value, Value, Value> fun) public void addBinaryFunction(String name, BiFunction<Value, Value, Value> fun)
{ {
functions.put(name, new AbstractFunction(2) functions.put(name, new AbstractFunction(2, name)
{ {
@Override @Override
public Value eval(List<Value> parameters) public Value eval(List<Value> parameters)
@@ -339,7 +339,7 @@ public class Expression
public void addFunction(String name, Function<List<Value>, Value> fun) public void addFunction(String name, Function<List<Value>, Value> fun)
{ {
functions.put(name, new AbstractFunction(-1) functions.put(name, new AbstractFunction(-1, name)
{ {
@Override @Override
public Value eval(List<Value> parameters) public Value eval(List<Value> parameters)
@@ -370,7 +370,7 @@ public class Expression
public void addLazyFunction(String name, int num_params, TriFunction<Context, Integer, List<LazyValue>, LazyValue> fun) public void addLazyFunction(String name, int num_params, TriFunction<Context, Integer, List<LazyValue>, LazyValue> fun)
{ {
functions.put(name, new AbstractLazyFunction(num_params) functions.put(name, new AbstractLazyFunction(num_params, name)
{ {
@Override @Override
public LazyValue lazyEval(Context c, Integer i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams) public LazyValue lazyEval(Context c, Integer i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
@@ -389,7 +389,7 @@ public class Expression
public void addContextFunction(String name, int num_params, TriFunction<Context, Integer, List<Value>, Value> fun) public void addContextFunction(String name, int num_params, TriFunction<Context, Integer, List<Value>, Value> fun)
{ {
functions.put(name, new AbstractLazyFunction(num_params) functions.put(name, new AbstractLazyFunction(num_params, name)
{ {
@Override @Override
public LazyValue lazyEval(Context c, Integer i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams) public LazyValue lazyEval(Context c, Integer i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
@@ -409,7 +409,7 @@ public class Expression
public void addTypedContextFunction(String name, int num_params, int reqType, TriFunction<Context, Integer, List<Value>, Value> fun) public void addTypedContextFunction(String name, int num_params, int reqType, TriFunction<Context, Integer, List<Value>, Value> fun)
{ {
functions.put(name, new AbstractLazyFunction(num_params) functions.put(name, new AbstractLazyFunction(num_params, name)
{ {
@Override @Override
public LazyValue lazyEval(Context c, Integer i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams) public LazyValue lazyEval(Context c, Integer i, Expression e, Tokenizer.Token t, List<LazyValue> lazyParams)
+4 -5
View File
@@ -64,9 +64,10 @@ public abstract class Fluff
protected String name; protected String name;
int numParams; int numParams;
AbstractLazyFunction(int numParams) AbstractLazyFunction(int numParams, String name)
{ {
this.numParams = numParams; this.numParams = numParams;
this.name = name;
} }
@@ -115,12 +116,10 @@ public abstract class Fluff
public abstract static class AbstractFunction extends AbstractLazyFunction implements IFunction public abstract static class AbstractFunction extends AbstractLazyFunction implements IFunction
{ {
AbstractFunction(int numParams) { AbstractFunction(int numParams, String name) {
super(numParams); super(numParams, name);
} }
@Override @Override
public LazyValue lazyEval(Context cc, Integer type, Expression e, Tokenizer.Token t, final List<LazyValue> lazyParams) public LazyValue lazyEval(Context cc, Integer type, Expression e, Tokenizer.Token t, final List<LazyValue> lazyParams)
{ {
@@ -93,7 +93,7 @@ public class Functions {
}); });
// lazy because of typed evaluation of the argument // lazy because of typed evaluation of the argument
expression.addLazyUnaryOperator("...", Operators.precedence.get("def->..."), false, (c, t, lv) -> expression.addLazyUnaryOperator("...", Operators.precedence.get("unary+-!..."), false, (c, t, lv) ->
{ {
if (t == Context.LOCALIZATION) if (t == Context.LOCALIZATION)
return (cc, tt) -> new FunctionAnnotationValue(lv.evalValue(c), FunctionAnnotationValue.Type.VARARG); return (cc, tt) -> new FunctionAnnotationValue(lv.evalValue(c), FunctionAnnotationValue.Type.VARARG);
@@ -116,7 +116,7 @@ public class Functions {
//assigns const procedure to the lhs, returning its previous value //assigns const procedure to the lhs, returning its previous value
// must be lazy due to RHS being an expression to save to execute // must be lazy due to RHS being an expression to save to execute
expression.addLazyBinaryOperatorWithDelegation("->", Operators.precedence.get("def->..."), false, (c, type, e, t, lv1, lv2) -> expression.addLazyBinaryOperatorWithDelegation("->", Operators.precedence.get("def->"), false, (c, type, e, t, lv1, lv2) ->
{ {
if (type == Context.MAPDEF) if (type == Context.MAPDEF)
{ {
@@ -20,7 +20,7 @@ import java.util.Map;
public class Operators { public class Operators {
public static final Map<String, Integer> precedence = new HashMap<String,Integer>() {{ public static final Map<String, Integer> precedence = new HashMap<String,Integer>() {{
put("attribute~:", 80); put("attribute~:", 80);
put("unary+-!", 60); put("unary+-!...", 60);
put("exponent^", 40); put("exponent^", 40);
put("multiplication*/%", 30); put("multiplication*/%", 30);
put("addition+-", 20); put("addition+-", 20);
@@ -29,7 +29,7 @@ public class Operators {
put("and&&", 5); put("and&&", 5);
put("or||", 4); put("or||", 4);
put("assign=<>", 3); put("assign=<>", 3);
put("def->...", 2); put("def->", 2);
put("nextop;", 1); put("nextop;", 1);
}}; }};
@@ -216,7 +216,9 @@ public class Operators {
expression.addUnaryOperator("+", false, NumericValue::asNumber); expression.addUnaryOperator("+", false, NumericValue::asNumber);
// could be non-lazy, but who cares - its a small one. // could be non-lazy, but who cares - its a small one.
expression.addLazyUnaryOperator("!", precedence.get("unary+-!"), false, (c, t, lv)-> lv.evalValue(c, Context.BOOLEAN).getBoolean() ? (cc, tt)-> Value.FALSE : (cc, tt) -> Value.TRUE); // might need context boolean expression.addLazyUnaryOperator("!", precedence.get("unary+-!..."), false, (c, t, lv) ->
lv.evalValue(c, Context.BOOLEAN).getBoolean() ? (cc, tt)-> Value.FALSE : (cc, tt) -> Value.TRUE
); // might need context boolean
} }
} }
@@ -7,4 +7,17 @@ public class FunctionUnpackedArgumentsValue extends ListValue
public FunctionUnpackedArgumentsValue(List<Value> list) { public FunctionUnpackedArgumentsValue(List<Value> list) {
super(list); super(list);
} }
@Override
public Value clone()
{
return new FunctionUnpackedArgumentsValue(items);
}
@Override
public Value deepcopy()
{
ListValue copy = (ListValue)super.deepcopy();
return new FunctionUnpackedArgumentsValue(copy.items);
}
} }