Added down to Expression

This commit is contained in:
Bruder John 2024-05-09 00:13:23 +02:00
parent 03645f7a84
commit a86bede5a7
6 changed files with 51 additions and 4 deletions

View File

@ -2,6 +2,12 @@ public class Example {
public int test;
public static int test(char b){
test = 3;
}
}
public class Test {

View File

@ -9,9 +9,6 @@ import parser.ASTBuilder;
import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser;
import semantic.SemanticAnalyzer;
import ast.ClassNode;
import ast.ProgramNode;
import bytecode.ByteCodeGenerator;
import java.io.IOException;
import java.nio.file.Paths;

View File

@ -0,0 +1,14 @@
package ast;
public class AssignmentNode extends StatementNode {
public Identifier identifier;
public ExpressionNode expression;
public AssignmentNode(String identifier, ExpressionNode expression) {
this.identifier = new Identifier(identifier);
this.expression = expression;
}
}

View File

@ -1,4 +1,11 @@
package ast;
public class ExpressionNode {
public class ExpressionNode extends ASTNode {
int Value;
public ExpressionNode(int value) {
this.Value = value;
}
}

View File

@ -6,6 +6,7 @@ import parser.generated.SimpleJavaParser;
import java.util.ArrayList;
import java.util.List;
import parser.generated.SimpleJavaParser.LiteralContext;
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
@ -107,4 +108,26 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null;
}
@Override
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
ExpressionNode expressionNode = (ExpressionNode) visit(ctx.expression());
return new AssignmentNode(ctx.IDENTIFIER().getText(), expressionNode);
}
@Override
public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) {
if(ctx.literal() != null){
return visitLiteral(ctx.literal());
}
return null;
}
@Override
public ASTNode visitLiteral(SimpleJavaParser.LiteralContext ctx) {
if(ctx.INTEGERLITERAL() != null){
return new ExpressionNode(Integer.parseInt(ctx.INTEGERLITERAL().getSymbol().getText()));
}
return null;
}
}