| summaryrefslogtreecommitdiff |
diff options
| author | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2021-06-24 12:49:43 +0200 |
|---|---|---|
| committer | Nathanael Sensfelder <SpamShield0@MultiAgentSystems.org> | 2021-06-24 12:49:43 +0200 |
| commit | e90f73740402bc3d5679f5d1178e289427ed4da7 (patch) | |
| tree | dcbae6d246c91533b28f487e843edc62a68a9151 /src | |
| parent | fbe5a405d5f8f9be0794a2ff4ac96125869793fe (diff) | |
...
Diffstat (limited to 'src')
25 files changed, 399 insertions, 225 deletions
diff --git a/src/core/src/tonkadur/fate/v1/lang/World.java b/src/core/src/tonkadur/fate/v1/lang/World.java index 34b2364..a5f1bbe 100644 --- a/src/core/src/tonkadur/fate/v1/lang/World.java +++ b/src/core/src/tonkadur/fate/v1/lang/World.java @@ -25,8 +25,12 @@ import tonkadur.fate.v1.lang.meta.ExtraComputation; import tonkadur.fate.v1.lang.meta.RecurrentChecks; import tonkadur.fate.v1.lang.meta.Instruction; -import tonkadur.fate.v1.lang.type.Type; +import tonkadur.fate.v1.lang.type.CollectionType; +import tonkadur.fate.v1.lang.type.ConsType; +import tonkadur.fate.v1.lang.type.DictionaryType; +import tonkadur.fate.v1.lang.type.LambdaType; import tonkadur.fate.v1.lang.type.SequenceType; +import tonkadur.fate.v1.lang.type.Type; import tonkadur.fate.v1.lang.computation.SequenceReference; @@ -312,13 +316,24 @@ public class World try { type_collection.add(Type.BOOL); - //type_collection.add(Type.DICT); type_collection.add(Type.FLOAT); type_collection.add(Type.INT); - //type_collection.add(Type.LIST); - //type_collection.add(Type.SET); type_collection.add(Type.STRING); type_collection.add(Type.TEXT); + + type_collection.add(CollectionType.LIST_ARCHETYPE); + type_collection.add(CollectionType.SET_ARCHETYPE); + + type_collection.add(ConsType.ARCHETYPE); + + type_collection.add(DictionaryType.ARCHETYPE); + + type_collection.add(LambdaType.ARCHETYPE); + type_collection.add(SequenceType.ARCHETYPE); + + //type_collection.add(Type.SET); + // + //type_collection.add(Type.DICT); } catch (final Throwable t) { diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/FieldAccess.java b/src/core/src/tonkadur/fate/v1/lang/computation/FieldAccess.java index 3949aca..5096ff8 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/FieldAccess.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/FieldAccess.java @@ -14,6 +14,7 @@ import tonkadur.fate.v1.lang.meta.ComputationVisitor; import tonkadur.fate.v1.lang.meta.Computation; import tonkadur.fate.v1.lang.type.StructType; +import tonkadur.fate.v1.lang.type.PointerType; import tonkadur.fate.v1.lang.type.Type; import tonkadur.fate.v1.lang.computation.generic.AtReference; @@ -60,7 +61,7 @@ public class FieldAccess extends Computation current_type = parent.get_type(); - while (current_type.get_act_as_type().equals(Type.PTR)) + while (current_type.get_act_as_type().equals(PointerType.ARCHETYPE)) { parent = GenericComputation.build @@ -81,7 +82,7 @@ public class FieldAccess extends Computation ( origin, current_type, - Collections.singleton(Type.DICT), + Collections.singleton(Type.ANY), // FIXME: Type.STRUCT parent.toString() ) ); diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/Newline.java b/src/core/src/tonkadur/fate/v1/lang/computation/Newline.java index b05021d..2f70d3e 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/Newline.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/Newline.java @@ -3,9 +3,11 @@ package tonkadur.fate.v1.lang.computation; import tonkadur.parser.Origin; import tonkadur.fate.v1.lang.meta.ComputationVisitor; -import tonkadur.fate.v1.lang.meta.TextNode; +import tonkadur.fate.v1.lang.meta.Computation; -public class Newline extends TextNode +import tonkadur.fate.v1.lang.type.Type; + +public class Newline extends Computation { /***************************************************************************/ /**** PUBLIC ***************************************************************/ @@ -13,7 +15,7 @@ public class Newline extends TextNode /**** Constructors *********************************************************/ public Newline (final Origin origin) { - super(origin); + super(origin, Type.TEXT); } /**** Accessors ************************************************************/ diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/Paragraph.java b/src/core/src/tonkadur/fate/v1/lang/computation/Paragraph.java index f2959b2..db8e2cb 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/Paragraph.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/Paragraph.java @@ -6,9 +6,9 @@ import tonkadur.parser.Origin; import tonkadur.fate.v1.lang.meta.ComputationVisitor; import tonkadur.fate.v1.lang.meta.Computation; -import tonkadur.fate.v1.lang.meta.TextNode; +import tonkadur.fate.v1.lang.type.Type; -public class Paragraph extends TextNode +public class Paragraph extends Computation { /***************************************************************************/ /**** MEMBERS **************************************************************/ @@ -30,7 +30,7 @@ public class Paragraph extends TextNode final List<Computation> content ) { - super(origin); + super(origin, Type.TEXT); this.content = content; } diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/TextWithEffect.java b/src/core/src/tonkadur/fate/v1/lang/computation/TextWithEffect.java index 17fda37..d5647c0 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/TextWithEffect.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/TextWithEffect.java @@ -8,18 +8,19 @@ import tonkadur.parser.ParsingError; import tonkadur.fate.v1.lang.TextEffect; 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 TextWithEffect extends TextNode +import tonkadur.fate.v1.lang.type.Type; + +public class TextWithEffect extends Computation { /***************************************************************************/ /**** MEMBERS **************************************************************/ /***************************************************************************/ protected final TextEffect effect; protected final List<Computation> parameters; - protected final TextNode text; + protected final Computation text; /***************************************************************************/ /**** PROTECTED ************************************************************/ @@ -30,10 +31,10 @@ public class TextWithEffect extends TextNode final Origin origin, final TextEffect effect, final List<Computation> parameters, - final TextNode text + final Computation text ) { - super(origin); + super(origin, Type.TEXT); this.effect = effect; this.parameters = parameters; @@ -49,7 +50,7 @@ public class TextWithEffect extends TextNode final Origin origin, final TextEffect effect, final List<Computation> parameters, - final TextNode text + final Computation text ) throws ParsingError { @@ -81,7 +82,7 @@ public class TextWithEffect extends TextNode return parameters; } - public TextNode get_text () + public Computation get_text () { return text; } diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/ValueToText.java b/src/core/src/tonkadur/fate/v1/lang/computation/ValueToText.java index e81f8cd..ad4f8a3 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/ValueToText.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/ValueToText.java @@ -8,10 +8,9 @@ import tonkadur.fate.v1.error.IncomparableTypeException; 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; -public class ValueToText extends TextNode +public class ValueToText extends Computation { /***************************************************************************/ /**** MEMBERS **************************************************************/ @@ -24,7 +23,7 @@ public class ValueToText extends TextNode /**** Constructors *********************************************************/ protected ValueToText (final Computation value) { - super(value.get_origin()); + super(value.get_origin(), Type.TEXT); this.value = value; } diff --git a/src/core/src/tonkadur/fate/v1/lang/computation/generic/AtReference.java b/src/core/src/tonkadur/fate/v1/lang/computation/generic/AtReference.java index ec81da7..158cde6 100644 --- a/src/core/src/tonkadur/fate/v1/lang/computation/generic/AtReference.java +++ b/src/core/src/tonkadur/fate/v1/lang/computation/generic/AtReference.java @@ -121,7 +121,7 @@ public class AtReference extends GenericComputation ( origin, current_type, - Collections.singleton(Type.PTR) + Collections.singleton(PointerType.ARCHETYPE) ) ); diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Assert.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Assert.java index 4a6e7bc..3ec088e 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/Assert.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Assert.java @@ -5,7 +5,6 @@ import tonkadur.parser.ParsingError; import tonkadur.fate.v1.lang.meta.InstructionVisitor; import tonkadur.fate.v1.lang.meta.Instruction; -import tonkadur.fate.v1.lang.meta.TextNode; import tonkadur.fate.v1.lang.meta.Computation; import tonkadur.fate.v1.lang.meta.RecurrentChecks; @@ -17,7 +16,7 @@ public class Assert extends Instruction /**** MEMBERS **************************************************************/ /***************************************************************************/ protected final Computation condition; - protected final TextNode message; + protected final Computation message; /***************************************************************************/ /**** PROTECTED ************************************************************/ @@ -27,7 +26,7 @@ public class Assert extends Instruction ( final Origin origin, final Computation condition, - final TextNode message + final Computation message ) { super(origin); @@ -44,7 +43,7 @@ public class Assert extends Instruction ( final Origin origin, final Computation condition, - final TextNode message + final Computation message ) throws ParsingError { @@ -66,7 +65,7 @@ public class Assert extends Instruction return condition; } - public TextNode get_message () + public Computation get_message () { return message; } diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Display.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Display.java index 5e85d79..3d5d7ce 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/Display.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Display.java @@ -10,14 +10,14 @@ 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.TextNode; +import tonkadur.fate.v1.lang.meta.Computation; public class Display extends Instruction { /***************************************************************************/ /**** MEMBERS **************************************************************/ /***************************************************************************/ - protected final TextNode content; + protected final Computation content; /***************************************************************************/ /**** PROTECTED ************************************************************/ @@ -26,7 +26,7 @@ public class Display extends Instruction public Display ( final Origin origin, - final TextNode content + final Computation content ) { super(origin); @@ -47,7 +47,7 @@ public class Display extends Instruction iv.visit_display(this); } - public TextNode get_content () + public Computation get_content () { return content; } diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableCall.java b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableCall.java index 407f430..e834aaf 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableCall.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableCall.java @@ -62,7 +62,7 @@ public class SequenceVariableCall extends Instruction parameters ); - if (!sequence.get_type().get_act_as_type().equals(Type.SEQUENCE)) + if (!sequence.get_type().get_act_as_type().equals(SequenceType.ARCHETYPE)) { ErrorManager.handle ( @@ -70,7 +70,7 @@ public class SequenceVariableCall extends Instruction ( origin, sequence.get_type(), - Collections.singleton(Type.SEQUENCE) + Collections.singleton(SequenceType.ARCHETYPE) ) ); } diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableJump.java b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableJump.java index 52aa947..483ff95 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableJump.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/SequenceVariableJump.java @@ -62,7 +62,7 @@ public class SequenceVariableJump extends Instruction parameters ); - if (!sequence.get_type().get_act_as_type().equals(Type.SEQUENCE)) + if (!sequence.get_type().get_act_as_type().equals(SequenceType.ARCHETYPE)) { ErrorManager.handle ( @@ -70,7 +70,7 @@ public class SequenceVariableJump extends Instruction ( origin, sequence.get_type(), - Collections.singleton(Type.SEQUENCE) + Collections.singleton(SequenceType.ARCHETYPE) ) ); } diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/TextOption.java b/src/core/src/tonkadur/fate/v1/lang/instruction/TextOption.java index 9a1774d..e612a08 100644 --- a/src/core/src/tonkadur/fate/v1/lang/instruction/TextOption.java +++ b/src/core/src/tonkadur/fate/v1/lang/instruction/TextOption.java @@ -8,14 +8,14 @@ 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.TextNode; +import tonkadur.fate.v1.lang.meta.Computation; public class TextOption extends Instruction { /***************************************************************************/ /**** MEMBERS **************************************************************/ /***************************************************************************/ - protected final TextNode text; + protected final Computation text; protected final List<Instruction> effects; /***************************************************************************/ @@ -25,7 +25,7 @@ public class TextOption extends Instruction public TextOption ( final Origin origin, - final TextNode text, + final Computation text, final List<Instruction> effects ) { @@ -44,7 +44,7 @@ public class TextOption extends Instruction iv.visit_text_option(this); } - public TextNode get_text () + public Computation get_text () { return text; } diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/RecurrentChecks.java b/src/core/src/tonkadur/fate/v1/lang/meta/RecurrentChecks.java index 01925f7..b8ab9a6 100644 --- a/src/core/src/tonkadur/fate/v1/lang/meta/RecurrentChecks.java +++ b/src/core/src/tonkadur/fate/v1/lang/meta/RecurrentChecks.java @@ -101,7 +101,7 @@ public class RecurrentChecks ( o, t, - Collections.singletonList(Type.LIST) + Collections.singletonList(CollectionType.LIST_ARCHETYPE) ) ); } @@ -128,7 +128,7 @@ public class RecurrentChecks ( o, t, - Collections.singletonList(Type.SET) + Collections.singletonList(CollectionType.SET_ARCHETYPE) ) ); } @@ -372,7 +372,7 @@ public class RecurrentChecks ( l.get_origin(), l.get_type(), - Collections.singleton(Type.LAMBDA) + Collections.singleton(LambdaType.ARCHETYPE) ) ); } diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/TextNode.java b/src/core/src/tonkadur/fate/v1/lang/meta/TextNode.java deleted file mode 100644 index eec40bb..0000000 --- a/src/core/src/tonkadur/fate/v1/lang/meta/TextNode.java +++ /dev/null @@ -1,25 +0,0 @@ -package tonkadur.fate.v1.lang.meta; - -import tonkadur.parser.Origin; - -import tonkadur.fate.v1.lang.type.Type; - - -public abstract class TextNode extends Computation -{ - /***************************************************************************/ - /**** PROTECTED ************************************************************/ - /***************************************************************************/ - /**** Constructors *********************************************************/ - protected TextNode (final Origin origin) - { - super(origin, Type.TEXT); - } - - /***************************************************************************/ - /**** PUBLIC ***************************************************************/ - /***************************************************************************/ - /**** Constructors *********************************************************/ - /**** Accessors ************************************************************/ - /**** Misc. ****************************************************************/ -} diff --git a/src/core/src/tonkadur/fate/v1/lang/type/CollectionType.java b/src/core/src/tonkadur/fate/v1/lang/type/CollectionType.java index 443bc84..eb8b664 100644 --- a/src/core/src/tonkadur/fate/v1/lang/type/CollectionType.java +++ b/src/core/src/tonkadur/fate/v1/lang/type/CollectionType.java @@ -1,5 +1,7 @@ package tonkadur.fate.v1.lang.type; +import java.util.Collections; + import tonkadur.error.ErrorManager; import tonkadur.parser.ParsingError; @@ -11,10 +13,33 @@ import tonkadur.fate.v1.lang.meta.DeclaredEntity; public class CollectionType extends Type { + public static final CollectionType LIST_ARCHETYPE; + public static final CollectionType SET_ARCHETYPE; + + static + { + + LIST_ARCHETYPE = + new CollectionType + ( + Origin.BASE_LANGUAGE, + Type.ANY, + false, + "list" + ); + + SET_ARCHETYPE = + new CollectionType + ( + Origin.BASE_LANGUAGE, + Type.ANY, + true, + "set" + ); + } /***************************************************************************/ /**** MEMBERS **************************************************************/ /***************************************************************************/ - protected final Type content_type; protected final boolean is_set; /***************************************************************************/ @@ -59,7 +84,7 @@ public class CollectionType extends Type /**** Accessors ************************************************************/ public Type get_content_type () { - return content_type; + return parameters.get(0); } public boolean is_set () @@ -70,7 +95,7 @@ public class CollectionType extends Type @Override public Type get_act_as_type () { - return is_set? Type.SET : Type.LIST; + return is_set()? SET_ARCHETYPE: LIST_ARCHETYPE; } /**** Compatibility ********************************************************/ @@ -85,8 +110,8 @@ public class CollectionType extends Type return ( - (!ct.is_set || is_set) - && content_type.can_be_used_as(ct.content_type) + (!ct.is_set() || is_set()) + && get_content_type().can_be_used_as(ct.get_content_type()) ); } @@ -115,8 +140,14 @@ public class CollectionType extends Type new CollectionType ( get_origin(), - ((Type) content_type.generate_comparable_to(ct.content_type)), - (ct.is_set || is_set), + ( + (Type) get_content_type().generate_comparable_to + ( + ct.get_content_type() + ) + ), + // FIXME: not too sure about that line there: + ct.is_set(), name ); } @@ -126,7 +157,7 @@ public class CollectionType extends Type @Override public Type generate_alias (final Origin origin, final String name) { - return new CollectionType(origin, content_type, is_set, name); + return new CollectionType(origin, get_content_type(), is_set(), name); } @Override @@ -143,9 +174,9 @@ public class CollectionType extends Type sb.append("(List "); } - sb.append(content_type.toString()); + sb.append(get_content_type().toString()); sb.append(")::"); - sb.append(name); + sb.append(get_name()); return sb.toString(); } @@ -163,9 +194,8 @@ public class CollectionType extends Type final String name ) { - super(origin, null, name); + super(origin, null, name, Collections.singletonList(content_type)); this.is_set = is_set; - this.content_type = content_type; } } diff --git a/src/core/src/tonkadur/fate/v1/lang/type/ConsType.java b/src/core/src/tonkadur/fate/v1/lang/type/ConsType.java index e01a91b..7fe5a34 100644 --- a/src/core/src/tonkadur/fate/v1/lang/type/ConsType.java +++ b/src/core/src/tonkadur/fate/v1/lang/type/ConsType.java @@ -1,16 +1,27 @@ package tonkadur.fate.v1.lang.type; +import java.util.Arrays; + import tonkadur.parser.Origin; import tonkadur.fate.v1.lang.meta.DeclaredEntity; public class ConsType extends Type { - /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - protected final Type car; - protected final Type cdr; + public static final ConsType ARCHETYPE; + + static + { + + ARCHETYPE = + new ConsType + ( + Origin.BASE_LANGUAGE, + Type.ANY, + Type.ANY, + "cons" + ); + } /***************************************************************************/ /**** PUBLIC ***************************************************************/ @@ -25,21 +36,18 @@ public class ConsType extends Type final String name ) { - super(origin, null, name); - - this.car = car; - this.cdr = cdr; + super(origin, null, name, Arrays.asList(new Type[]{car, cdr})); } /**** Accessors ************************************************************/ public Type get_car_type () { - return car; + return parameters.get(0); } public Type get_cdr_type () { - return cdr; + return parameters.get(1); } /**** Compatibility ********************************************************/ @@ -53,8 +61,8 @@ public class ConsType extends Type dt = (ConsType) t; return - car.can_be_used_as(dt.car) - && cdr.can_be_used_as(dt.cdr); + get_car_type().can_be_used_as(dt.get_car_type()) + && get_cdr_type().can_be_used_as(dt.get_cdr_type()); } return false; @@ -79,8 +87,11 @@ public class ConsType extends Type dt = (ConsType) de; - resulting_car = (Type) car.generate_comparable_to(dt.car); - resulting_cdr = (Type) cdr.generate_comparable_to(dt.cdr); + resulting_car = + (Type) get_car_type().generate_comparable_to(dt.get_car_type()); + + resulting_cdr = + (Type) get_cdr_type().generate_comparable_to(dt.get_cdr_type()); return new ConsType(get_origin(), resulting_car, resulting_cdr, name); } @@ -88,14 +99,14 @@ public class ConsType extends Type @Override public Type get_act_as_type () { - return Type.CONS; + return ARCHETYPE; } /**** Misc. ****************************************************************/ @Override public Type generate_alias (final Origin origin, final String name) { - return new ConsType(origin, car, cdr, name); + return new ConsType(origin, get_car_type(), get_cdr_type(), name); } @Override @@ -104,9 +115,9 @@ public class ConsType extends Type final StringBuilder sb = new StringBuilder(); sb.append("(Cons "); - sb.append(car.toString()); + sb.append(get_car_type().toString()); sb.append(" "); - sb.append(cdr.toString()); + sb.append(get_cdr_type().toString()); sb.append(")::"); sb.append(name); diff --git a/src/core/src/tonkadur/fate/v1/lang/type/DictionaryType.java b/src/core/src/tonkadur/fate/v1/lang/type/DictionaryType.java index a1f6041..18f0a0e 100644 --- a/src/core/src/tonkadur/fate/v1/lang/type/DictionaryType.java +++ b/src/core/src/tonkadur/fate/v1/lang/type/DictionaryType.java @@ -11,6 +11,20 @@ import tonkadur.fate.v1.lang.meta.DeclaredEntity; public class DictionaryType extends Type { + public static final DictionaryType ARCHETYPE; + + static + { + ARCHETYPE = + new DictionaryType + ( + Origin.BASE_LANGUAGE, + Type.ANY, + Type.ANY, + "dict" + ); + } + /***************************************************************************/ /**** MEMBERS **************************************************************/ /***************************************************************************/ @@ -57,7 +71,7 @@ public class DictionaryType extends Type @Override public Type get_act_as_type () { - return Type.DICT; + return ARCHETYPE; } /**** Compatibility ********************************************************/ diff --git a/src/core/src/tonkadur/fate/v1/lang/type/ExtraType.java b/src/core/src/tonkadur/fate/v1/lang/type/ExtraType.java index 91543fc..aed727f 100644 --- a/src/core/src/tonkadur/fate/v1/lang/type/ExtraType.java +++ b/src/core/src/tonkadur/fate/v1/lang/type/ExtraType.java @@ -1,6 +1,8 @@ package tonkadur.fate.v1.lang.type; import java.util.List; +import java.util.HashMap; +import java.util.Map; import tonkadur.parser.Origin; @@ -10,10 +12,6 @@ import tonkadur.fate.v1.lang.meta.RecurrentChecks; public class ExtraType extends Type { /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - protected final List<Type> parameters; - /***************************************************************************/ /**** PUBLIC ***************************************************************/ /***************************************************************************/ @@ -21,26 +19,12 @@ public class ExtraType extends Type public ExtraType ( final Origin origin, - final String name, - final List<Type> parameters - ) - { - super(origin, null, name); - - this.parameters = parameters; - } - - public ExtraType - ( - final Origin origin, final ExtraType parent, final String name, final List<Type> parameters ) { - super(origin, parent, name); - - this.parameters = parameters; + super(origin, parent, name, parameters); } /**** Accessors ************************************************************/ @@ -97,8 +81,12 @@ public class ExtraType extends Type return get_base_type(); } - /**** Misc. ****************************************************************/ + public List<Type> get_parameters () + { + return parameters; + } + /**** Misc. ****************************************************************/ @Override public Type generate_alias (final Origin origin, final String name) { diff --git a/src/core/src/tonkadur/fate/v1/lang/type/LambdaType.java b/src/core/src/tonkadur/fate/v1/lang/type/LambdaType.java index a434112..1a6a111 100644 --- a/src/core/src/tonkadur/fate/v1/lang/type/LambdaType.java +++ b/src/core/src/tonkadur/fate/v1/lang/type/LambdaType.java @@ -10,10 +10,23 @@ import tonkadur.fate.v1.lang.meta.DeclaredEntity; public class LambdaType extends Type { + public static final LambdaType ARCHETYPE; + + static + { + ARCHETYPE = + new LambdaType + ( + Origin.BASE_LANGUAGE, + Type.ANY, + "lambda", + new ArrayList<Type>() + ); + } + /***************************************************************************/ /**** MEMBERS **************************************************************/ /***************************************************************************/ - protected final List<Type> signature; protected final Type return_type; /***************************************************************************/ @@ -29,9 +42,8 @@ public class LambdaType extends Type final List<Type> signature ) { - super(origin, null, name); + super(origin, null, name, signature); - this.signature = signature; this.return_type = return_type; } @@ -43,7 +55,7 @@ public class LambdaType extends Type public List<Type> get_signature () { - return signature; + return parameters; } /**** Compatibility ********************************************************/ @@ -59,15 +71,15 @@ public class LambdaType extends Type if ( - signature.size() != lt.signature.size() + parameters.size() != lt.parameters.size() || !return_type.can_be_used_as(lt.return_type) ) { return false; } - i0 = signature.iterator(); - i1 = lt.signature.iterator(); + i0 = parameters.iterator(); + i1 = lt.parameters.iterator(); while(i0.hasNext()) { @@ -104,7 +116,7 @@ public class LambdaType extends Type lt = (LambdaType) de; - if (lt.signature.size() != signature.size()) + if (lt.parameters.size() != parameters.size()) { return Type.ANY; } @@ -113,8 +125,8 @@ public class LambdaType extends Type resulting_return_type = (Type) return_type.generate_comparable_to(lt.return_type); - i0 = signature.iterator(); - i1 = lt.signature.iterator(); + i0 = parameters.iterator(); + i1 = lt.parameters.iterator(); while(i0.hasNext()) { @@ -137,14 +149,14 @@ public class LambdaType extends Type @Override public Type get_act_as_type () { - return Type.LAMBDA; + return get_base_type(); } /**** Misc. ****************************************************************/ @Override public Type generate_alias (final Origin origin, final String name) { - return new LambdaType(origin, return_type, name, signature); + return new LambdaType(origin, return_type, name, parameters); } @Override @@ -156,7 +168,7 @@ public class LambdaType extends Type sb.append(return_type.toString()); sb.append(" ("); - for (final Type t: signature) + for (final Type t: parameters) { sb.append(t.get_name()); } diff --git a/src/core/src/tonkadur/fate/v1/lang/type/PointerType.java b/src/core/src/tonkadur/fate/v1/lang/type/PointerType.java index 0269cf7..db1a18a 100644 --- a/src/core/src/tonkadur/fate/v1/lang/type/PointerType.java +++ b/src/core/src/tonkadur/fate/v1/lang/type/PointerType.java @@ -1,15 +1,29 @@ package tonkadur.fate.v1.lang.type; +import java.util.Collections; + import tonkadur.parser.Origin; import tonkadur.fate.v1.lang.meta.DeclaredEntity; public class PointerType extends Type { + public static final PointerType ARCHETYPE; + + static + { + ARCHETYPE = + new PointerType + ( + Origin.BASE_LANGUAGE, + Type.ANY, + "ptr" + ); + } + /***************************************************************************/ /**** MEMBERS **************************************************************/ /***************************************************************************/ - protected final Type referenced_type; /***************************************************************************/ /**** PUBLIC ***************************************************************/ @@ -23,15 +37,13 @@ public class PointerType extends Type final String name ) { - super(origin, null, name); - - this.referenced_type = referenced_type; + super(origin, null, name, Collections.singletonList(referenced_type)); } /**** Accessors ************************************************************/ public Type get_referenced_type () { - return referenced_type; + return parameters.get(0); } /**** Compatibility ********************************************************/ @@ -44,7 +56,7 @@ public class PointerType extends Type dt = (PointerType) t; - return referenced_type.can_be_used_as(dt.referenced_type); + return get_referenced_type().can_be_used_as(dt.get_referenced_type()); } return false; @@ -69,7 +81,10 @@ public class PointerType extends Type dt = (PointerType) de; resulting_referenced_type = - (Type) referenced_type.generate_comparable_to(dt.referenced_type); + (Type) get_referenced_type().generate_comparable_to + ( + dt.get_referenced_type() + ); return new PointerType(get_origin(), resulting_referenced_type, name); } @@ -77,14 +92,14 @@ public class PointerType extends Type @Override public Type get_act_as_type () { - return Type.PTR; + return ARCHETYPE; } /**** Misc. ****************************************************************/ @Override public Type generate_alias (final Origin origin, final String name) { - return new PointerType(origin, referenced_type, name); + return new PointerType(origin, get_referenced_type(), name); } @Override @@ -93,7 +108,7 @@ public class PointerType extends Type final StringBuilder sb = new StringBuilder(); sb.append("(Pointer to "); - sb.append(referenced_type.toString()); + sb.append(get_referenced_type().toString()); sb.append(")::"); sb.append(name); diff --git a/src/core/src/tonkadur/fate/v1/lang/type/SequenceType.java b/src/core/src/tonkadur/fate/v1/lang/type/SequenceType.java index c6e7ef3..0970a98 100644 --- a/src/core/src/tonkadur/fate/v1/lang/type/SequenceType.java +++ b/src/core/src/tonkadur/fate/v1/lang/type/SequenceType.java @@ -11,10 +11,22 @@ import tonkadur.fate.v1.lang.meta.Computation; public class SequenceType extends Type { + public static final SequenceType ARCHETYPE; + + static + { + ARCHETYPE = + new SequenceType + ( + Origin.BASE_LANGUAGE, + "sequence", + new ArrayList<Type>() + ); + } + /***************************************************************************/ /**** MEMBERS **************************************************************/ /***************************************************************************/ - protected final List<Type> signature; protected boolean signature_is_defined; /***************************************************************************/ @@ -26,12 +38,11 @@ public class SequenceType extends Type ( final Origin origin, final String name, - final List<Type> signature + final List<Type> parameters ) { - super(origin, null, name); + super(origin, null, name, parameters); - this.signature = signature; this.signature_is_defined = true; } @@ -41,16 +52,15 @@ public class SequenceType extends Type final String name ) { - super(origin, null, name); + super(origin, null, name, new ArrayList<Type>()); - this.signature = new ArrayList<Type>(); this.signature_is_defined = false; } /**** Accessors ************************************************************/ public List<Type> get_signature () { - return signature; + return parameters; } public void propose_signature (final List<Type> signature) @@ -60,7 +70,7 @@ public class SequenceType extends Type return; } - this.signature.addAll(signature); + this.parameters.addAll(signature); signature_is_defined = true; } @@ -77,7 +87,7 @@ public class SequenceType extends Type for (final Computation c: params) { - signature.add(c.get_type()); + parameters.add(c.get_type()); } signature_is_defined = true; @@ -96,13 +106,13 @@ public class SequenceType extends Type propose_signature(lt.get_signature()); - if (signature.size() != lt.signature.size()) + if (parameters.size() != lt.parameters.size()) { return false; } - i0 = signature.iterator(); - i1 = lt.signature.iterator(); + i0 = parameters.iterator(); + i1 = lt.parameters.iterator(); while(i0.hasNext()) { @@ -138,15 +148,15 @@ public class SequenceType extends Type lt = (SequenceType) de; - if (lt.signature.size() != signature.size()) + if (lt.parameters.size() != parameters.size()) { return Type.ANY; } resulting_signature = new ArrayList<Type>(); - i0 = signature.iterator(); - i1 = lt.signature.iterator(); + i0 = parameters.iterator(); + i1 = lt.parameters.iterator(); while(i0.hasNext()) { @@ -162,14 +172,14 @@ public class SequenceType extends Type @Override public Type get_act_as_type () { - return Type.SEQUENCE; + return ARCHETYPE; } /**** Misc. ****************************************************************/ @Override public Type generate_alias (final Origin origin, final String name) { - return new SequenceType(origin, name, signature); + return new SequenceType(origin, name, parameters); } @Override @@ -179,7 +189,7 @@ public class SequenceType extends Type sb.append("(Sequence ("); - for (final Type t: signature) + for (final Type t: parameters) { sb.append(t.get_name()); sb.append(" "); diff --git a/src/core/src/tonkadur/fate/v1/lang/type/StructType.java b/src/core/src/tonkadur/fate/v1/lang/type/StructType.java index c44e7f4..1d9ab89 100644 --- a/src/core/src/tonkadur/fate/v1/lang/type/StructType.java +++ b/src/core/src/tonkadur/fate/v1/lang/type/StructType.java @@ -154,7 +154,7 @@ public class StructType extends Type @Override public Type get_act_as_type () { - return Type.DICT; + return get_base_type(); } /**** Misc. ****************************************************************/ diff --git a/src/core/src/tonkadur/fate/v1/lang/type/Type.java b/src/core/src/tonkadur/fate/v1/lang/type/Type.java index 4385ed0..be9901b 100644 --- a/src/core/src/tonkadur/fate/v1/lang/type/Type.java +++ b/src/core/src/tonkadur/fate/v1/lang/type/Type.java @@ -21,17 +21,10 @@ public class Type extends DeclaredEntity { public static final Type ANY; public static final Type BOOL; - public static final Type CONS; - public static final Type DICT; public static final Type FLOAT; public static final Type INT; - public static final Type LAMBDA; - public static final Type LIST; - public static final Type PTR; - public static final Type TEXT; - public static final Type SEQUENCE; - public static final Type SET; public static final Type STRING; + public static final Type TEXT; public static final Set<Type> NUMBER_TYPES; public static final Set<Type> ALL_TYPES; @@ -50,31 +43,24 @@ public class Type extends DeclaredEntity ANY = new Type(base, null, "undetermined type"); BOOL = new Type(base, null, "bool"); - CONS = new Type(base, null, "cons"); - DICT = new Type(base, null, "dict"); FLOAT = new Type(base, null, "float"); INT = new Type(base, null, "int"); - LAMBDA = new Type(base, null, "lambda"); - LIST = new Type(base, null, "list"); - PTR = new Type(base, null, "ptr"); TEXT = new Type(base, null, "text"); - SEQUENCE = new Type(base, null, "sequence"); - SET = new Type(base, null, "set"); STRING = new Type(base, null, "string"); ALL_TYPES = new HashSet<Type>(); ALL_TYPES.add(ANY); ALL_TYPES.add(BOOL); - ALL_TYPES.add(CONS); - ALL_TYPES.add(DICT); + ALL_TYPES.add(ConsType.ARCHETYPE); + ALL_TYPES.add(DictionaryType.ARCHETYPE); ALL_TYPES.add(FLOAT); ALL_TYPES.add(INT); - ALL_TYPES.add(LAMBDA); - ALL_TYPES.add(LIST); - ALL_TYPES.add(PTR); + ALL_TYPES.add(LambdaType.ARCHETYPE); + ALL_TYPES.add(CollectionType.SET_ARCHETYPE); + ALL_TYPES.add(CollectionType.LIST_ARCHETYPE); + ALL_TYPES.add(PointerType.ARCHETYPE); ALL_TYPES.add(TEXT); - ALL_TYPES.add(SEQUENCE); - ALL_TYPES.add(SET); + ALL_TYPES.add(SequenceType.ARCHETYPE); ALL_TYPES.add(STRING); @@ -86,16 +72,16 @@ public class Type extends DeclaredEntity COMPARABLE_TYPES.add(FLOAT); COMPARABLE_TYPES.add(INT); - COMPARABLE_TYPES.add(SEQUENCE); - COMPARABLE_TYPES.add(LAMBDA); + COMPARABLE_TYPES.add(SequenceType.ARCHETYPE); + COMPARABLE_TYPES.add(LambdaType.ARCHETYPE); COMPARABLE_TYPES.add(STRING); COMPARABLE_TYPES.add(BOOL); - COMPARABLE_TYPES.add(PTR); + COMPARABLE_TYPES.add(PointerType.ARCHETYPE); COLLECTION_TYPES = new HashSet<Type>(); - COLLECTION_TYPES.add(SET); - COLLECTION_TYPES.add(LIST); + COLLECTION_TYPES.add(CollectionType.SET_ARCHETYPE); + COLLECTION_TYPES.add(CollectionType.LIST_ARCHETYPE); } public static Type value_on_missing () @@ -109,37 +95,22 @@ public class Type extends DeclaredEntity return "Type"; } + public static List<Type> generate_default_parameters (final int i) + { + return Collections.nCopies(i, Type.ANY); + } /***************************************************************************/ /**** MEMBERS **************************************************************/ /***************************************************************************/ protected final Type true_type; protected final Type parent; + protected final List<Type> parameters; /***************************************************************************/ /**** PUBLIC ***************************************************************/ /***************************************************************************/ - /**** Constructors *********************************************************/ - public static Type build - ( - final Origin origin, - final Type parent, - final String name - ) - throws InvalidTypeException - { - if (!COMPARABLE_TYPES.contains(parent.get_act_as_type())) - { - ErrorManager.handle - ( - new InvalidTypeException(origin, parent, COMPARABLE_TYPES) - ); - } - - return new Type(origin, parent, name); - } - /**** Accessors ************************************************************/ public Type get_base_type () { @@ -148,7 +119,7 @@ public class Type extends DeclaredEntity public Type get_act_as_type () { - return true_type; + return get_base_type(); } public boolean is_base_type () @@ -156,6 +127,11 @@ public class Type extends DeclaredEntity return (parent == null); } + public List<Type> get_parameters () + { + return parameters; + } + public Type try_merging_with (final Type t) { if (t.get_base_type() != get_base_type()) @@ -263,9 +239,31 @@ public class Type extends DeclaredEntity sb.append(name); + for (final Type t: parameters) + { + sb.append("<"); + sb.append(t.toString()); + sb.append(">"); + } + return sb.toString(); } + public Type generate_variant + ( + final Origin origin, + final List<Type> parameters + ) + throws Throwable + { + if (this.parameters.size() != parameters.size()) + { + // TODO: error; + } + + return new Type(origin, get_base_type(), name, parameters); + } + /***************************************************************************/ /**** PROTECTED ************************************************************/ /***************************************************************************/ @@ -273,6 +271,29 @@ public class Type extends DeclaredEntity ( final Origin origin, final Type parent, + final String name, + final List<Type> parameters + ) + { + super(origin, name); + + if (parent == null) + { + true_type = this; + } + else + { + true_type = parent.true_type; + } + + this.parent = parent; + this.parameters = parameters; + } + + protected Type + ( + final Origin origin, + final Type parent, final String name ) { @@ -288,6 +309,7 @@ public class Type extends DeclaredEntity } this.parent = parent; + this.parameters = generate_default_parameters(0); } protected boolean this_or_parent_equals (final Type t) diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 index a8a5d06..9400c8b 100644 --- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 +++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 @@ -206,9 +206,12 @@ first_level_instruction PARSER.get_world().extra_computations().add(extra_computation); } - | DECLARE_EXTRA_TYPE_KW identifier maybe_type_list WS* R_PAREN + | DECLARE_EXTRA_TYPE_KW identifier WS+ argc=WORD WS+ comp=WORD WS* R_PAREN { + final Type new_type; final Origin start_origin; + int arg_count; + boolean is_comparable; start_origin = PARSER.get_origin_at @@ -217,15 +220,48 @@ first_level_instruction ($DECLARE_EXTRA_TYPE_KW.getCharPositionInLine()) ); - PARSER.get_world().types().add - ( + try + { + arg_count = Integer.parseInt($argc.text); + + if (arg_count < 0) + { + // TODO: show error. + } + } + catch (final Exception e) + { + arg_count = 0; + // TODO: show error. + throw e; + } + + try + { + is_comparable = Boolean.parseBoolean($comp.text); + } + catch (final Exception e) + { + is_comparable = false; + // TODO: show error. + throw e; + } + + new_type = new ExtraType ( start_origin, + null, ($identifier.result), - ($maybe_type_list.result) - ) - ); + Type.generate_default_parameters(arg_count) + ); + + PARSER.get_world().types().add(new_type); + + if (is_comparable) + { + Type.COMPARABLE_TYPES.add(new_type); + } } | DECLARE_EVENT_TYPE_KW identifier maybe_type_list WS* R_PAREN @@ -385,7 +421,7 @@ first_level_instruction | REQUIRE_EXTENSION_KW WORD WS* R_PAREN { - WORLD.add_required_extension(($WORD.text)); + PARSER.get_world().add_required_extension(($WORD.text)); /* TODO: error report if extension not explicitly enabled. */ } @@ -440,7 +476,11 @@ first_level_instruction ($WORD.getCharPositionInLine()) ); - instr = WORLD.extension_first_level_instructions().get(($WORD.text)); + instr = + PARSER.get_world().extension_first_level_instructions().get + ( + ($WORD.text) + ); if (instr == null) { @@ -1544,24 +1584,63 @@ returns [Type result] ), ($WORD.text) ); + + if ($result.get_parameters().size() != 0) + { + // TODO: throw error. + } } - | L_PAREN identifier WS+ type_list WS* R_PAREN + | LAMBDA_KW type WS* L_PAREN maybe_type_list R_PAREN WS* R_PAREN { final Type t; t = - PARSER.get_world().types().get + new LambdaType ( PARSER.get_origin_at ( - ($L_PAREN.getLine()), - ($L_PAREN.getCharPositionInLine()) + ($LAMBDA_KW.getLine()), + ($LAMBDA_KW.getCharPositionInLine()) ), - ($identifier.result) + ($type.result), + "autogenerated lambda type", + ($maybe_type_list.result) + ); + } + + | SEQUENCE_KW maybe_type_list R_PAREN + { + final Type t; + + t = + new SequenceType + ( + PARSER.get_origin_at + ( + ($SEQUENCE_KW.getLine()), + ($SEQUENCE_KW.getCharPositionInLine()) + ), + "autogenerated sequence type", + ($maybe_type_list.result) + ); + } + + | L_PAREN identifier WS+ type_list WS* R_PAREN + { + final Origin origin; + final Type t; + + origin = + PARSER.get_origin_at + ( + ($L_PAREN.getLine()), + ($L_PAREN.getCharPositionInLine()) ); - $result = t.build($type_list.result); + t = PARSER.get_world().types().get(origin, ($identifier.result)); + + $result = t.generate_variant(origin, $type_list.result); } ; catch [final Throwable e] diff --git a/src/core/src/tonkadur/fate/v1/parser/ParserData.java b/src/core/src/tonkadur/fate/v1/parser/ParserData.java index 45ef985..ce73193 100644 --- a/src/core/src/tonkadur/fate/v1/parser/ParserData.java +++ b/src/core/src/tonkadur/fate/v1/parser/ParserData.java @@ -18,6 +18,7 @@ import org.antlr.v4.runtime.misc.ParseCancellationException; import tonkadur.parser.Context; import tonkadur.parser.Origin; +import tonkadur.parser.ParsingError; import tonkadur.error.ErrorManager; @@ -202,7 +203,7 @@ public class ParserData } public void add_file_content (final Origin origin, final String filename) - throws IOException + throws IOException, ParsingError { final CommonTokenStream tokens; final FateLexer lexer; |


