summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2021-04-28 21:47:25 +0200
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2021-04-28 21:47:25 +0200
commitd0e544bba4a204875b8357bb2d7e29d9ce29a461 (patch)
treee69469f6f81f26b56d5bcf96bbcbb43cc2a5b3bc
parent6b224c3d53920effdc8cff5846cfc49b4a358a19 (diff)
Adds TextJoin support.
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/computation/TextJoin.java102
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java3
-rw-r--r--src/core/src/tonkadur/fate/v1/parser/FateLexer.g41
-rw-r--r--src/core/src/tonkadur/fate/v1/parser/FateParser.g430
-rw-r--r--src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java107
5 files changed, 243 insertions, 0 deletions
diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/TextJoin.java b/src/core/src/tonkadur/fate/v1/lang/computation/TextJoin.java
new file mode 100644
index 0000000..831bd8f
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/lang/computation/TextJoin.java
@@ -0,0 +1,102 @@
+package tonkadur.fate.v1.lang.computation;
+
+import java.util.List;
+
+import tonkadur.parser.Origin;
+import tonkadur.parser.ParsingError;
+
+import tonkadur.fate.v1.lang.type.Type;
+
+import tonkadur.fate.v1.lang.meta.ComputationVisitor;
+import tonkadur.fate.v1.lang.meta.TextNode;
+import tonkadur.fate.v1.lang.meta.Computation;
+import tonkadur.fate.v1.lang.meta.RecurrentChecks;
+
+public class TextJoin extends TextNode
+{
+ /***************************************************************************/
+ /**** MEMBERS **************************************************************/
+ /***************************************************************************/
+ protected final Computation text_collection;
+ protected final Computation link;
+
+ /***************************************************************************/
+ /**** PROTECTED ************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ protected TextJoin
+ (
+ final Origin origin,
+ final Computation text_collection,
+ final Computation link
+ )
+ {
+ super(origin);
+
+ this.text_collection = text_collection;
+ this.link = link;
+ }
+
+ /***************************************************************************/
+ /**** PUBLIC ***************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ public static TextJoin build
+ (
+ final Origin origin,
+ final Computation text_collection,
+ final Computation link
+ )
+ throws ParsingError
+ {
+ RecurrentChecks.assert_is_a_collection_of
+ (
+ origin,
+ text_collection.get_type(),
+ origin,
+ Type.TEXT
+ );
+
+ RecurrentChecks.assert_can_be_used_as
+ (
+ link,
+ Type.TEXT
+ );
+
+ return new TextJoin (origin, text_collection, link);
+ }
+
+ /**** Accessors ************************************************************/
+ @Override
+ public void get_visited_by (final ComputationVisitor cv)
+ throws Throwable
+ {
+ cv.visit_text_join(this);
+ }
+
+ public Computation get_text_collection ()
+ {
+ return text_collection;
+ }
+
+ public Computation get_link ()
+ {
+ return link;
+ }
+
+ /**** Misc. ****************************************************************/
+ @Override
+ public String toString ()
+ {
+ final StringBuilder sb = new StringBuilder();
+
+ sb.append("(TextJoin ");
+ sb.append(link.toString());
+ sb.append(" ");
+ sb.append(text_collection.toString());
+
+ sb.append(")");
+
+ return sb.toString();
+ }
+}
diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java b/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java
index 7ae4a60..5d04922 100644
--- a/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java
+++ b/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java
@@ -10,6 +10,9 @@ public interface ComputationVisitor
public void visit_at_reference (final AtReference n)
throws Throwable;
+ public void visit_text_join (final TextJoin n)
+ throws Throwable;
+
public void visit_access (final Access 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 63517db..d5f21cd 100644
--- a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4
+++ b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4
@@ -55,6 +55,7 @@ END_KW: L_PAREN 'end)';
EQUALS_KW: L_PAREN ('equals'|'='|'=='|'eq') SEP+;
EXTENSION_FIRST_LEVEL_KW: L_PAREN '@';
EXTRA_INSTRUCTION_KW: L_PAREN '#';
+JOIN_KW: L_PAREN 'join' SEP+;
EXTRA_COMPUTATION_KW: L_PAREN '$';
FALSE_KW: L_PAREN 'false)';
IGNORE_ERROR_KW: L_PAREN 'ignore'US('error'|'warning') SEP+;
diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4
index 011a9b9..cba5111 100644
--- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4
+++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4
@@ -2711,6 +2711,21 @@ returns [TextNode result]:
$result = ValueToText.build(($sentence.result));
}
+ | JOIN_KW value WS+ non_text_value WS* R_PAREN
+ {
+ $result =
+ TextJoin.build
+ (
+ CONTEXT.get_origin_at
+ (
+ ($JOIN_KW.getLine()),
+ ($JOIN_KW.getCharPositionInLine())
+ ),
+ ($non_text_value.result),
+ ($value.result)
+ );
+ }
+
| ENABLE_TEXT_EFFECT_KW WORD WS+ paragraph WS* R_PAREN
{
final TextEffect effect;
@@ -3925,6 +3940,21 @@ returns [Computation result]
$result = ($paragraph.result);
}
+ | JOIN_KW value WS+ non_text_value WS* R_PAREN
+ {
+ $result =
+ TextJoin.build
+ (
+ CONTEXT.get_origin_at
+ (
+ ($JOIN_KW.getLine()),
+ ($JOIN_KW.getCharPositionInLine())
+ ),
+ ($non_text_value.result),
+ ($value.result)
+ );
+ }
+
| ENABLE_TEXT_EFFECT_KW WORD WS+ paragraph WS* R_PAREN
{
final TextEffect effect;
diff --git a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java
index 3508d56..2b0efaf 100644
--- a/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java
+++ b/src/core/src/tonkadur/wyrd/v1/compiler/fate/v1/ComputationCompiler.java
@@ -1308,6 +1308,113 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor
}
@Override
+ public void visit_text_join
+ (
+ final tonkadur.fate.v1.lang.computation.TextJoin n
+ )
+ throws Throwable
+ {
+ final Register i, collection_size, accumulator;
+ final List<Instruction> while_body;
+ final List<Computation> next_chain;
+ final ComputationCompiler link_cc, collection_cc;
+
+ link_cc = new ComputationCompiler(compiler);
+ collection_cc = new ComputationCompiler(compiler);
+
+ n.get_link().get_visited_by(link_cc);
+ n.get_text_collection().get_visited_by(collection_cc);
+
+ assimilate(link_cc);
+ assimilate(collection_cc);
+
+ next_chain = new ArrayList<Computation>();
+ while_body = new ArrayList<Instruction>();
+
+ i = reserve(Type.INT);
+ collection_size = reserve(Type.INT);
+ accumulator = reserve(Type.TEXT);
+
+ init_instructions.add
+ (
+ new SetValue(i.get_address(), Constant.ONE)
+ );
+ init_instructions.add
+ (
+ new SetValue
+ (
+ collection_size.get_address(),
+ new Size(collection_cc.get_address())
+ )
+ );
+
+ init_instructions.add
+ (
+ new SetValue
+ (
+ accumulator.get_address(),
+ new IfElseComputation
+ (
+ Operation.equals(collection_size.get_value(), Constant.ZERO),
+ new Text(new ArrayList<Computation>()),
+ new ValueOf
+ (
+ new RelativeAddress
+ (
+ collection_cc.get_address(),
+ new Cast(Constant.ZERO, Type.STRING),
+ Type.TEXT
+ )
+ )
+ )
+ )
+ );
+
+ next_chain.add(accumulator.get_value());
+ next_chain.add(link_cc.get_computation());
+ next_chain.add
+ (
+ new ValueOf
+ (
+ new RelativeAddress
+ (
+ collection_cc.get_address(),
+ new Cast(i.get_value(), Type.STRING),
+ Type.TEXT
+ )
+ )
+ );
+
+ while_body.add
+ (
+ new SetValue(accumulator.get_address(), new Text(next_chain))
+ );
+
+ while_body.add
+ (
+ new SetValue
+ (
+ i.get_address(),
+ Operation.plus(i.get_value(), Constant.ONE)
+ )
+ );
+
+ init_instructions.add
+ (
+ While.generate
+ (
+ compiler.registers(),
+ compiler.assembler(),
+ Operation.less_than(i.get_value(), collection_size.get_value()),
+ compiler.assembler().merge(while_body)
+ )
+ );
+
+ result_as_computation = accumulator.get_value();
+ }
+
+
+ @Override
public void visit_extra_computation
(
final tonkadur.fate.v1.lang.computation.ExtraComputationInstance n