Compare commits
No commits in common. "6250ee781c4b67562f98e00f91d998d6f4b529a1" and "f5da71478f051518b9b8a6c8cd59fc2c6b7d7ace" have entirely different histories.
6250ee781c
...
f5da71478f
@ -1,4 +1,3 @@
|
|||||||
import ast.ASTNode;
|
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.ProgramNode;
|
import ast.ProgramNode;
|
||||||
import bytecode.ByteCodeGenerator;
|
import bytecode.ByteCodeGenerator;
|
||||||
@ -18,14 +17,14 @@ public class Main {
|
|||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
try {
|
try {
|
||||||
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/CompilerInput.txt"));
|
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/CompilerInput.txt"));
|
||||||
parseFile(codeCharStream);
|
parsefile(codeCharStream);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Error reading the file: " + e.getMessage());
|
System.err.println("Error reading the file: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void parseFile(CharStream codeCharStream){
|
static void parsefile(CharStream codeCharStream){
|
||||||
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
SimpleJavaParser parser = new SimpleJavaParser(tokens);
|
SimpleJavaParser parser = new SimpleJavaParser(tokens);
|
||||||
@ -35,9 +34,9 @@ public class Main {
|
|||||||
ASTBuilder builder = new ASTBuilder();
|
ASTBuilder builder = new ASTBuilder();
|
||||||
ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST
|
ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST
|
||||||
|
|
||||||
ProgramNode typedAst = (ProgramNode) SemanticAnalyzer.generateTast(ast);
|
SemanticAnalyzer.generateTast(ast);
|
||||||
|
|
||||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
||||||
byteCodeGenerator.visit(typedAst);
|
byteCodeGenerator.visit(ast);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,10 +1,6 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
import semantic.SemanticVisitor;
|
public class VarNode implements ASTNode{
|
||||||
import typechecker.TypeCheckResult;
|
|
||||||
import typechecker.Visitable;
|
|
||||||
|
|
||||||
public class VarNode implements ASTNode, Visitable {
|
|
||||||
|
|
||||||
private String identifier;
|
private String identifier;
|
||||||
private String type;
|
private String type;
|
||||||
@ -22,8 +18,4 @@ public class VarNode implements ASTNode, Visitable {
|
|||||||
return identifier;
|
return identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
|
||||||
return visitor.analyze(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package ast.expression;
|
package ast.expression;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import typechecker.Visitable;
|
|
||||||
|
|
||||||
public interface ExpressionNode extends ASTNode, Visitable {
|
public class ExpressionNode implements ASTNode {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
@ -204,7 +204,6 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
try {
|
try {
|
||||||
int intValue = Integer.parseInt(literalContext.getText());
|
int intValue = Integer.parseInt(literalContext.getText());
|
||||||
LiteralNode literalNode = new LiteralNode(intValue);
|
LiteralNode literalNode = new LiteralNode(intValue);
|
||||||
|
|
||||||
literalNode.setType("int");
|
literalNode.setType("int");
|
||||||
return literalNode;
|
return literalNode;
|
||||||
} catch (NumberFormatException ignored) {}
|
} catch (NumberFormatException ignored) {}
|
||||||
|
@ -12,8 +12,6 @@ import ast.statement.AssignmentStatementNode;
|
|||||||
import ast.statement.StatementNode;
|
import ast.statement.StatementNode;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import ast.type.TypeNode;
|
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public class SemanticAnalyzer implements SemanticVisitor {
|
public class SemanticAnalyzer implements SemanticVisitor {
|
||||||
@ -38,41 +36,34 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
List<ClassNode> classes = node.classes;
|
List<ClassNode> classes = node.classes;
|
||||||
for (ClassNode classNode : classes) {
|
for (ClassNode classNode : classes) {
|
||||||
var result = classNode.accept(this);
|
classNode.accept(this);
|
||||||
valid = valid && result.isValid();
|
|
||||||
}
|
}
|
||||||
return new TypeCheckResult(valid, null);
|
return new TypeCheckResult(valid, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(ClassNode classNode) {
|
public TypeCheckResult analyze(ClassNode classNode) {
|
||||||
var valid = true;
|
|
||||||
List<MemberNode> members = classNode.members;
|
List<MemberNode> members = classNode.members;
|
||||||
for (MemberNode memberNode : members) {
|
for (MemberNode memberNode : members) {
|
||||||
if (memberNode instanceof FieldNode fieldNode) {
|
if (memberNode instanceof FieldNode fieldNode) {
|
||||||
var result = fieldNode.accept(this);
|
fieldNode.accept(this);
|
||||||
valid = valid && result.isValid();
|
|
||||||
} else if (memberNode instanceof MethodNode methodNode) {
|
} else if (memberNode instanceof MethodNode methodNode) {
|
||||||
var result = methodNode.accept(this);
|
methodNode.accept(this);
|
||||||
valid = valid && result.isValid();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new TypeCheckResult(valid, null);
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(MethodNode methodNode) {
|
public TypeCheckResult analyze(MethodNode methodNode) {
|
||||||
var valid = true;
|
|
||||||
List<StatementNode> statements = methodNode.statements;
|
List<StatementNode> statements = methodNode.statements;
|
||||||
for (StatementNode statement : statements) {
|
for (StatementNode statement : statements) {
|
||||||
if(statement instanceof AssignmentStatementNode assignmentStatementNode) {
|
if(statement instanceof AssignmentStatementNode assignmentStatementNode) {
|
||||||
var result = assignmentStatementNode.accept(this);
|
assignmentStatementNode.accept(this);
|
||||||
valid = valid && result.isValid();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new TypeCheckResult(valid, null);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -82,21 +73,20 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
}else {
|
}else {
|
||||||
currentFields.add(toCheck.identifier);
|
currentFields.add(toCheck.identifier);
|
||||||
}
|
}
|
||||||
return new TypeCheckResult(true, null);
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(AssignmentStatementNode assignmentStatementNode) {
|
public TypeCheckResult analyze(AssignmentStatementNode assignmentStatementNode) {
|
||||||
if(assignmentStatementNode.expression instanceof LiteralNode literalNode) {
|
if(assignmentStatementNode.expression instanceof LiteralNode literalNode) {
|
||||||
TypeCheckResult varResult = assignmentStatementNode.varNode.accept(this);
|
VarNode varNode = assignmentStatementNode.varNode;
|
||||||
TypeCheckResult expressionResult = assignmentStatementNode.expression.accept(this);
|
if(varNode.getType().equals(literalNode.getType())) {
|
||||||
|
System.out.println("Type is same");
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("Type mismatch");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return new TypeCheckResult(true, null);
|
return null;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult analyze(VarNode toCheck) {
|
|
||||||
return new TypeCheckResult(true, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -3,13 +3,13 @@ package semantic;
|
|||||||
|
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.ProgramNode;
|
import ast.ProgramNode;
|
||||||
import ast.VarNode;
|
|
||||||
import ast.member.FieldNode;
|
import ast.member.FieldNode;
|
||||||
import ast.member.MethodNode;
|
import ast.member.MethodNode;
|
||||||
import ast.statement.AssignmentStatementNode;
|
import ast.statement.AssignmentStatementNode;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public interface SemanticVisitor {
|
public interface SemanticVisitor {
|
||||||
|
// TypeCheckResult typeCheck(ASTNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult analyze(ProgramNode toCheck);
|
TypeCheckResult analyze(ProgramNode toCheck);
|
||||||
|
|
||||||
@ -20,8 +20,8 @@ public interface SemanticVisitor {
|
|||||||
TypeCheckResult analyze(FieldNode toCheck);
|
TypeCheckResult analyze(FieldNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult analyze(AssignmentStatementNode toCheck);
|
TypeCheckResult analyze(AssignmentStatementNode toCheck);
|
||||||
|
//
|
||||||
TypeCheckResult analyze(VarNode toCheck);
|
// TypeCheckResult typeCheck(MethodParameter toCheck);
|
||||||
//
|
//
|
||||||
// TypeCheckResult typeCheck(ForStmt forStmt);
|
// TypeCheckResult typeCheck(ForStmt forStmt);
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user