Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import utils.CompileTimeErrors;
- import semanticlib.SemType;
- import semanticlib.SymbolTable;
- aspect TypeAnalysis {
- void ASTNode.typeCheck(CompileTimeErrors errs) {
- for (int i = 0; i < getNumChild(); i++) {
- getChild(i).typeCheck(errs);
- }
- }
- public void Start.typeCheck(CompileTimeErrors errs) {
- for (int i = 0; i < getNumChild(); i++) {
- getChild(i).typeCheck(errs);
- }
- }
- SemType Type.type() {
- if (getTYPE().equals(SemType.INTEGER.toString()))
- return SemType.INTEGER;
- else if (getTYPE().equals(SemType.BOOLEAN.toString()))
- return SemType.BOOLEAN;
- else if (getTYPE().equals(SemType.VOID.toString()))
- return SemType.VOID;
- else
- return SemType.UNKNOWN;
- }
- SemType Expr.type() {
- return SemType.UNKNOWN;
- }
- SemType BoolExpr.type() {
- return SemType.BOOLEAN;
- }
- SemType IntExpr.type() {
- return SemType.INTEGER;
- }
- SemType MethodExpr.type() {
- return getIdUse().type;
- }
- void ReturnStmt.typeCheck(CompileTimeErrors errs) {
- ASTNode parent = getParent();
- while (!(parent instanceof DeclMethod) && parent != null) {
- parent = parent.getParent();
- }
- if (parent instanceof DeclMethod) {
- DeclMethod dm = (DeclMethod)parent;
- if (getIdUse().getID().equals("writeint")) {
- if (hasExpr) {
- errs.add(this, "Return-type is not the same as declared by method (" +
- dm.getType().getTYPE() + ")" +
- " at: " + startLineCol());
- }
- } else if (getIdUse().getID().equals("writeln")) {
- if (hasExpr) {
- errs.add(this, "Return-type is not the same as declared by method (" +
- dm.getType().getTYPE() + ")" +
- " at: " + startLineCol());
- }
- } else if (getIdUse().getID().equals("readint")) {
- if (!hasExpr || !dm.getType().getTYPE().equals(SemType.INTEGER)) {
- errs.add(this, "Return-type is not the same as declared by method (" +
- dm.getType().getTYPE() + ")" +
- " at: " + startLineCol());
- }
- } else if (!hasExpr()) {
- if (!dm.getType().getTYPE().equals(SemType.VOID.toString()))
- errs.add(this, "Return-type is not the same as declared by method (" +
- dm.getType().getTYPE() + ")" +
- " at: " + startLineCol());
- } else if (!dm.getType().getTYPE().equals(getExpr().type().toString())) {
- errs.add(this, "Return-type is not the same as declared by method (" +
- dm.getType().getTYPE() + " vs. " + getExpr().type().toString() + ")" +
- " at: " + startLineCol());
- }
- } else {
- errs.add(this, "No DeclMethod-parent" +
- " at: " + startLineCol());
- }
- }
- void Assignment.typeCheck(CompileTimeErrors errs) {
- SemType type = getIdUse().type;
- if (getExpr().type() != type) {
- errs.add(this, "Not right types in assignments: " + type + " vs. " + getExpr().type() +
- " at: " + startLineCol());
- }
- }
- void VarIntFactor.typeCheck(CompileTimeErrors errs) {
- SemType type = getIdUse().type;
- if (type != SemType.INTEGER) {
- errs.add(this, "Not right types in assignments: " +
- type + " vs. " + SemType.INTEGER +
- " at: " + startLineCol());
- }
- }
- void UseMethod.typeCheck(CompileTimeErrors errs) {
- if (getIdUse().getID().equals("writeint")) {
- if (!hasExprList() || getExprList().getExprs().getNumChild() != 1) {
- errs.add(this, "Incompatible parameter-list for " + getIdUse().getID() + " at: " + startLineCol());
- } else if (getExprList().getExpr(0).type() != SemType.INTEGER) {
- errs.add(this, "Wrong type of parameter, " +
- getExprList().getExpr(0).type() +
- " instead of integer, at: " + startLineCol());
- }
- } else if (getIdUse().getID().equals("writeln")) {
- if (hasExprList()) {
- errs.add(this, "Incompatible parameter-list for " + getIdUse().getID() + " at: " + startLineCol());
- }
- } else if (getIdUse().getID().equals("readint")) {
- if (hasExprList()) {
- errs.add(this, "Incompatible parameter-list for " + getIdUse().getID() + " at: " + startLineCol());
- }
- } else {
- DeclMethod dm = (DeclMethod)getIdUse().decl;
- if (dm != null && hasExprList() != dm.hasParams()) {
- errs.add(this, "Incompatible parameter-list for " + getIdUse().getID() + " at: " + startLineCol());
- }else if (getExprList().getExprs().getNumChild() != dm.getParams().size()) { //get size of DeclMethod
- errs.add(this, "Incorrect number of parameters for " + getIdUse().getID() + " at: " + startLineCol());
- } else{
- for(int i = 0; i < getExprList().size(); i++){ //check types of all parameters
- if(getExprList().getExpr(0).type() != dm.getParams.getNumChild(i).type()){
- errs.add(this, "Wrong type of parameter, " +
- getExprList().getExpr(0).type() +
- " instead of" + dm.getParams.getNumChild(i).type() + ", at: " + startLineCol());
- }
- }
- }
- }
- }
- void MethodExpr.typeCheck(CompileTimeErrors errs) {//chack that the method is used correctly in expressions
- }
- }
Add Comment
Please, Sign In to add comment