| summaryrefslogtreecommitdiff |
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2020-11-07 20:05:25 +0100 |
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2020-11-07 20:05:25 +0100 |
| commit | 0b92419a57fc6a27b2701a688cf7ac3b1707241f (patch) | |
| tree | b401d9f84358e5e25232a8902a081d0f8e282e9e /src/core | |
| parent | 659c4440918e2e1c68cf388e9d9c7eb3e83eb4d7 (diff) | |
More unit tests, fixes equals operator.
Diffstat (limited to 'src/core')
5 files changed, 226 insertions, 7 deletions
diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/FieldAccess.java b/src/core/src/tonkadur/fate/v1/lang/computation/FieldAccess.java new file mode 100644 index 0000000..bc0d6b5 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/computation/FieldAccess.java @@ -0,0 +1,152 @@ +package tonkadur.fate.v1.lang.computation; + +import java.util.Collections; +import java.util.List; + +import tonkadur.parser.Origin; + +import tonkadur.error.ErrorManager; + +import tonkadur.fate.v1.error.InvalidTypeException; +import tonkadur.fate.v1.error.UnknownDictionaryFieldException; + +import tonkadur.fate.v1.lang.meta.ComputationVisitor; +import tonkadur.fate.v1.lang.meta.Reference; +import tonkadur.fate.v1.lang.meta.Computation; + +import tonkadur.fate.v1.lang.type.DictType; +import tonkadur.fate.v1.lang.type.Type; + +public class FieldAccess extends Computation +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + protected final Computation parent; + protected final String field_name; + + /***************************************************************************/ + /**** PROTECTED ************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + protected FieldAccess + ( + final Origin origin, + final Computation parent, + final Type type, + final String field_name + ) + { + super(origin, type); + + this.parent = parent; + this.field_name = field_name; + } + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + /**** Constructors *********************************************************/ + public static FieldAccess build + ( + final Origin origin, + Computation parent, + final String field + ) + throws + InvalidTypeException, + UnknownDictionaryFieldException + { + Type current_type; + + current_type = parent.get_type(); + + while (current_type.get_act_as_type().equals(Type.REF)) + { + parent = AtReference.build(origin, (Reference) parent); + current_type = parent.get_type(); + } + + if (!(current_type instanceof DictType)) + { + ErrorManager.handle + ( + new InvalidTypeException + ( + origin, + current_type, + Collections.singleton(Type.DICT), + parent.toString() + ) + ); + + current_type = Type.ANY; + } + else + { + current_type = ((DictType) current_type).get_field_type(origin, field); + } + + return new FieldAccess(origin, parent, current_type, field); + } + + public static FieldAccess build + ( + final Origin origin, + Computation parent, + final List<String> field_sequence + ) + throws + InvalidTypeException, + UnknownDictionaryFieldException + { + for (final String field: field_sequence) + { + parent = build(origin, parent, field); + } + + if (parent instanceof FieldAccess) + { + return (FieldAccess) parent; + } + else + { + return null; + } + } + + /**** Accessors ************************************************************/ + @Override + public void get_visited_by (final ComputationVisitor cv) + throws Throwable + { + cv.visit_field_access(this); + } + + public String get_field_name () + { + return field_name; + } + + public Computation get_parent () + { + return parent; + } + + /**** Misc. ****************************************************************/ + @Override + public String toString () + { + final StringBuilder sb = new StringBuilder(); + + sb.append("(FieldAccess ("); + sb.append(type.get_name()); + sb.append(") "); + sb.append(parent.toString()); + sb.append(" "); + sb.append(field_name); + 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 23fb1c8..4631a2f 100644 --- a/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java +++ b/src/core/src/tonkadur/fate/v1/lang/meta/ComputationVisitor.java @@ -13,6 +13,9 @@ public interface ComputationVisitor public void visit_access_pointer (final AccessPointer n) throws Throwable; + public void visit_field_access (final FieldAccess n) + throws Throwable; + public void visit_access_as_reference (final AccessAsReference 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 077371f..c2ed078 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 @@ -59,6 +59,7 @@ FALSE_KW: L_PAREN 'false)'; IGNORE_ERROR_KW: L_PAREN 'ignore'US('error'|'warning') SEP+; FATE_VERSION_KW: L_PAREN 'fate'US'version' SEP+; FIELD_KW: L_PAREN 'field' SEP+; +FIELD_ACCESS_KW: L_PAREN (('get'US'field')|('field'US'access')) SEP+; FILTER_KW: L_PAREN 'filter' SEP+; INDEXED_FILTER_KW: L_PAREN 'indexed'US'filter' SEP+; IMP_FILTER_KW: L_PAREN 'filter!' SEP+; diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 index 7dd5081..88601f5 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 @@ -2501,7 +2501,7 @@ returns [RichTextNode result]: | ENABLE_TEXT_EFFECT_KW L_PAREN WORD WS+ - value_list + value_list WS* R_PAREN WS+ paragraph WS* R_PAREN @@ -3446,7 +3446,7 @@ returns [Computation result]: ); } - | COUNT_KW value WS+ value_reference WS* R_PAREN + | COUNT_KW value WS+ non_text_value WS* R_PAREN { $result = CountOperator.build @@ -3457,7 +3457,7 @@ returns [Computation result]: ($COUNT_KW.getCharPositionInLine()) ), ($value.result), - ($value_reference.result) + ($non_text_value.result) ); } ; @@ -3538,7 +3538,7 @@ returns [Computation result] | ENABLE_TEXT_EFFECT_KW L_PAREN WORD WS+ - value_list + value_list WS* R_PAREN WS+ paragraph WS* R_PAREN @@ -3667,7 +3667,7 @@ returns [Computation result] ); } - | ACCESS_KW collection=non_text_value WS+ index=non_text_value R_PAREN + | ACCESS_KW collection=non_text_value WS+ index=non_text_value WS* R_PAREN { $result = Access.build @@ -3682,7 +3682,7 @@ returns [Computation result] ); } - | ACCESS_POINTER_KW value_reference WS+ non_text_value R_PAREN + | ACCESS_POINTER_KW value_reference WS+ non_text_value WS* R_PAREN { $result = AccessPointer.build @@ -3697,6 +3697,21 @@ returns [Computation result] ); } + | FIELD_ACCESS_KW non_text_value WS+ WORD WS* R_PAREN + { + $result = + FieldAccess.build + ( + CONTEXT.get_origin_at + ( + ($FIELD_ACCESS_KW.getLine()), + ($FIELD_ACCESS_KW.getCharPositionInLine()) + ), + ($non_text_value.result), + ($WORD.text) + ); + } + | FOLDL_KW fun=non_text_value WS+ init=value WS+ 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 30fe7b8..1473c6c 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 @@ -503,6 +503,37 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor } @Override + public void visit_field_access + ( + final tonkadur.fate.v1.lang.computation.FieldAccess n + ) + throws Throwable + { + final ComputationCompiler n_cc; + + n_cc = new ComputationCompiler(compiler); + + n.get_parent().get_visited_by(n_cc); + + assimilate(n_cc); + + result_as_address = + new RelativeAddress + ( + n_cc.get_address(), + new Constant(Type.STRING, n.get_field_name()), + TypeCompiler.compile + ( + compiler, + ( + (tonkadur.fate.v1.lang.type.DictType) + n.get_parent().get_type() + ).get_field_type(null, n.get_field_name()) + ) + ); + } + + @Override public void visit_if_else_value ( final tonkadur.fate.v1.lang.computation.IfElseValue n @@ -1188,8 +1219,25 @@ implements tonkadur.fate.v1.lang.meta.ComputationVisitor ) ) { + final Iterator<Computation> operands_it; + final Computation first_elem; + + operands_it = operands.iterator(); + + first_elem = operands_it.next(); + result_as_computation = - Operation.equals(operands.get(0), operands.get(1)); + Operation.equals(first_elem, operands_it.next()); + + while (operands_it.hasNext()) + { + result_as_computation = + Operation.and + ( + result_as_computation, + Operation.equals(first_elem, operands_it.next()) + ); + } } else if ( |


