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 f0f643f..6d5c752 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 @@ -1,7 +1,5 @@ package com.annimon.ownlang.lib; -import java.util.Map; - /** * * @author aNNiMON @@ -9,10 +7,6 @@ import java.util.Map; public final class Functions { private Functions() { } - public static Map getFunctions() { - return ScopeHandler.functions(); - } - /** * @deprecated This function remains for backward compatibility with old separate modules * Use {@link ScopeHandler#setFunction(String, Function)} 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 f58dd51..25fe43b 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 @@ -1,7 +1,5 @@ package com.annimon.ownlang.lib; -import java.util.Map; - /** * * @author aNNiMON @@ -9,10 +7,6 @@ import java.util.Map; public final class Variables { private Variables() { } - public static Map variables() { - return ScopeHandler.variables(); - } - /** * @deprecated This function remains for backward compatibility with old separate modules * Use {@link ScopeHandler#setVariable(String, Value)} diff --git a/ownlang-utils/src/main/java/com/annimon/ownlang/utils/Repl.java b/ownlang-utils/src/main/java/com/annimon/ownlang/utils/Repl.java index 248008c..8d1bf06 100644 --- a/ownlang-utils/src/main/java/com/annimon/ownlang/utils/Repl.java +++ b/ownlang-utils/src/main/java/com/annimon/ownlang/utils/Repl.java @@ -4,9 +4,7 @@ import com.annimon.ownlang.Console; import com.annimon.ownlang.Version; import com.annimon.ownlang.exceptions.OwnLangParserException; import com.annimon.ownlang.exceptions.StoppedException; -import com.annimon.ownlang.lib.Functions; -import com.annimon.ownlang.lib.UserDefinedFunction; -import com.annimon.ownlang.lib.Variables; +import com.annimon.ownlang.lib.*; import com.annimon.ownlang.parser.*; import com.annimon.ownlang.parser.ast.BlockStatement; import com.annimon.ownlang.parser.ast.Statement; @@ -116,9 +114,9 @@ public final class Repl { System.out.println("Type in expressions to have them evaluated."); final List commands = new ArrayList<>(); if (full) { - commands.add(VARS + " - listing variables"); - commands.add(FUNCS + " - listing functions"); - commands.add(SOURCE + " - listing source"); + commands.add(VARS + " - list variables/constants"); + commands.add(FUNCS + " - list functions"); + commands.add(SOURCE + " - show source"); } commands.add(HELP + " - show help"); commands.add(RESET + " - clear buffer"); @@ -143,24 +141,45 @@ public final class Repl { } private static void printVariables() { - Variables.variables().entrySet().stream() + System.out.println("Variables:"); + ScopeHandler.variables().entrySet().stream() .sorted(Map.Entry.comparingByKey()) - .forEach(e -> System.out.printf("\t%s = %s%n", + .forEach(e -> System.out.printf(" %s = %s%n", + e.getKey(), e.getValue().toString())); + + System.out.println("Constants:"); + ScopeHandler.constants().entrySet().stream() + .sorted(Map.Entry.comparingByKey()) + .forEach(e -> System.out.printf(" %s = %s%n", e.getKey(), e.getValue().toString())); } private static void printFunctions() { - System.out.println("User functions:"); - Functions.getFunctions().entrySet().stream() - .filter(p -> p.getValue() instanceof UserDefinedFunction) - .sorted(Map.Entry.comparingByKey()) - .forEach(e -> System.out.printf("\t%s%s%n", - e.getKey(), ((UserDefinedFunction)e.getValue()).arguments)); + if (ScopeHandler.functions().isEmpty()) { + System.out.println("No functions declared yet!"); + return; + } - System.out.println("Library functions:"); - Functions.getFunctions().entrySet().stream() - .filter(p -> !(p.getValue() instanceof UserDefinedFunction)) + final var functions = ScopeHandler.functions().entrySet().stream() .sorted(Map.Entry.comparingByKey()) - .forEach(e -> System.out.printf("\t%s%n", e.getKey())); + .collect(Collectors.partitioningBy(p -> p.getValue() instanceof UserDefinedFunction)); + + final var userFunctions = functions.get(true); + if (!userFunctions.isEmpty()) { + System.out.println("User functions:"); + for (Map.Entry e : userFunctions) { + System.out.printf(" %s%s%n", + e.getKey(), ((UserDefinedFunction) e.getValue()).arguments); + } + } + + final var libraryFunctions = functions.get(false); + if (!libraryFunctions.isEmpty()) { + System.out.printf("Library functions:%n "); + final var libraryFunctionNames = libraryFunctions.stream() + .map(Map.Entry::getKey) + .collect(Collectors.joining(", ")); + System.out.println(libraryFunctionNames); + } } } diff --git a/ownlang-utils/src/main/java/com/annimon/ownlang/utils/repl/OwnLangCompleter.java b/ownlang-utils/src/main/java/com/annimon/ownlang/utils/repl/OwnLangCompleter.java index e0a2f9e..38e8aba 100644 --- a/ownlang-utils/src/main/java/com/annimon/ownlang/utils/repl/OwnLangCompleter.java +++ b/ownlang-utils/src/main/java/com/annimon/ownlang/utils/repl/OwnLangCompleter.java @@ -1,7 +1,6 @@ package com.annimon.ownlang.utils.repl; -import com.annimon.ownlang.lib.Functions; -import com.annimon.ownlang.lib.Variables; +import com.annimon.ownlang.lib.ScopeHandler; import com.annimon.ownlang.parser.Lexer; import java.util.Collections; import java.util.HashSet; @@ -28,7 +27,8 @@ public final class OwnLangCompleter extends StringsCompleter { getStrings().clear(); getStrings().addAll(Lexer.getKeywords()); getStrings().addAll(staticCandidates); - getStrings().addAll(Variables.variables().keySet()); - getStrings().addAll(Functions.getFunctions().keySet()); + getStrings().addAll(ScopeHandler.constants().keySet()); + getStrings().addAll(ScopeHandler.variables().keySet()); + getStrings().addAll(ScopeHandler.functions().keySet()); } }