mips without optimize
This commit is contained in:
146
frontend/ast/exp/AddExp.java
Normal file → Executable file
146
frontend/ast/exp/AddExp.java
Normal file → Executable file
@@ -1,73 +1,73 @@
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.NodeStack;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import midend.symbol.SymbolManager;
|
||||
import error.Errors;
|
||||
|
||||
public class AddExp extends Node {
|
||||
public AddExp(TokenStream ts) {
|
||||
super(SyntaxType.ADD_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
NodeStack stack = new NodeStack();
|
||||
while (true) {
|
||||
MulExp mep = new MulExp(this.ts);
|
||||
mep.parse(errors);
|
||||
stack.push(mep);
|
||||
if (isAddOp()) {
|
||||
stack.push(new TokenNode(ts)); // addop
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (stack.size() == 1) {
|
||||
this.addChild((MulExp) stack.pop());
|
||||
} else {
|
||||
AddExp temp = this;
|
||||
while (stack.size() > 1) {
|
||||
AddExp ae = new AddExp(this.ts);
|
||||
MulExp mep = (MulExp) stack.pop();
|
||||
TokenNode op = (TokenNode) stack.pop();
|
||||
temp.addChild(ae);
|
||||
temp.addChild(op);
|
||||
temp.addChild(mep);
|
||||
temp = ae;
|
||||
}
|
||||
temp.addChild((MulExp) stack.pop());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAddOp() {
|
||||
TokenType t = getCurrToken().getType();
|
||||
return t == TokenType.PLUS || t == TokenType.MINU;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
if (getChildren().size() == 1) {
|
||||
return ((MulExp) getChild(0)).getType();
|
||||
} else {
|
||||
return ((AddExp) getChild(0)).getType()
|
||||
| ((MulExp) getChild(2)).getType();
|
||||
}
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
if (getChild(0) instanceof MulExp) {
|
||||
return ((MulExp) getChild(0)).getValue();
|
||||
} else {
|
||||
int left = ((AddExp) getChild(0)).getValue();
|
||||
int right = ((MulExp) getChild(2)).getValue();
|
||||
if (((TokenNode) getChild(1)).getType() == TokenType.PLUS) {
|
||||
return left + right;
|
||||
} else {
|
||||
return left - right;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.NodeStack;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import midend.symbol.SymbolManager;
|
||||
import error.Errors;
|
||||
|
||||
public class AddExp extends Node {
|
||||
public AddExp(TokenStream ts) {
|
||||
super(SyntaxType.ADD_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
NodeStack stack = new NodeStack();
|
||||
while (true) {
|
||||
MulExp mep = new MulExp(this.ts);
|
||||
mep.parse(errors);
|
||||
stack.push(mep);
|
||||
if (isAddOp()) {
|
||||
stack.push(new TokenNode(ts)); // addop
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (stack.size() == 1) {
|
||||
this.addChild((MulExp) stack.pop());
|
||||
} else {
|
||||
AddExp temp = this;
|
||||
while (stack.size() > 1) {
|
||||
AddExp ae = new AddExp(this.ts);
|
||||
MulExp mep = (MulExp) stack.pop();
|
||||
TokenNode op = (TokenNode) stack.pop();
|
||||
temp.addChild(ae);
|
||||
temp.addChild(op);
|
||||
temp.addChild(mep);
|
||||
temp = ae;
|
||||
}
|
||||
temp.addChild((MulExp) stack.pop());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAddOp() {
|
||||
TokenType t = getCurrToken().getType();
|
||||
return t == TokenType.PLUS || t == TokenType.MINU;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
if (getChildren().size() == 1) {
|
||||
return ((MulExp) getChild(0)).getType();
|
||||
} else {
|
||||
return ((AddExp) getChild(0)).getType()
|
||||
| ((MulExp) getChild(2)).getType();
|
||||
}
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
if (getChild(0) instanceof MulExp) {
|
||||
return ((MulExp) getChild(0)).getValue();
|
||||
} else {
|
||||
int left = ((AddExp) getChild(0)).getValue();
|
||||
int right = ((MulExp) getChild(2)).getValue();
|
||||
if (((TokenNode) getChild(1)).getType() == TokenType.PLUS) {
|
||||
return left + right;
|
||||
} else {
|
||||
return left - right;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
36
frontend/ast/exp/Cond.java
Normal file → Executable file
36
frontend/ast/exp/Cond.java
Normal file → Executable file
@@ -1,18 +1,18 @@
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.SyntaxType;
|
||||
import error.Errors;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.ast.Node;
|
||||
|
||||
public class Cond extends Node {
|
||||
public Cond(TokenStream ts) {
|
||||
super(SyntaxType.COND_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
LOrExp lep = new LOrExp(this.ts);
|
||||
lep.parse(errors);
|
||||
addChild(lep);
|
||||
}
|
||||
}
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.SyntaxType;
|
||||
import error.Errors;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.ast.Node;
|
||||
|
||||
public class Cond extends Node {
|
||||
public Cond(TokenStream ts) {
|
||||
super(SyntaxType.COND_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
LOrExp lep = new LOrExp(this.ts);
|
||||
lep.parse(errors);
|
||||
addChild(lep);
|
||||
}
|
||||
}
|
||||
|
||||
0
frontend/ast/exp/ConstExp.java
Normal file → Executable file
0
frontend/ast/exp/ConstExp.java
Normal file → Executable file
98
frontend/ast/exp/EqExp.java
Normal file → Executable file
98
frontend/ast/exp/EqExp.java
Normal file → Executable file
@@ -1,49 +1,49 @@
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.NodeStack;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import error.Errors;
|
||||
|
||||
public class EqExp extends Node {
|
||||
public EqExp(TokenStream ts) {
|
||||
super(SyntaxType.EQ_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
NodeStack stack = new NodeStack();
|
||||
while (true) {
|
||||
RelExp relexp = new RelExp(this.ts);
|
||||
relexp.parse(errors);
|
||||
stack.push(relexp);
|
||||
if (isEqOp()) {
|
||||
stack.push(new TokenNode(ts)); // eqop
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(stack.size() == 1) {
|
||||
this.addChild((RelExp)stack.pop());
|
||||
} else {
|
||||
EqExp temp = this;
|
||||
while (stack.size() > 1) {
|
||||
EqExp eep = new EqExp(this.ts);
|
||||
RelExp relexp = (RelExp)stack.pop();
|
||||
TokenNode eqop = (TokenNode)stack.pop();
|
||||
temp.addChild(eep);
|
||||
temp.addChild(eqop);
|
||||
temp.addChild(relexp);
|
||||
temp = eep;
|
||||
}
|
||||
temp.addChild((RelExp)stack.pop());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEqOp() {
|
||||
TokenType t = getCurrToken().getType();
|
||||
return t == TokenType.EQL || t == TokenType.NEQ;
|
||||
}
|
||||
}
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.NodeStack;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import error.Errors;
|
||||
|
||||
public class EqExp extends Node {
|
||||
public EqExp(TokenStream ts) {
|
||||
super(SyntaxType.EQ_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
NodeStack stack = new NodeStack();
|
||||
while (true) {
|
||||
RelExp relexp = new RelExp(this.ts);
|
||||
relexp.parse(errors);
|
||||
stack.push(relexp);
|
||||
if (isEqOp()) {
|
||||
stack.push(new TokenNode(ts)); // eqop
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(stack.size() == 1) {
|
||||
this.addChild((RelExp)stack.pop());
|
||||
} else {
|
||||
EqExp temp = this;
|
||||
while (stack.size() > 1) {
|
||||
EqExp eep = new EqExp(this.ts);
|
||||
RelExp relexp = (RelExp)stack.pop();
|
||||
TokenNode eqop = (TokenNode)stack.pop();
|
||||
temp.addChild(eep);
|
||||
temp.addChild(eqop);
|
||||
temp.addChild(relexp);
|
||||
temp = eep;
|
||||
}
|
||||
temp.addChild((RelExp)stack.pop());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isEqOp() {
|
||||
TokenType t = getCurrToken().getType();
|
||||
return t == TokenType.EQL || t == TokenType.NEQ;
|
||||
}
|
||||
}
|
||||
|
||||
0
frontend/ast/exp/Exp.java
Normal file → Executable file
0
frontend/ast/exp/Exp.java
Normal file → Executable file
88
frontend/ast/exp/LAndExp.java
Normal file → Executable file
88
frontend/ast/exp/LAndExp.java
Normal file → Executable file
@@ -1,44 +1,44 @@
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.NodeStack;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import error.Errors;
|
||||
|
||||
public class LAndExp extends Node {
|
||||
public LAndExp(TokenStream ts) {
|
||||
super(SyntaxType.LAND_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
NodeStack stack = new NodeStack();
|
||||
while (true) {
|
||||
EqExp eep = new EqExp(this.ts);
|
||||
eep.parse(errors);
|
||||
stack.push(eep);
|
||||
if (getCurrToken().getType() == TokenType.AND) {
|
||||
stack.push(new TokenNode(ts)); // landop
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (stack.size() == 1) {
|
||||
this.addChild((EqExp)stack.pop());
|
||||
} else {
|
||||
LAndExp temp = this;
|
||||
while(stack.size() > 1) {
|
||||
LAndExp lae = new LAndExp(this.ts);
|
||||
EqExp eep = (EqExp)stack.pop();
|
||||
TokenNode landop = (TokenNode)stack.pop();
|
||||
temp.addChild(lae);
|
||||
temp.addChild(landop);
|
||||
temp.addChild(eep);
|
||||
temp = lae;
|
||||
}
|
||||
temp.addChild((EqExp)stack.pop());
|
||||
}
|
||||
}
|
||||
}
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.NodeStack;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import error.Errors;
|
||||
|
||||
public class LAndExp extends Node {
|
||||
public LAndExp(TokenStream ts) {
|
||||
super(SyntaxType.LAND_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
NodeStack stack = new NodeStack();
|
||||
while (true) {
|
||||
EqExp eep = new EqExp(this.ts);
|
||||
eep.parse(errors);
|
||||
stack.push(eep);
|
||||
if (getCurrToken().getType() == TokenType.AND) {
|
||||
stack.push(new TokenNode(ts)); // landop
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (stack.size() == 1) {
|
||||
this.addChild((EqExp)stack.pop());
|
||||
} else {
|
||||
LAndExp temp = this;
|
||||
while(stack.size() > 1) {
|
||||
LAndExp lae = new LAndExp(this.ts);
|
||||
EqExp eep = (EqExp)stack.pop();
|
||||
TokenNode landop = (TokenNode)stack.pop();
|
||||
temp.addChild(lae);
|
||||
temp.addChild(landop);
|
||||
temp.addChild(eep);
|
||||
temp = lae;
|
||||
}
|
||||
temp.addChild((EqExp)stack.pop());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
88
frontend/ast/exp/LOrExp.java
Normal file → Executable file
88
frontend/ast/exp/LOrExp.java
Normal file → Executable file
@@ -1,44 +1,44 @@
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.NodeStack;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import error.Errors;
|
||||
|
||||
public class LOrExp extends Node {
|
||||
public LOrExp(TokenStream ts) {
|
||||
super(SyntaxType.LOR_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
NodeStack stack = new NodeStack();
|
||||
while (true) {
|
||||
LAndExp andexp = new LAndExp(this.ts);
|
||||
andexp.parse(errors);
|
||||
stack.push(andexp);
|
||||
if (getCurrToken().getType() == TokenType.OR) {
|
||||
stack.push(new TokenNode(ts)); // lorop
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (stack.size() == 1) {
|
||||
this.addChild((LAndExp)stack.pop());
|
||||
} else {
|
||||
LOrExp temp = this;
|
||||
while(stack.size() > 1) {
|
||||
LOrExp loe = new LOrExp(this.ts);
|
||||
LAndExp lae = (LAndExp)stack.pop();
|
||||
TokenNode lorop = (TokenNode)stack.pop();
|
||||
temp.addChild(loe);
|
||||
temp.addChild(lorop);
|
||||
temp.addChild(lae);
|
||||
temp = loe;
|
||||
}
|
||||
temp.addChild((LAndExp)stack.pop());
|
||||
}
|
||||
}
|
||||
}
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.NodeStack;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import error.Errors;
|
||||
|
||||
public class LOrExp extends Node {
|
||||
public LOrExp(TokenStream ts) {
|
||||
super(SyntaxType.LOR_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
NodeStack stack = new NodeStack();
|
||||
while (true) {
|
||||
LAndExp andexp = new LAndExp(this.ts);
|
||||
andexp.parse(errors);
|
||||
stack.push(andexp);
|
||||
if (getCurrToken().getType() == TokenType.OR) {
|
||||
stack.push(new TokenNode(ts)); // lorop
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (stack.size() == 1) {
|
||||
this.addChild((LAndExp)stack.pop());
|
||||
} else {
|
||||
LOrExp temp = this;
|
||||
while(stack.size() > 1) {
|
||||
LOrExp loe = new LOrExp(this.ts);
|
||||
LAndExp lae = (LAndExp)stack.pop();
|
||||
TokenNode lorop = (TokenNode)stack.pop();
|
||||
temp.addChild(loe);
|
||||
temp.addChild(lorop);
|
||||
temp.addChild(lae);
|
||||
temp = loe;
|
||||
}
|
||||
temp.addChild((LAndExp)stack.pop());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
118
frontend/ast/exp/LVal.java
Normal file → Executable file
118
frontend/ast/exp/LVal.java
Normal file → Executable file
@@ -1,59 +1,59 @@
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import midend.symbol.SymbolManager;
|
||||
import midend.symbol.Symbol;
|
||||
import midend.symbol.ArraySymbol;
|
||||
import error.Errors;
|
||||
import error.Error;
|
||||
import error.ErrorType;
|
||||
|
||||
public class LVal extends Node {
|
||||
public LVal(TokenStream ts) {
|
||||
super(SyntaxType.LVAL_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
addChild(new TokenNode(this.ts)); // idenfr
|
||||
if (getCurrToken().getType() == TokenType.LBRACK) {
|
||||
addChild(new TokenNode(this.ts)); // lbrack
|
||||
Exp exp = new Exp(this.ts);
|
||||
exp.parse(errors);
|
||||
addChild(exp);
|
||||
if (getCurrToken().getType() == TokenType.RBRACK) {
|
||||
addChild(new TokenNode(this.ts)); // rbrack
|
||||
} else {
|
||||
errors.addError(new Error(this.ts.peek(-1).getLine(), ErrorType.k));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
if (getChildren().size() == 1) {
|
||||
TokenNode idenfr = (TokenNode) getChild(0);
|
||||
Symbol symbol = SymbolManager.getSymbol(idenfr.getName());
|
||||
if (symbol instanceof ArraySymbol) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
TokenNode idenfr = (TokenNode) getChild(0); //idenfr一定是个常量,可在符号表找到且有值
|
||||
if (getChildren().size() == 1) {
|
||||
Symbol symbol = SymbolManager.getSymbol(idenfr.getName());
|
||||
return symbol.getValue(0);
|
||||
} else {
|
||||
int index = ((Exp) getChild(2)).getValue();
|
||||
Symbol symbol = SymbolManager.getSymbol(idenfr.getName());
|
||||
return symbol.getValue(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import midend.symbol.SymbolManager;
|
||||
import midend.symbol.Symbol;
|
||||
import midend.symbol.ArraySymbol;
|
||||
import error.Errors;
|
||||
import error.Error;
|
||||
import error.ErrorType;
|
||||
|
||||
public class LVal extends Node {
|
||||
public LVal(TokenStream ts) {
|
||||
super(SyntaxType.LVAL_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
addChild(new TokenNode(this.ts)); // idenfr
|
||||
if (getCurrToken().getType() == TokenType.LBRACK) {
|
||||
addChild(new TokenNode(this.ts)); // lbrack
|
||||
Exp exp = new Exp(this.ts);
|
||||
exp.parse(errors);
|
||||
addChild(exp);
|
||||
if (getCurrToken().getType() == TokenType.RBRACK) {
|
||||
addChild(new TokenNode(this.ts)); // rbrack
|
||||
} else {
|
||||
errors.addError(new Error(this.ts.peek(-1).getLine(), ErrorType.k));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
if (getChildren().size() == 1) {
|
||||
TokenNode idenfr = (TokenNode) getChild(0);
|
||||
Symbol symbol = SymbolManager.getSymbol(idenfr.getName());
|
||||
if (symbol instanceof ArraySymbol) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
TokenNode idenfr = (TokenNode) getChild(0); //idenfr一定是个常量,可在符号表找到且有值
|
||||
if (getChildren().size() == 1) {
|
||||
Symbol symbol = SymbolManager.getSymbol(idenfr.getName());
|
||||
return symbol.getValue(0);
|
||||
} else {
|
||||
int index = ((Exp) getChild(2)).getValue();
|
||||
Symbol symbol = SymbolManager.getSymbol(idenfr.getName());
|
||||
return symbol.getValue(index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
150
frontend/ast/exp/MulExp.java
Normal file → Executable file
150
frontend/ast/exp/MulExp.java
Normal file → Executable file
@@ -1,75 +1,75 @@
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import frontend.ast.NodeStack;
|
||||
import midend.symbol.SymbolManager;
|
||||
import error.Errors;
|
||||
|
||||
public class MulExp extends Node {
|
||||
public MulExp(TokenStream ts) {
|
||||
super(SyntaxType.MUL_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
NodeStack stack = new NodeStack();
|
||||
while (true) {
|
||||
UnaryExp uep = new UnaryExp(this.ts);
|
||||
uep.parse(errors);
|
||||
stack.push(uep);
|
||||
if (isMulOp()) {
|
||||
stack.push(new TokenNode(ts)); // mulop
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (stack.size() == 1) {
|
||||
addChild((UnaryExp)stack.pop());
|
||||
} else {
|
||||
MulExp temp = this;
|
||||
while (stack.size() > 1) {
|
||||
MulExp mep = new MulExp(this.ts);
|
||||
UnaryExp uep = (UnaryExp) stack.pop();
|
||||
TokenNode mulop = (TokenNode) stack.pop();
|
||||
temp.addChild(mep);
|
||||
temp.addChild(mulop);
|
||||
temp.addChild(uep);
|
||||
temp = mep;
|
||||
}
|
||||
temp.addChild((UnaryExp)stack.pop());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isMulOp() {
|
||||
TokenType t = getCurrToken().getType();
|
||||
return t == TokenType.MULT || t == TokenType.DIV || t == TokenType.MOD;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
if (getChildren().size() == 1) {
|
||||
return ((UnaryExp) getChild(0)).getType();
|
||||
} else {
|
||||
return ((MulExp) getChild(0)).getType()
|
||||
| ((UnaryExp) getChild(2)).getType();
|
||||
}
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
if (getChild(0) instanceof UnaryExp) {
|
||||
return ((UnaryExp) getChild(0)).getValue();
|
||||
} else {
|
||||
int left = ((MulExp) getChild(0)).getValue();
|
||||
int right = ((UnaryExp) getChild(2)).getValue();
|
||||
if (((TokenNode) getChild(1)).getType() == TokenType.MULT) {
|
||||
return left * right;
|
||||
} else if (((TokenNode) getChild(1)).getType() == TokenType.DIV) {
|
||||
return left / right;
|
||||
} else {
|
||||
return left % right;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import frontend.ast.NodeStack;
|
||||
import midend.symbol.SymbolManager;
|
||||
import error.Errors;
|
||||
|
||||
public class MulExp extends Node {
|
||||
public MulExp(TokenStream ts) {
|
||||
super(SyntaxType.MUL_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
NodeStack stack = new NodeStack();
|
||||
while (true) {
|
||||
UnaryExp uep = new UnaryExp(this.ts);
|
||||
uep.parse(errors);
|
||||
stack.push(uep);
|
||||
if (isMulOp()) {
|
||||
stack.push(new TokenNode(ts)); // mulop
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (stack.size() == 1) {
|
||||
addChild((UnaryExp)stack.pop());
|
||||
} else {
|
||||
MulExp temp = this;
|
||||
while (stack.size() > 1) {
|
||||
MulExp mep = new MulExp(this.ts);
|
||||
UnaryExp uep = (UnaryExp) stack.pop();
|
||||
TokenNode mulop = (TokenNode) stack.pop();
|
||||
temp.addChild(mep);
|
||||
temp.addChild(mulop);
|
||||
temp.addChild(uep);
|
||||
temp = mep;
|
||||
}
|
||||
temp.addChild((UnaryExp)stack.pop());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isMulOp() {
|
||||
TokenType t = getCurrToken().getType();
|
||||
return t == TokenType.MULT || t == TokenType.DIV || t == TokenType.MOD;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
if (getChildren().size() == 1) {
|
||||
return ((UnaryExp) getChild(0)).getType();
|
||||
} else {
|
||||
return ((MulExp) getChild(0)).getType()
|
||||
| ((UnaryExp) getChild(2)).getType();
|
||||
}
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
if (getChild(0) instanceof UnaryExp) {
|
||||
return ((UnaryExp) getChild(0)).getValue();
|
||||
} else {
|
||||
int left = ((MulExp) getChild(0)).getValue();
|
||||
int right = ((UnaryExp) getChild(2)).getValue();
|
||||
if (((TokenNode) getChild(1)).getType() == TokenType.MULT) {
|
||||
return left * right;
|
||||
} else if (((TokenNode) getChild(1)).getType() == TokenType.DIV) {
|
||||
return left / right;
|
||||
} else {
|
||||
return left % right;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
42
frontend/ast/exp/NumberExp.java
Normal file → Executable file
42
frontend/ast/exp/NumberExp.java
Normal file → Executable file
@@ -1,21 +1,21 @@
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import error.Errors;
|
||||
import frontend.ast.token.TokenNode;
|
||||
|
||||
public class NumberExp extends Node {
|
||||
public NumberExp(TokenStream ts) {
|
||||
super(SyntaxType.NUMBER, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
addChild(new TokenNode(ts)); //intconst
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return Integer.parseInt(((TokenNode) getChild(0)).getValue());
|
||||
}
|
||||
}
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import error.Errors;
|
||||
import frontend.ast.token.TokenNode;
|
||||
|
||||
public class NumberExp extends Node {
|
||||
public NumberExp(TokenStream ts) {
|
||||
super(SyntaxType.NUMBER, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
addChild(new TokenNode(ts)); //intconst
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return Integer.parseInt(((TokenNode) getChild(0)).getValue());
|
||||
}
|
||||
}
|
||||
|
||||
116
frontend/ast/exp/PrimaryExp.java
Normal file → Executable file
116
frontend/ast/exp/PrimaryExp.java
Normal file → Executable file
@@ -1,58 +1,58 @@
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import error.Errors;
|
||||
import error.Error;
|
||||
import error.ErrorType;
|
||||
|
||||
public class PrimaryExp extends Node {
|
||||
public PrimaryExp(TokenStream ts) {
|
||||
super(SyntaxType.PRIMARY_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
if (getCurrToken().getType() == TokenType.LPARENT) {
|
||||
addChild(new TokenNode(ts)); // lparent
|
||||
Exp exp = new Exp(this.ts);
|
||||
exp.parse(errors);
|
||||
addChild(exp);
|
||||
if (getCurrToken().getType() == TokenType.RPARENT) {
|
||||
addChild(new TokenNode(ts)); // rparent
|
||||
} else {
|
||||
errors.addError(new Error(this.ts.peek(-1).getLine(), ErrorType.j));
|
||||
}
|
||||
} else if (getCurrToken().getType() == TokenType.IDENFR) {
|
||||
LVal lval = new LVal(this.ts);
|
||||
lval.parse(errors);
|
||||
addChild(lval);
|
||||
} else {
|
||||
NumberExp num = new NumberExp(this.ts);
|
||||
num.parse(errors);
|
||||
addChild(num);
|
||||
}
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
if (getChild(0) instanceof TokenNode) {
|
||||
return ((Exp) getChild(1)).getType();
|
||||
} else if (getChild(0) instanceof LVal) {
|
||||
return ((LVal) getChild(0)).getType();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
if (getChild(0) instanceof TokenNode) {
|
||||
return ((Exp) getChild(1)).getValue();
|
||||
} else if (getChild(0) instanceof LVal) {
|
||||
return ((LVal) getChild(0)).getValue();
|
||||
} else {
|
||||
return ((NumberExp) getChild(0)).getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import error.Errors;
|
||||
import error.Error;
|
||||
import error.ErrorType;
|
||||
|
||||
public class PrimaryExp extends Node {
|
||||
public PrimaryExp(TokenStream ts) {
|
||||
super(SyntaxType.PRIMARY_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
if (getCurrToken().getType() == TokenType.LPARENT) {
|
||||
addChild(new TokenNode(ts)); // lparent
|
||||
Exp exp = new Exp(this.ts);
|
||||
exp.parse(errors);
|
||||
addChild(exp);
|
||||
if (getCurrToken().getType() == TokenType.RPARENT) {
|
||||
addChild(new TokenNode(ts)); // rparent
|
||||
} else {
|
||||
errors.addError(new Error(this.ts.peek(-1).getLine(), ErrorType.j));
|
||||
}
|
||||
} else if (getCurrToken().getType() == TokenType.IDENFR) {
|
||||
LVal lval = new LVal(this.ts);
|
||||
lval.parse(errors);
|
||||
addChild(lval);
|
||||
} else {
|
||||
NumberExp num = new NumberExp(this.ts);
|
||||
num.parse(errors);
|
||||
addChild(num);
|
||||
}
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
if (getChild(0) instanceof TokenNode) {
|
||||
return ((Exp) getChild(1)).getType();
|
||||
} else if (getChild(0) instanceof LVal) {
|
||||
return ((LVal) getChild(0)).getType();
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
if (getChild(0) instanceof TokenNode) {
|
||||
return ((Exp) getChild(1)).getValue();
|
||||
} else if (getChild(0) instanceof LVal) {
|
||||
return ((LVal) getChild(0)).getValue();
|
||||
} else {
|
||||
return ((NumberExp) getChild(0)).getValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
100
frontend/ast/exp/RelExp.java
Normal file → Executable file
100
frontend/ast/exp/RelExp.java
Normal file → Executable file
@@ -1,50 +1,50 @@
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.NodeStack;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import error.Errors;
|
||||
|
||||
public class RelExp extends Node {
|
||||
public RelExp(TokenStream ts) {
|
||||
super(SyntaxType.REL_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
NodeStack stack = new NodeStack();
|
||||
while (true) {
|
||||
AddExp addexp = new AddExp(this.ts);
|
||||
addexp.parse(errors);
|
||||
stack.push(addexp);
|
||||
if (isRelOp()) {
|
||||
stack.push(new TokenNode(ts)); // relop
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (stack.size() == 1) {
|
||||
this.addChild((AddExp)stack.pop());
|
||||
} else {
|
||||
RelExp temp = this;
|
||||
while(stack.size() > 1) {
|
||||
RelExp rexp = new RelExp(this.ts);
|
||||
AddExp addexp = (AddExp)stack.pop();
|
||||
TokenNode relop = (TokenNode)stack.pop();
|
||||
temp.addChild(rexp);
|
||||
temp.addChild(relop);
|
||||
temp.addChild(addexp);
|
||||
temp = rexp;
|
||||
}
|
||||
temp.addChild((AddExp)stack.pop());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isRelOp() {
|
||||
TokenType t = getCurrToken().getType();
|
||||
return t == TokenType.LSS || t == TokenType.GRE
|
||||
|| t == TokenType.LEQ || t == TokenType.GEQ;
|
||||
}
|
||||
}
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.NodeStack;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import error.Errors;
|
||||
|
||||
public class RelExp extends Node {
|
||||
public RelExp(TokenStream ts) {
|
||||
super(SyntaxType.REL_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
NodeStack stack = new NodeStack();
|
||||
while (true) {
|
||||
AddExp addexp = new AddExp(this.ts);
|
||||
addexp.parse(errors);
|
||||
stack.push(addexp);
|
||||
if (isRelOp()) {
|
||||
stack.push(new TokenNode(ts)); // relop
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (stack.size() == 1) {
|
||||
this.addChild((AddExp)stack.pop());
|
||||
} else {
|
||||
RelExp temp = this;
|
||||
while(stack.size() > 1) {
|
||||
RelExp rexp = new RelExp(this.ts);
|
||||
AddExp addexp = (AddExp)stack.pop();
|
||||
TokenNode relop = (TokenNode)stack.pop();
|
||||
temp.addChild(rexp);
|
||||
temp.addChild(relop);
|
||||
temp.addChild(addexp);
|
||||
temp = rexp;
|
||||
}
|
||||
temp.addChild((AddExp)stack.pop());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isRelOp() {
|
||||
TokenType t = getCurrToken().getType();
|
||||
return t == TokenType.LSS || t == TokenType.GRE
|
||||
|| t == TokenType.LEQ || t == TokenType.GEQ;
|
||||
}
|
||||
}
|
||||
|
||||
248
frontend/ast/exp/UnaryExp.java
Normal file → Executable file
248
frontend/ast/exp/UnaryExp.java
Normal file → Executable file
@@ -1,124 +1,124 @@
|
||||
package frontend.ast.exp;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import frontend.ast.func.FuncRParams;
|
||||
import midend.symbol.FuncSymbol;
|
||||
import midend.symbol.SymbolManager;
|
||||
import error.Errors;
|
||||
|
||||
import error.Error;
|
||||
import error.ErrorType;
|
||||
|
||||
public class UnaryExp extends Node {
|
||||
public UnaryExp(TokenStream ts) {
|
||||
super(SyntaxType.UNARY_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
if (isUnaryOp()) {
|
||||
UnaryOp uop = new UnaryOp(ts);
|
||||
uop.parse(errors);
|
||||
addChild(uop);
|
||||
UnaryExp uep = new UnaryExp(ts);
|
||||
uep.parse(errors);
|
||||
addChild(uep);
|
||||
} else if (getCurrToken().getType() == TokenType.IDENFR
|
||||
&& this.ts.peek(1).getType() == TokenType.LPARENT) {
|
||||
addChild(new TokenNode(ts)); // idenfr
|
||||
addChild(new TokenNode(ts)); // lparent
|
||||
if (isExp()) {
|
||||
FuncRParams frp = new FuncRParams(ts);
|
||||
frp.parse(errors);
|
||||
addChild(frp);
|
||||
}
|
||||
if (getCurrToken().getType() == TokenType.RPARENT) {
|
||||
addChild(new TokenNode(ts)); // rparent
|
||||
} else {
|
||||
errors.addError(new Error(this.ts.peek(-1).getLine(), ErrorType.j));
|
||||
}
|
||||
} else {
|
||||
PrimaryExp pme = new PrimaryExp(ts);
|
||||
pme.parse(errors);
|
||||
addChild(pme);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isUnaryOp() {
|
||||
return getCurrToken().getType() == TokenType.PLUS
|
||||
|| getCurrToken().getType() == TokenType.MINU
|
||||
|| getCurrToken().getType() == TokenType.NOT;
|
||||
}
|
||||
|
||||
public boolean isExp() {
|
||||
TokenType t = getCurrToken().getType();
|
||||
return t == TokenType.PLUS || t == TokenType.MINU || t == TokenType.NOT
|
||||
|| t == TokenType.IDENFR || t == TokenType.LPARENT || t == TokenType.INTCON;
|
||||
}
|
||||
|
||||
public void handleFuncCall(Errors errors) { // 当其为函数调用形式时才会调用
|
||||
TokenNode funcIdenfr = (TokenNode) getChild(0);
|
||||
if (funcIdenfr.getName().equals("getint")) {
|
||||
if (getChildren().size() == 4) {
|
||||
errors.addError(new Error(funcIdenfr.getLine(), ErrorType.d));
|
||||
}
|
||||
return;
|
||||
}
|
||||
FuncSymbol funcSymbol = (FuncSymbol) SymbolManager.getSymbol(funcIdenfr.getName());
|
||||
if (funcSymbol == null) {
|
||||
errors.addError(new Error(funcIdenfr.getLine(), ErrorType.c));
|
||||
return;
|
||||
}
|
||||
int fparaNum = funcSymbol.getParamNum();
|
||||
int rparaNum = 0;
|
||||
if (getChildren().size() >= 3 && getChild(2) instanceof FuncRParams) {
|
||||
FuncRParams frp = (FuncRParams) getChild(2);
|
||||
rparaNum = frp.getParamNum();
|
||||
if (rparaNum == fparaNum) {
|
||||
frp.checkParamType(funcSymbol, errors, funcIdenfr.getLine());
|
||||
}
|
||||
}
|
||||
if (fparaNum != rparaNum) {
|
||||
errors.addError(new Error(funcIdenfr.getLine(), ErrorType.d));
|
||||
}
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
if (getChild(0) instanceof PrimaryExp) {
|
||||
return ((PrimaryExp) getChild(0)).getType();
|
||||
} else if (getChild(0) instanceof TokenNode) {
|
||||
return 0;
|
||||
} else {
|
||||
return ((UnaryExp) getChild(1)).getType();
|
||||
}
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
if (getChild(0) instanceof UnaryOp) {
|
||||
UnaryOp uop = (UnaryOp) getChild(0);
|
||||
TokenNode opToken = (TokenNode) uop.getChild(0);
|
||||
return opToken.getType() == TokenType.PLUS ? ((UnaryExp) getChild(1)).getValue()
|
||||
: -((UnaryExp) getChild(1)).getValue();
|
||||
} else if (getChild(0) instanceof PrimaryExp) {
|
||||
return ((PrimaryExp) getChild(0)).getValue();
|
||||
} else {
|
||||
return 0; // 0表示这个是函数,getvalue只是对于常量或常量表达式取值,所以正常情况调用getvalue函数时是不会跳转到这个分支的
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<Exp> getParamList() {
|
||||
if (!(getChild(0) instanceof TokenNode)) {
|
||||
return null;
|
||||
} else {
|
||||
if (getChildren().size() >= 3 && (getChild(2) instanceof FuncRParams)) {
|
||||
return ((FuncRParams) getChild(2)).getParamList();
|
||||
}
|
||||
return new ArrayList<Exp>();
|
||||
}
|
||||
}
|
||||
}
|
||||
package frontend.ast.exp;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import frontend.lexer.TokenType;
|
||||
import frontend.ast.token.TokenNode;
|
||||
import frontend.ast.func.FuncRParams;
|
||||
import midend.symbol.FuncSymbol;
|
||||
import midend.symbol.SymbolManager;
|
||||
import error.Errors;
|
||||
|
||||
import error.Error;
|
||||
import error.ErrorType;
|
||||
|
||||
public class UnaryExp extends Node {
|
||||
public UnaryExp(TokenStream ts) {
|
||||
super(SyntaxType.UNARY_EXP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
if (isUnaryOp()) {
|
||||
UnaryOp uop = new UnaryOp(ts);
|
||||
uop.parse(errors);
|
||||
addChild(uop);
|
||||
UnaryExp uep = new UnaryExp(ts);
|
||||
uep.parse(errors);
|
||||
addChild(uep);
|
||||
} else if (getCurrToken().getType() == TokenType.IDENFR
|
||||
&& this.ts.peek(1).getType() == TokenType.LPARENT) {
|
||||
addChild(new TokenNode(ts)); // idenfr
|
||||
addChild(new TokenNode(ts)); // lparent
|
||||
if (isExp()) {
|
||||
FuncRParams frp = new FuncRParams(ts);
|
||||
frp.parse(errors);
|
||||
addChild(frp);
|
||||
}
|
||||
if (getCurrToken().getType() == TokenType.RPARENT) {
|
||||
addChild(new TokenNode(ts)); // rparent
|
||||
} else {
|
||||
errors.addError(new Error(this.ts.peek(-1).getLine(), ErrorType.j));
|
||||
}
|
||||
} else {
|
||||
PrimaryExp pme = new PrimaryExp(ts);
|
||||
pme.parse(errors);
|
||||
addChild(pme);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isUnaryOp() {
|
||||
return getCurrToken().getType() == TokenType.PLUS
|
||||
|| getCurrToken().getType() == TokenType.MINU
|
||||
|| getCurrToken().getType() == TokenType.NOT;
|
||||
}
|
||||
|
||||
public boolean isExp() {
|
||||
TokenType t = getCurrToken().getType();
|
||||
return t == TokenType.PLUS || t == TokenType.MINU || t == TokenType.NOT
|
||||
|| t == TokenType.IDENFR || t == TokenType.LPARENT || t == TokenType.INTCON;
|
||||
}
|
||||
|
||||
public void handleFuncCall(Errors errors) { // 当其为函数调用形式时才会调用
|
||||
TokenNode funcIdenfr = (TokenNode) getChild(0);
|
||||
if (funcIdenfr.getName().equals("getint")) {
|
||||
if (getChildren().size() == 4) {
|
||||
errors.addError(new Error(funcIdenfr.getLine(), ErrorType.d));
|
||||
}
|
||||
return;
|
||||
}
|
||||
FuncSymbol funcSymbol = (FuncSymbol) SymbolManager.getSymbol(funcIdenfr.getName());
|
||||
if (funcSymbol == null) {
|
||||
errors.addError(new Error(funcIdenfr.getLine(), ErrorType.c));
|
||||
return;
|
||||
}
|
||||
int fparaNum = funcSymbol.getParamNum();
|
||||
int rparaNum = 0;
|
||||
if (getChildren().size() >= 3 && getChild(2) instanceof FuncRParams) {
|
||||
FuncRParams frp = (FuncRParams) getChild(2);
|
||||
rparaNum = frp.getParamNum();
|
||||
if (rparaNum == fparaNum) {
|
||||
frp.checkParamType(funcSymbol, errors, funcIdenfr.getLine());
|
||||
}
|
||||
}
|
||||
if (fparaNum != rparaNum) {
|
||||
errors.addError(new Error(funcIdenfr.getLine(), ErrorType.d));
|
||||
}
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
if (getChild(0) instanceof PrimaryExp) {
|
||||
return ((PrimaryExp) getChild(0)).getType();
|
||||
} else if (getChild(0) instanceof TokenNode) {
|
||||
return 0;
|
||||
} else {
|
||||
return ((UnaryExp) getChild(1)).getType();
|
||||
}
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
if (getChild(0) instanceof UnaryOp) {
|
||||
UnaryOp uop = (UnaryOp) getChild(0);
|
||||
TokenNode opToken = (TokenNode) uop.getChild(0);
|
||||
return opToken.getType() == TokenType.PLUS ? ((UnaryExp) getChild(1)).getValue()
|
||||
: -((UnaryExp) getChild(1)).getValue();
|
||||
} else if (getChild(0) instanceof PrimaryExp) {
|
||||
return ((PrimaryExp) getChild(0)).getValue();
|
||||
} else {
|
||||
return 0; // 0表示这个是函数,getvalue只是对于常量或常量表达式取值,所以正常情况调用getvalue函数时是不会跳转到这个分支的
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<Exp> getParamList() {
|
||||
if (!(getChild(0) instanceof TokenNode)) {
|
||||
return null;
|
||||
} else {
|
||||
if (getChildren().size() >= 3 && (getChild(2) instanceof FuncRParams)) {
|
||||
return ((FuncRParams) getChild(2)).getParamList();
|
||||
}
|
||||
return new ArrayList<Exp>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
34
frontend/ast/exp/UnaryOp.java
Normal file → Executable file
34
frontend/ast/exp/UnaryOp.java
Normal file → Executable file
@@ -1,17 +1,17 @@
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import error.Errors;
|
||||
import frontend.ast.token.TokenNode;
|
||||
|
||||
public class UnaryOp extends Node {
|
||||
public UnaryOp(TokenStream ts) {
|
||||
super(SyntaxType.UNARY_OP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
addChild(new TokenNode(ts)); // unary op
|
||||
}
|
||||
}
|
||||
package frontend.ast.exp;
|
||||
|
||||
import frontend.ast.Node;
|
||||
import frontend.ast.SyntaxType;
|
||||
import frontend.lexer.TokenStream;
|
||||
import error.Errors;
|
||||
import frontend.ast.token.TokenNode;
|
||||
|
||||
public class UnaryOp extends Node {
|
||||
public UnaryOp(TokenStream ts) {
|
||||
super(SyntaxType.UNARY_OP, ts);
|
||||
}
|
||||
|
||||
public void parse(Errors errors) {
|
||||
addChild(new TokenNode(ts)); // unary op
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user