refactor breaking up API and language defs
This commit is contained in:
1 parent
34d548523d
commit
59575ec607
22 files changed
+5596
-5282
No files matched your search
@@ -0,0 +1,70 @@
|
||||
package carpet.script.language;
|
||||
|
||||
import carpet.script.Context;
|
||||
import carpet.script.Expression;
|
||||
import carpet.script.LazyValue;
|
||||
import carpet.script.exception.ExitStatement;
|
||||
import carpet.script.exception.InternalExpressionException;
|
||||
import carpet.script.exception.ThrowStatement;
|
||||
import carpet.script.value.Value;
|
||||
|
||||
public class ControlFlow {
|
||||
public static void apply(Expression expression) // public just to get the javadoc right
|
||||
{
|
||||
expression.addLazyBinaryOperator(";", Operators.precedence.get("nextop;"), true, (c, t, lv1, lv2) ->
|
||||
{
|
||||
lv1.evalValue(c, Context.VOID);
|
||||
Value v2 = lv2.evalValue(c, t);
|
||||
return (cc, tt) -> v2;
|
||||
});
|
||||
|
||||
// if(cond1, expr1, cond2, expr2, ..., ?default) => value
|
||||
expression.addLazyFunction("if", -1, (c, t, lv) ->
|
||||
{
|
||||
if ( lv.size() < 2 )
|
||||
throw new InternalExpressionException("'if' statement needs to have at least one condition and one case");
|
||||
for (int i=0; i<lv.size()-1; i+=2)
|
||||
{
|
||||
if (lv.get(i).evalValue(c, Context.BOOLEAN).getBoolean())
|
||||
{
|
||||
//int iFinal = i;
|
||||
Value ret = lv.get(i+1).evalValue(c, t);
|
||||
return (cc, tt) -> ret;
|
||||
}
|
||||
}
|
||||
if (lv.size()%2 == 1)
|
||||
{
|
||||
Value ret = lv.get(lv.size() - 1).evalValue(c, t);
|
||||
return (cc, tt) -> ret;
|
||||
}
|
||||
return (cc, tt) -> Value.NULL;
|
||||
});
|
||||
|
||||
expression.addFunction("exit", (lv) -> { throw new ExitStatement(lv.size()==0?Value.NULL:lv.get(0)); });
|
||||
|
||||
expression.addFunction("throw", (lv)-> {throw new ThrowStatement(lv.size()==0?Value.NULL:lv.get(0)); });
|
||||
|
||||
expression.addLazyFunction("try", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size()==0)
|
||||
throw new InternalExpressionException("'try' needs at least an expression block");
|
||||
try
|
||||
{
|
||||
Value retval = lv.get(0).evalValue(c, t);
|
||||
return (c_, t_) -> retval;
|
||||
}
|
||||
catch (ThrowStatement ret)
|
||||
{
|
||||
if (lv.size() == 1)
|
||||
return (c_, t_) -> Value.NULL;
|
||||
LazyValue __ = c.getVariable("_");
|
||||
c.setVariable("_", (__c, __t) -> ret.retval.reboundedTo("_"));
|
||||
Value val = lv.get(1).evalValue(c, t);
|
||||
c.setVariable("_",__);
|
||||
return (c_, t_) -> val;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user