Added Basic Typecheck for integers
This commit is contained in:
parent
2bae802cb2
commit
e987faae64
4
.idea/misc.xml
generated
4
.idea/misc.xml
generated
@ -6,10 +6,10 @@
|
||||
<PerGrammarGenerationSettings>
|
||||
<option name="fileName" value="$PROJECT_DIR$/src/main/java/parser/SimpleJava.g4" />
|
||||
<option name="autoGen" value="true" />
|
||||
<option name="outputDir" value="C:\Users\Maxi\Documents\DHBW\Compilerbau\NichtHaskell2.0\src\main\java\parser" />
|
||||
<option name="outputDir" value="C:\Users\Johannes\Documents\Github\JavaCompiler\src\main\java" />
|
||||
<option name="libDir" value="" />
|
||||
<option name="encoding" value="" />
|
||||
<option name="pkg" value="generated" />
|
||||
<option name="pkg" value="parser.generated" />
|
||||
<option name="language" value="" />
|
||||
<option name="generateVisitor" value="true" />
|
||||
</PerGrammarGenerationSettings>
|
||||
|
@ -12,15 +12,14 @@ import typechecker.TypeCheckResult;
|
||||
import typechecker.Visitable;
|
||||
|
||||
public class ClassNode extends ASTNode implements Visitable {
|
||||
public IdentifierNode identifier;
|
||||
public String identifier;
|
||||
public AccessTypeNode accessType;
|
||||
public String name;
|
||||
public List<MemberNode> members = new ArrayList<>();
|
||||
public boolean hasConstructor = false;
|
||||
|
||||
public ClassNode(AccessTypeNode accessType, String name){
|
||||
public ClassNode(AccessTypeNode accessType, String identifier){
|
||||
this.accessType = accessType;
|
||||
this.name = name;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public void addMember(MemberNode member) {
|
||||
@ -32,7 +31,7 @@ public class ClassNode extends ASTNode implements Visitable {
|
||||
|
||||
public void ensureConstructor(){
|
||||
if(!hasConstructor) {
|
||||
ConstructorNode constructor = new ConstructorNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), name);
|
||||
ConstructorNode constructor = new ConstructorNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), identifier);
|
||||
members.add(0,constructor);
|
||||
}
|
||||
}
|
||||
|
@ -1,35 +0,0 @@
|
||||
package ast;
|
||||
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import typechecker.Visitable;
|
||||
|
||||
public class IdentifierNode extends ASTNode implements Visitable {
|
||||
|
||||
private String name;
|
||||
|
||||
public IdentifierNode(String name){
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean equals(Object obj) {
|
||||
if(obj instanceof IdentifierNode){
|
||||
IdentifierNode identifier = (IdentifierNode) obj;
|
||||
if(name.equals(identifier.getName())){
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return super.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.typeCheck(this);
|
||||
}
|
||||
}
|
@ -5,9 +5,19 @@ import ast.expression.ExpressionNode;
|
||||
public class LiteralNode extends ExpressionNode {
|
||||
|
||||
int value;
|
||||
private String type;
|
||||
|
||||
public LiteralNode(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
21
src/main/java/ast/VarNode.java
Normal file
21
src/main/java/ast/VarNode.java
Normal file
@ -0,0 +1,21 @@
|
||||
package ast;
|
||||
|
||||
public class VarNode extends ASTNode{
|
||||
|
||||
private String identifier;
|
||||
private String type;
|
||||
|
||||
public VarNode(String type, String identifier){
|
||||
this.type = type;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
public String getType(){
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getIdentifier(){
|
||||
return identifier;
|
||||
}
|
||||
|
||||
}
|
@ -4,4 +4,6 @@ import ast.ASTNode;
|
||||
|
||||
public class ExpressionNode extends ASTNode {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package ast.member;
|
||||
|
||||
import ast.IdentifierNode;
|
||||
import ast.type.AccessTypeNode;
|
||||
import ast.type.TypeNode;
|
||||
import semantic.SemanticVisitor;
|
||||
|
@ -1,6 +1,5 @@
|
||||
package ast.member;
|
||||
|
||||
import ast.IdentifierNode;
|
||||
import ast.parameter.ParameterListNode;
|
||||
import ast.statement.StatementNode;
|
||||
import ast.type.AccessTypeNode;
|
||||
|
@ -1,14 +1,22 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.IdentifierNode;
|
||||
import ast.VarNode;
|
||||
import ast.expression.ExpressionNode;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import typechecker.Visitable;
|
||||
|
||||
public class AssignmentStatementNode extends StatementNode {
|
||||
public IdentifierNode identifier;
|
||||
public class AssignmentStatementNode extends StatementNode implements Visitable {
|
||||
public VarNode varNode;
|
||||
public ExpressionNode expression;
|
||||
|
||||
public AssignmentStatementNode(String identifier, ExpressionNode expression) {
|
||||
this.identifier = new IdentifierNode(identifier);
|
||||
public AssignmentStatementNode(VarNode varNode, ExpressionNode expression) {
|
||||
this.varNode = varNode;
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.typeCheck(this);
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,15 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.IdentifierNode;
|
||||
import ast.type.TypeNode;
|
||||
import ast.expression.ExpressionNode;
|
||||
|
||||
public class VariableDeclarationStatementNode extends StatementNode {
|
||||
public TypeNode type;
|
||||
public IdentifierNode identifier;
|
||||
public String identifier;
|
||||
public ExpressionNode expression;
|
||||
public VariableDeclarationStatementNode(TypeNode type, String identifier, ExpressionNode expression) {
|
||||
this.type = type;
|
||||
this.identifier = new IdentifierNode(identifier);
|
||||
this.identifier = identifier;
|
||||
this.expression = expression;
|
||||
}
|
||||
}
|
@ -15,7 +15,7 @@ public class ClassCodeGen {
|
||||
public void generateClassCode(ClassNode classNode) {
|
||||
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||
Mapper mapper = new Mapper();
|
||||
classWriter.visit(Opcodes.V1_8, mapper.mapAccesTypeToOpcode(classNode.accessType), classNode.name, null,
|
||||
classWriter.visit(Opcodes.V1_8, mapper.mapAccesTypeToOpcode(classNode.accessType), classNode.identifier, null,
|
||||
"java/lang/Object", null);
|
||||
|
||||
for (MemberNode memberNode : classNode.members) {
|
||||
@ -29,7 +29,7 @@ public class ClassCodeGen {
|
||||
}
|
||||
|
||||
classWriter.visitEnd();
|
||||
printIntoClassFile(classWriter.toByteArray(), classNode.identifier.getName());
|
||||
printIntoClassFile(classWriter.toByteArray(), classNode.identifier);
|
||||
|
||||
classWriter.visitEnd();
|
||||
}
|
||||
|
@ -16,11 +16,9 @@ import ast.type.EnumAccessTypeNode;
|
||||
import ast.type.EnumTypeNode;
|
||||
import ast.type.TypeNode;
|
||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||
import parser.generated.SimpleJavaBaseVisitor;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import parser.generated.*;
|
||||
import parser.generated.SimpleJavaParser.LiteralContext;
|
||||
|
||||
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
@ -36,7 +34,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
@Override
|
||||
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
|
||||
ClassNode classNode = new ClassNode((AccessTypeNode) visit(ctx.accessType()),ctx.IDENTIFIER().getText());
|
||||
classNode.identifier = new IdentifierNode(ctx.IDENTIFIER().getText());
|
||||
classNode.identifier = ctx.IDENTIFIER().getText();
|
||||
for (SimpleJavaParser.MemberDeclarationContext member : ctx.memberDeclaration()) {
|
||||
classNode.addMember((MemberNode) visit(member));
|
||||
}
|
||||
@ -136,9 +134,14 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
|
||||
@Override
|
||||
public ASTNode visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) {
|
||||
String identifier = ctx.IDENTIFIER().getText();
|
||||
VarNode varNode = (VarNode) visit(ctx.var());
|
||||
ExpressionNode expression = (ExpressionNode) visit(ctx.expression());
|
||||
return new AssignmentStatementNode(identifier, expression);
|
||||
return new AssignmentStatementNode(varNode, expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ASTNode visitVar(SimpleJavaParser.VarContext ctx) {
|
||||
return new VarNode("int", ctx.getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -203,7 +206,9 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
LiteralContext literalContext = (LiteralContext) ctx;
|
||||
try {
|
||||
int intValue = Integer.parseInt(literalContext.getText());
|
||||
return new LiteralNode(intValue);
|
||||
LiteralNode literalNode = new LiteralNode(intValue);
|
||||
literalNode.setType("int");
|
||||
return literalNode;
|
||||
} catch (NumberFormatException ignored) {}
|
||||
|
||||
return null; // Return null or throw an exception if no valid expression found
|
||||
|
@ -29,7 +29,9 @@ statement
|
||||
|
||||
variableDeclarationStatement : type IDENTIFIER ('=' expression)? ';' ;
|
||||
|
||||
assignmentStatement : IDENTIFIER '=' expression ';' ;
|
||||
assignmentStatement : var '=' expression ';' ;
|
||||
|
||||
var: IDENTIFIER;
|
||||
|
||||
ifStatement : 'if' '(' expression ')' statement ('else' statement)? ;
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
@ -168,6 +168,18 @@ public class SimpleJavaBaseListener implements SimpleJavaListener {
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void exitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void enterVar(SimpleJavaParser.VarContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation does nothing.</p>
|
||||
*/
|
||||
@Override public void exitVar(SimpleJavaParser.VarContext ctx) { }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
|
||||
|
||||
@ -103,6 +103,13 @@ public class SimpleJavaBaseVisitor<T> extends AbstractParseTreeVisitor<T> implem
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* <p>The default implementation returns the result of calling
|
||||
* {@link #visitChildren} on {@code ctx}.</p>
|
||||
*/
|
||||
@Override public T visitVar(SimpleJavaParser.VarContext ctx) { return visitChildren(ctx); }
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
import org.antlr.v4.runtime.Lexer;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeListener;
|
||||
|
||||
@ -137,6 +137,16 @@ public interface SimpleJavaListener extends ParseTreeListener {
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void exitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx);
|
||||
/**
|
||||
* Enter a parse tree produced by {@link SimpleJavaParser#var}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void enterVar(SimpleJavaParser.VarContext ctx);
|
||||
/**
|
||||
* Exit a parse tree produced by {@link SimpleJavaParser#var}.
|
||||
* @param ctx the parse tree
|
||||
*/
|
||||
void exitVar(SimpleJavaParser.VarContext ctx);
|
||||
/**
|
||||
* Enter a parse tree produced by {@link SimpleJavaParser#ifStatement}.
|
||||
* @param ctx the parse tree
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Johannes/Documents/Github/JavaCompiler/src/main/java/parser/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||
|
||||
@ -88,6 +88,12 @@ public interface SimpleJavaVisitor<T> extends ParseTreeVisitor<T> {
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitAssignmentStatement(SimpleJavaParser.AssignmentStatementContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link SimpleJavaParser#var}.
|
||||
* @param ctx the parse tree
|
||||
* @return the visitor result
|
||||
*/
|
||||
T visitVar(SimpleJavaParser.VarContext ctx);
|
||||
/**
|
||||
* Visit a parse tree produced by {@link SimpleJavaParser#ifStatement}.
|
||||
* @param ctx the parse tree
|
||||
|
@ -25,7 +25,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
if (result.isValid()) {
|
||||
return node;
|
||||
} else {
|
||||
throw new RuntimeException("errorString ERROR");
|
||||
throw new RuntimeException("Not Valid");
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,14 +55,14 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult typeCheck(IdentifierNode identifierNode) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult typeCheck(MethodNode methodNode) {
|
||||
List<StatementNode> statements = methodNode.statements;
|
||||
for (StatementNode statement : statements) {
|
||||
if(statement instanceof AssignmentStatementNode assignmentStatementNode) {
|
||||
assignmentStatementNode.accept(this);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -76,4 +76,17 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult typeCheck(AssignmentStatementNode assignmentStatementNode) {
|
||||
if(assignmentStatementNode.expression instanceof LiteralNode literalNode) {
|
||||
VarNode varNode = assignmentStatementNode.varNode;
|
||||
if(varNode.getType().equals(literalNode.getType())) {
|
||||
System.out.println("Type is same");
|
||||
} else {
|
||||
System.out.println("Type Mismatch");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +1,11 @@
|
||||
package semantic;
|
||||
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.ClassNode;
|
||||
import ast.IdentifierNode;
|
||||
import ast.ProgramNode;
|
||||
import ast.member.FieldNode;
|
||||
import ast.member.MethodNode;
|
||||
import ast.statement.AssignmentStatementNode;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
public interface SemanticVisitor {
|
||||
@ -16,13 +15,11 @@ public interface SemanticVisitor {
|
||||
|
||||
TypeCheckResult typeCheck(ClassNode toCheck);
|
||||
|
||||
TypeCheckResult typeCheck(IdentifierNode toCheck);
|
||||
|
||||
TypeCheckResult typeCheck(MethodNode toCheck);
|
||||
|
||||
TypeCheckResult typeCheck(FieldNode toCheck);
|
||||
//
|
||||
// TypeCheckResult typeCheck(Assign toCheck);
|
||||
|
||||
TypeCheckResult typeCheck(AssignmentStatementNode toCheck);
|
||||
//
|
||||
// TypeCheckResult typeCheck(MethodParameter toCheck);
|
||||
//
|
||||
|
Loading…
Reference in New Issue
Block a user