void HandleSpecialAssginStatement(SpecialAssginStatement tree) { var f = GetCurrentFunction(); Instruction code; HandleExpRead(tree.var); var self_register = GenerateRegisterId(); var add_register = self_register + 1; if (tree.exp == null) { code = Instruction.ABx(OpType.OpType_LoadInt, add_register, 1); f.AddInstruction(code, tree.var.line); } else { HandleExpRead(tree.exp); } GenerateRegisterId();// for add_register OpType op = OpType.OpType_Sub; if (tree.is_add_op) { op = OpType.OpType_Add; } code = Instruction.ABC(op, self_register, self_register, add_register); f.AddInstruction(code, tree.line); // assign HandleVarWrite(tree.var, self_register); ResetRegisterId(self_register); }
SyntaxTree ParseOtherStatement() { // lua做了限制,其他语句只有两种,assign statement and func call // SS还增加几个语法支持,+=,-=,++,-- SyntaxTree exp; if (LookAhead().m_type == (int)TokenType.NAME) { exp = ParsePrefixExp(); if (IsVar(exp)) { if (LookAhead().m_type == (int)TokenType.ADD_ONE) { // ++ NextToken(); var special_statement = new SpecialAssginStatement(_current.m_line); special_statement.var = exp; special_statement.is_add_op = true; return(special_statement); } else if (LookAhead().m_type == (int)TokenType.ADD_SELF) { // += NextToken(); var special_statement = new SpecialAssginStatement(_current.m_line); special_statement.var = exp; special_statement.exp = ParseExp(); special_statement.is_add_op = true; return(special_statement); } else if (LookAhead().m_type == (int)TokenType.DEC_ONE) { // -- NextToken(); var special_statement = new SpecialAssginStatement(_current.m_line); special_statement.var = exp; special_statement.is_add_op = false; return(special_statement); } else if (LookAhead().m_type == (int)TokenType.DEC_SELF) { // -= NextToken(); var special_statement = new SpecialAssginStatement(_current.m_line); special_statement.var = exp; special_statement.exp = ParseExp(); special_statement.is_add_op = false; return(special_statement); } // assign statement var assign_statement = new AssignStatement(LookAhead().m_line); assign_statement.var_list.Add(exp); while (LookAhead().m_type != (int)'=') { if (NextToken().m_type != (int)',') { throw NewParserException("expect ',' to split var-list", _current); } if (LookAhead().m_type != (int)TokenType.NAME) { throw NewParserException("expect 'id' to start var", _look_ahead); } exp = ParsePrefixExp(); if (!IsVar(exp)) { throw NewParserException("expect var here", _current); } assign_statement.var_list.Add(exp); } NextToken();// skip '=' assign_statement.exp_list = ParseExpList(); return(assign_statement); } else { Debug.Assert(exp is FuncCall); return(exp); } } else { if (IsMainExp()) { throw NewParserException("unsupport statement", _look_ahead); } return(null); } }