Compare commits
No commits in common. "b03ea72eca44ca27bc7e7a4f3ada90e5aa9df3b4" and "f77143599b33a96a6d6971eb4f7635db8e016a90" have entirely different histories.
b03ea72eca
...
f77143599b
@ -5,7 +5,9 @@ public class Example {
|
||||
public static int testMethod(char b){
|
||||
|
||||
int a;
|
||||
int a;
|
||||
boolean b;
|
||||
char c;
|
||||
b = 3;
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
import ast.ASTNode;
|
||||
import ast.ClassNode;
|
||||
import ast.ProgramNode;
|
||||
import bytecode.ByteCodeGenerator;
|
||||
@ -18,14 +17,14 @@ public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
try {
|
||||
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/CompilerInput.txt"));
|
||||
parseFile(codeCharStream);
|
||||
parsefile(codeCharStream);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading the file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void parseFile(CharStream codeCharStream){
|
||||
static void parsefile(CharStream codeCharStream){
|
||||
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
SimpleJavaParser parser = new SimpleJavaParser(tokens);
|
||||
@ -35,9 +34,9 @@ public class Main {
|
||||
ASTBuilder builder = new ASTBuilder();
|
||||
ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST
|
||||
|
||||
ProgramNode typedAst = (ProgramNode) SemanticAnalyzer.generateTast(ast);
|
||||
SemanticAnalyzer.generateTast(ast);
|
||||
|
||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
||||
byteCodeGenerator.visit(typedAst);
|
||||
byteCodeGenerator.visit(ast);
|
||||
}
|
||||
}
|
@ -11,7 +11,7 @@ import java.util.List;
|
||||
import bytecode.visitor.ClassVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
import typechecker.Visitable;
|
||||
|
||||
public class ClassNode implements ASTNode, Visitable {
|
||||
public String identifier;
|
||||
|
@ -1,10 +1,8 @@
|
||||
package ast;
|
||||
|
||||
import ast.expression.ExpressionNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
public class LiteralNode implements ExpressionNode {
|
||||
public class LiteralNode extends ExpressionNode {
|
||||
|
||||
int value;
|
||||
private String type;
|
||||
@ -22,8 +20,4 @@ public class LiteralNode implements ExpressionNode {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import java.util.List;
|
||||
import bytecode.visitor.ProgramVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
import typechecker.Visitable;
|
||||
|
||||
public class ProgramNode implements ASTNode, Visitable{
|
||||
public List<ClassNode> classes = new ArrayList<>();
|
||||
|
@ -1,10 +1,6 @@
|
||||
package ast;
|
||||
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class VarNode implements ASTNode, Visitable {
|
||||
public class VarNode implements ASTNode{
|
||||
|
||||
private String identifier;
|
||||
private String type;
|
||||
@ -22,8 +18,4 @@ public class VarNode implements ASTNode, Visitable {
|
||||
return identifier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,6 @@
|
||||
package ast.expression;
|
||||
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class BinaryExpressionNode implements ExpressionNode, Visitable {
|
||||
public class BinaryExpressionNode extends ExpressionNode {
|
||||
public ExpressionNode left;
|
||||
public ExpressionNode right;
|
||||
public String operator; // Stores the operator as a string (e.g., "+", "-", "&&")
|
||||
@ -14,9 +10,4 @@ public class BinaryExpressionNode implements ExpressionNode, Visitable {
|
||||
this.right = right;
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
package ast.expression;
|
||||
|
||||
import ast.ASTNode;
|
||||
import visitor.Visitable;
|
||||
|
||||
public interface ExpressionNode extends ASTNode, Visitable {
|
||||
public class ExpressionNode implements ASTNode {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,22 +1,9 @@
|
||||
package ast.expression;
|
||||
|
||||
import ast.type.TypeNode;
|
||||
import semantic.SemanticVisitor;
|
||||
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class IdentifierExpressionNode implements ExpressionNode, Visitable {
|
||||
public class IdentifierExpressionNode extends ExpressionNode {
|
||||
public String name;
|
||||
public TypeNode type;
|
||||
|
||||
|
||||
public IdentifierExpressionNode(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,6 @@
|
||||
package ast.expression;
|
||||
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class UnaryExpressionNode implements ExpressionNode, Visitable {
|
||||
public class UnaryExpressionNode extends ExpressionNode {
|
||||
public ExpressionNode expression;
|
||||
public String operator; // Stores the operator (e.g., "-", "!")
|
||||
|
||||
@ -12,9 +8,4 @@ public class UnaryExpressionNode implements ExpressionNode, Visitable {
|
||||
this.expression = expression;
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package ast.member;
|
||||
|
||||
import ast.type.AccessTypeNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import visitor.Visitable;
|
||||
import typechecker.Visitable;
|
||||
|
||||
public class ConstructorNode extends MethodNode implements Visitable {
|
||||
public ConstructorNode(AccessTypeNode visibility, String name) {
|
||||
|
@ -5,7 +5,7 @@ import ast.type.TypeNode;
|
||||
import bytecode.visitor.ClassVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
import typechecker.Visitable;
|
||||
|
||||
public class FieldNode implements MemberNode, Visitable {
|
||||
public AccessTypeNode accessTypeNode;
|
||||
|
@ -3,15 +3,15 @@ package ast.member;
|
||||
import ast.parameter.ParameterListNode;
|
||||
import ast.statement.StatementNode;
|
||||
import ast.type.AccessTypeNode;
|
||||
import ast.type.TypeNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import ast.type.TypeNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
import typechecker.Visitable;
|
||||
|
||||
public class MethodNode implements MemberNode, Visitable {
|
||||
public AccessTypeNode visibility;
|
||||
|
@ -6,7 +6,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ParameterListNode implements ASTNode {
|
||||
public List<ParameterNode> parameters = new ArrayList<>();
|
||||
List<ParameterNode> parameters = new ArrayList<>();
|
||||
|
||||
public ParameterListNode(List<ParameterNode> parameters){
|
||||
this.parameters = parameters;
|
||||
|
@ -4,7 +4,7 @@ import ast.VarNode;
|
||||
import ast.expression.ExpressionNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
import typechecker.Visitable;
|
||||
|
||||
public class AssignmentStatementNode extends StatementNode implements Visitable {
|
||||
public VarNode varNode;
|
||||
|
@ -1,8 +1,6 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.expression.ExpressionNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
public class IfStatementNode extends StatementNode {
|
||||
public ExpressionNode condition;
|
||||
@ -14,9 +12,4 @@ public class IfStatementNode extends StatementNode {
|
||||
this.thenStatement = thenStatement;
|
||||
this.elseStatement = elseStatement;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.expression.ExpressionNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
public class ReturnStatementNode extends StatementNode {
|
||||
public ExpressionNode expression;
|
||||
@ -10,9 +8,4 @@ public class ReturnStatementNode extends StatementNode {
|
||||
public ReturnStatementNode(ExpressionNode expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
@ -1,8 +1,7 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.ASTNode;
|
||||
import visitor.Visitable;
|
||||
|
||||
public abstract class StatementNode implements ASTNode, Visitable {
|
||||
public abstract class StatementNode implements ASTNode {
|
||||
}
|
||||
|
||||
|
@ -1,9 +1,7 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.expression.ExpressionNode;
|
||||
import ast.type.TypeNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import ast.expression.ExpressionNode;
|
||||
|
||||
public class VariableDeclarationStatementNode extends StatementNode {
|
||||
public TypeNode type;
|
||||
@ -14,9 +12,4 @@ public class VariableDeclarationStatementNode extends StatementNode {
|
||||
this.identifier = identifier;
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.expression.ExpressionNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
public class WhileStatementNode extends StatementNode {
|
||||
public ExpressionNode condition;
|
||||
@ -12,9 +10,4 @@ public class WhileStatementNode extends StatementNode {
|
||||
this.condition = condition;
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
package ast.type;
|
||||
|
||||
import ast.ASTNode;
|
||||
|
||||
public class BaseTypeNode implements ASTNode, TypeNode {
|
||||
public class BaseTypeNode implements TypeNode{
|
||||
|
||||
public EnumTypeNode enumType;
|
||||
|
||||
|
@ -1,6 +1,4 @@
|
||||
package ast.type;
|
||||
|
||||
import ast.ASTNode;
|
||||
|
||||
public class ReferenceTypeNode implements ASTNode, TypeNode {
|
||||
public class ReferenceTypeNode implements TypeNode{
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import ast.ClassNode;
|
||||
import ast.member.FieldNode;
|
||||
import ast.member.MemberNode;
|
||||
import ast.member.MethodNode;
|
||||
import ast.type.BaseTypeNode;
|
||||
import bytecode.visitor.ClassVisitor;
|
||||
import java.io.File;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
@ -13,19 +12,14 @@ import org.objectweb.asm.Opcodes;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
public class ClassCodeGen implements ClassVisitor {
|
||||
private Mapper mapper;
|
||||
private ClassWriter classWriter;
|
||||
|
||||
public ClassCodeGen() {
|
||||
mapper = new Mapper();
|
||||
}
|
||||
Mapper mapper = new Mapper();
|
||||
ClassWriter classWriter;
|
||||
|
||||
@Override
|
||||
public void visit(ClassNode classNode) {
|
||||
classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||
classWriter.visit(Opcodes.V1_5, mapper.mapAccessTypeToOpcode(classNode.accessType), classNode.identifier, null,
|
||||
classWriter.visit(Opcodes.V1_8, mapper.mapAccesTypeToOpcode(classNode.accessType), classNode.identifier, null,
|
||||
"java/lang/Object", null);
|
||||
|
||||
for (MemberNode memberNode : classNode.members) {
|
||||
@ -45,9 +39,7 @@ public class ClassCodeGen implements ClassVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(FieldNode fieldNode) {
|
||||
if(fieldNode.type instanceof BaseTypeNode baseTypeNode){
|
||||
classWriter.visitField(mapper.mapAccessTypeToOpcode(fieldNode.accessTypeNode), fieldNode.identifier, mapper.getTypeChar(baseTypeNode.enumType), null, null );
|
||||
}
|
||||
classWriter.visitField(mapper.mapAccesTypeToOpcode(fieldNode.accessTypeNode), fieldNode.identifier, mapper.getTypeChar(fieldNode.type.enumTypeNode), null, null );
|
||||
classWriter.visitEnd();
|
||||
}
|
||||
|
||||
|
13
src/main/java/bytecode/FieldCodeGen.java
Normal file
13
src/main/java/bytecode/FieldCodeGen.java
Normal file
@ -0,0 +1,13 @@
|
||||
package bytecode;
|
||||
|
||||
import ast.member.FieldNode;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.FieldVisitor;
|
||||
|
||||
public class FieldCodeGen {
|
||||
|
||||
public void generateFieldCode(ClassWriter classWriter, FieldNode fieldNode) {
|
||||
Mapper mapper = new Mapper();
|
||||
FieldVisitor fieldVisitor = classWriter.visitField(mapper.mapAccesTypeToOpcode(fieldNode.accessTypeNode), fieldNode.identifier, "", null, null);
|
||||
}
|
||||
}
|
@ -1,13 +1,13 @@
|
||||
package bytecode;
|
||||
|
||||
import ast.parameter.ParameterListNode;
|
||||
import ast.parameter.ParameterNode;
|
||||
import ast.type.*;
|
||||
import ast.type.AccessTypeNode;
|
||||
import ast.type.EnumAccessTypeNode;
|
||||
import ast.type.EnumTypeNode;
|
||||
import ast.type.TypeNode;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import ast.type.BaseTypeNode;
|
||||
|
||||
public class Mapper {
|
||||
public int mapAccessTypeToOpcode(AccessTypeNode type) {
|
||||
public int mapAccesTypeToOpcode(AccessTypeNode type) {
|
||||
switch (type.enumAccessTypeNode) {
|
||||
case EnumAccessTypeNode.PUBLIC:
|
||||
return Opcodes.ACC_PUBLIC;
|
||||
@ -17,13 +17,9 @@ public class Mapper {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String generateMethodDescriptor(BaseTypeNode baseTypeNode, ParameterListNode parameterListNode) {
|
||||
String descriptor = "(";
|
||||
for(ParameterNode parameterNode : parameterListNode.parameters) {
|
||||
descriptor += getTypeChar(EnumTypeNode.INT);
|
||||
}
|
||||
descriptor += ")";
|
||||
descriptor += getTypeChar(baseTypeNode.enumType);
|
||||
public String generateMethodDescriptor(TypeNode typeNode) {
|
||||
String descriptor = "()";
|
||||
descriptor += getTypeChar(typeNode.enumTypeNode);
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
|
@ -2,99 +2,37 @@ package bytecode;
|
||||
|
||||
import ast.member.ConstructorNode;
|
||||
import ast.member.MethodNode;
|
||||
import ast.parameter.ParameterListNode;
|
||||
import ast.parameter.ParameterNode;
|
||||
import ast.type.BaseTypeNode;
|
||||
import ast.type.EnumTypeNode;
|
||||
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;
|
||||
|
||||
import static org.objectweb.asm.Opcodes.*;
|
||||
|
||||
|
||||
public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
|
||||
private ClassWriter classWriter;
|
||||
private Mapper mapper;
|
||||
private MethodVisitor methodVisitor;
|
||||
|
||||
private List<String> localVaribales;
|
||||
Mapper mapper = new Mapper();
|
||||
|
||||
public MethodCodeGen(ClassWriter classWriter) {
|
||||
this.classWriter = classWriter;
|
||||
mapper = new Mapper();
|
||||
localVaribales = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ConstructorNode constructorNode) {
|
||||
methodVisitor =
|
||||
classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.visibility),
|
||||
MethodVisitor constructor =
|
||||
classWriter.visitMethod(mapper.mapAccesTypeToOpcode(constructorNode.visibility),
|
||||
"<init>",
|
||||
"()V",
|
||||
null,
|
||||
null);
|
||||
methodVisitor.visitCode();
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
|
||||
methodVisitor.visitInsn(RETURN);
|
||||
methodVisitor.visitMaxs(1, 1);
|
||||
methodVisitor.visitEnd();
|
||||
constructor.visitEnd();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MethodNode methodNode) {
|
||||
if (methodNode.type instanceof BaseTypeNode baseTypeNode) {
|
||||
methodVisitor = classWriter.visitMethod(mapper.mapAccessTypeToOpcode(methodNode.visibility),
|
||||
MethodVisitor method = classWriter.visitMethod(mapper.mapAccesTypeToOpcode(methodNode.visibility),
|
||||
methodNode.identifier,
|
||||
mapper.generateMethodDescriptor(baseTypeNode, methodNode.parameters),
|
||||
mapper.generateMethodDescriptor(methodNode.type),
|
||||
null,
|
||||
null);
|
||||
|
||||
methodVisitor.visitCode();
|
||||
localVaribales.add("this");
|
||||
for (ParameterNode parameterNode : methodNode.parameters.parameters) {
|
||||
localVaribales.add(parameterNode.identifier);
|
||||
}
|
||||
|
||||
//test();
|
||||
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();
|
||||
method.visitEnd();
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
@ -13,12 +13,10 @@ import ast.parameter.ParameterNode;
|
||||
import ast.statement.*;
|
||||
import ast.type.*;
|
||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import parser.generated.*;
|
||||
import parser.generated.SimpleJavaParser.LiteralContext;
|
||||
import ast.type.BaseTypeNode;
|
||||
|
||||
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
@Override
|
||||
@ -76,9 +74,9 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
|
||||
@Override
|
||||
public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) {
|
||||
TypeNode typeNode = (TypeNode) visit(ctx.type());
|
||||
TypeNode type = (TypeNode) visit(ctx.type());
|
||||
String identifier = ctx.IDENTIFIER().getText();
|
||||
return new ParameterNode(typeNode, identifier);
|
||||
return new ParameterNode(type, identifier);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -206,7 +204,6 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
try {
|
||||
int intValue = Integer.parseInt(literalContext.getText());
|
||||
LiteralNode literalNode = new LiteralNode(intValue);
|
||||
|
||||
literalNode.setType("int");
|
||||
return literalNode;
|
||||
} catch (NumberFormatException ignored) {}
|
||||
|
@ -1,36 +0,0 @@
|
||||
package semantic;
|
||||
|
||||
import ast.type.TypeNode;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Stack;
|
||||
|
||||
public class Scope {
|
||||
|
||||
private Stack<HashMap<String, TypeNode>> localVars;
|
||||
|
||||
public void addLocalVar(String name, TypeNode type) {
|
||||
if (this.contains(name)) {
|
||||
throw new RuntimeException("Variable " + name + " already exists in this scope");
|
||||
}
|
||||
localVars.peek().put(name, type);
|
||||
}
|
||||
|
||||
public boolean contains(String name) {
|
||||
for (HashMap<String, TypeNode> map : localVars) {
|
||||
if (map.containsKey(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void pushScope() {
|
||||
localVars.push(new HashMap<String, TypeNode>());
|
||||
}
|
||||
|
||||
public void popScope() {
|
||||
localVars.pop();
|
||||
}
|
||||
|
||||
}
|
@ -2,26 +2,22 @@ package semantic;
|
||||
|
||||
|
||||
import ast.*;
|
||||
import ast.expression.BinaryExpressionNode;
|
||||
import ast.expression.IdentifierExpressionNode;
|
||||
import ast.expression.UnaryExpressionNode;
|
||||
import ast.expression.ExpressionNode;
|
||||
import ast.member.ConstructorNode;
|
||||
import ast.member.FieldNode;
|
||||
import ast.member.MemberNode;
|
||||
|
||||
import ast.member.MethodNode;
|
||||
import ast.statement.*;
|
||||
|
||||
import ast.statement.AssignmentStatementNode;
|
||||
import ast.statement.StatementNode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
public class SemanticAnalyzer implements SemanticVisitor {
|
||||
|
||||
private ArrayList<String> currentFields = new ArrayList<>();
|
||||
|
||||
private Scope currentScope;
|
||||
|
||||
public static ASTNode generateTast(ASTNode node) throws RuntimeException {
|
||||
SemanticAnalyzer semanticCheck = new SemanticAnalyzer();
|
||||
ProgramNode programNode = (ProgramNode) node;
|
||||
@ -40,106 +36,56 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
|
||||
List<ClassNode> classes = node.classes;
|
||||
for (ClassNode classNode : classes) {
|
||||
var result = classNode.accept(this);
|
||||
valid = valid && result.isValid();
|
||||
classNode.accept(this);
|
||||
}
|
||||
return new TypeCheckResult(valid, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(ClassNode classNode) {
|
||||
var valid = true;
|
||||
List<MemberNode> members = classNode.members;
|
||||
for (MemberNode memberNode : members) {
|
||||
if (memberNode instanceof FieldNode fieldNode) {
|
||||
var result = fieldNode.accept(this);
|
||||
valid = valid && result.isValid();
|
||||
fieldNode.accept(this);
|
||||
} else if (memberNode instanceof MethodNode methodNode) {
|
||||
var result = methodNode.accept(this);
|
||||
valid = valid && result.isValid();
|
||||
methodNode.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
return new TypeCheckResult(valid, null);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(MethodNode methodNode) {
|
||||
var valid = true;
|
||||
|
||||
currentLocalScope.pushScope();
|
||||
|
||||
List<StatementNode> statements = methodNode.statements;
|
||||
for (StatementNode statement : statements) {
|
||||
if (statement instanceof AssignmentStatementNode assignmentStatementNode) {
|
||||
var result = assignmentStatementNode.accept(this);
|
||||
valid = valid && result.isValid();
|
||||
} else if (statement instanceof VariableDeclarationStatementNode variableDeclarationStatementNode) {
|
||||
var result = variableDeclarationStatementNode.accept(this);
|
||||
valid = valid && result.isValid();
|
||||
if(statement instanceof AssignmentStatementNode assignmentStatementNode) {
|
||||
assignmentStatementNode.accept(this);
|
||||
}
|
||||
}
|
||||
return new TypeCheckResult(valid, null);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(FieldNode toCheck) {
|
||||
if (currentFields.contains(toCheck.identifier)) {
|
||||
if(currentFields.contains(toCheck.identifier)){
|
||||
throw new RuntimeException(toCheck.identifier + " Is Already Declared");
|
||||
} else {
|
||||
}else {
|
||||
currentFields.add(toCheck.identifier);
|
||||
}
|
||||
return new TypeCheckResult(true, null);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(AssignmentStatementNode assignmentStatementNode) {
|
||||
if (assignmentStatementNode.expression instanceof LiteralNode literalNode) {
|
||||
TypeCheckResult varResult = assignmentStatementNode.varNode.accept(this);
|
||||
TypeCheckResult expressionResult = assignmentStatementNode.expression.accept(this);
|
||||
if(assignmentStatementNode.expression instanceof LiteralNode literalNode) {
|
||||
VarNode varNode = assignmentStatementNode.varNode;
|
||||
if(varNode.getType().equals(literalNode.getType())) {
|
||||
System.out.println("Type is same");
|
||||
} else {
|
||||
throw new RuntimeException("Type mismatch");
|
||||
}
|
||||
return new TypeCheckResult(true, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(VarNode toCheck) {
|
||||
return new TypeCheckResult(true, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(BinaryExpressionNode toCheck) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(IdentifierExpressionNode toCheck) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(UnaryExpressionNode toCheck) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(VariableDeclarationStatementNode toCheck) {
|
||||
|
||||
return new TypeCheckResult(true, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(IfStatementNode toCheck) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(ReturnStatementNode toCheck) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(WhileStatementNode toCheck) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -3,16 +3,13 @@ package semantic;
|
||||
|
||||
import ast.ClassNode;
|
||||
import ast.ProgramNode;
|
||||
import ast.VarNode;
|
||||
import ast.expression.BinaryExpressionNode;
|
||||
import ast.expression.IdentifierExpressionNode;
|
||||
import ast.expression.UnaryExpressionNode;
|
||||
import ast.member.FieldNode;
|
||||
import ast.member.MethodNode;
|
||||
import ast.statement.*;
|
||||
import ast.statement.AssignmentStatementNode;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
public interface SemanticVisitor {
|
||||
// TypeCheckResult typeCheck(ASTNode toCheck);
|
||||
|
||||
TypeCheckResult analyze(ProgramNode toCheck);
|
||||
|
||||
@ -23,20 +20,43 @@ public interface SemanticVisitor {
|
||||
TypeCheckResult analyze(FieldNode toCheck);
|
||||
|
||||
TypeCheckResult analyze(AssignmentStatementNode toCheck);
|
||||
//
|
||||
// TypeCheckResult typeCheck(MethodParameter toCheck);
|
||||
//
|
||||
// TypeCheckResult typeCheck(ForStmt forStmt);
|
||||
//
|
||||
// TypeCheckResult typeCheck(WhileStmt whileStmt);
|
||||
//
|
||||
// TypeCheckResult typeCheck(ReturnStmt returnStmt);
|
||||
//
|
||||
// TypeCheckResult typeCheck(LocalVarDecl localVarDecl);
|
||||
//
|
||||
// TypeCheckResult typeCheck(IfStmt ifStmt);
|
||||
//
|
||||
// TypeCheckResult typeCheck(Block block);
|
||||
//
|
||||
// TypeCheckResult typeCheck(NewDecl newDecl);
|
||||
//
|
||||
// TypeCheckResult typeCheck(MethodCall methodCall);
|
||||
//
|
||||
// TypeCheckResult typeCheck(Unary unary);
|
||||
//
|
||||
// TypeCheckResult typeCheck(This aThis);
|
||||
//
|
||||
// TypeCheckResult typeCheck(Null aNull);
|
||||
//
|
||||
// TypeCheckResult typeCheck(LocalOrFieldVar localOrFieldVar);
|
||||
//
|
||||
// TypeCheckResult typeCheck(IntegerExpr integerExpr);
|
||||
//
|
||||
// TypeCheckResult typeCheck(InstVar instVar);
|
||||
//
|
||||
// TypeCheckResult typeCheck(CharExpr charExpr);
|
||||
//
|
||||
// TypeCheckResult typeCheck(BoolExpr boolExpr);
|
||||
//
|
||||
// TypeCheckResult typeCheck(Binary binary);
|
||||
//
|
||||
// TypeCheckResult typeCheck(StringExpr instVar);
|
||||
|
||||
TypeCheckResult analyze(VarNode toCheck);
|
||||
|
||||
TypeCheckResult analyze(BinaryExpressionNode toCheck);
|
||||
|
||||
TypeCheckResult analyze(IdentifierExpressionNode toCheck);
|
||||
|
||||
TypeCheckResult analyze(UnaryExpressionNode toCheck);
|
||||
|
||||
TypeCheckResult analyze(VariableDeclarationStatementNode toCheck);
|
||||
|
||||
TypeCheckResult analyze(IfStatementNode toCheck);
|
||||
|
||||
TypeCheckResult analyze(ReturnStatementNode toCheck);
|
||||
|
||||
TypeCheckResult analyze(WhileStatementNode toCheck);
|
||||
}
|
5
src/main/java/typechecker/Type.java
Normal file
5
src/main/java/typechecker/Type.java
Normal file
@ -0,0 +1,5 @@
|
||||
package typechecker;
|
||||
|
||||
public interface Type {
|
||||
boolean equals(Object obj);
|
||||
}
|
@ -1,14 +1,12 @@
|
||||
package typechecker;
|
||||
|
||||
|
||||
import ast.type.TypeNode;
|
||||
|
||||
public class TypeCheckResult {
|
||||
|
||||
private boolean valid;
|
||||
private TypeNode type;
|
||||
private Type type;
|
||||
|
||||
public TypeCheckResult(boolean valid, TypeNode type) {
|
||||
public TypeCheckResult(boolean valid, Type type) {
|
||||
this.valid = valid;
|
||||
this.type = type;
|
||||
}
|
||||
@ -17,7 +15,7 @@ public class TypeCheckResult {
|
||||
return valid;
|
||||
}
|
||||
|
||||
public TypeNode getType() {
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
9
src/main/java/typechecker/Typer.java
Normal file
9
src/main/java/typechecker/Typer.java
Normal file
@ -0,0 +1,9 @@
|
||||
package typechecker;
|
||||
|
||||
public class Typer {
|
||||
|
||||
public static void Typeify(){
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,10 +1,9 @@
|
||||
package visitor;
|
||||
package typechecker;
|
||||
|
||||
import bytecode.visitor.ClassVisitor;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import bytecode.visitor.ProgramVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
public interface Visitable {
|
||||
default void accept(ProgramVisitor programVisitor) {
|
Loading…
Reference in New Issue
Block a user