From 8483de1b5b9b686d9d148700a7c06c3c935266f8 Mon Sep 17 00:00:00 2001 From: Nathanael Sensfelder Date: Sun, 5 Jul 2020 13:47:48 +0200 Subject: Set -> (Collection w/ is_set = true). --- .../src/tonkadur/fate/v1/lang/CollectionType.java | 151 +++++++++++++++++++++ src/core/src/tonkadur/fate/v1/lang/SetType.java | 125 ----------------- src/core/src/tonkadur/fate/v1/lang/Type.java | 12 +- 3 files changed, 157 insertions(+), 131 deletions(-) create mode 100644 src/core/src/tonkadur/fate/v1/lang/CollectionType.java delete mode 100644 src/core/src/tonkadur/fate/v1/lang/SetType.java diff --git a/src/core/src/tonkadur/fate/v1/lang/CollectionType.java b/src/core/src/tonkadur/fate/v1/lang/CollectionType.java new file mode 100644 index 0000000..e581f13 --- /dev/null +++ b/src/core/src/tonkadur/fate/v1/lang/CollectionType.java @@ -0,0 +1,151 @@ +package tonkadur.fate.v1.lang; + +import tonkadur.error.ErrorManager; + +import tonkadur.parser.Origin; + +import tonkadur.fate.v1.error.InvalidTypeException; + +import tonkadur.fate.v1.lang.meta.DeclaredEntity; + +public class CollectionType extends Type +{ + /***************************************************************************/ + /**** MEMBERS **************************************************************/ + /***************************************************************************/ + protected final Type content_type; + protected final boolean is_set; + + /***************************************************************************/ + /**** PUBLIC ***************************************************************/ + /***************************************************************************/ + + /**** Constructors *********************************************************/ + public static CollectionType build + ( + final Origin origin, + final Type content_type, + final boolean is_set, + final String name + ) + throws InvalidTypeException + { + if + ( + !Type.COLLECTION_COMPATIBLE_TYPES.contains + ( + content_type.get_true_type() + ) + ) + { + ErrorManager.handle + ( + new InvalidTypeException + ( + origin, + content_type, + Type.COLLECTION_COMPATIBLE_TYPES + ) + ); + } + + return new CollectionType(origin, content_type, is_set, name); + } + + + /**** Accessors ************************************************************/ + public Type get_content_type () + { + return true_type; + } + + /**** Compatibility ********************************************************/ + @Override + public boolean can_be_used_as (final Type t) + { + if (t instanceof CollectionType) + { + final CollectionType ct; + + ct = (CollectionType) t; + + return + ( + (!ct.is_set || is_set) + && content_type.can_be_used_as(ct.content_type) + ); + } + + return false; + } + + /* + * This is for the very special case where a type is used despite not being + * even a sub-type of the expected one. Using this rather expensive function, + * the most restrictive shared type will be returned. If no such type exists, + * the ANY time is returned. + */ + @Override + public DeclaredEntity generate_comparable_to (final DeclaredEntity de) + { + final CollectionType ct; + + if (!(de instanceof CollectionType)) + { + return Type.ANY; + } + + ct = (CollectionType) de; + + return + new CollectionType + ( + get_origin(), + ((Type) content_type.generate_comparable_to (ct.content_type)), + (ct.is_set || is_set), + name + ); + } + + + /**** Misc. ****************************************************************/ + @Override + public String toString () + { + final StringBuilder sb = new StringBuilder(); + + if (is_set) + { + sb.append("(Set "); + } + else + { + sb.append("(List "); + } + + sb.append(content_type.toString()); + sb.append(")::"); + sb.append(name); + + return sb.toString(); + } + + /***************************************************************************/ + /**** PROTECTED ************************************************************/ + /***************************************************************************/ + + /**** Constructors *********************************************************/ + protected CollectionType + ( + final Origin origin, + final Type content_type, + final boolean is_set, + final String name + ) + { + super(origin, (is_set ? Type.SET : Type.LIST), name); + + this.is_set = is_set; + this.content_type = content_type; + } +} diff --git a/src/core/src/tonkadur/fate/v1/lang/SetType.java b/src/core/src/tonkadur/fate/v1/lang/SetType.java deleted file mode 100644 index 2b30139..0000000 --- a/src/core/src/tonkadur/fate/v1/lang/SetType.java +++ /dev/null @@ -1,125 +0,0 @@ -package tonkadur.fate.v1.lang; - -import tonkadur.error.ErrorManager; - -import tonkadur.parser.Origin; - -import tonkadur.fate.v1.error.InvalidTypeException; - -import tonkadur.fate.v1.lang.meta.DeclaredEntity; - -public class SetType extends Type -{ - /***************************************************************************/ - /**** MEMBERS **************************************************************/ - /***************************************************************************/ - protected final Type content_type; - - /***************************************************************************/ - /**** PUBLIC ***************************************************************/ - /***************************************************************************/ - - /**** Constructors *********************************************************/ - public static SetType build - ( - final Origin origin, - final Type content_type, - final String name - ) - throws InvalidTypeException - { - if (!Type.SET_COMPATIBLE_TYPES.contains(content_type.get_true_type())) - { - ErrorManager.handle - ( - new InvalidTypeException - ( - origin, - content_type, - Type.SET_COMPATIBLE_TYPES - ) - ); - } - - return new SetType(origin, content_type, name); - } - - - /**** Accessors ************************************************************/ - public Type get_content_type () - { - return true_type; - } - - /**** Compatibility ********************************************************/ - @Override - public boolean can_be_used_as (final Type t) - { - if (t instanceof SetType) - { - return content_type.can_be_used_as(((SetType) t).content_type); - } - - return false; - } - - /* - * This is for the very special case where a type is used despite not being - * even a sub-type of the expected one. Using this rather expensive function, - * the most restrictive shared type will be returned. If no such type exists, - * the ANY time is returned. - */ - @Override - public DeclaredEntity generate_comparable_to (final DeclaredEntity de) - { - if (!(de instanceof SetType)) - { - return Type.ANY; - } - - return - new SetType - ( - get_origin(), - ( - (Type) content_type.generate_comparable_to - ( - ((SetType) de).content_type - ) - ), - name - ); - } - - - /**** Misc. ****************************************************************/ - @Override - public String toString () - { - final StringBuilder sb = new StringBuilder(); - - sb.append("(Set "); - sb.append(content_type.toString()); - sb.append(")::"); - sb.append(name); - - return sb.toString(); - } - - /***************************************************************************/ - /**** PROTECTED ************************************************************/ - /***************************************************************************/ - - /**** Constructors *********************************************************/ - protected SetType - ( - final Origin origin, - final Type content_type, - final String name - ) - { - super(origin, Type.SET, name); - - this.content_type = content_type; - } -} diff --git a/src/core/src/tonkadur/fate/v1/lang/Type.java b/src/core/src/tonkadur/fate/v1/lang/Type.java index b5438e5..6c245e3 100644 --- a/src/core/src/tonkadur/fate/v1/lang/Type.java +++ b/src/core/src/tonkadur/fate/v1/lang/Type.java @@ -25,7 +25,7 @@ public class Type extends DeclaredEntity public static final Type STRING; public static final Set NUMBER_TYPES; - public static final Set SET_COMPATIBLE_TYPES; + public static final Set COLLECTION_COMPATIBLE_TYPES; static { @@ -50,12 +50,12 @@ public class Type extends DeclaredEntity NUMBER_TYPES.add(FLOAT); NUMBER_TYPES.add(INT); - SET_COMPATIBLE_TYPES = new HashSet(); + COLLECTION_COMPATIBLE_TYPES = new HashSet(); - SET_COMPATIBLE_TYPES.add(FLOAT); - SET_COMPATIBLE_TYPES.add(INT); - SET_COMPATIBLE_TYPES.add(STRING); - SET_COMPATIBLE_TYPES.add(BOOLEAN); + COLLECTION_COMPATIBLE_TYPES.add(FLOAT); + COLLECTION_COMPATIBLE_TYPES.add(INT); + COLLECTION_COMPATIBLE_TYPES.add(STRING); + COLLECTION_COMPATIBLE_TYPES.add(BOOLEAN); } public static Type value_on_missing () -- cgit v1.2.3-70-g09d2