Compare commits
3 Commits
514f7d724a
...
09e36a84dc
Author | SHA1 | Date | |
---|---|---|---|
09e36a84dc | |||
|
e395c4d96b | ||
|
8a5f307947 |
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_19" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -1,24 +1,25 @@
|
|||||||
package ast.literal;
|
package ast.literal;
|
||||||
|
|
||||||
import ast.expression.ExpressionNode;
|
import ast.expression.ExpressionNode;
|
||||||
|
import ast.type.TypeNode;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
public class LiteralNode implements ExpressionNode {
|
public class LiteralNode implements ExpressionNode {
|
||||||
|
|
||||||
public String value;
|
public String value;
|
||||||
private String type;
|
private TypeNode type;
|
||||||
|
|
||||||
public LiteralNode(String value, String type) {
|
public LiteralNode(String value, TypeNode type) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getType() {
|
public TypeNode getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setType(String type) {
|
public void setType(TypeNode type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Binary file not shown.
Binary file not shown.
@ -230,33 +230,33 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitLiteral(SimpleJavaParser.LiteralContext ctx) {
|
public ASTNode visitLiteral(SimpleJavaParser.LiteralContext ctx) {
|
||||||
String type;
|
TypeNode type;
|
||||||
String value;
|
String value;
|
||||||
if(ctx.INTEGERLITERAL() != null) {
|
if(ctx.INTEGERLITERAL() != null) {
|
||||||
type = "int";
|
type = new BaseTypeNode(EnumTypeNode.INT);
|
||||||
value = ctx.INTEGERLITERAL().getText();
|
value = ctx.INTEGERLITERAL().getText();
|
||||||
return new LiteralNode(type, value);
|
return new LiteralNode(value, type);
|
||||||
} else if(ctx.booleanLiteral() != null) {
|
} else if(ctx.booleanLiteral() != null) {
|
||||||
type= "boolean";
|
type= new BaseTypeNode(EnumTypeNode.BOOLEAN);
|
||||||
BooleanLiteralNode booleanNode = (BooleanLiteralNode) visitBooleanLiteral(ctx.booleanLiteral());
|
BooleanLiteralNode booleanNode = (BooleanLiteralNode) visitBooleanLiteral(ctx.booleanLiteral());
|
||||||
value = booleanNode.getValue();
|
value = booleanNode.getValue();
|
||||||
return new LiteralNode(type, value);
|
return new LiteralNode(value, type);
|
||||||
} else if(ctx.charLiteral() != null) {
|
} else if(ctx.charLiteral() != null) {
|
||||||
type= "char";
|
type= new BaseTypeNode(EnumTypeNode.CHAR);
|
||||||
CharLiteralNode charNode = (CharLiteralNode) visitCharLiteral(ctx.charLiteral());
|
CharLiteralNode charNode = (CharLiteralNode) visitCharLiteral(ctx.charLiteral());
|
||||||
value = charNode.getValue();
|
value = charNode.getValue();
|
||||||
return new LiteralNode(type, value);
|
return new LiteralNode(value, type);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitBooleanLiteral(SimpleJavaParser.BooleanLiteralContext ctx) {
|
public ASTNode visitBooleanLiteral(SimpleJavaParser.BooleanLiteralContext ctx) {
|
||||||
return super.visitBooleanLiteral(ctx);
|
return (ASTNode) new BooleanLiteralNode(ctx.getText());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitCharLiteral(SimpleJavaParser.CharLiteralContext ctx) {
|
public ASTNode visitCharLiteral(SimpleJavaParser.CharLiteralContext ctx) {
|
||||||
return super.visitCharLiteral(ctx);
|
return (ASTNode) new CharLiteralNode(ctx.getText());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -146,9 +146,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(AssignmentStatementNode assignmentStatementNode) {
|
public TypeCheckResult analyze(AssignmentStatementNode assignmentStatementNode) {
|
||||||
boolean valid = true;
|
boolean valid = true;
|
||||||
BinaryExpressionNode binaryExpressionNode = assignmentStatementNode.expression;
|
|
||||||
var result = binaryExpressionNode.accept(this);
|
|
||||||
valid = valid && result.isValid();
|
|
||||||
return new TypeCheckResult(valid, null);
|
return new TypeCheckResult(valid, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,10 @@ public class Example {
|
|||||||
|
|
||||||
public static int testMethod(char x){
|
public static int testMethod(char x){
|
||||||
|
|
||||||
|
a = 12;
|
||||||
|
|
||||||
|
a = x;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user