diff options
Diffstat (limited to 'src/minijava/symboltable/MStatement.java')
-rw-r--r-- | src/minijava/symboltable/MStatement.java | 84 |
1 files changed, 79 insertions, 5 deletions
diff --git a/src/minijava/symboltable/MStatement.java b/src/minijava/symboltable/MStatement.java index 6410ca0..3610719 100644 --- a/src/minijava/symboltable/MStatement.java +++ b/src/minijava/symboltable/MStatement.java @@ -1,5 +1,7 @@ package minijava.symboltable; +import minijava.typecheck.PrintError; + public class MStatement extends MType { public enum Keyword { Block, Assign, ArrAssign, If, While, Print; @@ -10,7 +12,8 @@ public class MStatement extends MType { public MExpression e_first, e_second; // for assign(e_first), array assign, if, while, print public MStatement s_first, s_second; // for if, while - public MStatement(Keyword keyw) { + public MStatement(int s_line, int s_column, Keyword keyw) { + super(s_line, s_column); s_type = keyw; s_list = null; s_id = null; @@ -18,6 +21,63 @@ public class MStatement extends MType { s_first = s_second = null; } + public void checkStatement(MMethod m) { + switch (s_type) { + case ArrAssign: + // id[expr] = expr + if (m.findVarByName(s_id.name).typename==MIdentifier.arrType + && e_first.exprType(m)==MIdentifier.intType + && e_second.exprType(m)==MIdentifier.intType) { + return; + } else { + PrintError.print(line, column, "type mismatch in array assign statement"); + } + break; + case Assign: + // id = expr + MVariable m_var = m.findVarByName(s_id.name); + if (m_var==null) { + PrintError.print(line, column, "variable "+s_id.name+" not exist!"); + return; + } else if (m_var.typename.equals(e_first.exprType(m))) { + return; + } else { + PrintError.print(line, column, "type mismatch in assign statement"); + } + break; + case Block: + s_list.checkStatements(m); + break; + case If: + if (e_first.exprType(m)==MIdentifier.boolType) { + s_first.checkStatement(m); + s_second.checkStatement(m); + } else { + PrintError.print(line, column, "invalid type in if statement."); + } + break; + case Print: + if (e_first.exprType(m)!=null) { + return; + } else { + PrintError.print(line, column, "Invalid type in print statement"); + } + break; + case While: + // while (expr) state + if (e_first.exprType(m)==MIdentifier.boolType) { + s_first.checkStatement(m); + } else { + PrintError.print(line, column, "invalid type in while statement."); + } + break; + default: + break; + + } + + } + public void printStatement(int spaces) { String sp = OutputFormat.spaces(spaces); @@ -31,17 +91,31 @@ public class MStatement extends MType { System.err.println(")"); break; case ArrAssign: - System.err.println(sp+"[]="); + System.err.println(sp+"[]= ("+s_id.getName()+") ("); + e_first.printExpr(0); + System.err.print(") ("); + e_second.printExpr(0); + System.err.println(")"); break; case If: - System.err.println(sp+"if"); + System.err.println(sp+"if ("); + e_first.printExpr(0); + System.err.println(")"); + s_first.printStatement(spaces+2); + s_second.printStatement(spaces+2); break; case While: - System.err.println(sp+"while"); + System.err.println(sp+"while ("); + e_first.printExpr(0); + System.err.println(")"); + s_first.printStatement(spaces+2); break; case Print: - System.err.println(sp+"print"); + System.err.println(sp+"print ("); + e_first.printExpr(0); + System.err.println(")"); break; } } + } |