code-generator #13

Merged
i22005 merged 29 commits from code-generator into main 2024-07-01 21:14:37 +00:00
4 changed files with 60 additions and 3 deletions
Showing only changes of commit 99948fef07 - Show all commits

View File

@ -3,6 +3,8 @@ package ast.members;
import ast.parameters.ParameterNode; import ast.parameters.ParameterNode;
import ast.statements.BlockNode; import ast.statements.BlockNode;
import ast.type.AccessModifierNode; import ast.type.AccessModifierNode;
import bytecode.visitor.MethodVisitor;
import visitor.Visitable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View File

@ -1,8 +1,12 @@
package ast.type; package ast.type;
import ast.ASTNode; import ast.ASTNode;
import bytecode.visitor.MethodVisitor;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import visitor.Visitable;
public class ValueNode implements ASTNode { public class ValueNode implements ASTNode, Visitable {
public EnumValueNode valueType; public EnumValueNode valueType;
public String value; public String value;
@ -10,4 +14,14 @@ public class ValueNode implements ASTNode {
this.valueType = valueType; this.valueType = valueType;
this.value = value; this.value = value;
} }
@Override
public void accept(MethodVisitor methodVisitor) {
methodVisitor.visit(this);
}
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return null;
}
} }

View File

@ -17,6 +17,7 @@ import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNod
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode; import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode; import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
import ast.statements.*; import ast.statements.*;
import ast.type.ValueNode;
import ast.type.type.BaseType; import ast.type.type.BaseType;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label; import org.objectweb.asm.Label;
@ -147,8 +148,8 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override @Override
public void visit(DotSubstractionNode dotSubstractionNode) { public void visit(DotSubstractionNode dotSubstractionNode) {
if(dotSubstractionNode.memberAccess == null) { // local var if (dotSubstractionNode.value != null) { // local var
dotSubstractionNode.value.accept(this);
} }
} }
@ -294,6 +295,42 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} }
@Override
public void visit(ValueNode valueNode) {
switch (valueNode.valueType) {
case INT_VALUE:
int intValue = Integer.parseInt(valueNode.value);
if(intValue >= Byte.MIN_VALUE && intValue <= Byte.MAX_VALUE ) { // load int as byte
methodVisitor.visitIntInsn(BIPUSH, intValue);
} else if (intValue >= Short.MIN_VALUE && intValue <= Short.MAX_VALUE ) { // load int as short
methodVisitor.visitIntInsn(SIPUSH, intValue);
} else { // load int as const
methodVisitor.visitLdcInsn(intValue);
}
break;
case BOOLEAN_VALUE:
if(valueNode.value.equals("true")) {
methodVisitor.visitInsn(ICONST_1);
} else {
methodVisitor.visitInsn(ICONST_0);
}
break;
case CHAR_VALUE:
char charValue = valueNode.value.charAt(0);
if(charValue <= Byte.MAX_VALUE ) { // load char as byte
methodVisitor.visitIntInsn(BIPUSH, charValue);
} else if (charValue <= Short.MAX_VALUE) { // load char as short
methodVisitor.visitIntInsn(SIPUSH, charValue);
} else { // load char as const
methodVisitor.visitLdcInsn(charValue);
}
break;
case NULL_VALUE:
methodVisitor.visitInsn(ACONST_NULL);
break;
}
}
@Override @Override
public void visit(ReturnNode returnNode) { public void visit(ReturnNode returnNode) {
if (returnNode.voidReturn) { // Return nothing if (returnNode.voidReturn) { // Return nothing

View File

@ -16,6 +16,7 @@ import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNod
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode; import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode; import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
import ast.statements.*; import ast.statements.*;
import ast.type.ValueNode;
public interface MethodVisitor { public interface MethodVisitor {
// members // members
@ -56,4 +57,7 @@ public interface MethodVisitor {
void visit(AssignNode assignNode); void visit(AssignNode assignNode);
void visit(NewDeclarationNode newDeclarationNode); void visit(NewDeclarationNode newDeclarationNode);
// type
void visit(ValueNode valueNode);
} }