summaryrefslogtreecommitdiff
path: root/src/minijava/symboltable/MExpression.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/minijava/symboltable/MExpression.java')
-rw-r--r--src/minijava/symboltable/MExpression.java87
1 files changed, 86 insertions, 1 deletions
diff --git a/src/minijava/symboltable/MExpression.java b/src/minijava/symboltable/MExpression.java
index 17d6db8..4cf4724 100644
--- a/src/minijava/symboltable/MExpression.java
+++ b/src/minijava/symboltable/MExpression.java
@@ -1,5 +1,8 @@
package minijava.symboltable;
+import minijava.syntaxtree.Identifier;
+import minijava.typecheck.PrintError;
+
public class MExpression extends MType {
public enum Operator {
And, Smaller, Plus, Minus, Times, ArrayLookup,
@@ -15,12 +18,90 @@ public class MExpression extends MType {
// Expression value (for primary expression)
public String e_val;
- public MExpression(Operator op) {
+ public MExpression(int line, int column, Operator op) {
+ super(line, column);
e_op = op;
e_exp = new MPrimaryExpr();
e_list = new MExpressionList();
}
+ public String exprType(MMethod m) {
+ switch (e_op) {
+ case And:
+ if (first.exprType(m)==MIdentifier.boolType
+ && second.exprType(m)==MIdentifier.boolType) {
+ return MIdentifier.boolType;
+ } else {
+ PrintError.print(line, column, "Type mismatch");
+ return null;
+ }
+ case ArrayLen:
+ if (first.exprType(m)==MIdentifier.arrType) {
+ return MIdentifier.intType;
+ } else {
+ PrintError.print(line, column, "Type mismatch");
+ return null;
+ }
+ case ArrayLookup:
+ if (first.exprType(m)==MIdentifier.arrType
+ && second.exprType(m)==MIdentifier.intType) {
+ return MIdentifier.intType;
+ } else {
+ PrintError.print(line, column, "Type mismatch");
+ return null;
+ }
+ case Smaller:
+ if (first.exprType(m)==MIdentifier.intType
+ && second.exprType(m)==MIdentifier.intType) {
+ return MIdentifier.boolType;
+ } else {
+ PrintError.print(line, column, "Type mismatch");
+ return null;
+ }
+ case Plus:
+ case Minus:
+ case Times:
+ if (first.exprType(m)==MIdentifier.intType
+ && second.exprType(m)==MIdentifier.intType) {
+ return MIdentifier.intType;
+ } else {
+ PrintError.print(line, column, "Type mismatch");
+ return null;
+ }
+ case MsgSend:
+ // expr.id(expr*)
+ String c_type = first.exprType(m);
+ MClass m_class = m.method_class.all_classes.findClassByName(c_type);
+ if (m_class==null) {
+ PrintError.print(line, column, "no class found in a message send expression");
+ return null;
+ }
+ int idx = m_class.methods.findMethod(e_id.name);
+ if (idx==-1) {
+ PrintError.print(line, column, "method " + e_id.name + " not found");
+ return null;
+ }
+ MMethod m_method = m_class.methods.methods.elementAt(idx);
+ if (e_list.size()!=m_method.paramList.size()) {
+ PrintError.print(line, column, "Method param size mismatch!");
+ return null;
+ }
+ for (int i=0; i<e_list.size(); i++) {
+ String t1 = e_list.e_list.elementAt(i).exprType(m);
+ String t2 = m_method.paramList.varlist.elementAt(i).typename;
+ if (!t1.equals(t2)) {
+ PrintError.print(line, column, "Method param type mismatch!");
+ return null;
+ }
+ }
+ return m_method.ret_type_name;
+ case Primary:
+ return e_exp.primExprType(m);
+ default:
+ return null;
+ }
+ }
+
public void printExpr(int spaces) {
System.err.print(OutputFormat.spaces(spaces));
@@ -54,6 +135,10 @@ public class MExpression extends MType {
second.printExpr(0);
break;
case ArrayLookup:
+ first.printExpr(0);
+ System.err.print("[");
+ second.printExpr(0);
+ System.err.print("]");
case ArrayLen:
case MsgSend:
}