From 5ba737fbc8edac0eb5ed750d92a5b3158fb2a32e Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Wed, 5 Aug 2020 22:11:19 +0200 Subject: Adds JSON export. --- src/core/src/tonkadur/wyrd/v1/lang/World.java | 20 ++ .../wyrd/v1/lang/computation/Operation.java | 7 +- .../wyrd/v1/lang/meta/ComputationVisitor.java | 24 +-- .../wyrd/v1/lang/meta/InstructionVisitor.java | 18 +- .../tonkadur/jsonexport/ComputationCompiler.java | 239 +++++++++++++++++++++ .../tonkadur/jsonexport/InstructionCompiler.java | 156 ++++++++++++++ .../src/tonkadur/jsonexport/Translator.java | 209 ++++++++++++++++++ .../src/tonkadur/plugin/JSONExport.java | 24 ++- 8 files changed, 674 insertions(+), 23 deletions(-) create mode 100644 src/json-export/src/tonkadur/jsonexport/ComputationCompiler.java create mode 100644 src/json-export/src/tonkadur/jsonexport/InstructionCompiler.java create mode 100644 src/json-export/src/tonkadur/jsonexport/Translator.java diff --git a/src/core/src/tonkadur/wyrd/v1/lang/World.java b/src/core/src/tonkadur/wyrd/v1/lang/World.java index 39e6485..3649756 100644 --- a/src/core/src/tonkadur/wyrd/v1/lang/World.java +++ b/src/core/src/tonkadur/wyrd/v1/lang/World.java @@ -43,11 +43,21 @@ public class World required_extensions.add(name); } + public Set get_required_extensions () + { + return required_extensions; + } + public DictType get_dict_type (final String name) { return dict_types.get(name); } + public List get_ordered_dict_types () + { + return dict_types_in_order; + } + public void add_dict_type (final DictType dict_type) { dict_types.put(dict_type.get_name(), dict_type); @@ -59,6 +69,11 @@ public class World return variables.get(name); } + public Map get_variables () + { + return variables; + } + public void add_variable (final Variable variable) { variables.put(variable.get_name(), variable); @@ -69,6 +84,11 @@ public class World sequence_labels.put(name, line); } + public Map get_sequence_labels () + { + return sequence_labels; + } + public void add_instruction (final Instruction i) { code.add(i); diff --git a/src/core/src/tonkadur/wyrd/v1/lang/computation/Operation.java b/src/core/src/tonkadur/wyrd/v1/lang/computation/Operation.java index 6806936..365bd07 100644 --- a/src/core/src/tonkadur/wyrd/v1/lang/computation/Operation.java +++ b/src/core/src/tonkadur/wyrd/v1/lang/computation/Operation.java @@ -199,7 +199,12 @@ public class Operation extends Computation return param_a; } - public Computation get_secomd_parameter () + public String get_operator () + { + return operator; + } + + public Computation get_second_parameter () { return param_b; } diff --git a/src/core/src/tonkadur/wyrd/v1/lang/meta/ComputationVisitor.java b/src/core/src/tonkadur/wyrd/v1/lang/meta/ComputationVisitor.java index a0e1dc1..e0944a4 100644 --- a/src/core/src/tonkadur/wyrd/v1/lang/meta/ComputationVisitor.java +++ b/src/core/src/tonkadur/wyrd/v1/lang/meta/ComputationVisitor.java @@ -5,38 +5,38 @@ import tonkadur.wyrd.v1.lang.computation.*; public interface ComputationVisitor { public void visit_add_rich_text_effect (final AddRichTextEffect n) - throws Exception; + throws Throwable; public void visit_cast (final Cast n) - throws Exception; + throws Throwable; public void visit_constant (final Constant n) - throws Exception; + throws Throwable; public void visit_if_else_computation (final IfElseComputation n) - throws Exception; + throws Throwable; public void visit_new (final New n) - throws Exception; + throws Throwable; public void visit_newline (final Newline n) - throws Exception; + throws Throwable; public void visit_operation (final Operation n) - throws Exception; + throws Throwable; public void visit_ref (final Ref n) - throws Exception; + throws Throwable; public void visit_relative_ref (final RelativeRef n) - throws Exception; + throws Throwable; public void visit_rich_text (final RichText n) - throws Exception; + throws Throwable; public void visit_size (final Size n) - throws Exception; + throws Throwable; public void visit_value_of (final ValueOf n) - throws Exception; + throws Throwable; } diff --git a/src/core/src/tonkadur/wyrd/v1/lang/meta/InstructionVisitor.java b/src/core/src/tonkadur/wyrd/v1/lang/meta/InstructionVisitor.java index cf28ae8..8db6db0 100644 --- a/src/core/src/tonkadur/wyrd/v1/lang/meta/InstructionVisitor.java +++ b/src/core/src/tonkadur/wyrd/v1/lang/meta/InstructionVisitor.java @@ -5,29 +5,29 @@ import tonkadur.wyrd.v1.lang.instruction.*; public interface InstructionVisitor { public void visit_add_choice (final AddChoice n) - throws Exception; + throws Throwable; public void visit_assert (final Assert n) - throws Exception; + throws Throwable; public void visit_display (final Display n) - throws Exception; + throws Throwable; public void visit_end (final End n) - throws Exception; + throws Throwable; public void visit_event_call (final EventCall n) - throws Exception; + throws Throwable; public void visit_remove (final Remove n) - throws Exception; + throws Throwable; public void visit_resolve_choices (final ResolveChoices n) - throws Exception; + throws Throwable; public void visit_set_pc (final SetPC n) - throws Exception; + throws Throwable; public void visit_set_value (final SetValue n) - throws Exception; + throws Throwable; } diff --git a/src/json-export/src/tonkadur/jsonexport/ComputationCompiler.java b/src/json-export/src/tonkadur/jsonexport/ComputationCompiler.java new file mode 100644 index 0000000..1d3122e --- /dev/null +++ b/src/json-export/src/tonkadur/jsonexport/ComputationCompiler.java @@ -0,0 +1,239 @@ +package tonkadur.jsonexport; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import tonkadur.wyrd.v1.lang.meta.ComputationVisitor; +import tonkadur.wyrd.v1.lang.meta.Computation; + +import tonkadur.wyrd.v1.lang.computation.*; + +public class ComputationCompiler implements ComputationVisitor +{ + protected JSONObject result; + + public void visit_add_rich_text_effect (final AddRichTextEffect n) + throws Throwable + { + final JSONArray params, content; + + params = new JSONArray(); + content = new JSONArray(); + + for (final Computation c: n.get_content()) + { + final ComputationCompiler content_cc; + + content_cc = new ComputationCompiler(); + + c.get_visited_by(content_cc); + + content.add(content_cc.get_result()); + } + + for (final Computation c: n.get_effect_parameters()) + { + final ComputationCompiler param_cc; + + param_cc = new ComputationCompiler(); + + c.get_visited_by(param_cc); + + params.add(param_cc.get_result()); + } + + result = new JSONObject(); + + result.put("category", "add_rich_text_effect"); + result.put("effect", n.get_effect_name()); + result.put("parameters", params); + result.put("content", content); + } + + public void visit_cast (final Cast n) + throws Throwable + { + final ComputationCompiler cc; + + cc = new ComputationCompiler(); + + n.get_parent().get_visited_by(cc); + + result = new JSONObject(); + + result.put("category", "cast"); + result.put("from", Translator.compile_type(n.get_parent().get_type())); + result.put("to", Translator.compile_type(n.get_type())); + result.put("content", cc.get_result()); + } + + public void visit_constant (final Constant n) + throws Throwable + { + result = new JSONObject(); + + result.put("category", "constant"); + result.put("type", Translator.compile_type(n.get_type())); + result.put("value", n.get_as_string()); + } + + public void visit_if_else_computation (final IfElseComputation n) + throws Throwable + { + final ComputationCompiler cond_cc, if_true_cc, if_false_cc; + + cond_cc = new ComputationCompiler(); + if_true_cc = new ComputationCompiler(); + if_false_cc = new ComputationCompiler(); + + n.get_condition().get_visited_by(cond_cc); + n.get_if_true().get_visited_by(if_true_cc); + n.get_if_false().get_visited_by(if_false_cc); + + result = new JSONObject(); + + result.put("category", "if_else"); + result.put("type", Translator.compile_type(n.get_type())); + result.put("condition", cond_cc.get_result()); + result.put("if_true", if_true_cc.get_result()); + result.put("if_false", if_false_cc.get_result()); + } + + public void visit_new (final New n) + throws Throwable + { + result = new JSONObject(); + + result.put("category", "new"); + result.put("target", Translator.compile_type(n.get_target_type())); + } + + public void visit_newline (final Newline n) + throws Throwable + { + result = new JSONObject(); + + result.put("category", "newline"); + } + + public void visit_operation (final Operation n) + throws Throwable + { + ComputationCompiler cc; + + cc = new ComputationCompiler(); + + result = new JSONObject(); + + n.get_first_parameter().get_visited_by(cc); + + result.put("category", "operation"); + result.put("operator", n.get_operator()); + result.put("type", Translator.compile_type(n.get_type())); + result.put("x", cc.get_result()); + + if (n.get_second_parameter() != null) + { + cc = new ComputationCompiler(); + + n.get_second_parameter().get_visited_by(cc); + + result.put("y", cc.get_result()); + } + } + + public void visit_ref (final Ref n) + throws Throwable + { + final ComputationCompiler cc; + + cc = new ComputationCompiler(); + + n.get_address().get_visited_by(cc); + + result = new JSONObject(); + + result.put("category", "ref"); + result.put("target_type", Translator.compile_type(n.get_type())); + result.put("address", cc.get_result()); + } + + public void visit_relative_ref (final RelativeRef n) + throws Throwable + { + final ComputationCompiler cc, param_cc; + + cc = new ComputationCompiler(); + param_cc = new ComputationCompiler(); + + n.get_address().get_visited_by(cc); + n.get_member().get_visited_by(param_cc); + + result = new JSONObject(); + + result.put("category", "relative_ref"); + result.put("type", Translator.compile_type(n.get_type())); + result.put("base", cc.get_result()); + result.put("extra", param_cc.get_result()); + } + + public void visit_rich_text (final RichText n) + throws Throwable + { + final JSONArray content; + + content = new JSONArray(); + + for (final Computation c: n.get_content()) + { + final ComputationCompiler cc; + + cc = new ComputationCompiler(); + + c.get_visited_by(cc); + + content.add(cc.get_result()); + } + + result = new JSONObject(); + + result.put("category", "rich_text"); + result.put("content", content); + } + + public void visit_size (final Size n) + throws Throwable + { + final ComputationCompiler cc; + + cc = new ComputationCompiler(); + + n.get_collection().get_visited_by(cc); + + result = new JSONObject(); + + result.put("category", "size"); + result.put("reference", cc.get_result()); + } + + public void visit_value_of (final ValueOf n) + throws Throwable + { + final ComputationCompiler cc; + + cc = new ComputationCompiler(); + + n.get_parent().get_visited_by(cc); + + result = new JSONObject(); + + result.put("category", "value_of"); + result.put("type", Translator.compile_type(n.get_type())); + result.put("reference", cc.get_result()); + } + + public JSONObject get_result () + { + return result; + } +} diff --git a/src/json-export/src/tonkadur/jsonexport/InstructionCompiler.java b/src/json-export/src/tonkadur/jsonexport/InstructionCompiler.java new file mode 100644 index 0000000..00445c7 --- /dev/null +++ b/src/json-export/src/tonkadur/jsonexport/InstructionCompiler.java @@ -0,0 +1,156 @@ +package tonkadur.jsonexport; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +import tonkadur.wyrd.v1.lang.meta.InstructionVisitor; +import tonkadur.wyrd.v1.lang.meta.Computation; + +import tonkadur.wyrd.v1.lang.instruction.*; + +public class InstructionCompiler implements InstructionVisitor +{ + protected JSONObject result; + + public void visit_add_choice (final AddChoice n) + throws Throwable + { + final ComputationCompiler label_cc, address_cc; + + label_cc = new ComputationCompiler(); + address_cc = new ComputationCompiler(); + + n.get_label().get_visited_by(label_cc); + n.get_address().get_visited_by(address_cc); + + result = new JSONObject(); + + result.put("category", "add_choice"); + result.put("label", label_cc.get_result()); + result.put("address", address_cc.get_result()); + } + + public void visit_assert (final Assert n) + throws Throwable + { + final ComputationCompiler cc; + + cc = new ComputationCompiler(); + + n.get_condition().get_visited_by(cc); + + result = new JSONObject(); + + result.put("category", "assert"); + result.put("condition", cc.get_result()); + } + + public void visit_display (final Display n) + throws Throwable + { + final ComputationCompiler cc; + + cc = new ComputationCompiler(); + + n.get_content().get_visited_by(cc); + + result = new JSONObject(); + + result.put("category", "display"); + result.put("content", cc.get_result()); + } + + public void visit_end (final End n) + throws Throwable + { + result = new JSONObject(); + + result.put("category", "end"); + } + + public void visit_event_call (final EventCall n) + throws Throwable + { + final JSONArray params; + + params = new JSONArray(); + + for (final Computation c: n.get_parameters()) + { + final ComputationCompiler cc; + + cc = new ComputationCompiler(); + + c.get_visited_by(cc); + + params.add(cc.get_result()); + } + + result = new JSONObject(); + + result.put("category", "event_call"); + result.put("event", n.get_name()); + result.put("parameters", params); + } + + public void visit_remove (final Remove n) + throws Throwable + { + final ComputationCompiler cc; + + cc = new ComputationCompiler(); + + n.get_reference().get_visited_by(cc); + + result = new JSONObject(); + + result.put("category", "remove"); + result.put("reference", cc.get_result()); + } + + public void visit_resolve_choices (final ResolveChoices n) + throws Throwable + { + result = new JSONObject(); + + result.put("category", "resolve_choices"); + } + + public void visit_set_pc (final SetPC n) + throws Throwable + { + final ComputationCompiler cc; + + cc = new ComputationCompiler(); + + n.get_value().get_visited_by(cc); + + result = new JSONObject(); + + result.put("category", "set_pc"); + result.put("value", cc.get_result()); + } + + public void visit_set_value (final SetValue n) + throws Throwable + { + final ComputationCompiler ref_cc, val_cc; + + ref_cc = new ComputationCompiler(); + val_cc = new ComputationCompiler(); + + n.get_reference().get_visited_by(ref_cc); + n.get_value().get_visited_by(val_cc); + + result = new JSONObject(); + + result.put("category", "set_value"); + result.put("reference", ref_cc.get_result()); + result.put("value", val_cc.get_result()); + } + + public JSONObject get_result () + { + return result; + } +} diff --git a/src/json-export/src/tonkadur/jsonexport/Translator.java b/src/json-export/src/tonkadur/jsonexport/Translator.java new file mode 100644 index 0000000..edd0a7f --- /dev/null +++ b/src/json-export/src/tonkadur/jsonexport/Translator.java @@ -0,0 +1,209 @@ +package tonkadur.jsonexport; + +import java.util.Map; + +import java.io.PrintWriter; + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + + +import tonkadur.wyrd.v1.lang.World; +import tonkadur.wyrd.v1.lang.type.*; +import tonkadur.wyrd.v1.lang.Variable; + +import tonkadur.wyrd.v1.lang.meta.Instruction; + +import tonkadur.wyrd.v1.lang.meta.Computation; + +public class Translator +{ + public static void toJSON (final World wyrd_world, final String output_file) + throws Throwable + { + final PrintWriter out; + final JSONObject result; + + result = new JSONObject(); + + result.put("extensions", get_compiled_extension_list(wyrd_world)); + result.put("structure_types", get_compiled_types(wyrd_world)); + result.put("variables", get_compiled_variables(wyrd_world)); + result.put("sequences", get_compiled_sequence_labels(wyrd_world)); + result.put("code", get_compiled_code(wyrd_world)); + + + out = new PrintWriter(output_file); + out.println(result.toJSONString()); + out.close(); + + System.out.println("JSON Result:"); + System.out.println(result.toJSONString()); + } + + public static JSONArray get_compiled_variables (final World wyrd_world) + throws Throwable + { + final JSONArray result; + + result = new JSONArray(); + + for + ( + final Map.Entry e: + wyrd_world.get_variables().entrySet() + ) + { + final JSONObject obj; + + obj = new JSONObject(); + + obj.put("name", e.getKey()); + obj.put("type", compile_type(e.getValue().get_type())); + + result.add(obj); + } + + return result; + } + + public static JSONArray get_compiled_sequence_labels (final World wyrd_world) + throws Throwable + { + final JSONArray result; + + result = new JSONArray(); + + for + ( + final Map.Entry i: + wyrd_world.get_sequence_labels().entrySet() + ) + { + final JSONObject obj; + + obj = new JSONObject(); + + obj.put("name", i.getKey()); + obj.put("line", i.getValue()); + + result.add(obj); + } + + return result; + } + + public static JSONArray get_compiled_extension_list (final World wyrd_world) + throws Throwable + { + final JSONArray result; + + result = new JSONArray(); + + for (final String e: wyrd_world.get_required_extensions()) + { + result.add(e); + } + + return result; + } + + public static JSONArray get_compiled_types (final World wyrd_world) + throws Throwable + { + final JSONArray result; + + result = new JSONArray(); + + for (final DictType structure: wyrd_world.get_ordered_dict_types()) + { + final JSONObject type; + final JSONArray fields; + + fields = new JSONArray(); + + for + ( + final Map.Entry field: + structure.get_fields().entrySet() + ) + { + final JSONObject f; + + f = new JSONObject(); + + f.put("name", field.getKey()); + f.put("type", compile_type(field.getValue())); + + fields.add(f); + } + + type = new JSONObject(); + + type.put("name", structure.get_name()); + type.put("fields", fields); + + result.add(type); + } + + return result; + } + + public static JSONArray get_compiled_code (final World wyrd_world) + throws Throwable + { + final JSONArray result; + + result = new JSONArray(); + + for (final Instruction code_line: wyrd_world.get_code()) + { + final InstructionCompiler ic; + + ic = new InstructionCompiler(); + + code_line.get_visited_by(ic); + + result.add(ic.get_result()); + } + + return result; + } + + public static JSONObject compile_type (final Type t) + { + final JSONObject result; + + result = new JSONObject(); + + if (t instanceof DictType) + { + result.put("category", "structure"); + result.put("name", t.get_name()); + } + else if (t instanceof PointerType) + { + result.put("category", "pointer"); + result.put + ( + "target", + compile_type(((PointerType) t).get_target_type()) + ); + } + else if (t instanceof MapType) + { + result.put("category", "list"); + result.put + ( + "member_type", + compile_type(((MapType) t).get_member_type()) + ); + } + else + { + result.put("category", t.get_name()); + } + + return result; + } +} diff --git a/src/json-export/src/tonkadur/plugin/JSONExport.java b/src/json-export/src/tonkadur/plugin/JSONExport.java index 33bf141..6d746f9 100644 --- a/src/json-export/src/tonkadur/plugin/JSONExport.java +++ b/src/json-export/src/tonkadur/plugin/JSONExport.java @@ -2,11 +2,33 @@ package tonkadur.plugin; import tonkadur.TonkadurPlugin; +import tonkadur.wyrd.v1.lang.World; + +import tonkadur.jsonexport.Translator; + public class JSONExport extends TonkadurPlugin { + World wyrd_world; + String output_file; + @Override public void initialize (final String[] args) + throws Throwable + { + output_file = (args[0] + ".json"); + } + + @Override + public void post_wyrd_compile (final World wyrd_world) + throws Throwable + { + this.wyrd_world = wyrd_world; + } + + @Override + public void finalize () + throws Throwable { - System.out.println("JSONExport plugin initialized."); + Translator.toJSON(wyrd_world, output_file); } } -- cgit v1.2.3-70-g09d2