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

View File

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

View File

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

View File

@ -13,21 +13,20 @@ public final class Variables {
return ScopeHandler.variables(); return ScopeHandler.variables();
} }
public static boolean isExists(String name) { /**
return ScopeHandler.isVariableOrConstantExists(name); * @deprecated This function remains for backward compatibility with old separate modules
} * Use {@link ScopeHandler#setVariable(String, Value)}
*/
public static Value get(String name) { @Deprecated
return ScopeHandler.getVariableOrConstant(name);
}
public static void set(String name, Value value) { public static void set(String name, Value value) {
ScopeHandler.setVariable(name, 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) { public static void define(String name, Value value) {
ScopeHandler.setConstant(name, 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.OperationIsNotSupportedException;
import com.annimon.ownlang.exceptions.TypeException; import com.annimon.ownlang.exceptions.TypeException;
import com.annimon.ownlang.lib.ArrayValue; import com.annimon.ownlang.lib.*;
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;
/** /**
* *
@ -66,8 +60,8 @@ public final class BinaryExpression implements Expression {
try { try {
return eval(value1, value2); return eval(value1, value2);
} catch (OperationIsNotSupportedException ex) { } catch (OperationIsNotSupportedException ex) {
if (Functions.isExists(operation.toString())) { if (ScopeHandler.isFunctionExists(operation.toString())) {
return Functions.get(operation.toString()).execute(value1, value2); return ScopeHandler.getFunction(operation.toString()).execute(value1, value2);
} }
throw ex; throw ex;
} }

View File

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

View File

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

View File

@ -20,8 +20,8 @@ public final class ObjectCreationExpression implements Expression {
final ClassDeclarationStatement cd = ClassDeclarations.get(className); final ClassDeclarationStatement cd = ClassDeclarations.get(className);
if (cd == null) { if (cd == null) {
// Is Instantiable? // Is Instantiable?
if (Variables.isExists(className)) { if (ScopeHandler.isVariableOrConstantExists(className)) {
final Value variable = Variables.get(className); final Value variable = ScopeHandler.getVariableOrConstant(className);
if (variable instanceof Instantiable instantiable) { if (variable instanceof Instantiable instantiable) {
return instantiable.newInstance(ctorArgs()); 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.exceptions.VariableDoesNotExistsException;
import com.annimon.ownlang.lib.ScopeHandler; import com.annimon.ownlang.lib.ScopeHandler;
import com.annimon.ownlang.lib.Value; 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 @Override
public Value get() { public Value get() {
if (!Variables.isExists(name)) throw new VariableDoesNotExistsException(name); if (!ScopeHandler.isVariableOrConstantExists(name)) throw new VariableDoesNotExistsException(name);
return Variables.get(name); return ScopeHandler.getVariableOrConstant(name);
} }
@Override @Override

View File

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

View File

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

View File

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

View File

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