summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-09-13 00:46:49 +0200
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-09-13 00:46:49 +0200
commit18581bcbd25bd48c54f41755a51444923c903db8 (patch)
tree7b6129de2d03726f3e3228b08a927fde073455a3
parent8c3a1980e0cdc3a96ece23e18301ef25effcdfa5 (diff)
An "assert" utility class is definitely needed at this point.
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/instruction/AddElement.java78
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/instruction/AddElementAt.java96
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/instruction/AddElementsOf.java83
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/instruction/Assert.java27
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/instruction/Clear.java30
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/instruction/CondInstruction.java22
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/instruction/Map.java2
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/meta/RecurrentChecks.java249
8 files changed, 281 insertions, 306 deletions
diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/AddElement.java b/src/core/src/tonkadur/fate/v1/lang/instruction/AddElement.java
index c60b87d..791e193 100644
--- a/src/core/src/tonkadur/fate/v1/lang/instruction/AddElement.java
+++ b/src/core/src/tonkadur/fate/v1/lang/instruction/AddElement.java
@@ -1,19 +1,12 @@
package tonkadur.fate.v1.lang.instruction;
-import tonkadur.error.ErrorManager;
-
import tonkadur.parser.Origin;
-
-import tonkadur.fate.v1.error.ConflictingTypeException;
-import tonkadur.fate.v1.error.IncomparableTypeException;
-import tonkadur.fate.v1.error.InvalidTypeException;
-
-import tonkadur.fate.v1.lang.type.CollectionType;
-import tonkadur.fate.v1.lang.type.Type;
+import tonkadur.parser.ParsingError;
import tonkadur.fate.v1.lang.meta.InstructionVisitor;
import tonkadur.fate.v1.lang.meta.Instruction;
import tonkadur.fate.v1.lang.meta.Computation;
+import tonkadur.fate.v1.lang.meta.RecurrentChecks;
public class AddElement extends Instruction
{
@@ -50,72 +43,9 @@ public class AddElement extends Instruction
final Computation element,
final Computation collection
)
- throws
- InvalidTypeException,
- ConflictingTypeException,
- IncomparableTypeException
+ throws ParsingError
{
- final Type hint;
- final Type collection_type;
- final CollectionType collection_true_type;
- final Type collection_element_type;
-
- collection_type = collection.get_type();
-
- if (!(collection_type instanceof CollectionType))
- {
- ErrorManager.handle
- (
- new InvalidTypeException
- (
- collection.get_origin(),
- collection.get_type(),
- Type.COLLECTION_TYPES
- )
- );
- }
-
- collection_true_type = (CollectionType) collection_type;
- collection_element_type = collection_true_type.get_content_type();
-
- if
- (
- element.get_type().can_be_used_as(collection_element_type)
- ||
- (element.get_type().try_merging_with(collection_element_type) != null)
- )
- {
- return new AddElement(origin, element, collection);
- }
-
- ErrorManager.handle
- (
- new ConflictingTypeException
- (
- element.get_origin(),
- element.get_type(),
- collection_element_type
- )
- );
-
- hint =
- (Type) element.get_type().generate_comparable_to
- (
- collection_element_type
- );
-
- if (hint.equals(Type.ANY))
- {
- ErrorManager.handle
- (
- new IncomparableTypeException
- (
- element.get_origin(),
- element.get_type(),
- collection_element_type
- )
- );
- }
+ RecurrentChecks.assert_is_a_collection_of(collection, element);
return new AddElement(origin, element, collection);
}
diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/AddElementAt.java b/src/core/src/tonkadur/fate/v1/lang/instruction/AddElementAt.java
index 4cd192f..156b54f 100644
--- a/src/core/src/tonkadur/fate/v1/lang/instruction/AddElementAt.java
+++ b/src/core/src/tonkadur/fate/v1/lang/instruction/AddElementAt.java
@@ -1,21 +1,14 @@
package tonkadur.fate.v1.lang.instruction;
-import java.util.Collections;
-
-import tonkadur.error.ErrorManager;
-
import tonkadur.parser.Origin;
+import tonkadur.parser.ParsingError;
-import tonkadur.fate.v1.error.ConflictingTypeException;
-import tonkadur.fate.v1.error.IncomparableTypeException;
-import tonkadur.fate.v1.error.InvalidTypeException;
-
-import tonkadur.fate.v1.lang.type.CollectionType;
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;
+import tonkadur.fate.v1.lang.meta.RecurrentChecks;
public class AddElementAt extends Instruction
{
@@ -56,89 +49,10 @@ public class AddElementAt extends Instruction
final Computation element,
final Computation collection
)
- throws
- InvalidTypeException,
- ConflictingTypeException,
- IncomparableTypeException
+ throws ParsingError
{
- final Type hint;
- final Type collection_type;
- final CollectionType collection_true_type;
- final Type collection_element_type;
-
- collection_type = collection.get_type();
-
- if
- (
- (!(collection_type instanceof CollectionType))
- || (((CollectionType) collection_type).is_set())
- )
- {
- ErrorManager.handle
- (
- new InvalidTypeException
- (
- collection.get_origin(),
- collection.get_type(),
- Collections.singletonList(Type.LIST)
- )
- );
- }
-
- if (!index.get_type().can_be_used_as(Type.INT))
- {
- ErrorManager.handle
- (
- new InvalidTypeException
- (
- index.get_origin(),
- index.get_type(),
- Collections.singletonList(Type.INT)
- )
- );
- }
-
- collection_true_type = (CollectionType) collection_type;
- collection_element_type = collection_true_type.get_content_type();
-
- if
- (
- element.get_type().can_be_used_as(collection_element_type)
- ||
- (element.get_type().try_merging_with(collection_element_type) != null)
- )
- {
- return new AddElementAt(origin, index, element, collection);
- }
-
- ErrorManager.handle
- (
- new ConflictingTypeException
- (
- element.get_origin(),
- element.get_type(),
- collection_element_type
- )
- );
-
- hint =
- (Type) element.get_type().generate_comparable_to
- (
- collection_element_type
- );
-
- if (hint.equals(Type.ANY))
- {
- ErrorManager.handle
- (
- new IncomparableTypeException
- (
- element.get_origin(),
- element.get_type(),
- collection_element_type
- )
- );
- }
+ RecurrentChecks.assert_is_a_list_of(collection, element);
+ RecurrentChecks.assert_can_be_used_as(index, Type.INT);
return new AddElementAt(origin, index, element, collection);
}
diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/AddElementsOf.java b/src/core/src/tonkadur/fate/v1/lang/instruction/AddElementsOf.java
index 7409b2b..0b4f378 100644
--- a/src/core/src/tonkadur/fate/v1/lang/instruction/AddElementsOf.java
+++ b/src/core/src/tonkadur/fate/v1/lang/instruction/AddElementsOf.java
@@ -1,19 +1,14 @@
package tonkadur.fate.v1.lang.instruction;
-import tonkadur.error.ErrorManager;
-
import tonkadur.parser.Origin;
-
-import tonkadur.fate.v1.error.ConflictingTypeException;
-import tonkadur.fate.v1.error.IncomparableTypeException;
-import tonkadur.fate.v1.error.InvalidTypeException;
+import tonkadur.parser.ParsingError;
import tonkadur.fate.v1.lang.type.CollectionType;
-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.Reference;
+import tonkadur.fate.v1.lang.meta.RecurrentChecks;
public class AddElementsOf extends Instruction
{
@@ -50,77 +45,17 @@ public class AddElementsOf extends Instruction
final Reference other_collection,
final Reference collection
)
- throws
- InvalidTypeException,
- ConflictingTypeException,
- IncomparableTypeException
+ throws ParsingError
{
- final Type hint;
- final Type collection_type, other_collection_type;
-
- collection_type = collection.get_type();
- other_collection_type = other_collection.get_type();
-
- if (!(collection_type instanceof CollectionType))
- {
- ErrorManager.handle
- (
- new InvalidTypeException
- (
- collection.get_origin(),
- collection_type,
- Type.COLLECTION_TYPES
- )
- );
- }
-
- if (!(other_collection_type instanceof CollectionType))
- {
- ErrorManager.handle
- (
- new InvalidTypeException
- (
- other_collection.get_origin(),
- other_collection_type,
- Type.COLLECTION_TYPES
- )
- );
- }
-
- if (other_collection_type.can_be_used_as(collection_type))
- {
- return new AddElementsOf(origin, other_collection, collection);
- }
-
- ErrorManager.handle
+ RecurrentChecks.assert_is_a_collection(collection);
+ RecurrentChecks.assert_is_a_collection(other_collection);
+ RecurrentChecks.assert_can_be_used_as
(
- new ConflictingTypeException
- (
- other_collection.get_origin(),
- other_collection_type,
- collection_type
- )
+ other_collection.get_origin(),
+ ((CollectionType) other_collection.get_type()).get_content_type(),
+ ((CollectionType) collection.get_type()).get_content_type()
);
- hint =
- (Type) other_collection.get_type().generate_comparable_to
- (
- collection_type
- );
-
- if (hint.equals(Type.ANY))
- {
- ErrorManager.handle
- (
- new IncomparableTypeException
- (
- other_collection.get_origin(),
- other_collection_type,
- collection_type
- )
- );
- }
-
return new AddElementsOf(origin, other_collection, collection);
}
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 c4bf9a9..d487d38 100644
--- a/src/core/src/tonkadur/fate/v1/lang/instruction/Assert.java
+++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Assert.java
@@ -1,19 +1,15 @@
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.parser.ParsingError;
import tonkadur.fate.v1.lang.meta.InstructionVisitor;
import tonkadur.fate.v1.lang.meta.Instruction;
import tonkadur.fate.v1.lang.meta.RichTextNode;
import tonkadur.fate.v1.lang.meta.Computation;
+import tonkadur.fate.v1.lang.meta.RecurrentChecks;
+
+import tonkadur.fate.v1.lang.type.Type;
public class Assert extends Instruction
{
@@ -50,20 +46,9 @@ public class Assert extends Instruction
final Computation condition,
final RichTextNode message
)
- throws InvalidTypeException
+ throws ParsingError
{
- if (!condition.get_type().get_base_type().equals(Type.BOOL))
- {
- ErrorManager.handle
- (
- new InvalidTypeException
- (
- condition.get_origin(),
- condition.get_type(),
- Collections.singleton(Type.BOOL)
- )
- );
- }
+ RecurrentChecks.assert_can_be_used_as(condition, Type.BOOL);
return new Assert(origin, condition, message);
}
diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Clear.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Clear.java
index fd8090f..fe8943f 100644
--- a/src/core/src/tonkadur/fate/v1/lang/instruction/Clear.java
+++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Clear.java
@@ -1,17 +1,12 @@
package tonkadur.fate.v1.lang.instruction;
-import tonkadur.error.ErrorManager;
-
import tonkadur.parser.Origin;
-
-import tonkadur.fate.v1.error.InvalidTypeException;
-
-import tonkadur.fate.v1.lang.type.CollectionType;
-import tonkadur.fate.v1.lang.type.Type;
+import tonkadur.parser.ParsingError;
import tonkadur.fate.v1.lang.meta.InstructionVisitor;
import tonkadur.fate.v1.lang.meta.Instruction;
import tonkadur.fate.v1.lang.meta.Computation;
+import tonkadur.fate.v1.lang.meta.RecurrentChecks;
public class Clear extends Instruction
{
@@ -44,26 +39,9 @@ public class Clear extends Instruction
final Origin origin,
final Computation collection
)
- throws InvalidTypeException
+ throws ParsingError
{
- if
- (
- !Type.COLLECTION_TYPES.contains
- (
- collection.get_type().get_act_as_type()
- )
- )
- {
- ErrorManager.handle
- (
- new InvalidTypeException
- (
- collection.get_origin(),
- collection.get_type(),
- Type.COLLECTION_TYPES
- )
- );
- }
+ RecurrentChecks.assert_is_a_collection(collection);
return new Clear(origin, collection);
}
diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/CondInstruction.java b/src/core/src/tonkadur/fate/v1/lang/instruction/CondInstruction.java
index 0236a7e..b7eb9bb 100644
--- a/src/core/src/tonkadur/fate/v1/lang/instruction/CondInstruction.java
+++ b/src/core/src/tonkadur/fate/v1/lang/instruction/CondInstruction.java
@@ -1,21 +1,18 @@
package tonkadur.fate.v1.lang.instruction;
-import java.util.Collections;
import java.util.List;
-import tonkadur.error.ErrorManager;
-
import tonkadur.functional.Cons;
import tonkadur.parser.Origin;
-
-import tonkadur.fate.v1.error.InvalidTypeException;
+import tonkadur.parser.ParsingError;
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;
+import tonkadur.fate.v1.lang.meta.RecurrentChecks;
public class CondInstruction extends Instruction
{
@@ -48,22 +45,11 @@ public class CondInstruction extends Instruction
final Origin origin,
final List<Cons<Computation, Instruction>> branches
)
- throws InvalidTypeException
+ throws ParsingError
{
for (final Cons<Computation, Instruction> branch: branches)
{
- if (!branch.get_car().get_type().get_base_type().equals(Type.BOOL))
- {
- ErrorManager.handle
- (
- new InvalidTypeException
- (
- branch.get_car().get_origin(),
- branch.get_car().get_type(),
- Collections.singleton(Type.BOOL)
- )
- );
- }
+ RecurrentChecks.assert_can_be_used_as(branch.get_car(), Type.BOOL);
}
return new CondInstruction(origin, branches);
diff --git a/src/core/src/tonkadur/fate/v1/lang/instruction/Map.java b/src/core/src/tonkadur/fate/v1/lang/instruction/Map.java
index 228423c..2123ee4 100644
--- a/src/core/src/tonkadur/fate/v1/lang/instruction/Map.java
+++ b/src/core/src/tonkadur/fate/v1/lang/instruction/Map.java
@@ -9,8 +9,6 @@ import tonkadur.parser.ParsingError;
import tonkadur.error.ErrorManager;
-import tonkadur.fate.v1.error.IncompatibleTypeException;
-import tonkadur.fate.v1.error.IncomparableTypeException;
import tonkadur.fate.v1.error.InvalidArityException;
import tonkadur.fate.v1.error.InvalidTypeException;
diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/RecurrentChecks.java b/src/core/src/tonkadur/fate/v1/lang/meta/RecurrentChecks.java
new file mode 100644
index 0000000..da9c233
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/lang/meta/RecurrentChecks.java
@@ -0,0 +1,249 @@
+package tonkadur.fate.v1.lang.meta;
+
+import java.util.Collections;
+
+import tonkadur.error.ErrorManager;
+
+import tonkadur.parser.Origin;
+import tonkadur.parser.ParsingError;
+
+import tonkadur.fate.v1.error.ConflictingTypeException;
+import tonkadur.fate.v1.error.IncomparableTypeException;
+import tonkadur.fate.v1.error.InvalidTypeException;
+
+import tonkadur.fate.v1.lang.type.CollectionType;
+import tonkadur.fate.v1.lang.type.Type;
+
+public class RecurrentChecks
+{
+ /* Utility Class */
+ private RecurrentChecks () { }
+
+ public static Type assert_can_be_used_as
+ (
+ final Computation a,
+ final Computation b
+ )
+ throws ParsingError
+ {
+ return assert_can_be_used_as(a.get_origin(), a.get_type(), b.get_type());
+ }
+
+ public static Type assert_can_be_used_as
+ (
+ final Computation a,
+ final Type t
+ )
+ throws ParsingError
+ {
+ return assert_can_be_used_as(a.get_origin(), a.get_type(), t);
+ }
+
+ public static Type assert_can_be_used_as
+ (
+ final Origin o,
+ final Type a,
+ final Type b
+ )
+ throws ParsingError
+ {
+ Type result;
+
+ if (a.can_be_used_as(b))
+ {
+ return b;
+ }
+
+ result = a.try_merging_with(b);
+
+ if (result != null)
+ {
+ return result;
+ }
+
+ ErrorManager.handle(new ConflictingTypeException(o, a, b));
+
+ result = (Type) a.generate_comparable_to(b);
+
+ if (result.equals(Type.ANY))
+ {
+ ErrorManager.handle(new IncomparableTypeException(o, a, b));
+ }
+
+ return result;
+ }
+
+ public static void assert_is_a_list (final Computation a)
+ throws ParsingError
+ {
+ assert_is_a_list(a.get_origin(), a.get_type());
+ }
+
+ public static void assert_is_a_list (final Origin o, final Type t)
+ throws ParsingError
+ {
+ if
+ (
+ (!(t instanceof CollectionType))
+ || ((CollectionType) t).is_set()
+ )
+ {
+ ErrorManager.handle
+ (
+ new InvalidTypeException
+ (
+ o,
+ t,
+ Collections.singletonList(Type.LIST)
+ )
+ );
+ }
+ }
+
+ public static void assert_is_a_set (final Computation a)
+ throws ParsingError
+ {
+ assert_is_a_set(a.get_origin(), a.get_type());
+ }
+
+ public static void assert_is_a_set (final Origin o, final Type t)
+ throws ParsingError
+ {
+ if
+ (
+ (!(t instanceof CollectionType))
+ || !((CollectionType) t).is_set()
+ )
+ {
+ ErrorManager.handle
+ (
+ new InvalidTypeException
+ (
+ o,
+ t,
+ Collections.singletonList(Type.SET)
+ )
+ );
+ }
+ }
+
+ public static void assert_is_a_collection (final Computation a)
+ throws ParsingError
+ {
+ assert_is_a_set(a.get_origin(), a.get_type());
+ }
+
+ public static void assert_is_a_collection (final Origin o, final Type t)
+ throws ParsingError
+ {
+ if
+ (
+ (!(t instanceof CollectionType))
+ )
+ {
+ ErrorManager.handle
+ (
+ new InvalidTypeException
+ (
+ o,
+ t,
+ Type.COLLECTION_TYPES
+ )
+ );
+ }
+ }
+
+ public static void assert_is_a_list_of (final Computation c, final Computation e)
+ throws ParsingError
+ {
+ assert_is_a_list_of
+ (
+ c.get_origin(),
+ c.get_type(),
+ e.get_origin(),
+ e.get_type()
+ );
+ }
+
+ public static void assert_is_a_list_of
+ (
+ final Origin oc,
+ final Type c,
+ final Origin oe,
+ final Type e
+ )
+ throws ParsingError
+ {
+ assert_is_a_list(oc, c);
+ assert_can_be_used_as
+ (
+ oe,
+ e,
+ ((CollectionType) c).get_content_type()
+ );
+ }
+
+ public static void assert_is_a_set_of (final Computation c, final Computation e)
+ throws ParsingError
+ {
+ assert_is_a_set_of
+ (
+ c.get_origin(),
+ c.get_type(),
+ e.get_origin(),
+ e.get_type()
+ );
+ }
+
+ public static void assert_is_a_set_of
+ (
+ final Origin oc,
+ final Type c,
+ final Origin oe,
+ final Type e
+ )
+ throws ParsingError
+ {
+ assert_is_a_set(oc, c);
+ assert_can_be_used_as
+ (
+ oe,
+ e,
+ ((CollectionType) c).get_content_type()
+ );
+ }
+
+ public static void assert_is_a_collection_of
+ (
+ final Computation c,
+ final Computation e
+ )
+ throws ParsingError
+ {
+ assert_is_a_collection_of
+ (
+ c.get_origin(),
+ c.get_type(),
+ e.get_origin(),
+ e.get_type()
+ );
+ }
+
+ public static void assert_is_a_collection_of
+ (
+ final Origin oc,
+ final Type c,
+ final Origin oe,
+ final Type e
+ )
+ throws ParsingError
+ {
+ assert_is_a_collection(oc, c);
+ assert_can_be_used_as
+ (
+ oe,
+ e,
+ ((CollectionType) c).get_content_type()
+ );
+ }
+}