Deprecate old Variables/Functions methods

This commit is contained in:
aNNiMON 2023-09-04 21:31:32 +03:00 committed by Victor Melnik
parent 2db88523bc
commit b7c376f01f
14 changed files with 48 additions and 61 deletions

View File

@ -1,13 +1,7 @@
package com.annimon.ownlang.modules.std;
import com.annimon.ownlang.Console;
import com.annimon.ownlang.lib.Arguments;
import com.annimon.ownlang.lib.Function;
import com.annimon.ownlang.lib.FunctionValue;
import com.annimon.ownlang.lib.Functions;
import com.annimon.ownlang.lib.NumberValue;
import com.annimon.ownlang.lib.Types;
import com.annimon.ownlang.lib.Value;
import com.annimon.ownlang.lib.*;
public final class std_thread implements Function {
@ -19,10 +13,10 @@ public final class std_thread implements Function {
if (args[0].type() == Types.FUNCTION) {
body = ((FunctionValue) args[0]).getValue();
} else {
body = Functions.get(args[0].asString());
body = ScopeHandler.getFunction(args[0].asString());
}
// Сдвигаем аргументы
// Shift arguments
final Value[] params = new Value[args.length - 1];
if (params.length > 0) {
System.arraycopy(args, 1, params, 0, params.length);

View File

@ -13,14 +13,11 @@ public final class Functions {
return ScopeHandler.functions();
}
public static boolean isExists(String name) {
return ScopeHandler.isFunctionExists(name);
}
public static Function get(String name) {
return ScopeHandler.getFunction(name);
}
/**
* @deprecated This function remains for backward compatibility with old separate modules
* Use {@link ScopeHandler#setFunction(String, Function)}
*/
@Deprecated
public static void set(String key, Function function) {
ScopeHandler.setFunction(key, function);
}

View File

@ -100,6 +100,10 @@ public final class ScopeHandler {
}
}
public static boolean isConstantExists(String name) {
return rootScope.containsConstant(name);
}
public static void setConstant(String name, Value value) {
rootScope.setConstant(name, value);
}

View File

@ -47,8 +47,8 @@ public final class StringValue implements Value {
case "isEmpty" -> Converters.voidToBoolean(value::isEmpty);
default -> {
if (Functions.isExists(prop)) {
final Function f = Functions.get(prop);
if (ScopeHandler.isFunctionExists(prop)) {
final Function f = ScopeHandler.getFunction(prop);
yield new FunctionValue(args -> {
final Value[] newArgs = new Value[args.length + 1];
newArgs[0] = this;

View File

@ -13,21 +13,20 @@ public final class Variables {
return ScopeHandler.variables();
}
public static boolean isExists(String name) {
return ScopeHandler.isVariableOrConstantExists(name);
}
public static Value get(String name) {
return ScopeHandler.getVariableOrConstant(name);
}
/**
* @deprecated This function remains for backward compatibility with old separate modules
* Use {@link ScopeHandler#setVariable(String, Value)}
*/
@Deprecated
public static void set(String name, Value value) {
ScopeHandler.setVariable(name, value);
}
/**
* For compatibility with other modules
* @deprecated This function remains for backward compatibility with old separate modules
* Use {@link ScopeHandler#setConstant(String, Value)}
*/
@Deprecated
public static void define(String name, Value value) {
ScopeHandler.setConstant(name, value);
}

View File

@ -2,13 +2,7 @@ package com.annimon.ownlang.parser.ast;
import com.annimon.ownlang.exceptions.OperationIsNotSupportedException;
import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.lib.ArrayValue;
import com.annimon.ownlang.lib.Functions;
import com.annimon.ownlang.lib.MapValue;
import com.annimon.ownlang.lib.NumberValue;
import com.annimon.ownlang.lib.StringValue;
import com.annimon.ownlang.lib.Types;
import com.annimon.ownlang.lib.Value;
import com.annimon.ownlang.lib.*;
/**
*
@ -66,8 +60,8 @@ public final class BinaryExpression implements Expression {
try {
return eval(value1, value2);
} catch (OperationIsNotSupportedException ex) {
if (Functions.isExists(operation.toString())) {
return Functions.get(operation.toString()).execute(value1, value2);
if (ScopeHandler.isFunctionExists(operation.toString())) {
return ScopeHandler.getFunction(operation.toString()).execute(value1, value2);
}
throw ex;
}

View File

@ -1,6 +1,6 @@
package com.annimon.ownlang.parser.ast;
import com.annimon.ownlang.lib.Functions;
import com.annimon.ownlang.lib.ScopeHandler;
import com.annimon.ownlang.lib.UserDefinedFunction;
/**
@ -21,7 +21,7 @@ public final class FunctionDefineStatement implements Statement {
@Override
public void execute() {
Functions.set(name, new UserDefinedFunction(arguments, body));
ScopeHandler.setFunction(name, new UserDefinedFunction(arguments, body));
}
@Override

View File

@ -17,7 +17,7 @@ public final class FunctionReferenceExpression extends InterruptableNode impleme
@Override
public FunctionValue eval() {
super.interruptionCheck();
return new FunctionValue(Functions.get(name));
return new FunctionValue(ScopeHandler.getFunction(name));
}
@Override

View File

@ -20,8 +20,8 @@ public final class ObjectCreationExpression implements Expression {
final ClassDeclarationStatement cd = ClassDeclarations.get(className);
if (cd == null) {
// Is Instantiable?
if (Variables.isExists(className)) {
final Value variable = Variables.get(className);
if (ScopeHandler.isVariableOrConstantExists(className)) {
final Value variable = ScopeHandler.getVariableOrConstant(className);
if (variable instanceof Instantiable instantiable) {
return instantiable.newInstance(ctorArgs());
}

View File

@ -3,7 +3,6 @@ package com.annimon.ownlang.parser.ast;
import com.annimon.ownlang.exceptions.VariableDoesNotExistsException;
import com.annimon.ownlang.lib.ScopeHandler;
import com.annimon.ownlang.lib.Value;
import com.annimon.ownlang.lib.Variables;
/**
*
@ -25,8 +24,8 @@ public final class VariableExpression extends InterruptableNode implements Expre
@Override
public Value get() {
if (!Variables.isExists(name)) throw new VariableDoesNotExistsException(name);
return Variables.get(name);
if (!ScopeHandler.isVariableOrConstantExists(name)) throw new VariableDoesNotExistsException(name);
return ScopeHandler.getVariableOrConstant(name);
}
@Override

View File

@ -1,7 +1,7 @@
package com.annimon.ownlang.parser.linters;
import com.annimon.ownlang.Console;
import com.annimon.ownlang.lib.Variables;
import com.annimon.ownlang.lib.ScopeHandler;
import com.annimon.ownlang.parser.ast.*;
/**
@ -15,7 +15,7 @@ public final class AssignValidator extends LintVisitor {
super.visit(s);
if (s.target instanceof VariableExpression varExpr) {
final String variable = varExpr.name;
if (Variables.isExists(variable)) {
if (ScopeHandler.isConstantExists(variable)) {
Console.error(String.format(
"Warning: variable \"%s\" overrides constant", variable));
}

View File

@ -1,7 +1,7 @@
package com.annimon.ownlang.parser.linters;
import com.annimon.ownlang.Console;
import com.annimon.ownlang.lib.Functions;
import com.annimon.ownlang.lib.ScopeHandler;
import com.annimon.ownlang.parser.ast.*;
public final class DefaultFunctionsOverrideValidator extends LintVisitor {
@ -9,7 +9,7 @@ public final class DefaultFunctionsOverrideValidator extends LintVisitor {
@Override
public void visit(FunctionDefineStatement s) {
super.visit(s);
if (Functions.isExists(s.name)) {
if (ScopeHandler.isFunctionExists(s.name)) {
Console.error(String.format(
"Warning: function \"%s\" overrides default module function", s.name));
}

View File

@ -1,7 +1,7 @@
package com.annimon.ownlang.parser;
import com.annimon.ownlang.lib.ScopeHandler;
import com.annimon.ownlang.lib.Value;
import com.annimon.ownlang.lib.Variables;
import com.annimon.ownlang.parser.ast.*;
import org.junit.jupiter.api.Test;
@ -35,7 +35,7 @@ public class ParserTest {
private static void assertEval(Value expectedValue, String input, Expression expected) {
BlockStatement program = assertExpression(input, expected);
program.execute();
final Value actual = Variables.get("a");
final Value actual = ScopeHandler.getVariable("a");
try {
assertEquals(expectedValue.asNumber(), actual.asNumber(), 0.001);
} catch (NumberFormatException nfe) {

View File

@ -32,27 +32,27 @@ public class ProgramsTest {
public void initialize() {
ScopeHandler.resetScope();
// Let's mock junit methods as ounit functions
Functions.set("assertEquals", (args) -> {
ScopeHandler.setFunction("assertEquals", (args) -> {
assertEquals(args[0], args[1]);
return NumberValue.ONE;
});
Functions.set("assertNotEquals", (args) -> {
ScopeHandler.setFunction("assertNotEquals", (args) -> {
assertNotEquals(args[0], args[1]);
return NumberValue.ONE;
});
Functions.set("assertSameType", (args) -> {
ScopeHandler.setFunction("assertSameType", (args) -> {
assertEquals(args[0].type(), args[1].type());
return NumberValue.ONE;
});
Functions.set("assertTrue", (args) -> {
ScopeHandler.setFunction("assertTrue", (args) -> {
assertTrue(args[0].asInt() != 0);
return NumberValue.ONE;
});
Functions.set("assertFalse", (args) -> {
ScopeHandler.setFunction("assertFalse", (args) -> {
assertFalse(args[0].asInt() != 0);
return NumberValue.ONE;
});
Functions.set("assertFail", (args) -> {
ScopeHandler.setFunction("assertFail", (args) -> {
assertThrows(Throwable.class,
() -> ((FunctionValue) args[0]).getValue().execute());
return NumberValue.ONE;
@ -73,7 +73,7 @@ public class ProgramsTest {
}
@Test
public void testOutput() throws IOException {
public void testOutput() {
OutputSettings oldSettings = Console.getSettings();
Console.useSettings(new StringOutputSettings());
String source = "for i = 0, i <= 5, i++\n print i";
@ -93,7 +93,7 @@ public class ProgramsTest {
public void visit(FunctionDefineStatement s) {
if (s.name.startsWith("test")) {
try {
Functions.get(s.name).execute();
ScopeHandler.getFunction(s.name).execute();
} catch (AssertionError err) {
throw new AssertionError(s.name + ": " + err.getMessage(), err);
}