diff --git a/modules/main/src/main/java/com/annimon/ownlang/modules/std/std_thread.java b/modules/main/src/main/java/com/annimon/ownlang/modules/std/std_thread.java index 07c8ead..d4ece89 100644 --- a/modules/main/src/main/java/com/annimon/ownlang/modules/std/std_thread.java +++ b/modules/main/src/main/java/com/annimon/ownlang/modules/std/std_thread.java @@ -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); diff --git a/ownlang-core/src/main/java/com/annimon/ownlang/lib/Functions.java b/ownlang-core/src/main/java/com/annimon/ownlang/lib/Functions.java index ed40469..f0f643f 100644 --- a/ownlang-core/src/main/java/com/annimon/ownlang/lib/Functions.java +++ b/ownlang-core/src/main/java/com/annimon/ownlang/lib/Functions.java @@ -12,15 +12,12 @@ public final class Functions { public static Map getFunctions() { 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); } diff --git a/ownlang-core/src/main/java/com/annimon/ownlang/lib/ScopeHandler.java b/ownlang-core/src/main/java/com/annimon/ownlang/lib/ScopeHandler.java index 179c401..79d6a4b 100644 --- a/ownlang-core/src/main/java/com/annimon/ownlang/lib/ScopeHandler.java +++ b/ownlang-core/src/main/java/com/annimon/ownlang/lib/ScopeHandler.java @@ -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); } diff --git a/ownlang-core/src/main/java/com/annimon/ownlang/lib/StringValue.java b/ownlang-core/src/main/java/com/annimon/ownlang/lib/StringValue.java index db975b8..1e6bdee 100644 --- a/ownlang-core/src/main/java/com/annimon/ownlang/lib/StringValue.java +++ b/ownlang-core/src/main/java/com/annimon/ownlang/lib/StringValue.java @@ -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; diff --git a/ownlang-core/src/main/java/com/annimon/ownlang/lib/Variables.java b/ownlang-core/src/main/java/com/annimon/ownlang/lib/Variables.java index a3b4121..f58dd51 100644 --- a/ownlang-core/src/main/java/com/annimon/ownlang/lib/Variables.java +++ b/ownlang-core/src/main/java/com/annimon/ownlang/lib/Variables.java @@ -12,22 +12,21 @@ public final class Variables { public static Map 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); } diff --git a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/BinaryExpression.java b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/BinaryExpression.java index d8e49d3..e6b5499 100644 --- a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/BinaryExpression.java +++ b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/BinaryExpression.java @@ -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; } diff --git a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/FunctionDefineStatement.java b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/FunctionDefineStatement.java index 460f864..80e64c7 100644 --- a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/FunctionDefineStatement.java +++ b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/FunctionDefineStatement.java @@ -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 diff --git a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/FunctionReferenceExpression.java b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/FunctionReferenceExpression.java index 702d2fa..891abeb 100644 --- a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/FunctionReferenceExpression.java +++ b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/FunctionReferenceExpression.java @@ -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 diff --git a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/ObjectCreationExpression.java b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/ObjectCreationExpression.java index bd56c59..1f5ec25 100644 --- a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/ObjectCreationExpression.java +++ b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/ObjectCreationExpression.java @@ -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()); } diff --git a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/VariableExpression.java b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/VariableExpression.java index 4d4bac0..36c3923 100644 --- a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/VariableExpression.java +++ b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/ast/VariableExpression.java @@ -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 diff --git a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/linters/AssignValidator.java b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/linters/AssignValidator.java index 7b680bf..1ce2b76 100644 --- a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/linters/AssignValidator.java +++ b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/linters/AssignValidator.java @@ -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)); } diff --git a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/linters/DefaultFunctionsOverrideValidator.java b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/linters/DefaultFunctionsOverrideValidator.java index e5bff52..285f157 100644 --- a/ownlang-parser/src/main/java/com/annimon/ownlang/parser/linters/DefaultFunctionsOverrideValidator.java +++ b/ownlang-parser/src/main/java/com/annimon/ownlang/parser/linters/DefaultFunctionsOverrideValidator.java @@ -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)); } diff --git a/ownlang-parser/src/test/java/com/annimon/ownlang/parser/ParserTest.java b/ownlang-parser/src/test/java/com/annimon/ownlang/parser/ParserTest.java index 21e11bf..b1ad779 100644 --- a/ownlang-parser/src/test/java/com/annimon/ownlang/parser/ParserTest.java +++ b/ownlang-parser/src/test/java/com/annimon/ownlang/parser/ParserTest.java @@ -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) { diff --git a/ownlang-parser/src/test/java/com/annimon/ownlang/parser/ProgramsTest.java b/ownlang-parser/src/test/java/com/annimon/ownlang/parser/ProgramsTest.java index efd6270..9a41306 100644 --- a/ownlang-parser/src/test/java/com/annimon/ownlang/parser/ProgramsTest.java +++ b/ownlang-parser/src/test/java/com/annimon/ownlang/parser/ProgramsTest.java @@ -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); }