code-generator #8
@ -5,9 +5,7 @@ public class Example {
|
|||||||
public static int testMethod(char b){
|
public static int testMethod(char b){
|
||||||
|
|
||||||
int a;
|
int a;
|
||||||
boolean b;
|
int a;
|
||||||
char c;
|
|
||||||
b = 3;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ import java.util.List;
|
|||||||
import bytecode.visitor.ClassVisitor;
|
import bytecode.visitor.ClassVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
import typechecker.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class ClassNode implements ASTNode, Visitable {
|
public class ClassNode implements ASTNode, Visitable {
|
||||||
public String identifier;
|
public String identifier;
|
||||||
|
@ -6,7 +6,7 @@ import java.util.List;
|
|||||||
import bytecode.visitor.ProgramVisitor;
|
import bytecode.visitor.ProgramVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
import typechecker.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class ProgramNode implements ASTNode, Visitable{
|
public class ProgramNode implements ASTNode, Visitable{
|
||||||
public List<ClassNode> classes = new ArrayList<>();
|
public List<ClassNode> classes = new ArrayList<>();
|
||||||
|
@ -2,7 +2,7 @@ package ast;
|
|||||||
|
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
import typechecker.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class VarNode implements ASTNode, Visitable {
|
public class VarNode implements ASTNode, Visitable {
|
||||||
|
|
||||||
|
@ -2,8 +2,9 @@ package ast.expression;
|
|||||||
|
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class BinaryExpressionNode implements ExpressionNode {
|
public class BinaryExpressionNode implements ExpressionNode, Visitable {
|
||||||
public ExpressionNode left;
|
public ExpressionNode left;
|
||||||
public ExpressionNode right;
|
public ExpressionNode right;
|
||||||
public String operator; // Stores the operator as a string (e.g., "+", "-", "&&")
|
public String operator; // Stores the operator as a string (e.g., "+", "-", "&&")
|
||||||
@ -16,6 +17,6 @@ public class BinaryExpressionNode implements ExpressionNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return null;
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package ast.expression;
|
package ast.expression;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import typechecker.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public interface ExpressionNode extends ASTNode, Visitable {
|
public interface ExpressionNode extends ASTNode, Visitable {
|
||||||
|
|
||||||
|
@ -1,11 +1,15 @@
|
|||||||
package ast.expression;
|
package ast.expression;
|
||||||
|
|
||||||
|
import ast.type.TypeNode;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
|
||||||
import typechecker.Visitable;
|
|
||||||
|
|
||||||
public class IdentifierExpressionNode implements ExpressionNode {
|
import typechecker.TypeCheckResult;
|
||||||
|
import visitor.Visitable;
|
||||||
|
|
||||||
|
public class IdentifierExpressionNode implements ExpressionNode, Visitable {
|
||||||
public String name;
|
public String name;
|
||||||
|
public TypeNode type;
|
||||||
|
|
||||||
|
|
||||||
public IdentifierExpressionNode(String name) {
|
public IdentifierExpressionNode(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@ -13,6 +17,6 @@ public class IdentifierExpressionNode implements ExpressionNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return null;
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,9 @@ package ast.expression;
|
|||||||
|
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class UnaryExpressionNode implements ExpressionNode {
|
public class UnaryExpressionNode implements ExpressionNode, Visitable {
|
||||||
public ExpressionNode expression;
|
public ExpressionNode expression;
|
||||||
public String operator; // Stores the operator (e.g., "-", "!")
|
public String operator; // Stores the operator (e.g., "-", "!")
|
||||||
|
|
||||||
@ -14,6 +15,6 @@ public class UnaryExpressionNode implements ExpressionNode {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return null;
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ package ast.member;
|
|||||||
|
|
||||||
import ast.type.AccessTypeNode;
|
import ast.type.AccessTypeNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import typechecker.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class ConstructorNode extends MethodNode implements Visitable {
|
public class ConstructorNode extends MethodNode implements Visitable {
|
||||||
public ConstructorNode(AccessTypeNode visibility, String name) {
|
public ConstructorNode(AccessTypeNode visibility, String name) {
|
||||||
|
@ -5,7 +5,7 @@ import ast.type.TypeNode;
|
|||||||
import bytecode.visitor.ClassVisitor;
|
import bytecode.visitor.ClassVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
import typechecker.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class FieldNode implements MemberNode, Visitable {
|
public class FieldNode implements MemberNode, Visitable {
|
||||||
public AccessTypeNode accessTypeNode;
|
public AccessTypeNode accessTypeNode;
|
||||||
|
@ -3,15 +3,15 @@ package ast.member;
|
|||||||
import ast.parameter.ParameterListNode;
|
import ast.parameter.ParameterListNode;
|
||||||
import ast.statement.StatementNode;
|
import ast.statement.StatementNode;
|
||||||
import ast.type.AccessTypeNode;
|
import ast.type.AccessTypeNode;
|
||||||
import ast.type.TypeNode;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import ast.type.TypeNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
import typechecker.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class MethodNode implements MemberNode, Visitable {
|
public class MethodNode implements MemberNode, Visitable {
|
||||||
public AccessTypeNode visibility;
|
public AccessTypeNode visibility;
|
||||||
|
@ -4,7 +4,7 @@ import ast.VarNode;
|
|||||||
import ast.expression.ExpressionNode;
|
import ast.expression.ExpressionNode;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
import typechecker.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class AssignmentStatementNode extends StatementNode implements Visitable {
|
public class AssignmentStatementNode extends StatementNode implements Visitable {
|
||||||
public VarNode varNode;
|
public VarNode varNode;
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package ast.statement;
|
package ast.statement;
|
||||||
|
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expression.ExpressionNode;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public class IfStatementNode extends StatementNode {
|
public class IfStatementNode extends StatementNode {
|
||||||
public ExpressionNode condition;
|
public ExpressionNode condition;
|
||||||
@ -12,4 +14,9 @@ public class IfStatementNode extends StatementNode {
|
|||||||
this.thenStatement = thenStatement;
|
this.thenStatement = thenStatement;
|
||||||
this.elseStatement = elseStatement;
|
this.elseStatement = elseStatement;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
package ast.statement;
|
package ast.statement;
|
||||||
|
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expression.ExpressionNode;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public class ReturnStatementNode extends StatementNode {
|
public class ReturnStatementNode extends StatementNode {
|
||||||
public ExpressionNode expression;
|
public ExpressionNode expression;
|
||||||
@ -8,4 +10,9 @@ public class ReturnStatementNode extends StatementNode {
|
|||||||
public ReturnStatementNode(ExpressionNode expression) {
|
public ReturnStatementNode(ExpressionNode expression) {
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,7 +1,8 @@
|
|||||||
package ast.statement;
|
package ast.statement;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
|
import visitor.Visitable;
|
||||||
|
|
||||||
public abstract class StatementNode implements ASTNode {
|
public abstract class StatementNode implements ASTNode, Visitable {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
package ast.statement;
|
package ast.statement;
|
||||||
|
|
||||||
import ast.type.TypeNode;
|
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expression.ExpressionNode;
|
||||||
|
import ast.type.TypeNode;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public class VariableDeclarationStatementNode extends StatementNode {
|
public class VariableDeclarationStatementNode extends StatementNode {
|
||||||
public TypeNode type;
|
public TypeNode type;
|
||||||
@ -12,4 +14,9 @@ public class VariableDeclarationStatementNode extends StatementNode {
|
|||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
package ast.statement;
|
package ast.statement;
|
||||||
|
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expression.ExpressionNode;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public class WhileStatementNode extends StatementNode {
|
public class WhileStatementNode extends StatementNode {
|
||||||
public ExpressionNode condition;
|
public ExpressionNode condition;
|
||||||
@ -10,4 +12,9 @@ public class WhileStatementNode extends StatementNode {
|
|||||||
this.condition = condition;
|
this.condition = condition;
|
||||||
this.body = body;
|
this.body = body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package ast.type;
|
package ast.type;
|
||||||
|
|
||||||
public class BaseTypeNode implements TypeNode{
|
import ast.ASTNode;
|
||||||
|
|
||||||
|
public class BaseTypeNode implements ASTNode, TypeNode {
|
||||||
|
|
||||||
public EnumTypeNode enumType;
|
public EnumTypeNode enumType;
|
||||||
|
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
package ast.type;
|
package ast.type;
|
||||||
|
|
||||||
public class ReferenceTypeNode implements TypeNode{
|
import ast.ASTNode;
|
||||||
|
|
||||||
|
public class ReferenceTypeNode implements ASTNode, TypeNode {
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import ast.parameter.ParameterListNode;
|
|||||||
import ast.parameter.ParameterNode;
|
import ast.parameter.ParameterNode;
|
||||||
import ast.type.*;
|
import ast.type.*;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
|
import ast.type.BaseTypeNode;
|
||||||
|
|
||||||
public class Mapper {
|
public class Mapper {
|
||||||
public int mapAccessTypeToOpcode(AccessTypeNode type) {
|
public int mapAccessTypeToOpcode(AccessTypeNode type) {
|
||||||
|
@ -13,10 +13,12 @@ import ast.parameter.ParameterNode;
|
|||||||
import ast.statement.*;
|
import ast.statement.*;
|
||||||
import ast.type.*;
|
import ast.type.*;
|
||||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import parser.generated.*;
|
import parser.generated.*;
|
||||||
import parser.generated.SimpleJavaParser.LiteralContext;
|
import parser.generated.SimpleJavaParser.LiteralContext;
|
||||||
|
import ast.type.BaseTypeNode;
|
||||||
|
|
||||||
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||||
@Override
|
@Override
|
||||||
@ -74,9 +76,9 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) {
|
public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) {
|
||||||
TypeNode type = (TypeNode) visit(ctx.type());
|
TypeNode typeNode = (TypeNode) visit(ctx.type());
|
||||||
String identifier = ctx.IDENTIFIER().getText();
|
String identifier = ctx.IDENTIFIER().getText();
|
||||||
return new ParameterNode(type, identifier);
|
return new ParameterNode(typeNode, identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
36
src/main/java/semantic/Scope.java
Normal file
36
src/main/java/semantic/Scope.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
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,101 +2,145 @@ package semantic;
|
|||||||
|
|
||||||
|
|
||||||
import ast.*;
|
import ast.*;
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expression.BinaryExpressionNode;
|
||||||
import ast.member.ConstructorNode;
|
import ast.expression.IdentifierExpressionNode;
|
||||||
|
import ast.expression.UnaryExpressionNode;
|
||||||
import ast.member.FieldNode;
|
import ast.member.FieldNode;
|
||||||
import ast.member.MemberNode;
|
import ast.member.MemberNode;
|
||||||
|
|
||||||
import ast.member.MethodNode;
|
import ast.member.MethodNode;
|
||||||
import ast.statement.AssignmentStatementNode;
|
import ast.statement.*;
|
||||||
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 {
|
||||||
|
|
||||||
private ArrayList<String> currentFields = new ArrayList<>();
|
private ArrayList<String> currentFields = new ArrayList<>();
|
||||||
|
|
||||||
public static ASTNode generateTast(ASTNode node) throws RuntimeException {
|
private Scope currentScope;
|
||||||
SemanticAnalyzer semanticCheck = new SemanticAnalyzer();
|
|
||||||
ProgramNode programNode = (ProgramNode) node;
|
|
||||||
var result = programNode.accept(semanticCheck);
|
|
||||||
if (result.isValid()) {
|
|
||||||
return node;
|
|
||||||
} else {
|
|
||||||
throw new RuntimeException("Not Valid");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
public static ASTNode generateTast(ASTNode node) throws RuntimeException {
|
||||||
public TypeCheckResult analyze(ProgramNode node) {
|
SemanticAnalyzer semanticCheck = new SemanticAnalyzer();
|
||||||
|
ProgramNode programNode = (ProgramNode) node;
|
||||||
var valid = true;
|
var result = programNode.accept(semanticCheck);
|
||||||
|
if (result.isValid()) {
|
||||||
List<ClassNode> classes = node.classes;
|
return node;
|
||||||
for (ClassNode classNode : classes) {
|
} else {
|
||||||
var result = classNode.accept(this);
|
throw new RuntimeException("Not Valid");
|
||||||
valid = valid && result.isValid();
|
}
|
||||||
}
|
|
||||||
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();
|
|
||||||
} else if (memberNode instanceof MethodNode methodNode) {
|
|
||||||
var result = methodNode.accept(this);
|
|
||||||
valid = valid && result.isValid();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new TypeCheckResult(valid, null);
|
@Override
|
||||||
|
public TypeCheckResult analyze(ProgramNode node) {
|
||||||
|
|
||||||
}
|
var valid = true;
|
||||||
|
|
||||||
@Override
|
List<ClassNode> classes = node.classes;
|
||||||
public TypeCheckResult analyze(MethodNode methodNode) {
|
for (ClassNode classNode : classes) {
|
||||||
var valid = true;
|
var result = classNode.accept(this);
|
||||||
List<StatementNode> statements = methodNode.statements;
|
valid = valid && result.isValid();
|
||||||
for (StatementNode statement : statements) {
|
}
|
||||||
if(statement instanceof AssignmentStatementNode assignmentStatementNode) {
|
return new TypeCheckResult(valid, null);
|
||||||
var result = assignmentStatementNode.accept(this);
|
|
||||||
valid = valid && result.isValid();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return new TypeCheckResult(valid, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(FieldNode toCheck) {
|
public TypeCheckResult analyze(ClassNode classNode) {
|
||||||
if(currentFields.contains(toCheck.identifier)){
|
var valid = true;
|
||||||
throw new RuntimeException(toCheck.identifier + " Is Already Declared");
|
List<MemberNode> members = classNode.members;
|
||||||
}else {
|
for (MemberNode memberNode : members) {
|
||||||
currentFields.add(toCheck.identifier);
|
if (memberNode instanceof FieldNode fieldNode) {
|
||||||
|
var result = fieldNode.accept(this);
|
||||||
|
valid = valid && result.isValid();
|
||||||
|
} else if (memberNode instanceof MethodNode methodNode) {
|
||||||
|
var result = methodNode.accept(this);
|
||||||
|
valid = valid && result.isValid();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new TypeCheckResult(valid, null);
|
||||||
|
|
||||||
}
|
}
|
||||||
return new TypeCheckResult(true, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(AssignmentStatementNode assignmentStatementNode) {
|
public TypeCheckResult analyze(MethodNode methodNode) {
|
||||||
if(assignmentStatementNode.expression instanceof LiteralNode literalNode) {
|
var valid = true;
|
||||||
TypeCheckResult varResult = assignmentStatementNode.varNode.accept(this);
|
|
||||||
TypeCheckResult expressionResult = assignmentStatementNode.expression.accept(this);
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new TypeCheckResult(valid, null);
|
||||||
}
|
}
|
||||||
return new TypeCheckResult(true, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(VarNode toCheck) {
|
public TypeCheckResult analyze(FieldNode toCheck) {
|
||||||
return new TypeCheckResult(true, null);
|
if (currentFields.contains(toCheck.identifier)) {
|
||||||
}
|
throw new RuntimeException(toCheck.identifier + " Is Already Declared");
|
||||||
|
} else {
|
||||||
|
currentFields.add(toCheck.identifier);
|
||||||
|
}
|
||||||
|
return new TypeCheckResult(true, 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);
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -4,9 +4,12 @@ package semantic;
|
|||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.ProgramNode;
|
import ast.ProgramNode;
|
||||||
import ast.VarNode;
|
import ast.VarNode;
|
||||||
|
import ast.expression.BinaryExpressionNode;
|
||||||
|
import ast.expression.IdentifierExpressionNode;
|
||||||
|
import ast.expression.UnaryExpressionNode;
|
||||||
import ast.member.FieldNode;
|
import ast.member.FieldNode;
|
||||||
import ast.member.MethodNode;
|
import ast.member.MethodNode;
|
||||||
import ast.statement.AssignmentStatementNode;
|
import ast.statement.*;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public interface SemanticVisitor {
|
public interface SemanticVisitor {
|
||||||
@ -22,41 +25,18 @@ public interface SemanticVisitor {
|
|||||||
TypeCheckResult analyze(AssignmentStatementNode toCheck);
|
TypeCheckResult analyze(AssignmentStatementNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult analyze(VarNode toCheck);
|
TypeCheckResult analyze(VarNode 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(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);
|
||||||
}
|
}
|
@ -1,5 +0,0 @@
|
|||||||
package typechecker;
|
|
||||||
|
|
||||||
public interface Type {
|
|
||||||
boolean equals(Object obj);
|
|
||||||
}
|
|
@ -1,12 +1,14 @@
|
|||||||
package typechecker;
|
package typechecker;
|
||||||
|
|
||||||
|
|
||||||
|
import ast.type.TypeNode;
|
||||||
|
|
||||||
public class TypeCheckResult {
|
public class TypeCheckResult {
|
||||||
|
|
||||||
private boolean valid;
|
private boolean valid;
|
||||||
private Type type;
|
private TypeNode type;
|
||||||
|
|
||||||
public TypeCheckResult(boolean valid, Type type) {
|
public TypeCheckResult(boolean valid, TypeNode type) {
|
||||||
this.valid = valid;
|
this.valid = valid;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
@ -15,7 +17,7 @@ public class TypeCheckResult {
|
|||||||
return valid;
|
return valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Type getType() {
|
public TypeNode getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,9 +0,0 @@
|
|||||||
package typechecker;
|
|
||||||
|
|
||||||
public class Typer {
|
|
||||||
|
|
||||||
public static void Typeify(){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,9 +1,10 @@
|
|||||||
package typechecker;
|
package visitor;
|
||||||
|
|
||||||
import bytecode.visitor.ClassVisitor;
|
import bytecode.visitor.ClassVisitor;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import bytecode.visitor.ProgramVisitor;
|
import bytecode.visitor.ProgramVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public interface Visitable {
|
public interface Visitable {
|
||||||
default void accept(ProgramVisitor programVisitor) {
|
default void accept(ProgramVisitor programVisitor) {
|
Loading…
Reference in New Issue
Block a user