Compare commits
11 Commits
8b6189ea33
...
f781d8eeb6
Author | SHA1 | Date | |
---|---|---|---|
|
f781d8eeb6 | ||
|
b64e2efc82 | ||
|
33df2c1c0a | ||
|
6318085c00 | ||
|
a8638fe70c | ||
|
1441e34078 | ||
|
dfe96d9a87 | ||
8e7b64c38d | |||
d27ec0643a | |||
79a140b683 | |||
|
1fbec0d754 |
2
.idea/misc.xml
generated
2
.idea/misc.xml
generated
@ -40,7 +40,7 @@
|
|||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -1,7 +1,13 @@
|
|||||||
public class Example {
|
public class Example {
|
||||||
|
|
||||||
public int testVar;
|
public int testVar;
|
||||||
|
|
||||||
public static int testMethod(char b){
|
public static int testMethod(char b){
|
||||||
|
|
||||||
int a;
|
int a;
|
||||||
a = 3;
|
int a;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,6 +1,9 @@
|
|||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import org.antlr.v4.runtime.*;
|
import org.antlr.v4.runtime.*;
|
||||||
import org.antlr.v4.runtime.Token;
|
import org.antlr.v4.runtime.Token;
|
||||||
|
import ast.ClassNode;
|
||||||
|
import ast.ProgramNode;
|
||||||
|
import bytecode.ByteCodeGenerator;
|
||||||
import org.antlr.v4.runtime.CharStream;
|
import org.antlr.v4.runtime.CharStream;
|
||||||
import org.antlr.v4.runtime.CharStreams;
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
import org.antlr.v4.runtime.tree.ParseTree;
|
import org.antlr.v4.runtime.tree.ParseTree;
|
||||||
@ -20,14 +23,13 @@ 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){
|
|
||||||
/* ------------------------- Scanner -> tokens ------------------------- */
|
/* ------------------------- Scanner -> tokens ------------------------- */
|
||||||
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
||||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||||
@ -39,7 +41,8 @@ public class Main {
|
|||||||
for (Token token : tokens) {
|
for (Token token : tokens) {
|
||||||
String tokenType = SimpleJavaLexer.VOCABULARY.getSymbolicName(token.getType());
|
String tokenType = SimpleJavaLexer.VOCABULARY.getSymbolicName(token.getType());
|
||||||
String tokenText = token.getText();
|
String tokenText = token.getText();
|
||||||
// System.out.println("Token Type: " + tokenType + ", Token Text: " + tokenText);
|
// System.out.println("Token Type: " + tokenType + ", Token Text: " +
|
||||||
|
// tokenText);
|
||||||
System.out.println(tokenType + " " + tokenText);
|
System.out.println(tokenType + " " + tokenText);
|
||||||
}
|
}
|
||||||
System.out.println();
|
System.out.println();
|
||||||
@ -50,9 +53,10 @@ public class Main {
|
|||||||
|
|
||||||
// Printing the parse tree
|
// Printing the parse tree
|
||||||
System.out.println("-------------------- Parser -> Parsetree --------------------");
|
System.out.println("-------------------- Parser -> Parsetree --------------------");
|
||||||
System.out.println (parseTree.toStringTree(parser));
|
System.out.println(parseTree.toStringTree(parser));
|
||||||
printTree(parseTree, parser, 0);
|
printTree(parseTree, parser, 0);
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
ProgramNode typedAst = (ProgramNode) SemanticAnalyzer.generateTast(ast);
|
||||||
|
|
||||||
/* ------------------------- AST builder -> AST ------------------------- */
|
/* ------------------------- AST builder -> AST ------------------------- */
|
||||||
ASTBuilder astBuilder = new ASTBuilder();
|
ASTBuilder astBuilder = new ASTBuilder();
|
||||||
@ -60,30 +64,36 @@ public class Main {
|
|||||||
|
|
||||||
// Printing the AST
|
// Printing the AST
|
||||||
System.out.println("-------------------- AST builder -> AST --------------------");
|
System.out.println("-------------------- AST builder -> AST --------------------");
|
||||||
//System.out.println("AST: " + ast.toString());
|
// System.out.println("AST: " + ast.toString());
|
||||||
printAST(abstractSyntaxTree, 0);
|
printAST(abstractSyntaxTree, 0);
|
||||||
System.out.println();
|
System.out.println();
|
||||||
|
|
||||||
/* ------------------------- Semantic Analyzer -> Tast ------------------------- */
|
/*
|
||||||
|
* ------------------------- Semantic Analyzer -> Tast -------------------------
|
||||||
|
*/
|
||||||
SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||||
|
|
||||||
// Printing the Tast
|
// Printing the Tast
|
||||||
System.out.println("Tast generated");
|
System.out.println("Tast generated");
|
||||||
|
|
||||||
/* ------------------------- Bytecode Generator -> Bytecode ------------------------- */
|
/*
|
||||||
|
* ------------------------- Bytecode Generator -> Bytecode
|
||||||
|
* -------------------------
|
||||||
|
*/
|
||||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
||||||
byteCodeGenerator.generateByteCode(abstractSyntaxTree);
|
byteCodeGenerator.generateByteCode(abstractSyntaxTree);
|
||||||
System.out.println("Bytecode generated");
|
System.out.println("Bytecode generated");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is used to print the parse tree in a structured format.
|
* This method is used to print the parse tree in a structured format.
|
||||||
* It recursively traverses the tree and prints the rule names and text of the nodes.
|
* It recursively traverses the tree and prints the rule names and text of the
|
||||||
|
* nodes.
|
||||||
*
|
*
|
||||||
* @param tree The parse tree to be printed.
|
* @param tree The parse tree to be printed.
|
||||||
* @param parser The parser used to parse the input. It's used to get the rule names.
|
* @param parser The parser used to parse the input. It's used to get the rule
|
||||||
|
* names.
|
||||||
* @param indent The current indentation level. It's used to format the output.
|
* @param indent The current indentation level. It's used to format the output.
|
||||||
*/
|
*/
|
||||||
public static void printTree(ParseTree tree, Parser parser, int indent) {
|
public static void printTree(ParseTree tree, Parser parser, int indent) {
|
||||||
@ -102,19 +112,20 @@ public class Main {
|
|||||||
System.out.println(indentString + tree.getText());
|
System.out.println(indentString + tree.getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recursively print the children of the current node, increasing the indentation level
|
// Recursively print the children of the current node, increasing the
|
||||||
|
// indentation level
|
||||||
for (int i = 0; i < tree.getChildCount(); i++) {
|
for (int i = 0; i < tree.getChildCount(); i++) {
|
||||||
printTree(tree.getChild(i), parser, indent + 1);
|
printTree(tree.getChild(i), parser, indent + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void printAST(ASTNode node, int indent) {
|
public static void printAST(ASTNode node, int indent) {
|
||||||
String indentString = " ".repeat(indent * 2);
|
String indentString = " ".repeat(indent * 2);
|
||||||
System.out.println(indentString + node.getClass().toString());
|
System.out.println(indentString + node.getClass().toString());
|
||||||
|
|
||||||
for (ASTNode child : node.) {
|
// for (ASTNode child : node.) {
|
||||||
printAST(child, indent + 1);
|
// printAST(child, indent + 1);
|
||||||
}
|
// }
|
||||||
|
byteCodeGenerator.visit(typedAst);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
public class ASTNode {
|
public interface ASTNode {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,11 +7,13 @@ import ast.type.EnumAccessTypeNode;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
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 extends ASTNode implements Visitable {
|
public class ClassNode implements ASTNode, Visitable {
|
||||||
public String identifier;
|
public String identifier;
|
||||||
public AccessTypeNode accessType;
|
public AccessTypeNode accessType;
|
||||||
public List<MemberNode> members = new ArrayList<>();
|
public List<MemberNode> members = new ArrayList<>();
|
||||||
@ -38,6 +40,11 @@ public class ClassNode extends ASTNode implements Visitable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.typeCheck(this);
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(ClassVisitor classVisitor) {
|
||||||
|
classVisitor.visit(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expression.ExpressionNode;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public class LiteralNode extends ExpressionNode {
|
public class LiteralNode implements ExpressionNode {
|
||||||
|
|
||||||
int value;
|
int value;
|
||||||
private String type;
|
private String type;
|
||||||
@ -20,4 +22,8 @@ public class LiteralNode extends ExpressionNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,13 @@ package ast;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
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 extends ASTNode implements Visitable{
|
public class ProgramNode implements ASTNode, Visitable{
|
||||||
public List<ClassNode> classes = new ArrayList<>();
|
public List<ClassNode> classes = new ArrayList<>();
|
||||||
|
|
||||||
public void addClass(ClassNode classNode) {
|
public void addClass(ClassNode classNode) {
|
||||||
@ -15,6 +17,11 @@ public class ProgramNode extends ASTNode implements Visitable{
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.typeCheck(this);
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(ProgramVisitor programVisitor) {
|
||||||
|
programVisitor.visit(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,10 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
public class VarNode extends ASTNode{
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
import visitor.Visitable;
|
||||||
|
|
||||||
|
public class VarNode implements ASTNode, Visitable {
|
||||||
|
|
||||||
private String identifier;
|
private String identifier;
|
||||||
private String type;
|
private String type;
|
||||||
@ -18,4 +22,8 @@ public class VarNode extends ASTNode{
|
|||||||
return identifier;
|
return identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package ast.expression;
|
package ast.expression;
|
||||||
|
|
||||||
public class BinaryExpressionNode extends ExpressionNode {
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
import visitor.Visitable;
|
||||||
|
|
||||||
|
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., "+", "-", "&&")
|
||||||
@ -10,4 +14,9 @@ public class BinaryExpressionNode extends ExpressionNode {
|
|||||||
this.right = right;
|
this.right = right;
|
||||||
this.operator = operator;
|
this.operator = operator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,9 +1,8 @@
|
|||||||
package ast.expression;
|
package ast.expression;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class ExpressionNode extends ASTNode {
|
public interface ExpressionNode extends ASTNode, Visitable {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,22 @@
|
|||||||
package ast.expression;
|
package ast.expression;
|
||||||
|
|
||||||
public class IdentifierExpressionNode extends ExpressionNode {
|
import ast.type.TypeNode;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package ast.expression;
|
package ast.expression;
|
||||||
|
|
||||||
public class UnaryExpressionNode extends ExpressionNode {
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
import visitor.Visitable;
|
||||||
|
|
||||||
|
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., "-", "!")
|
||||||
|
|
||||||
@ -8,4 +12,9 @@ public class UnaryExpressionNode extends ExpressionNode {
|
|||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
this.operator = operator;
|
this.operator = operator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,16 @@
|
|||||||
package ast.member;
|
package ast.member;
|
||||||
|
|
||||||
import ast.type.AccessTypeNode;
|
import ast.type.AccessTypeNode;
|
||||||
|
import bytecode.visitor.MethodVisitor;
|
||||||
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class ConstructorNode extends MethodNode {
|
public class ConstructorNode extends MethodNode implements Visitable {
|
||||||
public ConstructorNode(AccessTypeNode visibility, String name) {
|
public ConstructorNode(AccessTypeNode visibility, String name) {
|
||||||
super(visibility, name);
|
super(visibility, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(MethodVisitor methodVisitor) {
|
||||||
|
methodVisitor.visit(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,12 @@ package ast.member;
|
|||||||
|
|
||||||
import ast.type.AccessTypeNode;
|
import ast.type.AccessTypeNode;
|
||||||
import ast.type.TypeNode;
|
import ast.type.TypeNode;
|
||||||
|
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 extends MemberNode implements Visitable {
|
public class FieldNode implements MemberNode, Visitable {
|
||||||
public AccessTypeNode accessTypeNode;
|
public AccessTypeNode accessTypeNode;
|
||||||
public TypeNode type;
|
public TypeNode type;
|
||||||
public String identifier;
|
public String identifier;
|
||||||
@ -19,6 +20,11 @@ public class FieldNode extends MemberNode implements Visitable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.typeCheck(this);
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(ClassVisitor classVisitor) {
|
||||||
|
classVisitor.visit(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,5 +2,5 @@ package ast.member;
|
|||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
|
|
||||||
public class MemberNode extends ASTNode {
|
public interface MemberNode extends ASTNode {
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,17 @@ 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 semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
import typechecker.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class MethodNode extends MemberNode implements Visitable {
|
public class MethodNode implements MemberNode, Visitable {
|
||||||
public AccessTypeNode visibility;
|
public AccessTypeNode visibility;
|
||||||
public TypeNode type;
|
public TypeNode type;
|
||||||
public String identifier;
|
public String identifier;
|
||||||
@ -36,6 +38,11 @@ public class MethodNode extends MemberNode implements Visitable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.typeCheck(this);
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(MethodVisitor methodVisitor) {
|
||||||
|
methodVisitor.visit(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import ast.ASTNode;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ParameterListNode extends ASTNode {
|
public class ParameterListNode implements ASTNode {
|
||||||
List<ParameterNode> parameters = new ArrayList<>();
|
List<ParameterNode> parameters = new ArrayList<>();
|
||||||
|
|
||||||
public ParameterListNode(List<ParameterNode> parameters){
|
public ParameterListNode(List<ParameterNode> parameters){
|
||||||
|
@ -3,7 +3,7 @@ package ast.parameter;
|
|||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.type.TypeNode;
|
import ast.type.TypeNode;
|
||||||
|
|
||||||
public class ParameterNode extends ASTNode {
|
public class ParameterNode implements ASTNode {
|
||||||
public TypeNode type;
|
public TypeNode type;
|
||||||
public String identifier;
|
public String identifier;
|
||||||
|
|
||||||
|
@ -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;
|
||||||
@ -17,6 +17,6 @@ public class AssignmentStatementNode extends StatementNode implements Visitable
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.typeCheck(this);
|
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 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 extends 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ package ast.type;
|
|||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
|
|
||||||
public class AccessTypeNode extends ASTNode {
|
public class AccessTypeNode implements ASTNode {
|
||||||
public EnumAccessTypeNode enumAccessTypeNode;
|
public EnumAccessTypeNode enumAccessTypeNode;
|
||||||
|
|
||||||
public AccessTypeNode(EnumAccessTypeNode enumAccessTypeNode) {
|
public AccessTypeNode(EnumAccessTypeNode enumAccessTypeNode) {
|
||||||
|
13
src/main/java/ast/type/BaseTypeNode.java
Normal file
13
src/main/java/ast/type/BaseTypeNode.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package ast.type;
|
||||||
|
|
||||||
|
import ast.ASTNode;
|
||||||
|
|
||||||
|
public class BaseTypeNode implements ASTNode, TypeNode {
|
||||||
|
|
||||||
|
public EnumTypeNode enumType;
|
||||||
|
|
||||||
|
public BaseTypeNode(EnumTypeNode enumType) {
|
||||||
|
this.enumType = enumType;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
6
src/main/java/ast/type/ReferenceTypeNode.java
Normal file
6
src/main/java/ast/type/ReferenceTypeNode.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package ast.type;
|
||||||
|
|
||||||
|
import ast.ASTNode;
|
||||||
|
|
||||||
|
public class ReferenceTypeNode implements ASTNode, TypeNode {
|
||||||
|
}
|
@ -2,10 +2,5 @@ package ast.type;
|
|||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
|
|
||||||
public class TypeNode extends ASTNode {
|
public interface TypeNode extends ASTNode {
|
||||||
public EnumTypeNode enumTypeNode;
|
|
||||||
|
|
||||||
public TypeNode(EnumTypeNode enumTypeNode) {
|
|
||||||
this.enumTypeNode = enumTypeNode;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -2,13 +2,15 @@ package bytecode;
|
|||||||
|
|
||||||
import ast.ProgramNode;
|
import ast.ProgramNode;
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
|
import bytecode.visitor.ProgramVisitor;
|
||||||
|
|
||||||
public class ByteCodeGenerator {
|
public class ByteCodeGenerator implements ProgramVisitor {
|
||||||
|
|
||||||
public void generateByteCode(ProgramNode ast) {
|
@Override
|
||||||
for (ClassNode classDeclarationNode : ast.classes) {
|
public void visit(ProgramNode programNode) {
|
||||||
|
for (ClassNode classDeclarationNode : programNode.classes) {
|
||||||
ClassCodeGen classCodeGen = new ClassCodeGen();
|
ClassCodeGen classCodeGen = new ClassCodeGen();
|
||||||
classCodeGen.generateClassCode(classDeclarationNode);
|
classDeclarationNode.accept(classCodeGen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,8 @@ import ast.ClassNode;
|
|||||||
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.type.BaseTypeNode;
|
||||||
|
import bytecode.visitor.ClassVisitor;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import org.objectweb.asm.ClassWriter;
|
import org.objectweb.asm.ClassWriter;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
@ -11,20 +13,22 @@ import org.objectweb.asm.Opcodes;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public class ClassCodeGen {
|
public class ClassCodeGen implements ClassVisitor {
|
||||||
public void generateClassCode(ClassNode classNode) {
|
Mapper mapper = new Mapper();
|
||||||
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
ClassWriter classWriter;
|
||||||
Mapper mapper = new Mapper();
|
|
||||||
|
@Override
|
||||||
|
public void visit(ClassNode classNode) {
|
||||||
|
classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||||
classWriter.visit(Opcodes.V1_8, mapper.mapAccesTypeToOpcode(classNode.accessType), classNode.identifier, null,
|
classWriter.visit(Opcodes.V1_8, mapper.mapAccesTypeToOpcode(classNode.accessType), classNode.identifier, null,
|
||||||
"java/lang/Object", null);
|
"java/lang/Object", null);
|
||||||
|
|
||||||
for (MemberNode memberNode : classNode.members) {
|
for (MemberNode memberNode : classNode.members) {
|
||||||
if (memberNode instanceof FieldNode) {
|
if (memberNode instanceof FieldNode) {
|
||||||
FieldCodeGen fieldCodeGen = new FieldCodeGen();
|
visit((FieldNode) memberNode);
|
||||||
fieldCodeGen.generateFieldCode(classWriter, (FieldNode) memberNode);
|
|
||||||
} else if (memberNode instanceof MethodNode) {
|
} else if (memberNode instanceof MethodNode) {
|
||||||
MethodCodeGen methodCodeGen = new MethodCodeGen();
|
MethodCodeGen methodCodeGen = new MethodCodeGen(classWriter);
|
||||||
methodCodeGen.generateMethodCode(classWriter, (MethodNode) memberNode);
|
((MethodNode) memberNode).accept(methodCodeGen);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,6 +38,14 @@ public class ClassCodeGen {
|
|||||||
classWriter.visitEnd();
|
classWriter.visitEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(FieldNode fieldNode) {
|
||||||
|
if(fieldNode.type instanceof BaseTypeNode baseTypeNode){
|
||||||
|
classWriter.visitField(mapper.mapAccesTypeToOpcode(fieldNode.accessTypeNode), fieldNode.identifier, mapper.getTypeChar(baseTypeNode.enumType), null, null );
|
||||||
|
}
|
||||||
|
classWriter.visitEnd();
|
||||||
|
}
|
||||||
|
|
||||||
private void printIntoClassFile(byte[] byteCode, String name) {
|
private void printIntoClassFile(byte[] byteCode, String name) {
|
||||||
String directoryPath = "src/main/java/classFileOutput";
|
String directoryPath = "src/main/java/classFileOutput";
|
||||||
File directory = new File(directoryPath);
|
File directory = new File(directoryPath);
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package bytecode;
|
package bytecode;
|
||||||
|
|
||||||
import ast.type.AccessTypeNode;
|
import ast.type.*;
|
||||||
import ast.type.EnumAccessTypeNode;
|
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
|
import ast.type.BaseTypeNode;
|
||||||
|
|
||||||
public class Mapper {
|
public class Mapper {
|
||||||
public int mapAccesTypeToOpcode(AccessTypeNode type) {
|
public int mapAccesTypeToOpcode(AccessTypeNode type) {
|
||||||
@ -14,4 +14,26 @@ public class Mapper {
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String generateMethodDescriptor(BaseTypeNode baseTypeNode) {
|
||||||
|
String descriptor = "()";
|
||||||
|
descriptor += getTypeChar(baseTypeNode.enumType);
|
||||||
|
return descriptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTypeChar(EnumTypeNode enumTypeNode) {
|
||||||
|
String typeChar = "";
|
||||||
|
switch (enumTypeNode) {
|
||||||
|
case EnumTypeNode.INT:
|
||||||
|
typeChar = "I";
|
||||||
|
break;
|
||||||
|
case EnumTypeNode.CHAR:
|
||||||
|
typeChar = "C";
|
||||||
|
break;
|
||||||
|
case EnumTypeNode.BOOLEAN:
|
||||||
|
typeChar = "Z";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return typeChar;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,40 @@
|
|||||||
package bytecode;
|
package bytecode;
|
||||||
|
|
||||||
|
import ast.member.ConstructorNode;
|
||||||
import ast.member.MethodNode;
|
import ast.member.MethodNode;
|
||||||
|
import ast.type.BaseTypeNode;
|
||||||
import org.objectweb.asm.ClassWriter;
|
import org.objectweb.asm.ClassWriter;
|
||||||
import org.objectweb.asm.MethodVisitor;
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
|
||||||
public class MethodCodeGen {
|
public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||||
public void generateMethodCode(ClassWriter classWriter, MethodNode methodNode) {
|
|
||||||
Mapper mapper = new Mapper();
|
private ClassWriter classWriter;
|
||||||
|
Mapper mapper = new Mapper();
|
||||||
|
|
||||||
|
public MethodCodeGen(ClassWriter classWriter) {
|
||||||
|
this.classWriter = classWriter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(ConstructorNode constructorNode) {
|
||||||
MethodVisitor constructor =
|
MethodVisitor constructor =
|
||||||
classWriter.visitMethod(mapper.mapAccesTypeToOpcode(methodNode.visibility),
|
classWriter.visitMethod(mapper.mapAccesTypeToOpcode(constructorNode.visibility),
|
||||||
"<init>",
|
"<init>",
|
||||||
"()V",
|
"()V",
|
||||||
null,
|
null,
|
||||||
null);
|
null);
|
||||||
|
constructor.visitEnd();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visit(MethodNode methodNode) {
|
||||||
|
if(methodNode.type instanceof BaseTypeNode baseTypeNode){
|
||||||
|
MethodVisitor method = classWriter.visitMethod(mapper.mapAccesTypeToOpcode(methodNode.visibility),
|
||||||
|
methodNode.identifier,
|
||||||
|
mapper.generateMethodDescriptor(baseTypeNode),
|
||||||
|
null,
|
||||||
|
null);
|
||||||
|
method.visitEnd();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
10
src/main/java/bytecode/visitor/ClassVisitor.java
Normal file
10
src/main/java/bytecode/visitor/ClassVisitor.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package bytecode.visitor;
|
||||||
|
|
||||||
|
import ast.ClassNode;
|
||||||
|
import ast.member.FieldNode;
|
||||||
|
import org.objectweb.asm.ClassWriter;
|
||||||
|
|
||||||
|
public interface ClassVisitor {
|
||||||
|
void visit(ClassNode classNode);
|
||||||
|
void visit(FieldNode fieldNode);
|
||||||
|
}
|
9
src/main/java/bytecode/visitor/MethodVisitor.java
Normal file
9
src/main/java/bytecode/visitor/MethodVisitor.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package bytecode.visitor;
|
||||||
|
|
||||||
|
import ast.member.ConstructorNode;
|
||||||
|
import ast.member.MethodNode;
|
||||||
|
|
||||||
|
public interface MethodVisitor {
|
||||||
|
void visit(ConstructorNode constructorNode);
|
||||||
|
void visit(MethodNode methodNode);
|
||||||
|
}
|
7
src/main/java/bytecode/visitor/ProgramVisitor.java
Normal file
7
src/main/java/bytecode/visitor/ProgramVisitor.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package bytecode.visitor;
|
||||||
|
|
||||||
|
import ast.ProgramNode;
|
||||||
|
|
||||||
|
public interface ProgramVisitor {
|
||||||
|
void visit(ProgramNode programNode);
|
||||||
|
}
|
Binary file not shown.
@ -11,15 +11,14 @@ import ast.member.MethodNode;
|
|||||||
import ast.parameter.ParameterListNode;
|
import ast.parameter.ParameterListNode;
|
||||||
import ast.parameter.ParameterNode;
|
import ast.parameter.ParameterNode;
|
||||||
import ast.statement.*;
|
import ast.statement.*;
|
||||||
import ast.type.AccessTypeNode;
|
import ast.type.*;
|
||||||
import ast.type.EnumAccessTypeNode;
|
|
||||||
import ast.type.EnumTypeNode;
|
|
||||||
import ast.type.TypeNode;
|
|
||||||
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
|
||||||
@ -77,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
|
||||||
@ -87,11 +86,11 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
String typeStr = ctx.getText();
|
String typeStr = ctx.getText();
|
||||||
switch (typeStr) {
|
switch (typeStr) {
|
||||||
case "int":
|
case "int":
|
||||||
return new TypeNode(EnumTypeNode.INT);
|
return new BaseTypeNode(EnumTypeNode.INT);
|
||||||
case "boolean":
|
case "boolean":
|
||||||
return new TypeNode(EnumTypeNode.BOOLEAN);
|
return new BaseTypeNode(EnumTypeNode.BOOLEAN);
|
||||||
case "char":
|
case "char":
|
||||||
return new TypeNode(EnumTypeNode.CHAR);
|
return new BaseTypeNode(EnumTypeNode.CHAR);
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Unsupported type: " + typeStr);
|
throw new IllegalArgumentException("Unsupported type: " + typeStr);
|
||||||
}
|
}
|
||||||
@ -207,6 +206,7 @@ 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) {}
|
||||||
|
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,91 +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 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 typeCheck(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 {
|
||||||
classNode.accept(this);
|
throw new RuntimeException("Not Valid");
|
||||||
}
|
}
|
||||||
return new TypeCheckResult(valid, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult typeCheck(ClassNode classNode) {
|
|
||||||
List<MemberNode> members = classNode.members;
|
|
||||||
for (MemberNode memberNode : members) {
|
|
||||||
if (memberNode instanceof FieldNode fieldNode) {
|
|
||||||
fieldNode.accept(this);
|
|
||||||
} else if (memberNode instanceof MethodNode methodNode) {
|
|
||||||
methodNode.accept(this);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
@Override
|
||||||
}
|
public TypeCheckResult analyze(ProgramNode node) {
|
||||||
|
|
||||||
@Override
|
var valid = true;
|
||||||
public TypeCheckResult typeCheck(MethodNode methodNode) {
|
|
||||||
List<StatementNode> statements = methodNode.statements;
|
|
||||||
for (StatementNode statement : statements) {
|
|
||||||
if(statement instanceof AssignmentStatementNode assignmentStatementNode) {
|
|
||||||
assignmentStatementNode.accept(this);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
List<ClassNode> classes = node.classes;
|
||||||
public TypeCheckResult typeCheck(FieldNode toCheck) {
|
for (ClassNode classNode : classes) {
|
||||||
if(currentFields.contains(toCheck.identifier)){
|
var result = classNode.accept(this);
|
||||||
throw new RuntimeException(toCheck.identifier + " Is Already Declared");
|
valid = valid && result.isValid();
|
||||||
}else {
|
}
|
||||||
currentFields.add(toCheck.identifier);
|
return new TypeCheckResult(valid, null);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult typeCheck(AssignmentStatementNode assignmentStatementNode) {
|
public TypeCheckResult analyze(ClassNode classNode) {
|
||||||
if(assignmentStatementNode.expression instanceof LiteralNode literalNode) {
|
var valid = true;
|
||||||
VarNode varNode = assignmentStatementNode.varNode;
|
List<MemberNode> members = classNode.members;
|
||||||
if(varNode.getType().equals(literalNode.getType())) {
|
for (MemberNode memberNode : members) {
|
||||||
System.out.println("Type is same");
|
if (memberNode instanceof FieldNode fieldNode) {
|
||||||
} else {
|
var result = fieldNode.accept(this);
|
||||||
System.out.println("Type Mismatch");
|
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(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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new TypeCheckResult(valid, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult analyze(FieldNode toCheck) {
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -3,60 +3,40 @@ package semantic;
|
|||||||
|
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.ProgramNode;
|
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.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 {
|
||||||
// TypeCheckResult typeCheck(ASTNode toCheck);
|
|
||||||
|
|
||||||
TypeCheckResult typeCheck(ProgramNode toCheck);
|
TypeCheckResult analyze(ProgramNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult typeCheck(ClassNode toCheck);
|
TypeCheckResult analyze(ClassNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult typeCheck(MethodNode toCheck);
|
TypeCheckResult analyze(MethodNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult typeCheck(FieldNode toCheck);
|
TypeCheckResult analyze(FieldNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult typeCheck(AssignmentStatementNode 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);
|
||||||
}
|
}
|
@ -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,16 +0,0 @@
|
|||||||
package typechecker;
|
|
||||||
|
|
||||||
import semantic.SemanticVisitor;
|
|
||||||
|
|
||||||
public interface Visitable {
|
|
||||||
// default void accept(ProgramCodeVisitor visitor) {
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// default void accept(ClassCodeVisitor visitor) {
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// default void accept(MethodCodeVisitor visitor) {
|
|
||||||
// }
|
|
||||||
|
|
||||||
TypeCheckResult accept(SemanticVisitor visitor);
|
|
||||||
}
|
|
21
src/main/java/visitor/Visitable.java
Normal file
21
src/main/java/visitor/Visitable.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package visitor;
|
||||||
|
|
||||||
|
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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
default void accept(ClassVisitor classVisitor) {
|
||||||
|
|
||||||
|
}
|
||||||
|
default void accept(MethodVisitor methodVisitor) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
TypeCheckResult accept(SemanticVisitor visitor);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user