Bytecode for ValueNode
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
i22007 2024-06-23 14:35:57 -04:00
parent 66c9481b3e
commit 99948fef07
4 changed files with 60 additions and 3 deletions

View File

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

View File

@ -1,8 +1,12 @@
package ast.type;
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 String value;
@ -10,4 +14,14 @@ public class ValueNode implements ASTNode {
this.valueType = valueType;
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.TargetNode;
import ast.statements.*;
import ast.type.ValueNode;
import ast.type.type.BaseType;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Label;
@ -147,8 +148,8 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override
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
public void visit(ReturnNode returnNode) {
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.TargetNode;
import ast.statements.*;
import ast.type.ValueNode;
public interface MethodVisitor {
// members
@ -56,4 +57,7 @@ public interface MethodVisitor {
void visit(AssignNode assignNode);
void visit(NewDeclarationNode newDeclarationNode);
// type
void visit(ValueNode valueNode);
}