semi-auto cleanup
This commit is contained in:
1 parent
cad80f82c6
commit
8f61271606
99 files changed
+11463
-8007
No files matched your search
@@ -19,61 +19,70 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class Functions {
|
||||
public static void apply(Expression expression) // public just to get the javadoc right
|
||||
public class Functions
|
||||
{
|
||||
public static void apply(final Expression expression) // public just to get the javadoc right
|
||||
{
|
||||
// artificial construct to handle user defined functions and function definitions
|
||||
expression.addContextFunction("import", -1, (c, t, lv) ->
|
||||
{
|
||||
if (lv.size() < 1) throw new InternalExpressionException("'import' needs at least a module name to import, and list of values to import");
|
||||
if (lv.size() < 1)
|
||||
{
|
||||
throw new InternalExpressionException("'import' needs at least a module name to import, and list of values to import");
|
||||
}
|
||||
String moduleName = lv.get(0).getString();
|
||||
c.host.importModule(c, moduleName);
|
||||
moduleName = moduleName.toLowerCase(Locale.ROOT);
|
||||
if (lv.size() > 1)
|
||||
{
|
||||
c.host.importNames(c, expression.module, moduleName, lv.subList(1, lv.size()).stream().map(Value::getString).toList());
|
||||
if (t == Context.VOID)
|
||||
return Value.NULL;
|
||||
return ListValue.wrap(c.host.availableImports(moduleName).map(StringValue::new));
|
||||
}
|
||||
return t == Context.VOID ? Value.NULL : ListValue.wrap(c.host.availableImports(moduleName).map(StringValue::new));
|
||||
});
|
||||
|
||||
|
||||
// needs to be lazy because of custom context of execution of arguments as a signature
|
||||
expression.addCustomFunction("call", new Fluff.AbstractLazyFunction(-1, "call") {
|
||||
expression.addCustomFunction("call", new Fluff.AbstractLazyFunction(-1, "call")
|
||||
{
|
||||
@Override
|
||||
public LazyValue lazyEval(Context c, Context.Type t, Expression expr, Tokenizer.Token tok, List<LazyValue> lv)
|
||||
public LazyValue lazyEval(final Context c, final Context.Type t, final Expression expr, final Tokenizer.Token tok, final List<LazyValue> lv)
|
||||
{
|
||||
if (lv.size() == 0)
|
||||
{
|
||||
throw new InternalExpressionException("'call' expects at least function name to call");
|
||||
}
|
||||
//lv.remove(lv.size()-1); // aint gonna cut it // maybe it will because of the eager eval changes
|
||||
if (t != Context.SIGNATURE) // just call the function
|
||||
{
|
||||
List<Value> args = Fluff.AbstractFunction.unpackLazy(lv, c, Context.NONE);
|
||||
FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, args, 0, false, true);
|
||||
FunctionValue fun = functionArgument.function;
|
||||
final List<Value> args = Fluff.AbstractFunction.unpackLazy(lv, c, Context.NONE);
|
||||
final FunctionArgument functionArgument = FunctionArgument.findIn(c, expression.module, args, 0, false, true);
|
||||
final FunctionValue fun = functionArgument.function;
|
||||
return fun.callInContext(c, t, functionArgument.args);
|
||||
}
|
||||
// gimme signature
|
||||
String name = lv.get(0).evalValue(c, Context.NONE).getString();
|
||||
List<String> args = new ArrayList<>();
|
||||
List<String> globals = new ArrayList<>();
|
||||
final String name = lv.get(0).evalValue(c, Context.NONE).getString();
|
||||
final List<String> args = new ArrayList<>();
|
||||
final List<String> globals = new ArrayList<>();
|
||||
String varArgs = null;
|
||||
for (int i = 1; i < lv.size(); i++)
|
||||
{
|
||||
Value v = lv.get(i).evalValue(c, Context.LOCALIZATION);
|
||||
final Value v = lv.get(i).evalValue(c, Context.LOCALIZATION);
|
||||
if (!v.isBound())
|
||||
{
|
||||
throw new InternalExpressionException("Only variables can be used in function signature, not " + v.getString());
|
||||
}
|
||||
if (v instanceof FunctionAnnotationValue)
|
||||
if (v instanceof final FunctionAnnotationValue fav)
|
||||
{
|
||||
if (((FunctionAnnotationValue) v).type == FunctionAnnotationValue.Type.GLOBAL)
|
||||
if (fav.type == FunctionAnnotationValue.Type.GLOBAL)
|
||||
{
|
||||
globals.add(v.boundVariable);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (varArgs != null)
|
||||
throw new InternalExpressionException("Variable argument identifier is already defined as "+varArgs+", trying to overwrite with "+v.boundVariable);
|
||||
{
|
||||
throw new InternalExpressionException("Variable argument identifier is already defined as " + varArgs + ", trying to overwrite with " + v.boundVariable);
|
||||
}
|
||||
varArgs = v.boundVariable;
|
||||
}
|
||||
}
|
||||
@@ -82,7 +91,7 @@ public class Functions {
|
||||
args.add(v.boundVariable);
|
||||
}
|
||||
}
|
||||
Value retval = new FunctionSignatureValue(name, args, varArgs, globals);
|
||||
final Value retval = new FunctionSignatureValue(name, args, varArgs, globals);
|
||||
return (cc, tt) -> retval;
|
||||
}
|
||||
|
||||
@@ -93,14 +102,15 @@ public class Functions {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean transitive() {
|
||||
public boolean transitive()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Context.Type staticType(Context.Type outerType)
|
||||
public Context.Type staticType(final Context.Type outerType)
|
||||
{
|
||||
return outerType==Context.SIGNATURE?Context.LOCALIZATION:Context.NONE;
|
||||
return outerType == Context.SIGNATURE ? Context.LOCALIZATION : Context.NONE;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -108,7 +118,9 @@ public class Functions {
|
||||
expression.addContextFunction("outer", 1, (c, t, lv) ->
|
||||
{
|
||||
if (t != Context.LOCALIZATION)
|
||||
{
|
||||
throw new InternalExpressionException("Outer scoping of variables is only possible in function signatures.");
|
||||
}
|
||||
return new FunctionAnnotationValue(lv.get(0), FunctionAnnotationValue.Type.GLOBAL);
|
||||
});
|
||||
|
||||
@@ -118,16 +130,20 @@ public class Functions {
|
||||
{
|
||||
if (type == Context.MAPDEF)
|
||||
{
|
||||
Value result = ListValue.of(lv1.evalValue(c), lv2.evalValue(c));
|
||||
final Value result = ListValue.of(lv1.evalValue(c), lv2.evalValue(c));
|
||||
return (cc, tt) -> result;
|
||||
}
|
||||
Value v1 = lv1.evalValue(c, Context.SIGNATURE);
|
||||
if (!(v1 instanceof FunctionSignatureValue sign))
|
||||
final Value v1 = lv1.evalValue(c, Context.SIGNATURE);
|
||||
if (!(v1 instanceof final FunctionSignatureValue sign))
|
||||
{
|
||||
throw new InternalExpressionException("'->' operator requires a function signature on the LHS");
|
||||
Value result = expression.createUserDefinedFunction(c, sign.getName(), e, t, sign.getArgs(), sign.getVarArgs(), sign.getGlobals(), lv2);
|
||||
}
|
||||
final Value result = expression.createUserDefinedFunction(c, sign.identifier(), e, t, sign.arguments(), sign.varArgs(), sign.globals(), lv2);
|
||||
return (cc, tt) -> result;
|
||||
});
|
||||
|
||||
expression.addImpureFunction("return", (lv) -> { throw new ReturnStatement(lv.size()==0?Value.NULL:lv.get(0));} );
|
||||
expression.addImpureFunction("return", (lv) -> {
|
||||
throw new ReturnStatement(lv.size() == 0 ? Value.NULL : lv.get(0));
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user