| summaryrefslogtreecommitdiff | 
diff options
Diffstat (limited to 'src/json-export')
4 files changed, 627 insertions, 1 deletions
| 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<String, Variable> 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<String, Integer> 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<String, Type> 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);     }  } | 


