Add listing constants to REPL

This commit is contained in:
aNNiMON 2023-10-01 16:27:36 +03:00 committed by Victor Melnik
parent ce14581bf4
commit 42e55bd4cc
4 changed files with 41 additions and 34 deletions

View File

@ -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<String, Function> getFunctions() {
return ScopeHandler.functions();
}
/**
* @deprecated This function remains for backward compatibility with old separate modules
* Use {@link ScopeHandler#setFunction(String, Function)}

View File

@ -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<String, Value> variables() {
return ScopeHandler.variables();
}
/**
* @deprecated This function remains for backward compatibility with old separate modules
* Use {@link ScopeHandler#setVariable(String, Value)}

View File

@ -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<String> 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<String, Function> 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);
}
}
}

View File

@ -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());
}
}