mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 00:34:20 +03:00
Обновлён ModulesInfoCreator
This commit is contained in:
parent
7e161985da
commit
059b4c46d0
@ -2,15 +2,21 @@ package com.annimon.ownlang.utils;
|
||||
|
||||
import com.annimon.ownlang.annotations.Modules;
|
||||
import com.annimon.ownlang.lib.Functions;
|
||||
import com.annimon.ownlang.lib.MapValue;
|
||||
import com.annimon.ownlang.lib.Types;
|
||||
import com.annimon.ownlang.lib.Value;
|
||||
import com.annimon.ownlang.lib.Variables;
|
||||
import com.annimon.ownlang.lib.modules.Module;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
@ -32,6 +38,7 @@ public final class ModulesInfoCreator {
|
||||
final ModuleInfo moduleInfo = new ModuleInfo(moduleClass.getSimpleName());
|
||||
moduleInfo.functions.addAll(Functions.getFunctions().keySet());
|
||||
moduleInfo.constants.putAll(Variables.variables());
|
||||
moduleInfo.types.addAll(listValues(moduleClass));
|
||||
moduleInfos.add(moduleInfo);
|
||||
}
|
||||
|
||||
@ -52,15 +59,30 @@ public final class ModulesInfoCreator {
|
||||
);
|
||||
}
|
||||
|
||||
private static List<String> listValues(Class moduleClass) {
|
||||
return Arrays.stream(moduleClass.getDeclaredClasses())
|
||||
.filter(clazz -> getAllInterfaces(clazz).stream().anyMatch(i -> i.equals(Value.class)))
|
||||
.map(Class::getSimpleName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static Set<Class> getAllInterfaces(Class clazz) {
|
||||
if (clazz.getSuperclass() == null) return Collections.emptySet();
|
||||
return Stream.concat(Arrays.stream(clazz.getInterfaces()), getAllInterfaces(clazz.getSuperclass()).stream())
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
static class ModuleInfo {
|
||||
private final String name;
|
||||
List<String> functions;
|
||||
Map<String, Value> constants;
|
||||
List<String> types;
|
||||
|
||||
public ModuleInfo(String name) {
|
||||
this.name = name;
|
||||
functions = new ArrayList<>();
|
||||
constants = new HashMap<>();
|
||||
types = new ArrayList<>();
|
||||
}
|
||||
|
||||
public JSONArray constantsJSON() {
|
||||
@ -74,7 +96,16 @@ public final class ModulesInfoCreator {
|
||||
constant.put("name", entry.getKey());
|
||||
constant.put("type", value.type());
|
||||
constant.put("typeName", Types.typeToString(value.type()));
|
||||
if (value.type() == Types.MAP) {
|
||||
String text = ((Map<Value, Value>) value.raw()).entrySet().stream()
|
||||
.sorted(Comparator.comparing(
|
||||
e -> ((MapValue)value).size() > 16 ? e.getKey() : e.getValue()))
|
||||
.map(s -> s.toString())
|
||||
.collect(Collectors.joining(", ", "{", "}"));
|
||||
constant.put("value", text);
|
||||
} else {
|
||||
constant.put("value", value.asString());
|
||||
}
|
||||
result.put(constant);
|
||||
});
|
||||
return result;
|
||||
@ -85,6 +116,15 @@ public final class ModulesInfoCreator {
|
||||
json.put("name", name);
|
||||
json.put("functions", functions.stream().sorted().toArray());
|
||||
json.put("constants", constantsJSON());
|
||||
if (!types.isEmpty()) {
|
||||
json.put("types", types.stream().sorted()
|
||||
.map(s -> {
|
||||
final JSONObject type = new JSONObject();
|
||||
type.put("name", s);
|
||||
return type;
|
||||
})
|
||||
.toArray());
|
||||
}
|
||||
return json;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user