Package: IROperatorLookup

IROperatorLookup

nameinstructionbranchcomplexitylinemethod
IROperatorLookup()
M: 0 C: 248
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 27
100%
M: 0 C: 1
100%
find(Class)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * VDM Code Generator
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.codegen.ir;
23:
24: import java.util.HashMap;
25:
26: import org.overture.codegen.ir.expressions.AAddrEqualsBinaryExpIR;
27: import org.overture.codegen.ir.expressions.AAddrNotEqualsBinaryExpIR;
28: import org.overture.codegen.ir.expressions.AAndBoolBinaryExpIR;
29: import org.overture.codegen.ir.expressions.ACastUnaryExpIR;
30: import org.overture.codegen.ir.expressions.ADivideNumericBinaryExpIR;
31: import org.overture.codegen.ir.expressions.AEqualsBinaryExpIR;
32: import org.overture.codegen.ir.expressions.AGreaterEqualNumericBinaryExpIR;
33: import org.overture.codegen.ir.expressions.AGreaterNumericBinaryExpIR;
34: import org.overture.codegen.ir.expressions.AIntDivNumericBinaryExpIR;
35: import org.overture.codegen.ir.expressions.ALessEqualNumericBinaryExpIR;
36: import org.overture.codegen.ir.expressions.ALessNumericBinaryExpIR;
37: import org.overture.codegen.ir.expressions.AMinusUnaryExpIR;
38: import org.overture.codegen.ir.expressions.AModNumericBinaryExpIR;
39: import org.overture.codegen.ir.expressions.ANotEqualsBinaryExpIR;
40: import org.overture.codegen.ir.expressions.ANotUnaryExpIR;
41: import org.overture.codegen.ir.expressions.AOrBoolBinaryExpIR;
42: import org.overture.codegen.ir.expressions.APlusNumericBinaryExpIR;
43: import org.overture.codegen.ir.expressions.APlusUnaryExpIR;
44: import org.overture.codegen.ir.expressions.APowerNumericBinaryExpIR;
45: import org.overture.codegen.ir.expressions.ARemNumericBinaryExpIR;
46: import org.overture.codegen.ir.expressions.ASubtractNumericBinaryExpIR;
47: import org.overture.codegen.ir.expressions.ATernaryIfExpIR;
48: import org.overture.codegen.ir.expressions.ATimesNumericBinaryExpIR;
49: import org.overture.codegen.ir.expressions.AXorBoolBinaryExpIR;
50:
51: public class IROperatorLookup
52: {
53:         // TODO: Operators must be added as they come. Are there more to be added?
54:
55:         // In the VDM Languge Reference Manual the "ternary if" fits the "Constructor"
56:         // class of operators. (see p. 203)
57:         private static final int TERNARY_IF = -1;
58:
59:         // Arithmetic
60:         private static final int PLUS = 1;
61:         private static final int SUB = 1;
62:         private static final int TIMES = 2;
63:         private static final int DIVIDE = 2;
64:         private static final int REM = 2;
65:         private static final int MOD = 2;
66:         private static final int DIV = 2;
67:
68:         // Relation
69:         private static final int EQUALS = 1;
70:         private static final int NOT_EQUALS = 1;
71:         private static final int GREATER_EQUAL = 1;
72:         private static final int GREATER = 1;
73:         private static final int LESS_EQUAL = 1;
74:         private static final int LESS = 1;
75:
76:         // TODO: This is not the way to do it! Fix operator precedence!
77:         private static final int POWER = 7;
78:
79:         private static final int CAST = 9;
80:
81:         private static final int OR = 10;
82:         private static final int AND = 11;
83:         private static final int XOR = 12;
84:         private static final int NOT = 13;
85:
86:         // Unary
87:         private static final int UNARY_PLUS = 13;
88:         private static final int UNARY_MINUS = 13;
89:
90:         private HashMap<Class<? extends SExpIR>, IROperatorInfo> lookup;
91:
92:         public IROperatorInfo find(Class<? extends SExpIR> key)
93:         {
94:                 return lookup.get(key);
95:         }
96:
97:         public IROperatorLookup()
98:         {
99:                 this.lookup = new HashMap<Class<? extends SExpIR>, IROperatorInfo>();
100:
101:                 lookup.put(APlusNumericBinaryExpIR.class, new IROperatorInfo(PLUS, "+"));
102:                 lookup.put(ASubtractNumericBinaryExpIR.class, new IROperatorInfo(SUB, "-"));
103:                 lookup.put(ATimesNumericBinaryExpIR.class, new IROperatorInfo(TIMES, "*"));
104:                 lookup.put(AIntDivNumericBinaryExpIR.class, new IROperatorInfo(DIV, "/"));
105:                 lookup.put(ARemNumericBinaryExpIR.class, new IROperatorInfo(REM, "%"));
106:                 lookup.put(AModNumericBinaryExpIR.class, new IROperatorInfo(MOD, "%"));
107:                 lookup.put(ADivideNumericBinaryExpIR.class, new IROperatorInfo(DIVIDE, "/"));
108:
109:                 lookup.put(AEqualsBinaryExpIR.class, new IROperatorInfo(EQUALS, "="));
110:                 lookup.put(ANotEqualsBinaryExpIR.class, new IROperatorInfo(NOT_EQUALS, "<>"));
111:                 lookup.put(AAddrEqualsBinaryExpIR.class, new IROperatorInfo(EQUALS, "=="));
112:                 lookup.put(AAddrNotEqualsBinaryExpIR.class, new IROperatorInfo(NOT_EQUALS, "!="));
113:
114:                 lookup.put(AGreaterEqualNumericBinaryExpIR.class, new IROperatorInfo(GREATER_EQUAL, ">="));
115:                 lookup.put(AGreaterNumericBinaryExpIR.class, new IROperatorInfo(GREATER, ">"));
116:                 lookup.put(ALessEqualNumericBinaryExpIR.class, new IROperatorInfo(LESS_EQUAL, "<="));
117:                 lookup.put(ALessNumericBinaryExpIR.class, new IROperatorInfo(LESS, "<"));
118:
119:                 lookup.put(APowerNumericBinaryExpIR.class, new IROperatorInfo(POWER, "**"));
120:
121:                 lookup.put(ACastUnaryExpIR.class, new IROperatorInfo(CAST, "()"));
122:
123:                 lookup.put(AOrBoolBinaryExpIR.class, new IROperatorInfo(OR, "or"));
124:                 lookup.put(AAndBoolBinaryExpIR.class, new IROperatorInfo(AND, "and"));
125:                 lookup.put(AXorBoolBinaryExpIR.class, new IROperatorInfo(XOR, "*^"));
126:                 lookup.put(ANotUnaryExpIR.class, new IROperatorInfo(NOT, "not"));
127:
128:                 lookup.put(AMinusUnaryExpIR.class, new IROperatorInfo(UNARY_MINUS, "-"));
129:                 lookup.put(APlusUnaryExpIR.class, new IROperatorInfo(UNARY_PLUS, "+"));
130:
131:                 lookup.put(ATernaryIfExpIR.class, new IROperatorInfo(TERNARY_IF, "?:"));
132:         }
133: }