Compare commits

..

No commits in common. "6250ee781c4b67562f98e00f91d998d6f4b529a1" and "f5da71478f051518b9b8a6c8cd59fc2c6b7d7ace" have entirely different histories.

7 changed files with 25 additions and 44 deletions

View File

@ -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);
}
}

View File

@ -1,10 +1,6 @@
package ast;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import typechecker.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);
}
}

View File

@ -1,8 +1,9 @@
package ast.expression;
import ast.ASTNode;
import typechecker.Visitable;
public interface ExpressionNode extends ASTNode, Visitable {
public class ExpressionNode implements ASTNode {
}

View File

@ -204,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) {}

View File

@ -12,8 +12,6 @@ import ast.statement.AssignmentStatementNode;
import ast.statement.StatementNode;
import java.util.ArrayList;
import java.util.List;
import ast.type.TypeNode;
import typechecker.TypeCheckResult;
public class SemanticAnalyzer implements SemanticVisitor {
@ -38,41 +36,34 @@ 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;
List<StatementNode> statements = methodNode.statements;
for (StatementNode statement : statements) {
if(statement instanceof AssignmentStatementNode assignmentStatementNode) {
var result = assignmentStatementNode.accept(this);
valid = valid && result.isValid();
assignmentStatementNode.accept(this);
}
}
return new TypeCheckResult(valid, null);
return null;
}
@Override
@ -82,21 +73,20 @@ public class SemanticAnalyzer implements SemanticVisitor {
}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);
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);
return null;
}
}

View File

@ -3,13 +3,13 @@ package semantic;
import ast.ClassNode;
import ast.ProgramNode;
import ast.VarNode;
import ast.member.FieldNode;
import ast.member.MethodNode;
import ast.statement.AssignmentStatementNode;
import typechecker.TypeCheckResult;
public interface SemanticVisitor {
// TypeCheckResult typeCheck(ASTNode toCheck);
TypeCheckResult analyze(ProgramNode toCheck);
@ -20,8 +20,8 @@ public interface SemanticVisitor {
TypeCheckResult analyze(FieldNode toCheck);
TypeCheckResult analyze(AssignmentStatementNode toCheck);
TypeCheckResult analyze(VarNode toCheck);
//
// TypeCheckResult typeCheck(MethodParameter toCheck);
//
// TypeCheckResult typeCheck(ForStmt forStmt);
//