Revision 9
Added by Willibald K. about 9 years ago
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/expressions/QValConstructor.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
* OOAS Compiler |
|
4 |
* |
|
5 |
* Copyright 2015, AIT Austrian Institute of Technology. |
|
6 |
* This code is based on the C# Version of the OOAS Compiler, which is |
|
7 |
* copyright 2015 by the Institute of Software Technology, Graz University |
|
8 |
* of Technology with portions copyright by the AIT Austrian Institute of |
|
9 |
* Technology. All rights reserved. |
|
10 |
* |
|
11 |
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED. |
|
12 |
* |
|
13 |
* If you modify the file please update the list of contributors below to in- |
|
14 |
* clude your name. Please also stick to the coding convention of using TABs |
|
15 |
* to do the basic (block-level) indentation and spaces for anything after |
|
16 |
* that. (Enable the display of special chars and it should be pretty obvious |
|
17 |
* what this means.) Also, remove all trailing whitespace. |
|
18 |
* |
|
19 |
* Contributors: |
|
20 |
* Willibald Krenn (AIT) |
|
21 |
* Stephan Zimmerer (AIT) |
|
22 |
* Markus Demetz (AIT) |
|
23 |
* Christoph Czurda (AIT) |
|
24 |
* |
|
25 |
*/ |
|
26 |
|
|
27 |
|
|
28 |
package org.momut.ooas.ast.expressions; |
|
29 |
|
|
30 |
import java.util.Arrays; |
|
31 |
|
|
32 |
import org.momut.ooas.ast.IAstVisitor; |
|
33 |
|
|
34 |
/////////////////////////////////////////////// |
|
35 |
/// Constructor: Qualitative Value |
|
36 |
/// |
|
37 |
public final class QValConstructor extends TypeConstructionExpression |
|
38 |
{ |
|
39 |
public enum QValDeriv { DonTCare, Dec, Steady, Inc } |
|
40 |
|
|
41 |
private Expression[] m_value; |
|
42 |
private QValDeriv m_derivValue; |
|
43 |
|
|
44 |
public Expression[] value() { return m_value; } |
|
45 |
public QValDeriv valueDeriv() { return m_derivValue; } |
|
46 |
|
|
47 |
|
|
48 |
public QValConstructor(int line, int pos) |
|
49 |
{ |
|
50 |
super (ExpressionKind.QValConstr, line, pos); |
|
51 |
m_value = new Expression[1]; |
|
52 |
m_value[0] = null; |
|
53 |
m_derivValue = QValDeriv.DonTCare; |
|
54 |
} |
|
55 |
|
|
56 |
public QValConstructor(QValConstructor toCopy) |
|
57 |
{ |
|
58 |
super (toCopy); |
|
59 |
m_value = Arrays.copyOf(toCopy.m_value, toCopy.m_value.length); |
|
60 |
m_derivValue = toCopy.m_derivValue; |
|
61 |
} |
|
62 |
|
|
63 |
@Override |
|
64 |
public /*override*/ Expression Clone() |
|
65 |
{ |
|
66 |
return new QValConstructor(this); |
|
67 |
} |
|
68 |
|
|
69 |
|
|
70 |
public /*override*/ void Accept(IAstVisitor visitor) |
|
71 |
{ |
|
72 |
visitor.visit(this); |
|
73 |
} |
|
74 |
|
|
75 |
public void SetValue(Expression aval) |
|
76 |
{ |
|
77 |
m_value[0] = aval; |
|
78 |
} |
|
79 |
|
|
80 |
public void AddRange(Expression toRange) |
|
81 |
{ |
|
82 |
Expression currVal = m_value[0]; |
|
83 |
m_value = new Expression[2]; |
|
84 |
m_value[0] = currVal; |
|
85 |
m_value[1] = toRange; |
|
86 |
} |
|
87 |
|
|
88 |
public void SetDerivation(QValDeriv newValue) |
|
89 |
{ |
|
90 |
m_derivValue = newValue; |
|
91 |
} |
|
92 |
} |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/expressions/Expression.java | ||
---|---|---|
40 | 40 |
import org.momut.ooas.ast.types.FloatType; |
41 | 41 |
import org.momut.ooas.ast.types.IntType; |
42 | 42 |
import org.momut.ooas.ast.types.TypeKind; |
43 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
43 |
import org.momut.ooas.ast.types.Type; |
|
44 | 44 |
import org.momut.ooas.ast.types.ValuedEnumType; |
45 | 45 |
import org.momut.ooas.math.AbstractOperations; |
46 | 46 |
import org.momut.ooas.math.AbstractRange; |
... | ... | |
81 | 81 |
public abstract class Expression extends AstNode implements IAst |
82 | 82 |
{ |
83 | 83 |
protected ExpressionKind m_kind; |
84 |
protected UlyssesType m_type;
|
|
84 |
protected Type m_type; |
|
85 | 85 |
protected int m_line; |
86 | 86 |
protected int m_pos; |
87 | 87 |
protected SymbolTable m_freeVariables; |
88 | 88 |
protected ArrayList<FunctionIdentifier> m_callTargets; |
89 | 89 |
|
90 | 90 |
public ExpressionKind kind() { return m_kind; } |
91 |
public UlyssesType type() { return m_type; }
|
|
91 |
public Type type() { return m_type; } |
|
92 | 92 |
public int line() { return m_line; } |
93 | 93 |
public int pos() { return m_pos; } |
94 | 94 |
public SymbolTable freeVariables() { return m_freeVariables; } |
... | ... | |
117 | 117 |
throw new NotImplementedException(); |
118 | 118 |
} |
119 | 119 |
|
120 |
public void SetType(UlyssesType aType)
|
|
120 |
public void SetType(Type aType) |
|
121 | 121 |
{ |
122 | 122 |
if (aType == null) |
123 | 123 |
throw new ArgumentException(); |
... | ... | |
158 | 158 |
/// This is different from the cover-method defined at the type level. |
159 | 159 |
/// Note: We do saturating operations on the type boundaries. |
160 | 160 |
/// </summary> |
161 |
public static UlyssesType ArithmeticCover(UlyssesType type1, UlyssesType type2, ExpressionKind op)
|
|
161 |
public static Type ArithmeticCover(Type type1, Type type2, ExpressionKind op)
|
|
162 | 162 |
{ |
163 | 163 |
if (!type1.IsNumeric() || (type2 != null && !type2.IsNumeric())) |
164 | 164 |
throw new ArgumentException(); |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/expressions/ExpressionKind.java | ||
---|---|---|
52 | 52 |
/*unary quantors*/ |
53 | 53 |
forall(47), exists(48), |
54 | 54 |
/*Constructors*/ |
55 |
ListConstr(49), SetConstr(50), MapConstr(51), TupleConstr(52), ObjectConstr(53), QValConstr(54), |
|
55 |
ListConstr(49), SetConstr(50), MapConstr(51), TupleConstr(52), ObjectConstr(53), |
|
56 |
// QValConstr(54), -- no longer supported |
|
56 | 57 |
/* identifier */ |
57 | 58 |
Identifier(55), UnresolvedIdentifier(56), Type(57), |
58 | 59 |
/* tuple access and method call */ |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/expressions/ObjectConstructor.java | ||
---|---|---|
33 | 33 |
import org.momut.ooas.ast.IAstVisitor; |
34 | 34 |
import org.momut.ooas.ast.types.OoActionSystemInstance; |
35 | 35 |
import org.momut.ooas.ast.types.OpaqueType; |
36 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
36 |
import org.momut.ooas.ast.types.Type; |
|
37 | 37 |
|
38 | 38 |
public final class ObjectConstructor extends TypeConstructionExpression |
39 | 39 |
{ |
... | ... | |
71 | 71 |
m_fixedObjectName = cleanup(aName); |
72 | 72 |
} |
73 | 73 |
|
74 |
public ObjectConstructor(UlyssesType aType, String aName, int line, int pos)
|
|
74 |
public ObjectConstructor(Type aType, String aName, int line, int pos) |
|
75 | 75 |
{ |
76 | 76 |
super (ExpressionKind.ObjectConstr, line, pos); |
77 | 77 |
SetType(aType); |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/expressions/TypeExpression.java | ||
---|---|---|
28 | 28 |
package org.momut.ooas.ast.expressions; |
29 | 29 |
|
30 | 30 |
import org.momut.ooas.ast.IAstVisitor; |
31 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
31 |
import org.momut.ooas.ast.types.Type; |
|
32 | 32 |
|
33 | 33 |
/////////////////////////////////////////////// |
34 | 34 |
/// Some identifier |
35 | 35 |
/// |
36 | 36 |
public class TypeExpression extends LeafExpression |
37 | 37 |
{ |
38 |
public TypeExpression(UlyssesType atype, int line, int pos)
|
|
38 |
public TypeExpression(Type atype, int line, int pos) |
|
39 | 39 |
{ |
40 | 40 |
super (LeafTypeEnum.type, ExpressionKind.Type, line, pos); |
41 | 41 |
m_type = atype; |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/expressions/UnaryOperator.java | ||
---|---|---|
28 | 28 |
package org.momut.ooas.ast.expressions; |
29 | 29 |
|
30 | 30 |
import org.momut.ooas.ast.IAstVisitor; |
31 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
31 |
import org.momut.ooas.ast.types.Type; |
|
32 | 32 |
import org.momut.ooas.utils.exceptions.ArgumentException; |
33 | 33 |
|
34 | 34 |
|
... | ... | |
70 | 70 |
visitor.visit(this); |
71 | 71 |
} |
72 | 72 |
|
73 |
public static Expression CoerceUp(Expression anExpr, UlyssesType target)
|
|
73 |
public static Expression CoerceUp(Expression anExpr, Type target) |
|
74 | 74 |
{ |
75 | 75 |
if (target == null) |
76 | 76 |
throw new ArgumentException(); |
77 | 77 |
|
78 | 78 |
if (anExpr.kind() == ExpressionKind.Value |
79 | 79 |
&& anExpr.type().kind() == target.kind() |
80 |
&& UlyssesType.TypeEqual(UlyssesType.CoverType(anExpr.type(), target),target))
|
|
80 |
&& Type.TypeEqual(Type.CoverType(anExpr.type(), target),target))
|
|
81 | 81 |
{ |
82 | 82 |
// if we are widening the range of a type assigned to a value, then just set the |
83 | 83 |
// new type and skip constructing a new node. |
... | ... | |
92 | 92 |
} |
93 | 93 |
} |
94 | 94 |
|
95 |
public static Expression TryCoerceUp(Expression anExpr, UlyssesType target)
|
|
95 |
public static Expression TryCoerceUp(Expression anExpr, Type target) |
|
96 | 96 |
{ |
97 |
if (!UlyssesType.TypeEqual(anExpr.type(), target))
|
|
97 |
if (!Type.TypeEqual(anExpr.type(), target)) |
|
98 | 98 |
{ |
99 | 99 |
return CoerceUp(anExpr, target); |
100 | 100 |
} |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/expressions/ValueExpression.java | ||
---|---|---|
28 | 28 |
package org.momut.ooas.ast.expressions; |
29 | 29 |
|
30 | 30 |
import org.momut.ooas.ast.IAstVisitor; |
31 |
import org.momut.ooas.ast.types.QrType; |
|
32 | 31 |
import org.momut.ooas.utils.exceptions.ArgumentException; |
33 | 32 |
|
34 | 33 |
/////////////////////////////////////////////// |
... | ... | |
54 | 53 |
m_valueType = LeafTypeEnum.real; |
55 | 54 |
else if (Character.class.isAssignableFrom(clazz)) |
56 | 55 |
m_valueType = LeafTypeEnum.chr; |
57 |
else if (QrType.class.isAssignableFrom(clazz)) |
|
58 |
m_valueType = LeafTypeEnum.qval; |
|
59 | 56 |
else if (Object.class.isAssignableFrom(clazz)) |
60 | 57 |
m_valueType = LeafTypeEnum.reference; |
61 | 58 |
else |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/LandmarkIdentifier.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
* OOAS Compiler |
|
4 |
* |
|
5 |
* Copyright 2015, AIT Austrian Institute of Technology. |
|
6 |
* This code is based on the C# Version of the OOAS Compiler, which is |
|
7 |
* copyright 2015 by the Institute of Software Technology, Graz University |
|
8 |
* of Technology with portions copyright by the AIT Austrian Institute of |
|
9 |
* Technology. All rights reserved. |
|
10 |
* |
|
11 |
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED. |
|
12 |
* |
|
13 |
* If you modify the file please update the list of contributors below to in- |
|
14 |
* clude your name. Please also stick to the coding convention of using TABs |
|
15 |
* to do the basic (block-level) indentation and spaces for anything after |
|
16 |
* that. (Enable the display of special chars and it should be pretty obvious |
|
17 |
* what this means.) Also, remove all trailing whitespace. |
|
18 |
* |
|
19 |
* Contributors: |
|
20 |
* Willibald Krenn (AIT) |
|
21 |
* Stephan Zimmerer (AIT) |
|
22 |
* Markus Demetz (AIT) |
|
23 |
* Christoph Czurda (AIT) |
|
24 |
* |
|
25 |
*/ |
|
26 |
|
|
27 |
|
|
28 |
package org.momut.ooas.ast.identifiers; |
|
29 |
|
|
30 |
import org.antlr.runtime.Token; |
|
31 |
import org.momut.ooas.ast.IAstVisitor; |
|
32 |
import org.momut.ooas.ast.IScope; |
|
33 |
import org.momut.ooas.ast.types.QrType; |
|
34 |
|
|
35 |
public final class LandmarkIdentifier extends ValueIdentifier |
|
36 |
{ |
|
37 |
public LandmarkIdentifier(Token aToken, |
|
38 |
QrType aType, |
|
39 |
IScope aDefiningScope) |
|
40 |
{ |
|
41 |
super(aToken, aType, IdentifierKind.LandmarkIdentifier, aDefiningScope); |
|
42 |
} |
|
43 |
|
|
44 |
public LandmarkIdentifier(LandmarkIdentifier toCopy) |
|
45 |
{ |
|
46 |
super(toCopy); |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
public /*override*/ Identifier Clone() |
|
51 |
{ |
|
52 |
return this; |
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public /*override*/ void Accept(IAstVisitor visitor) |
|
57 |
{ |
|
58 |
visitor.visit(this); |
|
59 |
} |
|
60 |
} |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/AttributeIdentifier.java | ||
---|---|---|
32 | 32 |
import org.momut.ooas.ast.IAstVisitor; |
33 | 33 |
import org.momut.ooas.ast.IScope; |
34 | 34 |
import org.momut.ooas.ast.expressions.Expression; |
35 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
35 |
import org.momut.ooas.ast.types.Type; |
|
36 | 36 |
import org.momut.ooas.utils.exceptions.ArgumentException; |
37 | 37 |
|
38 | 38 |
public final class AttributeIdentifier extends ValueReferenceIdentifier |
... | ... | |
48 | 48 |
public boolean isControllable() { return m_isControllable; } |
49 | 49 |
|
50 | 50 |
public AttributeIdentifier(Token aToken, |
51 |
UlyssesType aType,
|
|
51 |
Type aType, |
|
52 | 52 |
IScope aDefiningScope, |
53 | 53 |
Expression anInitializer, |
54 | 54 |
boolean isStatic, |
... | ... | |
63 | 63 |
} |
64 | 64 |
|
65 | 65 |
public AttributeIdentifier(String name, |
66 |
UlyssesType aType,
|
|
66 |
Type aType, |
|
67 | 67 |
IScope aDefiningScope, |
68 | 68 |
Expression anInitializer, |
69 | 69 |
boolean isStatic, |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/ExpressionVariableIdentifier.java | ||
---|---|---|
30 | 30 |
import org.antlr.runtime.Token; |
31 | 31 |
import org.momut.ooas.ast.IAstVisitor; |
32 | 32 |
import org.momut.ooas.ast.IScope; |
33 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
33 |
import org.momut.ooas.ast.types.Type; |
|
34 | 34 |
|
35 | 35 |
public final class ExpressionVariableIdentifier extends LocalVariableIdentifier |
36 | 36 |
{ |
... | ... | |
39 | 39 |
public boolean initialized() { return m_isInitialized; } |
40 | 40 |
|
41 | 41 |
public ExpressionVariableIdentifier(Token aToken, |
42 |
UlyssesType aType,
|
|
42 |
Type aType, |
|
43 | 43 |
IScope aDefiningScope) |
44 | 44 |
{ |
45 | 45 |
super (aToken, aType, aDefiningScope); |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/FunctionIdentifier.java | ||
---|---|---|
34 | 34 |
import org.antlr.runtime.Token; |
35 | 35 |
import org.momut.ooas.ast.IScope; |
36 | 36 |
import org.momut.ooas.ast.statements.Statement; |
37 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
37 |
import org.momut.ooas.ast.types.Type; |
|
38 | 38 |
import org.momut.ooas.utils.exceptions.InternalCompilerException; |
39 | 39 |
|
40 | 40 |
public abstract class FunctionIdentifier extends Scope |
... | ... | |
47 | 47 |
|
48 | 48 |
|
49 | 49 |
public FunctionIdentifier(Token aToken, |
50 |
UlyssesType aType,
|
|
50 |
Type aType, |
|
51 | 51 |
IdentifierKind aKind, |
52 | 52 |
IScope aDefiningScope) |
53 | 53 |
{ |
... | ... | |
63 | 63 |
|
64 | 64 |
public FunctionIdentifier( |
65 | 65 |
String name, |
66 |
UlyssesType aType,
|
|
66 |
Type aType, |
|
67 | 67 |
IdentifierKind aKind, |
68 | 68 |
IScope aDefiningScope) |
69 | 69 |
{ |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/Identifier.java | ||
---|---|---|
33 | 33 |
import org.momut.ooas.ast.IAst; |
34 | 34 |
import org.momut.ooas.ast.IAstVisitor; |
35 | 35 |
import org.momut.ooas.ast.IScope; |
36 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
36 |
import org.momut.ooas.ast.types.Type; |
|
37 | 37 |
import org.momut.ooas.utils.exceptions.ArgumentException; |
38 | 38 |
import org.momut.ooas.utils.exceptions.NotImplementedException; |
39 | 39 |
|
... | ... | |
46 | 46 |
protected IScope m_definingScope; |
47 | 47 |
protected IdentifierKind m_identifierKind; |
48 | 48 |
protected String m_tokenText; |
49 |
protected UlyssesType m_Type;
|
|
49 |
protected Type m_Type; |
|
50 | 50 |
|
51 | 51 |
// basic attributes of an Identifier |
52 | 52 |
public final IdentifierKind kind() { return m_identifierKind; } // kind of identifier |
53 |
public final UlyssesType type() { return m_Type; } // type of identifier
|
|
53 |
public final Type type() { return m_Type; } // type of identifier |
|
54 | 54 |
public final IScope definingScope() { return m_definingScope; } // scope where it's defined |
55 | 55 |
|
56 | 56 |
// from lexer |
... | ... | |
63 | 63 |
|
64 | 64 |
|
65 | 65 |
public Identifier(Token aToken, |
66 |
UlyssesType aType,
|
|
66 |
Type aType, |
|
67 | 67 |
IdentifierKind aKind, |
68 | 68 |
IScope aDefiningScope) |
69 | 69 |
{ |
... | ... | |
75 | 75 |
m_definingScope = aDefiningScope; |
76 | 76 |
} |
77 | 77 |
|
78 |
public Identifier(String name, UlyssesType aType, IdentifierKind aKind, IScope aDefiningScope)
|
|
78 |
public Identifier(String name, Type aType, IdentifierKind aKind, IScope aDefiningScope) |
|
79 | 79 |
{ |
80 | 80 |
m_tokenText = name; |
81 | 81 |
m_line = 0; |
... | ... | |
107 | 107 |
throw new NotImplementedException(); |
108 | 108 |
} |
109 | 109 |
|
110 |
public final void SetType(UlyssesType aType)
|
|
110 |
public final void SetType(Type aType) |
|
111 | 111 |
{ |
112 | 112 |
if (aType == null) |
113 | 113 |
throw new ArgumentException("New type can not be null."); |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/IdentifierKind.java | ||
---|---|---|
31 | 31 |
{ |
32 | 32 |
TypeIdentifier(0), // some named type |
33 | 33 |
EnumIdentifier(1), // identifier of enumerated type |
34 |
LandmarkIdentifier(2), // landmark
|
|
34 |
// LandmarkIdentifier(2), // landmark -- originally used for QR types, no longer supported.
|
|
35 | 35 |
AttributeIdentifier(3), // global attribute |
36 | 36 |
LocalVariableIdentifier(4), // local variable |
37 | 37 |
ParameterIdentifier(5), // method/named action argument |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/LocalVariableIdentifier.java | ||
---|---|---|
31 | 31 |
import org.antlr.runtime.Token; |
32 | 32 |
import org.momut.ooas.ast.IAstVisitor; |
33 | 33 |
import org.momut.ooas.ast.IScope; |
34 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
34 |
import org.momut.ooas.ast.types.Type; |
|
35 | 35 |
|
36 | 36 |
|
37 | 37 |
public class LocalVariableIdentifier extends ValueReferenceIdentifier |
38 | 38 |
{ |
39 | 39 |
public LocalVariableIdentifier(Token aToken, |
40 |
UlyssesType aType,
|
|
40 |
Type aType, |
|
41 | 41 |
IScope aDefiningScope) |
42 | 42 |
{ |
43 | 43 |
super (aToken, aType, IdentifierKind.LocalVariableIdentifier, aDefiningScope); |
44 | 44 |
} |
45 | 45 |
|
46 | 46 |
public LocalVariableIdentifier(String name, |
47 |
UlyssesType aType,
|
|
47 |
Type aType, |
|
48 | 48 |
IScope aDefiningScope) |
49 | 49 |
{ |
50 | 50 |
super (name, aType, IdentifierKind.LocalVariableIdentifier, aDefiningScope); |
... | ... | |
61 | 61 |
return new LocalVariableIdentifier(this); |
62 | 62 |
} |
63 | 63 |
|
64 |
public LocalVariableIdentifier(String name, UlyssesType aType, IdentifierKind aKind, IScope aDefiningScope)
|
|
64 |
public LocalVariableIdentifier(String name, Type aType, IdentifierKind aKind, IScope aDefiningScope) |
|
65 | 65 |
{ |
66 | 66 |
super (name, aType, aKind, aDefiningScope); |
67 | 67 |
} |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/MethodIdentifier.java | ||
---|---|---|
31 | 31 |
import org.antlr.runtime.Token; |
32 | 32 |
import org.momut.ooas.ast.IAstVisitor; |
33 | 33 |
import org.momut.ooas.ast.IScope; |
34 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
34 |
import org.momut.ooas.ast.types.Type; |
|
35 | 35 |
|
36 | 36 |
public final class MethodIdentifier extends FunctionIdentifier |
37 | 37 |
{ |
38 | 38 |
public MethodIdentifier(Token aToken, |
39 |
UlyssesType aType,
|
|
39 |
Type aType, |
|
40 | 40 |
IScope aDefiningScope) |
41 | 41 |
{ |
42 | 42 |
super (aToken, aType, IdentifierKind.MethodIdentifier, aDefiningScope); |
43 | 43 |
} |
44 | 44 |
|
45 | 45 |
public MethodIdentifier(String aName, |
46 |
UlyssesType aType,
|
|
46 |
Type aType, |
|
47 | 47 |
IScope aDefiningScope) |
48 | 48 |
{ |
49 | 49 |
super (aName, aType, IdentifierKind.MethodIdentifier, aDefiningScope); |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/NamedActionIdentifier.java | ||
---|---|---|
31 | 31 |
import org.antlr.runtime.Token; |
32 | 32 |
import org.momut.ooas.ast.IAstVisitor; |
33 | 33 |
import org.momut.ooas.ast.IScope; |
34 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
34 |
import org.momut.ooas.ast.types.Type; |
|
35 | 35 |
|
36 | 36 |
public final class NamedActionIdentifier extends FunctionIdentifier |
37 | 37 |
{ |
38 | 38 |
public NamedActionIdentifier(Token aToken, |
39 |
UlyssesType aType,
|
|
39 |
Type aType, |
|
40 | 40 |
IScope aDefiningScope) |
41 | 41 |
{ |
42 | 42 |
super (aToken, aType, IdentifierKind.NamedActionIdentifier, aDefiningScope); |
43 | 43 |
} |
44 | 44 |
|
45 | 45 |
public NamedActionIdentifier(String aName, |
46 |
UlyssesType aType,
|
|
46 |
Type aType, |
|
47 | 47 |
IScope aDefiningScope) |
48 | 48 |
{ |
49 | 49 |
super (aName, aType, IdentifierKind.NamedActionIdentifier, aDefiningScope); |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/ParameterIdentifier.java | ||
---|---|---|
30 | 30 |
import org.antlr.runtime.Token; |
31 | 31 |
import org.momut.ooas.ast.IAstVisitor; |
32 | 32 |
import org.momut.ooas.ast.IScope; |
33 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
33 |
import org.momut.ooas.ast.types.Type; |
|
34 | 34 |
|
35 | 35 |
public final class ParameterIdentifier extends LocalVariableIdentifier |
36 | 36 |
{ |
37 | 37 |
public ParameterIdentifier(Token aToken, |
38 |
UlyssesType aType,
|
|
38 |
Type aType, |
|
39 | 39 |
IScope aDefiningScope) |
40 | 40 |
{ |
41 | 41 |
super(aToken, aType, aDefiningScope); |
42 | 42 |
m_identifierKind = IdentifierKind.ParameterIdentifier; |
43 | 43 |
} |
44 | 44 |
|
45 |
public ParameterIdentifier(String name, UlyssesType aType, IScope aDefiningScope)
|
|
45 |
public ParameterIdentifier(String name, Type aType, IScope aDefiningScope) |
|
46 | 46 |
{ |
47 | 47 |
super (name, aType, IdentifierKind.ParameterIdentifier, aDefiningScope); |
48 | 48 |
} |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/Scope.java | ||
---|---|---|
29 | 29 |
|
30 | 30 |
import org.antlr.runtime.Token; |
31 | 31 |
import org.momut.ooas.ast.IScope; |
32 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
32 |
import org.momut.ooas.ast.types.Type; |
|
33 | 33 |
import org.momut.ooas.parser.SymbolTable; |
34 | 34 |
|
35 | 35 |
/////////////////////////////////////////////// |
... | ... | |
40 | 40 |
protected SymbolTable m_symbolTable; |
41 | 41 |
|
42 | 42 |
public Scope(Token aToken, |
43 |
UlyssesType aType,
|
|
43 |
Type aType, |
|
44 | 44 |
IdentifierKind aKind, |
45 | 45 |
IScope aDefiningScope) |
46 | 46 |
{ |
... | ... | |
50 | 50 |
|
51 | 51 |
|
52 | 52 |
public Scope(String aName, |
53 |
UlyssesType aType,
|
|
53 |
Type aType, |
|
54 | 54 |
IdentifierKind aKind, |
55 | 55 |
IScope aDefiningScope) |
56 | 56 |
{ |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/SelfTypeIdentifier.java | ||
---|---|---|
29 | 29 |
|
30 | 30 |
import org.momut.ooas.ast.IAstVisitor; |
31 | 31 |
import org.momut.ooas.ast.IScope; |
32 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
32 |
import org.momut.ooas.ast.types.Type; |
|
33 | 33 |
|
34 | 34 |
public final class SelfTypeIdentifier extends TypeIdentifier |
35 | 35 |
{ |
36 | 36 |
public SelfTypeIdentifier(String aName, |
37 |
UlyssesType aType,
|
|
37 |
Type aType, |
|
38 | 38 |
IScope aDefiningScope) |
39 | 39 |
{ |
40 | 40 |
super (aName, aType, aDefiningScope); |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/TypeIdentifier.java | ||
---|---|---|
30 | 30 |
import org.antlr.runtime.Token; |
31 | 31 |
import org.momut.ooas.ast.IAstVisitor; |
32 | 32 |
import org.momut.ooas.ast.IScope; |
33 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
33 |
import org.momut.ooas.ast.types.Type; |
|
34 | 34 |
|
35 | 35 |
/////////////////////////////////////////////// |
36 | 36 |
/// Type Identifier |
... | ... | |
41 | 41 |
public String prefix = ""; |
42 | 42 |
|
43 | 43 |
protected TypeIdentifier(Token aToken, |
44 |
UlyssesType aType,
|
|
44 |
Type aType, |
|
45 | 45 |
IdentifierKind someKind, |
46 | 46 |
IScope aDefiningScope) |
47 | 47 |
{ |
... | ... | |
49 | 49 |
} |
50 | 50 |
|
51 | 51 |
protected TypeIdentifier(String aToken, |
52 |
UlyssesType aType,
|
|
52 |
Type aType, |
|
53 | 53 |
IdentifierKind someKind, |
54 | 54 |
IScope aDefiningScope) |
55 | 55 |
{ |
... | ... | |
58 | 58 |
|
59 | 59 |
|
60 | 60 |
public TypeIdentifier(Token aToken, |
61 |
UlyssesType aType,
|
|
61 |
Type aType, |
|
62 | 62 |
IScope aDefiningScope) |
63 | 63 |
{ |
64 | 64 |
super (aToken, aType, IdentifierKind.TypeIdentifier, aDefiningScope); |
... | ... | |
66 | 66 |
|
67 | 67 |
|
68 | 68 |
public TypeIdentifier(String aName, |
69 |
UlyssesType aType,
|
|
69 |
Type aType, |
|
70 | 70 |
IScope aDefiningScope) |
71 | 71 |
{ |
72 | 72 |
super (aName, aType, IdentifierKind.TypeIdentifier, aDefiningScope); |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/ValueIdentifier.java | ||
---|---|---|
30 | 30 |
|
31 | 31 |
import org.antlr.runtime.Token; |
32 | 32 |
import org.momut.ooas.ast.IScope; |
33 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
33 |
import org.momut.ooas.ast.types.Type; |
|
34 | 34 |
|
35 | 35 |
/////////////////////////////////////////////// |
36 | 36 |
/// Value Identifiers |
37 | 37 |
/// |
38 | 38 |
public abstract class ValueIdentifier extends Identifier { |
39 | 39 |
public ValueIdentifier(Token aToken, |
40 |
UlyssesType aType,
|
|
40 |
Type aType, |
|
41 | 41 |
IdentifierKind aKind, |
42 | 42 |
IScope aDefiningScope) |
43 | 43 |
{ |
... | ... | |
45 | 45 |
} |
46 | 46 |
|
47 | 47 |
public ValueIdentifier(String name, |
48 |
UlyssesType aType,
|
|
48 |
Type aType, |
|
49 | 49 |
IdentifierKind aKind, |
50 | 50 |
IScope aDefiningScope) |
51 | 51 |
{ |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/identifiers/ValueReferenceIdentifier.java | ||
---|---|---|
29 | 29 |
|
30 | 30 |
import org.antlr.runtime.Token; |
31 | 31 |
import org.momut.ooas.ast.IScope; |
32 |
import org.momut.ooas.ast.types.UlyssesType;
|
|
32 |
import org.momut.ooas.ast.types.Type; |
|
33 | 33 |
|
34 | 34 |
/////////////////////////////////////////////// |
35 | 35 |
/// Variable Identifiers |
... | ... | |
38 | 38 |
public abstract class ValueReferenceIdentifier extends Identifier |
39 | 39 |
{ |
40 | 40 |
public ValueReferenceIdentifier(Token aToken, |
41 |
UlyssesType aType,
|
|
41 |
Type aType, |
|
42 | 42 |
IdentifierKind aKind, |
43 | 43 |
IScope aDefiningScope) |
44 | 44 |
{ |
... | ... | |
50 | 50 |
super(toCopy); |
51 | 51 |
} |
52 | 52 |
|
53 |
public ValueReferenceIdentifier(String name, UlyssesType aType, IdentifierKind aKind, IScope aDefiningScope)
|
|
53 |
public ValueReferenceIdentifier(String name, Type aType, IdentifierKind aKind, IScope aDefiningScope) |
|
54 | 54 |
{ |
55 | 55 |
super(name, aType, aKind, aDefiningScope); |
56 | 56 |
} |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/statements/QualitativeConstraintStatement.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
* OOAS Compiler |
|
4 |
* |
|
5 |
* Copyright 2015, AIT Austrian Institute of Technology. |
|
6 |
* This code is based on the C# Version of the OOAS Compiler, which is |
|
7 |
* copyright 2015 by the Institute of Software Technology, Graz University |
|
8 |
* of Technology with portions copyright by the AIT Austrian Institute of |
|
9 |
* Technology. All rights reserved. |
|
10 |
* |
|
11 |
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED. |
|
12 |
* |
|
13 |
* If you modify the file please update the list of contributors below to in- |
|
14 |
* clude your name. Please also stick to the coding convention of using TABs |
|
15 |
* to do the basic (block-level) indentation and spaces for anything after |
|
16 |
* that. (Enable the display of special chars and it should be pretty obvious |
|
17 |
* what this means.) Also, remove all trailing whitespace. |
|
18 |
* |
|
19 |
* Contributors: |
|
20 |
* Willibald Krenn (AIT) |
|
21 |
* Stephan Zimmerer (AIT) |
|
22 |
* Markus Demetz (AIT) |
|
23 |
* Christoph Czurda (AIT) |
|
24 |
* |
|
25 |
*/ |
|
26 |
|
|
27 |
|
|
28 |
package org.momut.ooas.ast.statements; |
|
29 |
|
|
30 |
import org.momut.ooas.ast.IAstVisitor; |
|
31 |
import org.momut.ooas.ast.identifiers.Identifier; |
|
32 |
import org.momut.ooas.utils.exceptions.ArgumentException; |
|
33 |
|
|
34 |
/////////////////////////////////////////////// |
|
35 |
/// Qualitative Constraints |
|
36 |
/// |
|
37 |
public final class QualitativeConstraintStatement extends Statement |
|
38 |
{ |
|
39 |
public enum QualitativeConstraintOperation { Equal, Deriv, Sum, Prod, Diff } |
|
40 |
|
|
41 |
private Identifier m_variable0; |
|
42 |
private Identifier m_variable1; |
|
43 |
private Identifier m_variable2; |
|
44 |
private final QualitativeConstraintOperation m_operation; |
|
45 |
public boolean tag = false; // says that this is an obs assignment (important for prolog code target) |
|
46 |
|
|
47 |
public Identifier variable0() { return m_variable0; } |
|
48 |
public Identifier variable1() { return m_variable1; } |
|
49 |
public Identifier variable2() { return m_variable2; } |
|
50 |
public QualitativeConstraintOperation operation() { return m_operation; } |
|
51 |
|
|
52 |
public QualitativeConstraintStatement(Identifier var0, Identifier var1, QualitativeConstraintOperation op, int aline, int apos) |
|
53 |
{ |
|
54 |
super (StatementKind.QualConstraint, aline, apos); |
|
55 |
if (op == QualitativeConstraintOperation.Diff |
|
56 |
|| op == QualitativeConstraintOperation.Prod |
|
57 |
|| op == QualitativeConstraintOperation.Sum) |
|
58 |
throw new ArgumentException(); |
|
59 |
m_variable0 = var0; |
|
60 |
m_variable1 = var1; |
|
61 |
m_variable2 = null; |
|
62 |
m_operation = op; |
|
63 |
} |
|
64 |
|
|
65 |
public QualitativeConstraintStatement(QualitativeConstraintStatement toCopy) |
|
66 |
{ |
|
67 |
super (toCopy); |
|
68 |
m_variable0 = toCopy.m_variable0; |
|
69 |
m_variable1 = toCopy.m_variable1; |
|
70 |
m_variable2 = toCopy.m_variable2; |
|
71 |
m_operation = toCopy.m_operation; |
|
72 |
tag = toCopy.tag; |
|
73 |
} |
|
74 |
|
|
75 |
|
|
76 |
public QualitativeConstraintStatement(Identifier var0, Identifier var1, Identifier var2, QualitativeConstraintOperation op, int aline, int apos) |
|
77 |
{ |
|
78 |
super (StatementKind.QualConstraint, aline, apos); |
|
79 |
if (op == QualitativeConstraintOperation.Deriv |
|
80 |
|| op == QualitativeConstraintOperation.Equal) |
|
81 |
throw new ArgumentException(); |
|
82 |
m_variable0 = var0; |
|
83 |
m_variable1 = var1; |
|
84 |
m_variable2 = var2; |
|
85 |
m_operation = op; |
|
86 |
} |
|
87 |
|
|
88 |
@Override |
|
89 |
public /*override*/ void Accept(IAstVisitor visitor) |
|
90 |
{ |
|
91 |
visitor.visit(this); |
|
92 |
} |
|
93 |
|
|
94 |
@Override |
|
95 |
public /*override*/ Statement Clone() |
|
96 |
{ |
|
97 |
return new QualitativeConstraintStatement(this); |
|
98 |
} |
|
99 |
|
|
100 |
public void SetVariable0(Identifier newVal) |
|
101 |
{ |
|
102 |
m_variable0 = newVal; |
|
103 |
} |
|
104 |
|
|
105 |
public void SetVariable1(Identifier newVal) |
|
106 |
{ |
|
107 |
m_variable1 = newVal; |
|
108 |
} |
|
109 |
|
|
110 |
public void SetVariable2(Identifier newVal) |
|
111 |
{ |
|
112 |
m_variable2 = newVal; |
|
113 |
} |
|
114 |
} |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/statements/StatementKind.java | ||
---|---|---|
38 | 38 |
Skip(6), |
39 | 39 |
Abort(7), |
40 | 40 |
Kill(8), |
41 |
QualConstraint(9),
|
|
41 |
// QualConstraint(9), // no longer supported
|
|
42 | 42 |
Break (10); |
43 | 43 |
|
44 | 44 |
public final int integerValue; // do not change, this is needed by the backends! i.e. public API |
trunk/compiler/ooasCompiler/src/org/momut/ooas/ast/types/UlyssesType.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
* OOAS Compiler |
|
4 |
* |
|
5 |
* Copyright 2015, AIT Austrian Institute of Technology. |
|
6 |
* This code is based on the C# Version of the OOAS Compiler, which is |
|
7 |
* copyright 2015 by the Institute of Software Technology, Graz University |
|
8 |
* of Technology with portions copyright by the AIT Austrian Institute of |
|
9 |
* Technology. All rights reserved. |
|
10 |
* |
|
11 |
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED. |
|
12 |
* |
|
13 |
* If you modify the file please update the list of contributors below to in- |
|
14 |
* clude your name. Please also stick to the coding convention of using TABs |
|
15 |
* to do the basic (block-level) indentation and spaces for anything after |
|
16 |
* that. (Enable the display of special chars and it should be pretty obvious |
|
17 |
* what this means.) Also, remove all trailing whitespace. |
|
18 |
* |
|
19 |
* Contributors: |
|
20 |
* Willibald Krenn (AIT) |
|
21 |
* Stephan Zimmerer (AIT) |
|
22 |
* Markus Demetz (AIT) |
|
23 |
* Christoph Czurda (AIT) |
|
24 |
* |
|
25 |
*/ |
|
26 |
|
|
27 |
|
|
28 |
package org.momut.ooas.ast.types; |
|
29 |
|
|
30 |
import java.util.Iterator; |
|
31 |
|
|
32 |
import org.momut.ooas.ast.AstNode; |
|
33 |
import org.momut.ooas.ast.AstNodeTypeEnum; |
|
34 |
import org.momut.ooas.ast.IAst; |
|
35 |
import org.momut.ooas.ast.IAstVisitor; |
|
36 |
import org.momut.ooas.ast.identifiers.TypeIdentifier; |
|
37 |
import org.momut.ooas.utils.exceptions.ArgumentException; |
|
38 |
import org.momut.ooas.utils.exceptions.NotImplementedException; |
|
39 |
import org.momut.ooas.utils.exceptions.OoasCompilerRuntimeException; |
|
40 |
|
|
41 |
public abstract class UlyssesType extends AstNode implements IAst |
|
42 |
{ |
|
43 |
private String m_origAnonName; |
|
44 |
protected TypeKind m_kind; |
|
45 |
protected TypeIdentifier m_identifier; |
|
46 |
protected int m_hash; |
|
47 |
// protected long TimeStamp = DateTime.Now.Ticks; |
|
48 |
|
|
49 |
|
|
50 |
public TypeKind kind() { return m_kind; } |
|
51 |
|
|
52 |
public TypeIdentifier identifier() { |
|
53 |
synchronized(this) { |
|
54 |
if (m_identifier == null) |
|
55 |
SetupAnonymousName(); |
|
56 |
} |
|
57 |
return m_identifier; |
|
58 |
} |
|
59 |
|
|
60 |
public boolean isAnonymousType() { |
|
61 |
final String anonName = AnonymousName(); |
|
62 |
if (!m_origAnonName.equals(anonName)) |
|
63 |
throw new OoasCompilerRuntimeException("Internal Error: Anonymous Name changed from '%s' to '%s'.", m_origAnonName, anonName); |
|
64 |
return anonName.equals(m_identifier.tokenText()); |
|
65 |
} |
|
66 |
|
|
67 |
protected UlyssesType(TypeKind aKind, TypeIdentifier anIdentifier) |
|
68 |
{ |
|
69 |
m_kind = aKind; |
|
70 |
m_identifier = anIdentifier; |
|
71 |
m_hash = aKind.toString().hashCode(); //Enum.GetName(typeof(TypeKind), aKind).GetHashCode(); |
|
72 |
} |
|
73 |
|
|
74 |
@Override |
|
75 |
public /*override*/ String toString() |
|
76 |
{ |
|
77 |
return identifier().tokenText(); |
|
78 |
} |
|
79 |
|
|
80 |
public /*virtual*/ String AnonymousName() |
|
81 |
{ |
|
82 |
throw new NotImplementedException(); |
|
83 |
} |
|
84 |
|
|
85 |
|
|
86 |
public void SetTypeIdentifier(TypeIdentifier anId) |
|
87 |
{ |
|
88 |
m_identifier = anId; |
|
89 |
} |
|
90 |
|
|
91 |
public void SetupAnonymousName() |
|
92 |
{ |
|
93 |
if (m_identifier == null) |
|
94 |
{ |
|
95 |
final String anonymousName = this.AnonymousName(); |
|
96 |
m_identifier = new TypeIdentifier(anonymousName, this, null); |
|
97 |
m_origAnonName = anonymousName; |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
|
|
102 |
@Override |
|
103 |
public AstNodeTypeEnum nodeType() { return AstNodeTypeEnum.type; } |
|
104 |
|
|
105 |
@Override |
|
106 |
public void Accept(IAstVisitor visitor) |
|
107 |
{ |
|
108 |
throw new NotImplementedException(); |
|
109 |
} |
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
public boolean IsNumeric() |
|
114 |
{ |
|
115 |
final Object o = this; |
|
116 |
return (m_kind == TypeKind.IntType) || |
|
117 |
(m_kind == TypeKind.FloatType) || |
|
118 |
(m_kind == TypeKind.EnumeratedType && o instanceof ValuedEnumType); |
|
119 |
} |
|
120 |
|
|
121 |
public boolean IsQualitative() |
|
122 |
{ |
|
123 |
return (m_kind == TypeKind.QrType); |
|
124 |
} |
|
125 |
|
|
126 |
/** |
|
127 |
* returns the number of unique values of the type (2 for bool, etc..) |
|
128 |
*/ |
|
129 |
public int valueCount() { |
|
130 |
throw new NotImplementedException(); // must be overridden by child |
|
131 |
} |
|
132 |
|
|
133 |
public static String High(UlyssesType atype) |
|
134 |
{ |
|
135 |
switch (atype.kind()) |
|
136 |
{ |
|
137 |
case BoolType: |
|
138 |
return "1"; |
|
139 |
case EnumeratedType: |
|
140 |
return Integer.toString((((EnumType)atype).listOfEnumSymbols().size() - 1)); |
|
141 |
case IntType: |
|
142 |
return Integer.toString(((IntType)atype).rangeHigh()); |
|
143 |
case FloatType: |
|
144 |
return Double.toString(((FloatType)atype).high()); |
|
145 |
// case TypeKind.QrType: |
|
146 |
// return (((QrType)atype).landmarks.Count - 1).ToString(); |
|
147 |
default: |
|
148 |
throw new NotImplementedException(); |
|
149 |
} |
|
150 |
} |
|
151 |
|
|
152 |
public static String Low(UlyssesType atype) |
|
153 |
{ |
|
154 |
switch (atype.kind()) |
|
155 |
{ |
|
156 |
case BoolType: |
|
157 |
return "0"; |
|
158 |
case EnumeratedType: |
|
159 |
return "0"; |
|
160 |
case IntType: |
|
161 |
return Integer.toString(((IntType)atype).rangeLow()); |
|
162 |
case FloatType: |
|
163 |
return Double.toString(((FloatType)atype).low()); |
|
164 |
// case TypeKind.QrType: |
|
165 |
// return "0"; |
|
166 |
default: |
|
167 |
throw new NotImplementedException(); |
|
168 |
} |
|
169 |
} |
|
170 |
|
|
171 |
|
|
172 |
/// <summary> |
|
173 |
/// Returns a cover for two types (if possible). |
|
174 |
/// note the cover type for two ints, floats, etc. will have max/min values |
|
175 |
/// that reflect the maximum of the two operands. |
|
176 |
/// </summary> |
|
177 |
|
|
178 |
public static UlyssesType CoverType(UlyssesType type1, UlyssesType type2) |
|
179 |
{ |
|
180 |
/*first some sanity checks*/ |
|
181 |
if (type1 == null) |
|
182 |
throw new ArgumentException("type1 null"); |
|
183 |
if (type2 == null) |
|
184 |
throw new ArgumentException("type2 null"); |
|
185 |
|
|
186 |
if (type1.kind() == TypeKind.OpaqueType) |
|
187 |
throw new ArgumentException("type1 opaque type"); |
|
188 |
if (type2.kind() == TypeKind.OpaqueType) |
|
189 |
throw new ArgumentException("type2 opaque type"); |
|
190 |
|
|
191 |
if (type1 == type2) |
|
192 |
return type1; |
|
193 |
if (UlyssesType.TypeEqual(type1, type2)) |
|
194 |
return type1; |
|
195 |
|
|
196 |
final TypeKind tk1 = type1.kind(); |
|
197 |
final TypeKind tk2 = type2.kind(); |
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
if (tk1 != tk2) |
|
202 |
{ |
|
203 |
// the only notion we support here is int->float and valenum -> int |
|
204 |
if ((tk1 == TypeKind.IntType || tk2 == TypeKind.IntType) |
|
205 |
&& (tk1 == TypeKind.FloatType || tk2 == TypeKind.FloatType)) |
|
206 |
{ |
|
207 |
final IntType aInt = tk1 == TypeKind.IntType ? (IntType)type1 : (IntType)type2; |
|
208 |
final FloatType aFloat = tk1 == TypeKind.IntType ? (FloatType)type2 : (FloatType)type1; |
|
209 |
|
|
210 |
final double low = aInt.rangeLow() < aFloat.low() ? (double)aInt.rangeLow() : aFloat.low(); |
|
211 |
final double high = aInt.rangeHigh() < aFloat.high() ? aFloat.high() : (double)aInt.rangeHigh(); |
|
212 |
final double precision = aFloat.precision() > 1 ? 1.0 : aFloat.precision(); |
|
213 |
|
|
214 |
return new FloatType(low, high, precision, null); |
|
215 |
|
|
216 |
} |
|
217 |
else if ((tk1 == TypeKind.IntType || tk2 == TypeKind.IntType) |
|
218 |
&& (tk1 == TypeKind.EnumeratedType || tk2 == TypeKind.EnumeratedType)) |
|
219 |
{ |
|
220 |
final IntType intt1 = tk1 == TypeKind.IntType ? (IntType)type1 : (IntType)type2; |
|
221 |
final EnumType anEnum = tk1 == TypeKind.IntType ? (EnumType)type2 : (EnumType)type1; |
|
222 |
|
|
223 |
if (anEnum instanceof ValuedEnumType) |
|
224 |
{ |
|
225 |
final IntType intt2 = ((ValuedEnumType)anEnum).getIntType(); |
|
226 |
final int low = intt1.rangeLow() < intt2.rangeLow() ? intt1.rangeLow() : intt2.rangeLow(); |
|
227 |
final int high = intt1.rangeHigh() > intt2.rangeHigh() ? intt1.rangeHigh() : intt2.rangeHigh(); |
|
228 |
return new IntType(low, high, null); |
|
229 |
} |
|
230 |
else |
|
231 |
return null; |
|
232 |
} |
|
233 |
else if (tk1 == TypeKind.Any || tk2 == TypeKind.Any) |
|
234 |
{ |
|
235 |
final AnyType freeVar = tk1 == TypeKind.Any ? (AnyType)type1 : (AnyType)type2; |
|
236 |
final UlyssesType fixedType = tk1 == TypeKind.Any ? type2 : type1; |
|
237 |
final UlyssesType newcover = freeVar.VariableIdentifier().type().kind() == TypeKind.Any |
|
238 |
? fixedType : CoverType(fixedType, freeVar.VariableIdentifier().type()); |
|
239 |
freeVar.VariableIdentifier().SetType(newcover); |
|
240 |
return newcover; |
|
241 |
} |
|
242 |
else if (tk1 == TypeKind.OoActionSystemType && tk2 == TypeKind.Null) |
|
243 |
{ |
|
244 |
return type1; |
|
245 |
} |
|
246 |
else if (tk2 == TypeKind.OoActionSystemType && tk1 == TypeKind.Null) |
|
247 |
{ |
|
248 |
return type2; |
|
249 |
} |
|
250 |
|
|
251 |
else |
|
252 |
return null; |
|
253 |
} |
|
254 |
else |
|
255 |
{ |
|
256 |
switch (tk1) |
|
257 |
{ |
|
258 |
case Any: |
|
259 |
return type1; |
|
260 |
|
|
261 |
case IntType: |
|
262 |
final IntType intt1 = (IntType)type1; |
|
263 |
final IntType intt2 = (IntType)type2; |
|
264 |
final int low = intt1.rangeLow() < intt2.rangeLow() ? intt1.rangeLow() : intt2.rangeLow(); |
|
265 |
final int high = intt1.rangeHigh() > intt2.rangeHigh() ? intt1.rangeHigh() : intt2.rangeHigh(); |
|
266 |
return new IntType(low, high, null); |
|
267 |
case BoolType: |
|
268 |
return type1; |
|
269 |
case FloatType: |
|
270 |
final FloatType floatt1 = (FloatType)type1; |
|
271 |
final FloatType floatt2 = (FloatType)type2; |
|
272 |
final double flow = floatt1.low() < floatt2.low() ? floatt1.low() : floatt2.low(); |
|
273 |
final double fhigh = floatt1.high() > floatt2.high() ? floatt1.high() : floatt2.high(); |
|
274 |
final double fprec = floatt1.precision() < floatt2.precision() ? floatt1.precision() : floatt2.precision(); |
|
275 |
return new FloatType(flow, fhigh, fprec, null); |
|
276 |
case EnumeratedType: |
|
277 |
return null; |
|
278 |
case ListType: |
|
279 |
final ListType listt1 = (ListType)type1; |
|
280 |
final ListType listt2 = (ListType)type2; |
|
281 |
int maxelems = listt1.maxNumberOfElements() > listt2.maxNumberOfElements() ? |
|
282 |
listt1.maxNumberOfElements() : listt2.maxNumberOfElements(); |
|
283 |
// empty lists lack type, so take the other one |
|
284 |
if ((listt1.innerType().kind() == TypeKind.Null) && (listt2.innerType().kind() != TypeKind.Null)) |
|
285 |
return listt2; |
|
286 |
else if ((listt2.innerType().kind() == TypeKind.Null) && (listt1.innerType().kind() != TypeKind.Null)) |
|
287 |
return listt1; |
|
288 |
else if (listt1.innerType().kind() == TypeKind.Null && listt2.innerType().kind() == TypeKind.Null) |
|
289 |
return listt1; |
|
290 |
else |
|
291 |
{ |
|
292 |
final UlyssesType subtype = UlyssesType.CoverType(listt1.innerType(), listt2.innerType()); |
|
293 |
if (subtype != null) |
|
294 |
return new ListType(subtype, maxelems, null); |
|
295 |
else |
|
296 |
return null; |
|
297 |
} |
|
298 |
case MapType: |
|
299 |
final MapType mapt1 = (MapType)type1; |
|
300 |
final MapType mapt2 = (MapType)type2; |
|
301 |
// empty maps lack type, so take the other one. |
|
302 |
if ((mapt1.fromType().kind() == TypeKind.Null && mapt1.toType().kind() == TypeKind.Null) |
|
303 |
&& (mapt2.fromType().kind() != TypeKind.Null && mapt2.toType().kind() != TypeKind.Null)) |
|
304 |
return mapt2; |
|
305 |
else if ((mapt2.fromType().kind() == TypeKind.Null && mapt2.toType().kind() == TypeKind.Null) |
|
306 |
&& (mapt1.fromType().kind() != TypeKind.Null && mapt1.toType().kind() != TypeKind.Null)) |
|
307 |
return mapt1; |
|
308 |
else if ((mapt2.fromType().kind() == TypeKind.Null && mapt2.toType().kind() == TypeKind.Null) |
|
309 |
&& (mapt1.fromType().kind() == TypeKind.Null && mapt1.toType().kind() == TypeKind.Null)) |
|
310 |
return mapt1; |
|
311 |
else |
|
312 |
{ |
|
313 |
final UlyssesType sub1 = UlyssesType.CoverType(mapt1.fromType(), mapt2.fromType()); |
|
314 |
final UlyssesType sub2 = UlyssesType.CoverType(mapt2.toType(), mapt2.toType()); |
|
315 |
maxelems = mapt1.maxNumberOfElements() > mapt2.maxNumberOfElements() ? |
|
316 |
mapt1.maxNumberOfElements() : mapt2.maxNumberOfElements(); |
|
317 |
if (sub1 != null && sub2 != null) |
|
318 |
return new MapType(sub1, sub2, maxelems, null); |
|
319 |
else |
|
320 |
return null; |
|
321 |
} |
|
322 |
case QrType: |
|
323 |
return null; /*if refs are equal, we do not reach this statement! see above..*/ |
|
324 |
case TupleType: |
|
325 |
final TupleType tuplet1 = (TupleType)type1; |
|
326 |
final TupleType tuplet2 = (TupleType)type2; |
|
327 |
final TupleType result = new TupleType(null); |
|
328 |
final Iterator<UlyssesType> innert1 = tuplet1.innerTypes().iterator(); |
|
329 |
final Iterator<UlyssesType> innert2 = tuplet2.innerTypes().iterator(); |
|
330 |
while (innert1.hasNext()) |
|
331 |
{ |
|
332 |
final UlyssesType newinner = UlyssesType.CoverType(innert1.next(), innert2.next()); |
|
333 |
if (newinner == null) |
|
334 |
return null; |
|
335 |
result.AddType(newinner); |
|
336 |
} |
|
337 |
return result; |
|
338 |
case OoActionSystemType: |
|
339 |
if (type1.kind() == TypeKind.Null && type2.kind() != TypeKind.Null) |
|
340 |
return type2; |
|
341 |
else if (type2.kind() == TypeKind.Null && type1.kind() != TypeKind.Null) |
|
342 |
return type1; |
|
343 |
else if (type1.kind() == TypeKind.Null && type2.kind() == TypeKind.Null) |
|
344 |
return type2; |
|
345 |
else if (type1 == type2) // ref equals! |
|
346 |
return type1; |
|
347 |
else |
|
348 |
return ClassBaseType((OoActionSystemType)type1, (OoActionSystemType)type2); |
|
349 |
case OpaqueType: |
|
350 |
assert(false); |
|
351 |
return null; |
|
352 |
default: |
|
353 |
throw new NotImplementedException(); |
|
354 |
} |
|
355 |
} |
|
356 |
} |
|
357 |
|
|
358 |
private static UlyssesType ClassBaseType(OoActionSystemType type1, OoActionSystemType type2) |
|
359 |
{ |
|
360 |
// this is rather inefficient.. should be done differently |
|
361 |
final OoActionSystemType typea = type1; |
|
362 |
final OoActionSystemType typeb = type2; |
|
363 |
OoActionSystemType basea = type1; |
|
364 |
OoActionSystemType baseb = type2; |
|
365 |
while (basea != null) |
|
366 |
{ |
|
367 |
if (basea == typeb) // ref equals |
|
368 |
return basea; |
|
369 |
basea = basea.baseType(); |
|
370 |
} |
|
371 |
while (baseb != null) |
|
372 |
{ |
|
373 |
if (baseb == typea) // ref equals |
|
374 |
return baseb; |
|
375 |
baseb = baseb.baseType(); |
|
376 |
} |
|
377 |
return null; |
|
378 |
} |
|
379 |
|
|
380 |
public static boolean FirstTypeLessRange(UlyssesType type1, UlyssesType type2) |
|
381 |
{ |
|
382 |
final TypeKind tk1 = type1.kind(); |
|
383 |
final TypeKind tk2 = type2.kind(); |
|
384 |
|
|
385 |
if (tk1 != tk2) |
|
386 |
throw new ArgumentException("Types need to be equal"); |
|
387 |
|
|
388 |
switch (tk1) |
|
389 |
{ |
|
390 |
case Any: |
|
391 |
return false; |
|
392 |
case IntType: |
|
393 |
final IntType Int1 = (IntType)type1; |
|
394 |
final IntType Int2 = (IntType)type2; |
|
395 |
return Int1.rangeLow() > Int2.rangeLow() || |
|
396 |
Int1.rangeHigh() < Int2.rangeHigh(); |
|
397 |
case BoolType: |
|
398 |
return false; |
|
399 |
case FloatType: |
|
400 |
final FloatType Float1 = (FloatType)type1; |
|
401 |
final FloatType Float2 = (FloatType)type2; |
|
402 |
return Float1.low() > Float2.low() |
|
403 |
|| Float1.high() < Float2.high() |
|
404 |
|| Float1.precision() > Float2.precision(); |
|
405 |
case EnumeratedType: |
|
406 |
return false; |
|
407 |
case ListType: |
|
408 |
final ListType listt1 = (ListType)type1; |
|
409 |
final ListType listt2 = (ListType)type2; |
|
410 |
return (listt1.maxNumberOfElements() < listt2.maxNumberOfElements()) || |
|
411 |
UlyssesType.FirstTypeLessRange(listt1.innerType(), listt2.innerType()); |
|
412 |
case MapType: |
|
413 |
final MapType mapt1 = (MapType)type1; |
|
414 |
final MapType mapt2 = (MapType)type2; |
|
415 |
return (mapt1.maxNumberOfElements() < mapt2.maxNumberOfElements()) || |
|
416 |
UlyssesType.FirstTypeLessRange(mapt1.fromType(), mapt2.fromType()) || |
|
417 |
UlyssesType.FirstTypeLessRange(mapt1.toType(), mapt2.toType()); |
|
418 |
case QrType: |
|
419 |
return false; |
|
420 |
case TupleType: |
|
421 |
final TupleType tuplet1 = (TupleType)type1; |
|
422 |
final TupleType tuplet2 = (TupleType)type2; |
|
423 |
if (tuplet1.innerTypes().size() != tuplet2.innerTypes().size()) |
|
424 |
return false; |
|
425 |
final Iterator<UlyssesType> innert1 = tuplet1.innerTypes().iterator(); |
|
426 |
final Iterator<UlyssesType> innert2 = tuplet2.innerTypes().iterator(); |
|
427 |
while (innert1.hasNext()) |
|
428 |
{ |
|
429 |
if (UlyssesType.FirstTypeLessRange(innert1.next(), innert2.next())) |
|
430 |
return true; |
|
431 |
} |
|
432 |
return false; |
|
433 |
case OoActionSystemType: |
|
434 |
return false; |
|
435 |
case OpaqueType: |
|
436 |
assert(false); |
|
437 |
return false; |
|
438 |
default: |
|
439 |
throw new NotImplementedException(); |
|
440 |
} |
|
441 |
} |
|
442 |
|
|
443 |
public static boolean TypeEqualByKind(UlyssesType type1, UlyssesType type2) |
|
444 |
{ |
|
445 |
if ((type1 == null) || (type2 == null)) |
|
446 |
return false; |
|
447 |
|
|
448 |
final TypeKind tk1 = type1.kind(); |
|
449 |
final TypeKind tk2 = type2.kind(); |
|
450 |
|
|
451 |
// if of different kind, then return false.. |
|
452 |
if (tk1 != tk2) |
|
453 |
return false; |
|
454 |
|
|
455 |
// if same kind, make a rigorous check |
|
456 |
switch (tk1) |
|
457 |
{ |
|
458 |
case Any: |
|
459 |
return true; |
|
460 |
|
|
461 |
case IntType: |
|
462 |
case BoolType: |
|
463 |
case FloatType: |
|
464 |
return true; |
|
465 |
case EnumeratedType: |
|
466 |
return type1 == type2; // ref equals |
|
467 |
case ListType: |
|
468 |
final ListType listt1 = (ListType)type1; |
|
469 |
final ListType listt2 = (ListType)type2; |
|
470 |
return UlyssesType.TypeEqualByKind(listt1.innerType(), listt2.innerType()); |
|
471 |
case MapType: |
|
472 |
final MapType mapt1 = (MapType)type1; |
|
473 |
final MapType mapt2 = (MapType)type2; |
|
474 |
return UlyssesType.TypeEqualByKind(mapt1.fromType(), mapt2.fromType()) && |
|
475 |
UlyssesType.TypeEqualByKind(mapt1.toType(), mapt2.toType()); |
|
476 |
case QrType: |
|
477 |
final QrType qr1 = (QrType)type1; |
|
478 |
final QrType qr2 = (QrType)type2; |
|
479 |
return qr1 == qr2; // ref equals |
|
480 |
case TupleType: |
|
481 |
final TupleType tuplet1 = (TupleType)type1; |
|
482 |
final TupleType tuplet2 = (TupleType)type2; |
|
483 |
if (tuplet1.innerTypes().size() != tuplet2.innerTypes().size()) |
|
484 |
return false; |
|
485 |
final Iterator<UlyssesType> innert1 = tuplet1.innerTypes().iterator(); |
|
486 |
final Iterator<UlyssesType> innert2 = tuplet2.innerTypes().iterator(); |
|
487 |
while (innert1.hasNext()) |
|
488 |
{ |
|
489 |
if (!UlyssesType.TypeEqualByKind(innert1.next(), innert2.next())) |
|
490 |
return false; |
|
491 |
} |
|
492 |
return true; |
|
493 |
case OoActionSystemType: |
|
494 |
return type1 == type2; // ref equ. |
|
495 |
case OpaqueType: |
|
496 |
assert(false); |
|
497 |
return false; |
|
498 |
default: |
|
499 |
throw new NotImplementedException(); |
|
500 |
} |
|
501 |
} |
|
502 |
|
|
503 |
|
|
504 |
public static boolean TypeEqual(UlyssesType type1, UlyssesType type2) |
|
505 |
{ |
|
506 |
// this will also work with opaque types... |
|
507 |
while (type1 instanceof OpaqueType) |
|
508 |
type1 = ((OpaqueType)type1).resolvedType(); |
|
509 |
while (type2 instanceof OpaqueType) |
|
510 |
type2 = ((OpaqueType)type2).resolvedType(); |
|
511 |
|
|
512 |
if ((type1 == null) || (type2 == null)) |
|
513 |
return false; |
|
514 |
|
|
515 |
final TypeKind tk1 = type1.kind(); |
|
516 |
final TypeKind tk2 = type2.kind(); |
|
517 |
|
|
518 |
// if of different kind, then return false.. |
|
519 |
if (tk1 != tk2) |
|
520 |
return false; |
|
521 |
|
|
522 |
// if same kind, make a rigorous check |
|
523 |
switch (tk1) |
|
524 |
{ |
|
525 |
case IntType: |
|
526 |
final IntType intt1 = (IntType)type1; |
|
527 |
final IntType intt2 = (IntType)type2; |
|
528 |
return intt1.rangeLow() == intt2.rangeLow() && |
|
529 |
intt1.rangeHigh() == intt2.rangeHigh(); |
|
530 |
case BoolType: |
|
531 |
return true; |
|
532 |
case FloatType: |
|
533 |
final FloatType floatt1 = (FloatType)type1; |
|
534 |
final FloatType floatt2 = (FloatType)type2; |
|
535 |
return floatt1.low() == floatt2.low() && |
|
536 |
floatt1.high() == floatt2.high() && |
|
537 |
floatt1.precision() == floatt2.precision(); |
|
538 |
case EnumeratedType: |
|
539 |
return type1 == type2; // ref equ |
|
540 |
case ListType: |
|
541 |
final ListType listt1 = (ListType)type1; |
|
542 |
final ListType listt2 = (ListType)type2; |
|
543 |
// an empty list can be of any type.. |
|
544 |
/*if ((listt1.innerType.kind == TypeKind.Null) || (listt2.innerType.kind == TypeKind.Null)) |
|
545 |
return true; |
|
546 |
else*/ |
|
547 |
return UlyssesType.TypeEqual(listt1.innerType(), listt2.innerType()) && |
|
548 |
listt1.maxNumberOfElements() == listt2.maxNumberOfElements(); // need this because of covertype |
|
549 |
case MapType: |
|
550 |
final MapType mapt1 = (MapType)type1; |
|
551 |
final MapType mapt2 = (MapType)type2; |
|
552 |
return UlyssesType.TypeEqual(mapt1.fromType(), mapt2.fromType()) && |
|
553 |
UlyssesType.TypeEqual(mapt1.toType(), mapt2.toType()) && |
|
554 |
mapt1.maxNumberOfElements() == mapt2.maxNumberOfElements(); // need this because of covertype |
|
555 |
case QrType: |
|
556 |
final QrType qr1 = (QrType)type1; |
|
557 |
final QrType qr2 = (QrType)type2; |
|
558 |
return qr1 == qr2; // ref equ. |
|
559 |
case TupleType: |
|
560 |
final TupleType tuplet1 = (TupleType)type1; |
|
561 |
final TupleType tuplet2 = (TupleType)type2; |
|
562 |
if (tuplet1.innerTypes().size() != tuplet2.innerTypes().size()) |
|
563 |
return false; |
|
564 |
final Iterator<UlyssesType> innert1 = tuplet1.innerTypes().iterator(); |
|
565 |
final Iterator<UlyssesType> innert2 = tuplet2.innerTypes().iterator(); |
|
566 |
while (innert1.hasNext()) |
|
567 |
{ |
|
568 |
if (!UlyssesType.TypeEqual(innert1.next(), innert2.next())) |
|
569 |
return false; |
|
570 |
} |
|
571 |
return true; |
|
572 |
case OoActionSystemType: |
|
573 |
return type1 == type2; // ref equ. // || Covariance((OoActionSystemType)type1, (OoActionSystemType)type2); |
|
574 |
case OpaqueType: |
|
575 |
assert(false); |
|
576 |
return false; |
|
577 |
case Null: |
|
578 |
case Any: |
|
579 |
return true; |
|
580 |
default: |
|
581 |
throw new NotImplementedException(); |
|
582 |
} |
|
583 |
} |
|
584 |
|
|
585 |
/// <summary> |
|
586 |
/// checks if type 2 is a desc. of type 1 |
|
587 |
/// </summary> |
|
588 |
public static boolean Covariance(OoActionSystemType ooActionSystemType, OoActionSystemType ooActionSystemType_2) |
|
589 |
{ |
|
590 |
// check if type 2 is a desc. of type 1 |
|
591 |
while (ooActionSystemType != ooActionSystemType_2 && ooActionSystemType_2.baseType() != null) // ref equ. |
|
592 |
ooActionSystemType_2 = ooActionSystemType_2.baseType(); |
|
593 |
|
|
594 |
return ooActionSystemType == ooActionSystemType_2; // ref equ. |
|
595 |
} |
|
596 |
} |
Also available in: Unified diff
remove support for Qualitative Action Systems, rename UlyssesType to Type