add char and boolean values to ast
This commit is contained in:
parent
bc984a89e1
commit
23f541ff7d
@ -16,3 +16,12 @@ class Example2 {
|
||||
return n;
|
||||
}
|
||||
}
|
||||
|
||||
class Example3 {
|
||||
char isNice;
|
||||
char m(int n) {
|
||||
boolean l;
|
||||
l = false;
|
||||
return 't';
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,27 @@
|
||||
package abstractSyntaxTree.Expression;
|
||||
|
||||
import TypeCheck.AbstractType;
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public class BooleanConstantExpression extends AbstractType implements IExpression{
|
||||
public boolean value;
|
||||
|
||||
public BooleanConstantExpression(boolean value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package abstractSyntaxTree.Expression;
|
||||
|
||||
import TypeCheck.AbstractType;
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public class CharConstantExpression extends AbstractType implements IExpression{
|
||||
public char value;
|
||||
|
||||
public CharConstantExpression(char value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
|
||||
}
|
||||
}
|
@ -132,7 +132,8 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
Node statement = visitStatement(stmt);
|
||||
stmts.add((IStatement) statement);
|
||||
}
|
||||
return new BlockStatement(stmts, "void");
|
||||
String returnType = getReturnType(stmts);
|
||||
return new BlockStatement(stmts, returnType);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -191,12 +192,11 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
return super.visitMethodCall(ctx);
|
||||
}
|
||||
|
||||
//add the expression list later to the constructor
|
||||
@Override
|
||||
public Node visitNewDecl(DecafParser.NewDeclContext ctx) {
|
||||
String name = ctx.Identifier().getText();
|
||||
List<IExpression> expressions = generateExpressions(ctx.argumentList());
|
||||
return new NewStatementExpression(name);
|
||||
return new NewStatementExpression(name, expressions);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -216,7 +216,7 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
} else if (ctx.nonCalcExpr() != null) {
|
||||
return visitNonCalcExpr(ctx.nonCalcExpr());
|
||||
} else if (ctx.value() != null) {
|
||||
//todo
|
||||
return visitValue(ctx.value());
|
||||
} else if (ctx.binaryExpr() != null) {
|
||||
//todo
|
||||
}
|
||||
@ -258,6 +258,8 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
return new LocalVarIdentifier(identifier);
|
||||
} else if(ctx.instVar() != null) {
|
||||
return visitInstVar(ctx.instVar());
|
||||
} else if(ctx.calcExpr() != null) {
|
||||
return visitCalcExpr(ctx.calcExpr());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -307,4 +309,28 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
}
|
||||
return expressions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node visitValue(DecafParser.ValueContext ctx) {
|
||||
if (ctx.IntValue() != null) {
|
||||
return new IntConstantExpression(Integer.parseInt(ctx.IntValue().getText()));
|
||||
} else if (ctx.BooleanValue() != null) {
|
||||
return new BooleanConstantExpression(Boolean.parseBoolean(ctx.BooleanValue().getText()));
|
||||
} else if (ctx.CharValue() != null) {
|
||||
String value = ctx.CharValue().toString();
|
||||
return new CharConstantExpression(value.charAt(1));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getReturnType(List<IStatement> statements){
|
||||
for(IStatement stmt: statements) {
|
||||
if(stmt instanceof ReturnStatement) {
|
||||
return "not";
|
||||
}
|
||||
}
|
||||
return "void";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user