mirror of
https://github.com/aNNiMON/Own-Programming-Language-Tutorial.git
synced 2024-09-20 08:44:20 +03:00
Вывод информации о всех функция и константах модулей
This commit is contained in:
parent
66415a6c31
commit
3a75727982
13
src/com/annimon/ownlang/annotations/Modules.java
Normal file
13
src/com/annimon/ownlang/annotations/Modules.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package com.annimon.ownlang.annotations;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.PACKAGE)
|
||||||
|
public @interface Modules {
|
||||||
|
|
||||||
|
Class[] modules();
|
||||||
|
}
|
@ -42,6 +42,10 @@ public final class Variables {
|
|||||||
scope.variables.put("false", NumberValue.ZERO);
|
scope.variables.put("false", NumberValue.ZERO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Map<String, Value> variables() {
|
||||||
|
return scope.variables;
|
||||||
|
}
|
||||||
|
|
||||||
public static void push() {
|
public static void push() {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
final Scope newScope = new Scope(scope);
|
final Scope newScope = new Scope(scope);
|
||||||
|
16
src/com/annimon/ownlang/lib/modules/package-info.java
Normal file
16
src/com/annimon/ownlang/lib/modules/package-info.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
@Modules(modules = {
|
||||||
|
canvas.class,
|
||||||
|
canvasfx.class,
|
||||||
|
files.class,
|
||||||
|
functional.class,
|
||||||
|
http.class,
|
||||||
|
json.class,
|
||||||
|
math.class,
|
||||||
|
ounit.class,
|
||||||
|
robot.class,
|
||||||
|
std.class,
|
||||||
|
types.class
|
||||||
|
})
|
||||||
|
package com.annimon.ownlang.lib.modules;
|
||||||
|
|
||||||
|
import com.annimon.ownlang.annotations.Modules;
|
91
src/com/annimon/ownlang/utils/ModulesInfoCreator.java
Normal file
91
src/com/annimon/ownlang/utils/ModulesInfoCreator.java
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
package com.annimon.ownlang.utils;
|
||||||
|
|
||||||
|
import com.annimon.ownlang.annotations.Modules;
|
||||||
|
import com.annimon.ownlang.lib.Functions;
|
||||||
|
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.Comparator;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
public final class ModulesInfoCreator {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
|
||||||
|
final Class<Module> clazz = Module.class; // get classloader for package
|
||||||
|
|
||||||
|
final List<ModuleInfo> moduleInfos = new ArrayList<>();
|
||||||
|
|
||||||
|
final Package modulesPackage = Package.getPackage("com.annimon.ownlang.lib.modules");
|
||||||
|
final Modules annotation = modulesPackage.getAnnotation(Modules.class);
|
||||||
|
for (Class moduleClass : annotation.modules()) {
|
||||||
|
Functions.getFunctions().clear();
|
||||||
|
Variables.variables().clear();
|
||||||
|
final Module module = (Module) moduleClass.newInstance();
|
||||||
|
module.init();
|
||||||
|
|
||||||
|
final ModuleInfo moduleInfo = new ModuleInfo(moduleClass.getSimpleName());
|
||||||
|
moduleInfo.functions.addAll(Functions.getFunctions().keySet());
|
||||||
|
moduleInfo.constants.putAll(Variables.variables());
|
||||||
|
moduleInfos.add(moduleInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
final JSONArray modulesJson = new JSONArray();
|
||||||
|
for (ModuleInfo moduleInfo : moduleInfos) {
|
||||||
|
modulesJson.put(moduleInfo.toJSON());
|
||||||
|
}
|
||||||
|
System.out.println(modulesJson.toString(2));
|
||||||
|
|
||||||
|
System.out.println("Total modules: " + moduleInfos.size());
|
||||||
|
System.out.println("Total functions: " + moduleInfos.stream()
|
||||||
|
.flatMap(m -> m.functions.stream())
|
||||||
|
.count()
|
||||||
|
);
|
||||||
|
System.out.println("Total constants: " + moduleInfos.stream()
|
||||||
|
.flatMap(m -> m.constants.keySet().stream())
|
||||||
|
.count()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static class ModuleInfo {
|
||||||
|
private final String name;
|
||||||
|
List<String> functions;
|
||||||
|
Map<String, Value> constants;
|
||||||
|
|
||||||
|
public ModuleInfo(String name) {
|
||||||
|
this.name = name;
|
||||||
|
functions = new ArrayList<>();
|
||||||
|
constants = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONArray constantsJSON() {
|
||||||
|
final JSONArray result = new JSONArray();
|
||||||
|
constants.entrySet().stream()
|
||||||
|
.sorted(Comparator.comparing(e -> e.getKey()))
|
||||||
|
.forEach(entry -> {
|
||||||
|
final Value value = entry.getValue();
|
||||||
|
|
||||||
|
final JSONObject constant = new JSONObject();
|
||||||
|
constant.put("name", entry.getKey());
|
||||||
|
constant.put("type", value.type());
|
||||||
|
constant.put("typeName", Types.typeToString(value.type()));
|
||||||
|
constant.put("value", value.asString());
|
||||||
|
result.put(constant);
|
||||||
|
});
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public JSONObject toJSON() {
|
||||||
|
final JSONObject json = new JSONObject();
|
||||||
|
json.put("name", name);
|
||||||
|
json.put("functions", functions.stream().sorted().toArray());
|
||||||
|
json.put("constants", constantsJSON());
|
||||||
|
return json;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user