| summaryrefslogtreecommitdiff |
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2020-08-30 16:15:36 +0200 |
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2020-08-30 16:15:36 +0200 |
| commit | 23c5a3b1bd89e7a394a4cc4881e0764d601632c7 (patch) | |
| tree | 18db85b29fdd6f12e0ca1bc5fda96584b0af62ac | |
| parent | d68040d87385c360719e35941ad4f0112d9d0c03 (diff) | |
Adds user prompts for String and Integer.
I think so are seen frequently enough to warrant an addition.
11 files changed, 703 insertions, 25 deletions
diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/PromptInteger.java b/src/core/src/tonkadur/fate/v1/lang/instruction/PromptInteger.java new file mode 100644 index 0000000..ae28617 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/PromptInteger.java @@ -0,0 +1,150 @@ +package tonkadur.fate.v1.lang.instruction; + +import java.util.Collections; + +import tonkadur.error.ErrorManager; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Instruction; +import tonkadur.fate.v1.lang.meta.Computation; + +public class PromptInteger extends Instruction +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + protected final Computation target; + protected final Computation min; + protected final Computation max; + protected final Computation label; + + /***************************************************************************/ + /**** PROTECTED ************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + protected PromptInteger + ( + final Origin origin, + final Computation target, + final Computation min, + final Computation max, + final Computation label + ) + { + super(origin); + + this.target = target; + this.min = min; + this.max = max; + this.label = label; + } + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + public static PromptInteger build + ( + final Origin origin, + final Computation target, + final Computation min, + final Computation max, + final Computation label + ) + throws InvalidTypeException + { + if (!target.get_type().can_be_used_as(Type.INT)) + { + ErrorManager.handle + ( + new InvalidTypeException + ( + target.get_origin(), + target.get_type(), + Collections.singletonList(Type.INT) + ) + ); + } + + if (!min.get_type().can_be_used_as(Type.INT)) + { + ErrorManager.handle + ( + new InvalidTypeException + ( + min.get_origin(), + min.get_type(), + Collections.singletonList(Type.INT) + ) + ); + } + + if (!max.get_type().can_be_used_as(Type.INT)) + { + ErrorManager.handle + ( + new InvalidTypeException + ( + min.get_origin(), + min.get_type(), + Collections.singletonList(Type.INT) + ) + ); + } + + return new PromptInteger(origin, target, min, max, label); + } + + /**** Accessors ************************************************************/ + @Override + public void get_visited_by (final InstructionVisitor iv) + throws Throwable + { + iv.visit_prompt_integer(this); + } + + public Computation get_target () + { + return target; + } + + public Computation get_max () + { + return max; + } + + public Computation get_min () + { + return min; + } + + public Computation get_label () + { + return label; + } + + /**** Misc. ****************************************************************/ + @Override + public String toString () + { + final StringBuilder sb = new StringBuilder(); + + sb.append("(PromptInteger "); + sb.append(target.toString()); + sb.append(" "); + sb.append(min.toString()); + sb.append(" "); + sb.append(max.toString()); + sb.append(" "); + sb.append(label.toString()); + sb.append(")"); + + return sb.toString(); + } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/PromptString.java b/src/core/src/tonkadur/fate/v1/lang/instruction/PromptString.java new file mode 100644 index 0000000..e857952 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/PromptString.java @@ -0,0 +1,150 @@ +package tonkadur.fate.v1.lang.instruction; + +import java.util.Collections; + +import tonkadur.error.ErrorManager; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.type.Type; + +import tonkadur.fate.v1.lang.meta.InstructionVisitor; +import tonkadur.fate.v1.lang.meta.Instruction; +import tonkadur.fate.v1.lang.meta.Computation; + +public class PromptString extends Instruction +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + protected final Computation target; + protected final Computation min; + protected final Computation max; + protected final Computation label; + + /***************************************************************************/ + /**** PROTECTED ************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + protected PromptString + ( + final Origin origin, + final Computation target, + final Computation min, + final Computation max, + final Computation label + ) + { + super(origin); + + this.target = target; + this.min = min; + this.max = max; + this.label = label; + } + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + public static PromptString build + ( + final Origin origin, + final Computation target, + final Computation min, + final Computation max, + final Computation label + ) + throws InvalidTypeException + { + if (!target.get_type().can_be_used_as(Type.STRING)) + { + ErrorManager.handle + ( + new InvalidTypeException + ( + target.get_origin(), + target.get_type(), + Collections.singletonList(Type.STRING) + ) + ); + } + + if (!min.get_type().can_be_used_as(Type.INT)) + { + ErrorManager.handle + ( + new InvalidTypeException + ( + min.get_origin(), + min.get_type(), + Collections.singletonList(Type.INT) + ) + ); + } + + if (!max.get_type().can_be_used_as(Type.INT)) + { + ErrorManager.handle + ( + new InvalidTypeException + ( + min.get_origin(), + min.get_type(), + Collections.singletonList(Type.INT) + ) + ); + } + + return new PromptString(origin, target, min, max, label); + } + + /**** Accessors ************************************************************/ + @Override + public void get_visited_by (final InstructionVisitor iv) + throws Throwable + { + iv.visit_prompt_string(this); + } + + public Computation get_target () + { + return target; + } + + public Computation get_max () + { + return max; + } + + public Computation get_min () + { + return min; + } + + public Computation get_label () + { + return label; + } + + /**** Misc. ****************************************************************/ + @Override + public String toString () + { + final StringBuilder sb = new StringBuilder(); + + sb.append("(PromptString "); + sb.append(target.toString()); + sb.append(" "); + sb.append(min.toString()); + sb.append(" "); + sb.append(max.toString()); + sb.append(" "); + sb.append(label.toString()); + sb.append(")"); + + return sb.toString(); + } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java b/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java index afaa151..6e0da72 100644 --- a/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java +++ b/src/core/src/tonkadur/fate/v1/lang/meta/InstructionVisitor.java @@ -65,6 +65,12 @@ public interface InstructionVisitor public void visit_instruction_list (final InstructionList n) throws Throwable; + public void visit_prompt_integer (final PromptInteger n) + throws Throwable; + + public void visit_prompt_string (final PromptString n) + throws Throwable; + public void visit_player_choice (final PlayerChoice n) throws Throwable; diff --git a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 index 1525d42..271a766 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 @@ -85,6 +85,8 @@ REMOVE_ONE_KW: L_PAREN 'remove'US'one' SEP+; REMOVE_AT_KW: L_PAREN ('remove'US('elem'('ent')?US)?'at') SEP+; REQUIRE_EXTENSION_KW: L_PAREN 'require'US'extension' SEP+; REQUIRE_KW: L_PAREN 'require' SEP+; +PROMPT_STRING_KW: L_PAREN 'prompt_string' SEP+; +PROMPT_INTEGER_KW: L_PAREN 'prompt_int'('eger'?) SEP+; SET_FIELDS_KW: L_PAREN 'set'US'fields' SEP+; SET_KW: L_PAREN 'set'(US(('val''ue'?)|('var''iable'?)))? SEP+; LIST_KW: L_PAREN 'list' SEP+; diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 index 2c6aeca..4c0a7a0 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 @@ -512,6 +512,50 @@ returns [Instruction result] } } + | PROMPT_STRING_KW + target=value_reference WS+ + min_size=value WS+ + max_size=value WS+ + paragraph WS* + R_PAREN + { + $result = + PromptString.build + ( + CONTEXT.get_origin_at + ( + ($PROMPT_STRING_KW.getLine()), + ($PROMPT_STRING_KW.getCharPositionInLine()) + ), + ($target.result), + ($min_size.result), + ($max_size.result), + ($paragraph.result) + ); + } + + | PROMPT_INTEGER_KW + target=value_reference WS+ + min_size=value WS+ + max_size=value WS+ + paragraph WS* + R_PAREN + { + $result = + PromptInteger.build + ( + CONTEXT.get_origin_at + ( + ($PROMPT_INTEGER_KW.getLine()), + ($PROMPT_INTEGER_KW.getCharPositionInLine()) + ), + ($target.result), + ($min_size.result), + ($max_size.result), + ($paragraph.result) + ); + } + | ADD_KW value WS+ value_reference WS* R_PAREN { $result = @@ -1420,13 +1464,10 @@ returns [Instruction result] } WS+ { - BREAKABLE_LEVELS++; HIERARCHICAL_VARIABLES.push(new ArrayList()); } player_choice_list { - BREAKABLE_LEVELS--; - for (final String s: HIERARCHICAL_VARIABLES.pop()) { LOCAL_VARIABLES.peekFirst().remove(s); diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java index 4672f21..deb0772 100644 --- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java +++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/InstructionCompiler.java @@ -33,6 +33,8 @@ import tonkadur.wyrd.v1.lang.instruction.Display; import tonkadur.wyrd.v1.lang.instruction.End; import tonkadur.wyrd.v1.lang.instruction.EventCall; import tonkadur.wyrd.v1.lang.instruction.Remove; +import tonkadur.wyrd.v1.lang.instruction.PromptString; +import tonkadur.wyrd.v1.lang.instruction.PromptInteger; import tonkadur.wyrd.v1.lang.instruction.ResolveChoices; import tonkadur.wyrd.v1.lang.instruction.SetPC; import tonkadur.wyrd.v1.lang.instruction.SetValue; @@ -1310,14 +1312,7 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor result.add(cc.get_init()); } - labels_only.add - ( - new AddChoice - ( - cc.get_computation(), - compiler.assembler().get_label_constant(start_of_effect) - ) - ); + labels_only.add(new AddChoice(cc.get_computation())); labels_only.add ( @@ -1974,4 +1969,130 @@ implements tonkadur.fate.v1.lang.meta.InstructionVisitor value_cc.release_registers(); address_cc.release_registers(); } + + @Override + public void visit_prompt_integer + ( + final tonkadur.fate.v1.lang.instruction.PromptInteger n + ) + throws Throwable + { + /* + * Fate: (prompt_integer target min max label) + * Wyrd: (prompt_integer target min max label) + */ + final ComputationCompiler target_cc, min_cc, max_cc, label_cc; + + target_cc = new ComputationCompiler(compiler); + min_cc = new ComputationCompiler(compiler); + max_cc = new ComputationCompiler(compiler); + label_cc = new ComputationCompiler(compiler); + + n.get_target().get_visited_by(target_cc); + + if (target_cc.has_init()) + { + result.add(target_cc.get_init()); + } + + n.get_min().get_visited_by(min_cc); + + if (min_cc.has_init()) + { + result.add(min_cc.get_init()); + } + + n.get_max().get_visited_by(max_cc); + + if (max_cc.has_init()) + { + result.add(max_cc.get_init()); + } + + n.get_label().get_visited_by(label_cc); + + if (label_cc.has_init()) + { + result.add(label_cc.get_init()); + } + + result.add + ( + new PromptInteger + ( + target_cc.get_address(), + min_cc.get_computation(), + max_cc.get_computation(), + label_cc.get_computation() + ) + ); + + target_cc.release_registers(); + min_cc.release_registers(); + max_cc.release_registers(); + label_cc.release_registers(); + } + + @Override + public void visit_prompt_string + ( + final tonkadur.fate.v1.lang.instruction.PromptString n + ) + throws Throwable + { + /* + * Fate: (prompt_integer target min max label) + * Wyrd: (prompt_integer target min max label) + */ + final ComputationCompiler target_cc, min_cc, max_cc, label_cc; + + target_cc = new ComputationCompiler(compiler); + min_cc = new ComputationCompiler(compiler); + max_cc = new ComputationCompiler(compiler); + label_cc = new ComputationCompiler(compiler); + + n.get_target().get_visited_by(target_cc); + + if (target_cc.has_init()) + { + result.add(target_cc.get_init()); + } + + n.get_min().get_visited_by(min_cc); + + if (min_cc.has_init()) + { + result.add(min_cc.get_init()); + } + + n.get_max().get_visited_by(max_cc); + + if (max_cc.has_init()) + { + result.add(max_cc.get_init()); + } + + n.get_label().get_visited_by(label_cc); + + if (label_cc.has_init()) + { + result.add(label_cc.get_init()); + } + + result.add + ( + new PromptString + ( + target_cc.get_address(), + min_cc.get_computation(), + max_cc.get_computation(), + label_cc.get_computation() + ) + ); + + target_cc.release_registers(); + min_cc.release_registers(); + max_cc.release_registers(); + label_cc.release_registers(); + } } diff --git a/src/core/src/tonkadur/wyrd/v1/lang/instruction/AddChoice.java b/src/core/src/tonkadur/wyrd/v1/lang/instruction/AddChoice.java index e8da2c5..02f4bc8 100644 --- a/src/core/src/tonkadur/wyrd/v1/lang/instruction/AddChoice.java +++ b/src/core/src/tonkadur/wyrd/v1/lang/instruction/AddChoice.java @@ -10,16 +10,14 @@ public class AddChoice extends Instruction /**** MEMBERS **************************************************************/ /***************************************************************************/ protected final Computation label; - protected final Computation address; /***************************************************************************/ /**** PUBLIC ***************************************************************/ /***************************************************************************/ /**** Constructors *********************************************************/ - public AddChoice (final Computation label, final Computation address) + public AddChoice (final Computation label) { this.label = label; - this.address = address; } /**** Accessors ************************************************************/ @@ -28,11 +26,6 @@ public class AddChoice extends Instruction return label; } - public Computation get_address () - { - return address; - } - @Override public void get_visited_by (final InstructionVisitor iv) throws Throwable @@ -50,8 +43,6 @@ public class AddChoice extends Instruction sb.append("(AddChoice "); sb.append(label.toString()); - sb.append(" "); - sb.append(address.toString()); sb.append(")"); return sb.toString(); diff --git a/src/core/src/tonkadur/wyrd/v1/lang/instruction/PromptInteger.java b/src/core/src/tonkadur/wyrd/v1/lang/instruction/PromptInteger.java new file mode 100644 index 0000000..237a9a5 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/lang/instruction/PromptInteger.java @@ -0,0 +1,83 @@ +package tonkadur.wyrd.v1.lang.instruction; + +import tonkadur.wyrd.v1.lang.computation.Address; + +import tonkadur.wyrd.v1.lang.meta.Computation; +import tonkadur.wyrd.v1.lang.meta.Instruction; +import tonkadur.wyrd.v1.lang.meta.InstructionVisitor; + +public class PromptInteger extends Instruction +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + protected final Address target; + protected final Computation min, max, label; + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + public PromptInteger + ( + final Address target, + final Computation min, + final Computation max, + final Computation label + ) + { + this.target = target; + this.min = min; + this.max = max; + this.label = label; + } + + /**** Accessors ************************************************************/ + public Address get_target() + { + return target; + } + + public Computation get_min() + { + return min; + } + + public Computation get_max() + { + return max; + } + + public Computation get_label() + { + return label; + } + + @Override + public void get_visited_by (final InstructionVisitor iv) + throws Throwable + { + iv.visit_prompt_integer(this); + } + + /**** Misc. ****************************************************************/ + @Override + public String toString () + { + final StringBuilder sb; + + sb = new StringBuilder(); + + sb.append("(PromptInteger "); + sb.append(target.toString()); + sb.append(" "); + sb.append(min.toString()); + sb.append(" "); + sb.append(max.toString()); + sb.append(" "); + sb.append(label.toString()); + sb.append(")"); + + return sb.toString(); + } +} diff --git a/src/core/src/tonkadur/wyrd/v1/lang/instruction/PromptString.java b/src/core/src/tonkadur/wyrd/v1/lang/instruction/PromptString.java new file mode 100644 index 0000000..a24ac61 --- /dev/null +++ b/src/core/src/tonkadur/wyrd/v1/lang/instruction/PromptString.java @@ -0,0 +1,83 @@ +package tonkadur.wyrd.v1.lang.instruction; + +import tonkadur.wyrd.v1.lang.computation.Address; + +import tonkadur.wyrd.v1.lang.meta.Computation; +import tonkadur.wyrd.v1.lang.meta.Instruction; +import tonkadur.wyrd.v1.lang.meta.InstructionVisitor; + +public class PromptString extends Instruction +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + protected final Address target; + protected final Computation min, max, label; + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + public PromptString + ( + final Address target, + final Computation min, + final Computation max, + final Computation label + ) + { + this.target = target; + this.min = min; + this.max = max; + this.label = label; + } + + /**** Accessors ************************************************************/ + public Address get_target() + { + return target; + } + + public Computation get_min() + { + return min; + } + + public Computation get_max() + { + return max; + } + + public Computation get_label() + { + return label; + } + + @Override + public void get_visited_by (final InstructionVisitor iv) + throws Throwable + { + iv.visit_prompt_string(this); + } + + /**** Misc. ****************************************************************/ + @Override + public String toString () + { + final StringBuilder sb; + + sb = new StringBuilder(); + + sb.append("(PromptString "); + sb.append(target.toString()); + sb.append(" "); + sb.append(min.toString()); + sb.append(" "); + sb.append(max.toString()); + sb.append(" "); + sb.append(label.toString()); + sb.append(")"); + + return sb.toString(); + } +} 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 8db6db0..63ca737 100644 --- a/src/core/src/tonkadur/wyrd/v1/lang/meta/InstructionVisitor.java +++ b/src/core/src/tonkadur/wyrd/v1/lang/meta/InstructionVisitor.java @@ -28,6 +28,12 @@ public interface InstructionVisitor public void visit_set_pc (final SetPC n) throws Throwable; + public void visit_prompt_integer (final PromptInteger n) + throws Throwable; + + public void visit_prompt_string (final PromptString n) + throws Throwable; + public void visit_set_value (final SetValue n) throws Throwable; } diff --git a/src/json-export/src/tonkadur/jsonexport/InstructionCompiler.java b/src/json-export/src/tonkadur/jsonexport/InstructionCompiler.java index 8376be4..01b8e76 100644 --- a/src/json-export/src/tonkadur/jsonexport/InstructionCompiler.java +++ b/src/json-export/src/tonkadur/jsonexport/InstructionCompiler.java @@ -15,19 +15,16 @@ public class InstructionCompiler implements InstructionVisitor public void visit_add_choice (final AddChoice n) throws Throwable { - final ComputationCompiler label_cc, address_cc; + final ComputationCompiler label_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) @@ -152,6 +149,54 @@ public class InstructionCompiler implements InstructionVisitor result.put("value", val_cc.get_result()); } + public void visit_prompt_integer (final PromptInteger n) + throws Throwable + { + final ComputationCompiler target_cc, min_cc, max_cc, label_cc; + + target_cc = new ComputationCompiler(); + min_cc = new ComputationCompiler(); + max_cc = new ComputationCompiler(); + label_cc = new ComputationCompiler(); + + n.get_target().get_visited_by(target_cc); + n.get_min().get_visited_by(min_cc); + n.get_max().get_visited_by(max_cc); + n.get_label().get_visited_by(label_cc); + + result = new JSONObject(); + + result.put("category", "prompt_integer"); + result.put("target", target_cc.get_result()); + result.put("min", min_cc.get_result()); + result.put("max", max_cc.get_result()); + result.put("label", label_cc.get_result()); + } + + public void visit_prompt_string (final PromptString n) + throws Throwable + { + final ComputationCompiler target_cc, min_cc, max_cc, label_cc; + + target_cc = new ComputationCompiler(); + min_cc = new ComputationCompiler(); + max_cc = new ComputationCompiler(); + label_cc = new ComputationCompiler(); + + n.get_target().get_visited_by(target_cc); + n.get_min().get_visited_by(min_cc); + n.get_max().get_visited_by(max_cc); + n.get_label().get_visited_by(label_cc); + + result = new JSONObject(); + + result.put("category", "prompt_string"); + result.put("target", target_cc.get_result()); + result.put("min", min_cc.get_result()); + result.put("max", max_cc.get_result()); + result.put("label", label_cc.get_result()); + } + public JSONObject get_result () { return result; |


