Code for assignment
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This commit is contained in:
parent
1c77a99f54
commit
407b03620e
5
pom.xml
5
pom.xml
@ -33,6 +33,11 @@
|
||||
<artifactId>asm</artifactId>
|
||||
<version>9.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.ow2.asm</groupId>
|
||||
<artifactId>asm-util</artifactId>
|
||||
<version>9.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -1,12 +1,14 @@
|
||||
package ast;
|
||||
|
||||
import ast.expression.ExpressionNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class LiteralNode implements ExpressionNode {
|
||||
public class LiteralNode implements ExpressionNode, Visitable {
|
||||
|
||||
int value;
|
||||
public int value;
|
||||
private String type;
|
||||
|
||||
public LiteralNode(int value) {
|
||||
@ -26,4 +28,9 @@ public class LiteralNode implements ExpressionNode {
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ast.expression;
|
||||
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
@ -19,4 +20,9 @@ public class BinaryExpressionNode implements ExpressionNode, Visitable {
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package ast.expression;
|
||||
|
||||
import ast.type.TypeNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
|
||||
import typechecker.TypeCheckResult;
|
||||
@ -11,12 +12,18 @@ public class IdentifierExpressionNode implements ExpressionNode, Visitable {
|
||||
public TypeNode type;
|
||||
|
||||
|
||||
public IdentifierExpressionNode(String name) {
|
||||
public IdentifierExpressionNode(String name, TypeNode type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ast.expression;
|
||||
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
@ -17,6 +18,11 @@ public class UnaryExpressionNode implements ExpressionNode, Visitable {
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.expression.BinaryExpressionNode;
|
||||
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
@ -16,4 +18,9 @@ public class AssignmentStatementNode extends StatementNode implements Visitable
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,12 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.expression.ExpressionNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class IfStatementNode extends StatementNode {
|
||||
public class IfStatementNode extends StatementNode implements Visitable {
|
||||
public ExpressionNode condition;
|
||||
public StatementNode thenStatement;
|
||||
public StatementNode elseStatement;
|
||||
@ -19,4 +21,9 @@ public class IfStatementNode extends StatementNode {
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
@ -1,10 +1,12 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.expression.ExpressionNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class ReturnStatementNode extends StatementNode {
|
||||
public class ReturnStatementNode extends StatementNode implements Visitable {
|
||||
public ExpressionNode expression;
|
||||
|
||||
public ReturnStatementNode(ExpressionNode expression) {
|
||||
@ -15,4 +17,9 @@ public class ReturnStatementNode extends StatementNode {
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.ASTNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import visitor.Visitable;
|
||||
|
||||
public abstract class StatementNode implements ASTNode, Visitable {
|
||||
|
@ -2,10 +2,12 @@ package ast.statement;
|
||||
|
||||
import ast.expression.ExpressionNode;
|
||||
import ast.type.TypeNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class VariableDeclarationStatementNode extends StatementNode {
|
||||
public class VariableDeclarationStatementNode extends StatementNode implements Visitable {
|
||||
public TypeNode type;
|
||||
public String identifier;
|
||||
public ExpressionNode expression;
|
||||
@ -19,4 +21,9 @@ public class VariableDeclarationStatementNode extends StatementNode {
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
@ -1,10 +1,12 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.expression.ExpressionNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class WhileStatementNode extends StatementNode {
|
||||
public class WhileStatementNode extends StatementNode implements Visitable {
|
||||
public ExpressionNode condition;
|
||||
public StatementNode body;
|
||||
|
||||
@ -17,4 +19,9 @@ public class WhileStatementNode extends StatementNode {
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
@ -8,9 +8,13 @@ public class ByteCodeGenerator implements ProgramVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(ProgramNode programNode) {
|
||||
//ASMCodeGenerator asmCodeGenerator = new ASMCodeGenerator();
|
||||
//asmCodeGenerator.test();
|
||||
|
||||
for (ClassNode classDeclarationNode : programNode.classes) {
|
||||
ClassCodeGen classCodeGen = new ClassCodeGen();
|
||||
classDeclarationNode.accept(classCodeGen);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ public class ClassCodeGen implements ClassVisitor {
|
||||
}
|
||||
|
||||
private void printIntoClassFile(byte[] byteCode, String name) {
|
||||
String directoryPath = "src/main/java/classFileOutput";
|
||||
String directoryPath = "src/main/resources/classFileOutput";
|
||||
File directory = new File(directoryPath);
|
||||
if (!directory.exists()) {
|
||||
directory.mkdirs();
|
||||
|
@ -1,15 +1,18 @@
|
||||
package bytecode;
|
||||
|
||||
import ast.LiteralNode;
|
||||
import ast.expression.BinaryExpressionNode;
|
||||
import ast.expression.IdentifierExpressionNode;
|
||||
import ast.expression.UnaryExpressionNode;
|
||||
import ast.member.ConstructorNode;
|
||||
import ast.member.MethodNode;
|
||||
import ast.parameter.ParameterListNode;
|
||||
import ast.parameter.ParameterNode;
|
||||
import ast.statement.*;
|
||||
import ast.type.BaseTypeNode;
|
||||
import ast.type.EnumTypeNode;
|
||||
import ast.type.ReferenceTypeNode;
|
||||
import ast.type.TypeNode;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -50,7 +53,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
@Override
|
||||
public void visit(MethodNode methodNode) {
|
||||
if (methodNode.type instanceof BaseTypeNode baseTypeNode) {
|
||||
methodVisitor = classWriter.visitMethod(mapper.mapAccessTypeToOpcode(methodNode.visibility),
|
||||
methodVisitor = classWriter.visitMethod(mapper.mapAccessTypeToOpcode(methodNode.visibility) | ACC_STATIC,
|
||||
methodNode.identifier,
|
||||
mapper.generateMethodDescriptor(baseTypeNode, methodNode.parameters),
|
||||
null,
|
||||
@ -58,43 +61,90 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
|
||||
methodVisitor.visitCode();
|
||||
localVaribales.add("this");
|
||||
// Add all method parameters to localVariables
|
||||
for (ParameterNode parameterNode : methodNode.parameters.parameters) {
|
||||
localVaribales.add(parameterNode.identifier);
|
||||
}
|
||||
|
||||
//test();
|
||||
// Visit all statements
|
||||
for(StatementNode statementNode : methodNode.statements) {
|
||||
statementNode.accept(this);
|
||||
}
|
||||
|
||||
methodVisitor.visitMaxs(1, localVaribales.size());
|
||||
methodVisitor.visitEnd();
|
||||
}
|
||||
}
|
||||
|
||||
public void test() {
|
||||
Label start = new Label();
|
||||
Label loop = new Label();
|
||||
Label end = new Label();
|
||||
methodVisitor.visitLabel(start);
|
||||
//methodVisitor.visitVarInsn(Opcodes.ICONST_M1, 99);
|
||||
//methodVisitor.visitInsn(Opcodes.ICONST_5);
|
||||
methodVisitor.visitLdcInsn(99);
|
||||
// methodVisitor.visitInsn(Opcodes.ICONST_0);
|
||||
//methodVisitor.visitVarInsn(Opcodes.ILOAD, 2);
|
||||
methodVisitor.visitVarInsn(Opcodes.ISTORE, 1);
|
||||
methodVisitor.visitLabel(loop);
|
||||
methodVisitor.visitVarInsn(Opcodes.ILOAD, 1);
|
||||
methodVisitor.visitInsn(Opcodes.ICONST_5);
|
||||
methodVisitor.visitJumpInsn(Opcodes.IF_ICMPGE, end);
|
||||
methodVisitor.visitFieldInsn(Opcodes.GETSTATIC,
|
||||
"java/lang/System", "out",
|
||||
"Ljava/io/PrintStream;");
|
||||
methodVisitor.visitLdcInsn("Bytecode");
|
||||
methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
|
||||
"java/io/PrintStream", "println",
|
||||
"(Ljava/lang/String;)V", false);
|
||||
methodVisitor.visitIincInsn(1, 1);
|
||||
methodVisitor.visitJumpInsn(Opcodes.GOTO, loop);
|
||||
methodVisitor.visitLabel(end);
|
||||
methodVisitor.visitVarInsn(Opcodes.ILOAD, 1);
|
||||
methodVisitor.visitInsn(Opcodes.IRETURN);
|
||||
methodVisitor.visitEnd();
|
||||
@Override
|
||||
public void visit(AssignmentStatementNode assignmentStatementNode) {
|
||||
BinaryExpressionNode expressionNode = assignmentStatementNode.expression;
|
||||
|
||||
if(expressionNode.left instanceof IdentifierExpressionNode) { // Local var
|
||||
expressionNode.right.accept(this);
|
||||
localVarAssignment((IdentifierExpressionNode) expressionNode.left);
|
||||
} else if(expressionNode.left instanceof BinaryExpressionNode) { // Global var
|
||||
expressionNode.right.accept(this);
|
||||
globalVarAssignment((BinaryExpressionNode) expressionNode.left);
|
||||
}
|
||||
|
||||
assignmentStatementNode.expression.accept(this);
|
||||
methodVisitor.visitVarInsn(ISTORE, 1);
|
||||
}
|
||||
|
||||
private void localVarAssignment(IdentifierExpressionNode expressionNode) {
|
||||
methodVisitor.visitVarInsn(ILOAD, localVaribales.indexOf(expressionNode.name));
|
||||
}
|
||||
|
||||
private void globalVarAssignment(BinaryExpressionNode expressionNode) {
|
||||
IdentifierExpressionNode identifierExpressionNode = (IdentifierExpressionNode) expressionNode.left;
|
||||
IdentifierExpressionNode identifierExpressionNode1 = (IdentifierExpressionNode) expressionNode.right;
|
||||
TypeNode type = identifierExpressionNode1.type;
|
||||
if(type instanceof BaseTypeNode) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, identifierExpressionNode.name, identifierExpressionNode1.name, mapper.getTypeChar(((BaseTypeNode) type).enumType));
|
||||
} else if (type instanceof ReferenceTypeNode) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(IfStatementNode ifStatementNode) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ReturnStatementNode returnStatementNode) {
|
||||
returnStatementNode.expression.accept(this);
|
||||
methodVisitor.visitInsn(IRETURN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(VariableDeclarationStatementNode variableDeclarationStatementNode) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(WhileStatementNode whileStatementNode) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(BinaryExpressionNode binaryExpressionNode) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(IdentifierExpressionNode identifierExpressionNode) {
|
||||
methodVisitor.visitVarInsn(ILOAD, localVaribales.indexOf(identifierExpressionNode.name));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(UnaryExpressionNode unaryExpressionNode) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(LiteralNode literalNode) {
|
||||
methodVisitor.visitVarInsn(BIPUSH, literalNode.value);
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,23 @@
|
||||
package bytecode.visitor;
|
||||
|
||||
import ast.LiteralNode;
|
||||
import ast.expression.BinaryExpressionNode;
|
||||
import ast.expression.IdentifierExpressionNode;
|
||||
import ast.expression.UnaryExpressionNode;
|
||||
import ast.member.ConstructorNode;
|
||||
import ast.member.MethodNode;
|
||||
import ast.statement.*;
|
||||
|
||||
public interface MethodVisitor {
|
||||
void visit(ConstructorNode constructorNode);
|
||||
void visit(MethodNode methodNode);
|
||||
void visit(AssignmentStatementNode assignmentStatementNode);
|
||||
void visit(IfStatementNode ifStatementNode);
|
||||
void visit(ReturnStatementNode returnStatementNode);
|
||||
void visit(VariableDeclarationStatementNode variableDeclarationStatementNode);
|
||||
void visit(WhileStatementNode whileStatementNode);
|
||||
void visit(BinaryExpressionNode binaryExpressionNode);
|
||||
void visit(IdentifierExpressionNode identifierExpressionNode);
|
||||
void visit(UnaryExpressionNode unaryExpressionNode);
|
||||
void visit(LiteralNode literalNode);
|
||||
}
|
||||
|
@ -198,7 +198,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
return visit(ctx.literal());
|
||||
}
|
||||
else if (ctx.IDENTIFIER() != null) {
|
||||
return new IdentifierExpressionNode(ctx.IDENTIFIER().getText());
|
||||
return new IdentifierExpressionNode(ctx.IDENTIFIER().getText(), null);
|
||||
}
|
||||
|
||||
return null; // Return null or throw an exception if no valid expression found
|
||||
|
@ -17,6 +17,7 @@ import ast.type.AccessTypeNode;
|
||||
import ast.type.BaseTypeNode;
|
||||
import ast.type.EnumAccessTypeNode;
|
||||
import ast.type.EnumTypeNode;
|
||||
import bytecode.ByteCodeGenerator;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import semantic.exeptions.AlreadyDeclearedException;
|
||||
@ -79,8 +80,8 @@ public class SemanticTest {
|
||||
|
||||
List<StatementNode> statementNodeList = new ArrayList<StatementNode>();
|
||||
|
||||
ExpressionNode expressionNodeObjectVariableLeft = new IdentifierExpressionNode("this");
|
||||
ExpressionNode expressionNodeObjectVariableRight = new IdentifierExpressionNode("objectVar");
|
||||
ExpressionNode expressionNodeObjectVariableLeft = new IdentifierExpressionNode("Example", null);
|
||||
ExpressionNode expressionNodeObjectVariableRight = new IdentifierExpressionNode("objectVar", new BaseTypeNode(EnumTypeNode.INT));
|
||||
|
||||
ExpressionNode expressionNodeLeft = new BinaryExpressionNode(expressionNodeObjectVariableLeft, expressionNodeObjectVariableRight, ExpresssionOperator.DOT);
|
||||
|
||||
@ -94,6 +95,8 @@ public class SemanticTest {
|
||||
MemberNode memberNode3 = new MethodNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar2",parameterListNode, statementNodeList );
|
||||
classNode.members.add(memberNode3);
|
||||
|
||||
|
||||
|
||||
classList.add(classNode);
|
||||
programNode.classes = classList;
|
||||
|
||||
@ -102,6 +105,9 @@ public class SemanticTest {
|
||||
assertEquals(0, SemanticAnalyzer.errors.size());
|
||||
assertEquals(programNode, typedAst);
|
||||
|
||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
||||
byteCodeGenerator.visit((ProgramNode) typedAst);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user