diff --git a/src/main/java/ast/members/ConstructorNode.java b/src/main/java/ast/members/ConstructorNode.java index 2b421fd..3f9b019 100644 --- a/src/main/java/ast/members/ConstructorNode.java +++ b/src/main/java/ast/members/ConstructorNode.java @@ -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; diff --git a/src/main/java/ast/type/ValueNode.java b/src/main/java/ast/type/ValueNode.java index b51f799..600361b 100644 --- a/src/main/java/ast/type/ValueNode.java +++ b/src/main/java/ast/type/ValueNode.java @@ -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; + } } diff --git a/src/main/java/bytecode/MethodCodeGen.java b/src/main/java/bytecode/MethodCodeGen.java index aea940d..95b5c40 100644 --- a/src/main/java/bytecode/MethodCodeGen.java +++ b/src/main/java/bytecode/MethodCodeGen.java @@ -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 diff --git a/src/main/java/bytecode/visitor/MethodVisitor.java b/src/main/java/bytecode/visitor/MethodVisitor.java index 3426646..1108a86 100644 --- a/src/main/java/bytecode/visitor/MethodVisitor.java +++ b/src/main/java/bytecode/visitor/MethodVisitor.java @@ -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); + }