From edb150710a65d6237a5a81821f88610486842acf Mon Sep 17 00:00:00 2001 From: Iru Cai Date: Fri, 10 Oct 2014 01:24:13 +0800 Subject: Finalize the symbol table construction --- src/minijava/symboltable/MExpression.java | 61 ++++ src/minijava/symboltable/MExpressionList.java | 19 ++ src/minijava/symboltable/MMethod.java | 12 + src/minijava/symboltable/MPrimaryExpr.java | 54 ++++ src/minijava/symboltable/MStatement.java | 47 +++ src/minijava/symboltable/MStatementList.java | 26 ++ src/minijava/visitor/BuildSymbolTableVisitor.java | 373 ++++++++++++---------- 7 files changed, 416 insertions(+), 176 deletions(-) create mode 100644 src/minijava/symboltable/MExpression.java create mode 100644 src/minijava/symboltable/MExpressionList.java create mode 100644 src/minijava/symboltable/MPrimaryExpr.java create mode 100644 src/minijava/symboltable/MStatement.java create mode 100644 src/minijava/symboltable/MStatementList.java diff --git a/src/minijava/symboltable/MExpression.java b/src/minijava/symboltable/MExpression.java new file mode 100644 index 0000000..17d6db8 --- /dev/null +++ b/src/minijava/symboltable/MExpression.java @@ -0,0 +1,61 @@ +package minijava.symboltable; + +public class MExpression extends MType { + public enum Operator { + And, Smaller, Plus, Minus, Times, ArrayLookup, + ArrayLen, MsgSend, Primary; + } + Operator e_op; + public MExpression first, second; + // for msgsend, we need an identifier and a expression list + public MIdentifier e_id; + public MExpressionList e_list; + // Expression type + public MPrimaryExpr e_exp; + // Expression value (for primary expression) + public String e_val; + + public MExpression(Operator op) { + e_op = op; + e_exp = new MPrimaryExpr(); + e_list = new MExpressionList(); + } + + public void printExpr(int spaces) { + System.err.print(OutputFormat.spaces(spaces)); + + switch (e_op) { + case Primary: + e_exp.printPrimExpr(0); + break; + case And: + first.printExpr(0); + System.err.print("&&"); + second.printExpr(0); + break; + case Smaller: + first.printExpr(0); + System.err.print("<"); + second.printExpr(0); + break; + case Plus: + first.printExpr(0); + System.err.print("+"); + second.printExpr(0); + break; + case Minus: + first.printExpr(0); + System.err.print("-"); + second.printExpr(0); + break; + case Times: + first.printExpr(0); + System.err.print("*"); + second.printExpr(0); + break; + case ArrayLookup: + case ArrayLen: + case MsgSend: + } + } +} diff --git a/src/minijava/symboltable/MExpressionList.java b/src/minijava/symboltable/MExpressionList.java new file mode 100644 index 0000000..71e7c12 --- /dev/null +++ b/src/minijava/symboltable/MExpressionList.java @@ -0,0 +1,19 @@ +package minijava.symboltable; + +import java.util.Vector; + +public class MExpressionList extends MType { + Vector e_list; + + public MExpressionList() { + e_list = new Vector(); + } + + public void add_expr(MExpression e) { + e_list.addElement(e); + } + + public int size() { + return e_list.size(); + } +} diff --git a/src/minijava/symboltable/MMethod.java b/src/minijava/symboltable/MMethod.java index 3b0888e..7f57a40 100644 --- a/src/minijava/symboltable/MMethod.java +++ b/src/minijava/symboltable/MMethod.java @@ -5,12 +5,15 @@ public class MMethod extends MLocalVarType { String ret_type_name; MClass method_class; MVarList paramList; + public MStatementList statements; + public MExpression ret_expr; public MMethod(String m_name, String ret_type, int m_line, int m_column) { super(m_line, m_column); this.name = m_name; this.ret_type_name = ret_type; paramList = new MVarList(); + statements = new MStatementList(); } public String insertVar(MVariable var) { @@ -37,14 +40,23 @@ public class MMethod extends MLocalVarType { public void printMethod(int spaces) { String ps = OutputFormat.spaces(spaces); System.err.print(ps + this.ret_type_name + " " + this.name + "("); + // 输出形式参数 for (int i=0; i0) System.err.print(","); paramList.varlist.elementAt(i).printVar(0); } System.err.println(")"); + // 输出局部变量 for (int i=0; i IntegerLiteral() + * | TrueLiteral() + * | FalseLiteral() + * | Identifier() + * | ThisExpression() + * | ArrayAllocationExpression() + * | AllocationExpression() + * | NotExpression() + * | BracketExpression() + */ + + public enum E_type { + Int, True, False, Id, This, ArrayAlloc, Alloc, Not, Braket; + } + public E_type e_type; + public int i_val; // int + public MIdentifier e_id = null; // Id, ArrayAlloc, Alloc + public MExpression e_exp = null; // ArrayAlloc, Not, Braket + + public int toInt() { + return i_val; + } + + public boolean toBool() { + if (e_type==E_type.True) { + return true; + } else if (e_type==E_type.False) { + return false; + } else { + throw new IllegalArgumentException("invalid call to toBool()"); + } + } + + public void printPrimExpr(int spaces) { + System.err.print(OutputFormat.spaces(spaces)); + + switch (e_type) { + case Int: + System.err.print(i_val); + break; + case True: + case False: + System.err.print(this.toBool()); + break; + case Id: + System.err.print(e_id.getName()); + break; + } + } +} diff --git a/src/minijava/symboltable/MStatement.java b/src/minijava/symboltable/MStatement.java new file mode 100644 index 0000000..6410ca0 --- /dev/null +++ b/src/minijava/symboltable/MStatement.java @@ -0,0 +1,47 @@ +package minijava.symboltable; + +public class MStatement extends MType { + public enum Keyword { + Block, Assign, ArrAssign, If, While, Print; + } + Keyword s_type; + public MStatementList s_list; // for block statement + public MIdentifier s_id; // for assign, array assign + 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) { + s_type = keyw; + s_list = null; + s_id = null; + e_first = e_second = null; + s_first = s_second = null; + } + + public void printStatement(int spaces) { + String sp = OutputFormat.spaces(spaces); + + switch (s_type) { + case Block: + System.err.println(sp+"Block statement:"); + break; + case Assign: + System.err.print(sp+"="+" ("+s_id.getName()+") ("); + e_first.printExpr(0); + System.err.println(")"); + break; + case ArrAssign: + System.err.println(sp+"[]="); + break; + case If: + System.err.println(sp+"if"); + break; + case While: + System.err.println(sp+"while"); + break; + case Print: + System.err.println(sp+"print"); + break; + } + } +} diff --git a/src/minijava/symboltable/MStatementList.java b/src/minijava/symboltable/MStatementList.java new file mode 100644 index 0000000..034bc8b --- /dev/null +++ b/src/minijava/symboltable/MStatementList.java @@ -0,0 +1,26 @@ +package minijava.symboltable; + +import java.util.Vector; + +public class MStatementList extends MType { + Vector s_list; + + public MStatementList() { + s_list = new Vector(); + } + + public void addStatement(MStatement s) { + s_list.addElement(s); + } + + public int size() { + return s_list.size(); + } + + public void printStatements(int spaces) { + String sp = OutputFormat.spaces(spaces); + for (int i=0; i { String class_name; String error_msg; - n.f0.accept(this, argu); - // 处理类定义的标识符Identifier() // 获得名字 - class_name = ((MIdentifier) n.f1.accept(this, argu)).getName(); + class_name = ((MIdentifier) n.f1.accept(this, null)).getName(); // 在符号表中插入该类,如果出错,打印出错信息 m_class = new MClass(class_name, (MClasses) argu, n.f1.f0.beginLine, @@ -110,25 +108,18 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { error_msg = ((MClasses) argu).InsertClass(m_class); if (error_msg != null) PrintError.print(m_class.getLine(), m_class.getColumn(), error_msg); - - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - + // 往main class中添加main方法 MMethod main_method = new MMethod("main", "void", n.f6.beginLine, n.f6.beginColumn); - MIdentifier argid = (MIdentifier) n.f11.accept(this, argu); + MIdentifier argid = (MIdentifier) n.f11.accept(this, null); MVariable param = new MVariable(argid.getName(), "String[]", argid.getLine(), argid.getColumn()); main_method.addParam(param); m_class.insertMethod(main_method); - n.f12.accept(this, argu); - n.f13.accept(this, argu); - // printStatement - n.f14.accept(this, argu); - n.f15.accept(this, argu); - n.f16.accept(this, argu); + // add printStatement to main method + n.f14.accept(this, main_method.statements); + return _ret; } @@ -156,11 +147,9 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { String class_name; String error_msg; - n.f0.accept(this, argu); - // 处理类定义的标识符Identifier() // 获得名字 - class_name = ((MIdentifier) n.f1.accept(this, argu)).getName(); + class_name = ((MIdentifier) n.f1.accept(this, null)).getName(); // 在符号表中插入该类,如果出错,打印出错信息 m_class = new MClass(class_name, (MClasses) argu, n.f1.f0.beginLine, @@ -169,10 +158,11 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { if (error_msg != null) PrintError.print(m_class.getLine(), m_class.getColumn(), error_msg); - n.f2.accept(this, argu); + // 处理类变量声明 n.f3.accept(this, m_class); + + // 处理类方法 n.f4.accept(this, m_class); - n.f5.accept(this, argu); return _ret; } @@ -191,26 +181,24 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { MClass m_class; String class_name; String error_msg; - - n.f0.accept(this, argu); - + // 处理子类定义,并加入符号表 - class_name = ((MIdentifier) n.f1.accept(this, argu)).getName(); + class_name = ((MIdentifier) n.f1.accept(this, null)).getName(); m_class = new MClass(class_name, (MClasses) argu, n.f1.f0.beginLine, n.f1.f0.beginColumn); error_msg = ((MClasses) argu).InsertClass(m_class); if (error_msg != null) PrintError.print(m_class.getLine(), m_class.getColumn(), error_msg); - - n.f2.accept(this, argu); - + // 将父类名称加入类属性 - m_class.extend_class_name = ((MIdentifier) n.f3.accept(this, argu)).getName(); + m_class.extend_class_name = ((MIdentifier) n.f3.accept(this, null)).getName(); - n.f4.accept(this, argu); + // 处理类变量声明 n.f5.accept(this, m_class); + + // 处理类方法 n.f6.accept(this, m_class); - n.f7.accept(this, argu); + return _ret; } @@ -222,8 +210,8 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { public MType visit(VarDeclaration n, MType argu) { MType _ret=null; MLocalVarType m_class = (MLocalVarType)argu; // m_class可能是类或方法 - String var_name = n.f1.accept(this, argu).getName(); - String type_name = n.f0.accept(this, argu).getName(); + String var_name = n.f1.accept(this, null).getName(); + String type_name = n.f0.accept(this, null).getName(); // debug // System.out.println("Variable: \'" + var_name + "\' from class/method " + m_class.getName()); // System.out.println("It has type: " + type_name); @@ -236,8 +224,7 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { PrintError.print(var.getLine(), var.getColumn(), _err); } - n.f2.accept(this, argu); - return _ret; + return _ret; } /** @@ -258,8 +245,8 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { public MType visit(MethodDeclaration n, MType argu) { MType _ret = null; MClass m_class = (MClass)argu; - String ret_type = n.f1.accept(this, argu).getName(); - String method_name = n.f2.accept(this, argu).getName(); + String ret_type = n.f1.accept(this, null).getName(); + String method_name = n.f2.accept(this, null).getName(); MMethod m_method = new MMethod(method_name, ret_type, n.f2.f0.beginLine, n.f2.f0.beginColumn); // 将定义的方法插入类中,如果出错则打印错误信息 @@ -274,11 +261,12 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { // 函数局部变量处理 n.f7.accept(this, m_method); - n.f8.accept(this, argu); - n.f9.accept(this, argu); - n.f10.accept(this, argu); - n.f11.accept(this, argu); - n.f12.accept(this, argu); + // 语句处理 + n.f8.accept(this, m_method.statements); + + // 返回表达式处理 + m_method.ret_expr = (MExpression) n.f10.accept(this, null); + return _ret; } @@ -300,8 +288,8 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { public MType visit(FormalParameter n, MType argu) { MType _ret=null; MMethod m_method = (MMethod)argu; - String p_type = n.f0.accept(this, argu).getName(); - String p_name = n.f1.accept(this, argu).getName(); + String p_type = n.f0.accept(this, null).getName(); + String p_name = n.f1.accept(this, null).getName(); MVariable param = new MVariable(p_name, p_type, n.f1.f0.beginLine, n.f1.f0.beginColumn); // 插入方法的参数表 String _err = m_method.addParam(param); @@ -367,9 +355,7 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * | PrintStatement() */ public MType visit(Statement n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - return _ret; + return n.f0.accept(this, argu); } /** @@ -378,11 +364,13 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f2 -> "}" */ public MType visit(Block n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; + MStatement s = new MStatement(MStatement.Keyword.Block); + s.s_list = new MStatementList(); + if (argu!=null) { // should be a statement list + ((MStatementList)argu).addStatement(s); + } + n.f1.accept(this, s.s_list); + return s; } /** @@ -392,12 +380,14 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f3 -> ";" */ public MType visit(AssignmentStatement n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - return _ret; + MStatementList m_list = (MStatementList) argu; + MStatement s = new MStatement(MStatement.Keyword.Assign); + s.s_id = (MIdentifier) n.f0.accept(this, null); + s.e_first = (MExpression) n.f2.accept(this, null); + if (m_list!=null) { // a statement list + m_list.addStatement(s); + } + return s; } /** @@ -410,15 +400,18 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f6 -> ";" */ public MType visit(ArrayAssignmentStatement n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - n.f6.accept(this, argu); - return _ret; + MStatementList m_list = (MStatementList) argu; + MStatement s = new MStatement(MStatement.Keyword.ArrAssign); + MIdentifier m_id = (MIdentifier) n.f0.accept(this, null); + MExpression m_expr1 = (MExpression) n.f2.accept(this, argu); + MExpression m_expr2 = (MExpression) n.f5.accept(this, argu); + s.s_id = m_id; + s.e_first = m_expr1; + s.e_second = m_expr2; + if (m_list!=null) { // a statement list + m_list.addStatement(s); + } + return s; } /** @@ -431,15 +424,15 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f6 -> Statement() */ public MType visit(IfStatement n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - n.f6.accept(this, argu); - return _ret; + MStatementList m_list = (MStatementList) argu; + MStatement s = new MStatement(MStatement.Keyword.If); + s.s_id = (MIdentifier) n.f2.accept(this, argu); + s.s_first = (MStatement) n.f4.accept(this, null); + s.s_second = (MStatement) n.f6.accept(this, null); + if (m_list!=null) { // a statement list + m_list.addStatement(s); + } + return s; } /** @@ -450,13 +443,14 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f4 -> Statement() */ public MType visit(WhileStatement n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - return _ret; + MStatementList m_list = (MStatementList)argu; + MStatement s = new MStatement(MStatement.Keyword.While); + s.e_first = (MExpression) n.f2.accept(this, argu); + s.s_first = (MStatement) n.f4.accept(this, null); + if (m_list!=null) { + m_list.addStatement(s); + } + return s; } /** @@ -467,13 +461,13 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f4 -> ";" */ public MType visit(PrintStatement n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - return _ret; + MStatementList m_list = (MStatementList)argu; + MStatement s = new MStatement(MStatement.Keyword.Print); + s.e_first = (MExpression) n.f2.accept(this, argu); + if (m_list!=null) { + m_list.addStatement(s); + } + return s; } /** @@ -488,9 +482,7 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * | PrimaryExpression() */ public MType visit(Expression n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - return _ret; + return n.f0.accept(this, argu); } /** @@ -499,11 +491,13 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f2 -> PrimaryExpression() */ public MType visit(AndExpression n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; + MExpression e = new MExpression(MExpression.Operator.And); + e.first = (MExpression) n.f0.accept(this, null); + e.second = (MExpression) n.f2.accept(this, null); + if (argu!=null) { + ((MExpressionList)argu).add_expr(e); + } + return e; } /** @@ -512,11 +506,13 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f2 -> PrimaryExpression() */ public MType visit(CompareExpression n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; + MExpression e = new MExpression(MExpression.Operator.Smaller); + e.first = (MExpression) n.f0.accept(this, null); + e.second = (MExpression) n.f2.accept(this, null); + if (argu!=null) { + ((MExpressionList)argu).add_expr(e); + } + return e; } /** @@ -525,11 +521,13 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f2 -> PrimaryExpression() */ public MType visit(PlusExpression n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; + MExpression e = new MExpression(MExpression.Operator.Plus); + e.first = (MExpression) n.f0.accept(this, null); + e.second = (MExpression) n.f2.accept(this, null); + if (argu!=null) { + ((MExpressionList)argu).add_expr(e); + } + return e; } /** @@ -538,11 +536,13 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f2 -> PrimaryExpression() */ public MType visit(MinusExpression n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; + MExpression e = new MExpression(MExpression.Operator.Minus); + e.first = (MExpression) n.f0.accept(this, null); + e.second = (MExpression) n.f2.accept(this, null); + if (argu!=null) { + ((MExpressionList)argu).add_expr(e); + } + return e; } /** @@ -551,11 +551,13 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f2 -> PrimaryExpression() */ public MType visit(TimesExpression n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; + MExpression e = new MExpression(MExpression.Operator.Times); + e.first = (MExpression) n.f0.accept(this, null); + e.second = (MExpression) n.f2.accept(this, null); + if (argu!=null) { + ((MExpressionList)argu).add_expr(e); + } + return e; } /** @@ -565,12 +567,13 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f3 -> "]" */ public MType visit(ArrayLookup n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - return _ret; + MExpression e = new MExpression(MExpression.Operator.ArrayLookup); + e.first = (MExpression) n.f0.accept(this, null); + e.second = (MExpression) n.f2.accept(this, null); + if (argu!=null) { + ((MExpressionList)argu).add_expr(e); + } + return e; } /** @@ -579,11 +582,12 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f2 -> "length" */ public MType visit(ArrayLength n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; + MExpression e = new MExpression(MExpression.Operator.ArrayLen); + e.first = (MExpression) n.f0.accept(this, null); + if (argu!=null) { + ((MExpressionList)argu).add_expr(e); + } + return e; } /** @@ -595,14 +599,12 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f5 -> ")" */ public MType visit(MessageSend n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - n.f5.accept(this, argu); - return _ret; + MExpression e = new MExpression(MExpression.Operator.MsgSend); + e.first = (MExpression) n.f0.accept(this, null); + e.e_id = (MIdentifier) n.f2.accept(this, null); + // 将表达式列表的内容添加至e.e_list + n.f4.accept(this, e.e_list); + return e; } /** @@ -639,45 +641,60 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * | BracketExpression() */ public MType visit(PrimaryExpression n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - return _ret; + MExpression e = new MExpression(MExpression.Operator.Primary); + n.f0.accept(this, e.e_exp); + if (argu!=null) { // a expression list + ((MExpressionList)argu).add_expr(e); + } + return e; } /** * f0 -> */ public MType visit(IntegerLiteral n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - return _ret; + int val = Integer.parseInt(n.f0.tokenImage); + if (argu!=null) { // should be primary expression type + MPrimaryExpr e = (MPrimaryExpr)argu; + e.e_type = MPrimaryExpr.E_type.Int; + e.i_val = val; + } + return null; } /** * f0 -> "true" */ public MType visit(TrueLiteral n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - return _ret; + if (argu!=null) { // should be primary expression type + MPrimaryExpr e = (MPrimaryExpr)argu; + e.e_type = MPrimaryExpr.E_type.True; + } + return null; } /** * f0 -> "false" */ public MType visit(FalseLiteral n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - return _ret; - } + if (argu!=null) { // should be primary expression type + MPrimaryExpr e = (MPrimaryExpr)argu; + e.e_type = MPrimaryExpr.E_type.False; + } + return null; + } /** * f0 -> */ public MType visit(Identifier n, MType argu) { String identifier_name = n.f0.toString(); - MType _ret = null; - _ret = new MIdentifier(identifier_name, n.f0.beginLine, n.f0.beginColumn); + MIdentifier _ret = new MIdentifier(identifier_name, n.f0.beginLine, n.f0.beginColumn); + if (argu!=null) { // should be primary expression type + MPrimaryExpr e = (MPrimaryExpr)argu; + e.e_type = MPrimaryExpr.E_type.Id; + e.e_id = _ret; + } return _ret; } @@ -685,9 +702,11 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f0 -> "this" */ public MType visit(ThisExpression n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - return _ret; + if (argu!=null) { // should be primary expression + MPrimaryExpr e = (MPrimaryExpr)argu; + e.e_type = MPrimaryExpr.E_type.This; + } + return null; } /** @@ -698,13 +717,12 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f4 -> "]" */ public MType visit(ArrayAllocationExpression n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - n.f4.accept(this, argu); - return _ret; + if (argu!=null) { // should be primary expression + MPrimaryExpr e = (MPrimaryExpr)argu; + e.e_type = MPrimaryExpr.E_type.ArrayAlloc; + e.e_exp = (MExpression) n.f3.accept(this, null); + } + return null; } /** @@ -714,12 +732,12 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f3 -> ")" */ public MType visit(AllocationExpression n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - n.f3.accept(this, argu); - return _ret; + if (argu!=null) { // should be primary expression + MPrimaryExpr e = (MPrimaryExpr)argu; + e.e_type = MPrimaryExpr.E_type.Alloc; + e.e_id = (MIdentifier) n.f1.accept(this, null); + } + return null; } /** @@ -727,10 +745,12 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f1 -> Expression() */ public MType visit(NotExpression n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - return _ret; + if (argu!=null) { // should be primary expression + MPrimaryExpr e = (MPrimaryExpr)argu; + e.e_type = MPrimaryExpr.E_type.Not; + e.e_exp = (MExpression) n.f1.accept(this, null); + } + return null; } /** @@ -739,11 +759,12 @@ public class BuildSymbolTableVisitor extends GJDepthFirst { * f2 -> ")" */ public MType visit(BracketExpression n, MType argu) { - MType _ret=null; - n.f0.accept(this, argu); - n.f1.accept(this, argu); - n.f2.accept(this, argu); - return _ret; + if (argu!=null) { // should be primary expression + MPrimaryExpr e = (MPrimaryExpr)argu; + e.e_type = MPrimaryExpr.E_type.Braket; + e.e_exp = (MExpression) n.f1.accept(this, null); + } + return null; } } \ No newline at end of file -- cgit v1.2.3