summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorElias Haugsbakk <[email protected]>2026-09-20 23:25:08 +0200
committerElias Haugsbakk <[email protected]>2026-09-20 23:25:08 +0200
commit10e0580ca3e920a5c7c126abdd70683c6fe2188f (patch)
tree26f176023df595dd0d10170380db66d6c334dd91 /src/main/java
parentb8ccfae5f782aa09343b885225dca2b1d36052aa (diff)
refactor Type to be a record class
- now with method to check type compatability
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/no/eliashaugsbakk/kompilator/IRGeneration/IRGenerator.java2
-rw-r--r--src/main/java/no/eliashaugsbakk/kompilator/parsing/Type.java27
-rw-r--r--src/main/java/no/eliashaugsbakk/kompilator/semanticAnalysis/Analyzer.java20
3 files changed, 16 insertions, 33 deletions
diff --git a/src/main/java/no/eliashaugsbakk/kompilator/IRGeneration/IRGenerator.java b/src/main/java/no/eliashaugsbakk/kompilator/IRGeneration/IRGenerator.java
index 8ade66e..f8a5756 100644
--- a/src/main/java/no/eliashaugsbakk/kompilator/IRGeneration/IRGenerator.java
+++ b/src/main/java/no/eliashaugsbakk/kompilator/IRGeneration/IRGenerator.java
@@ -69,7 +69,7 @@ public class IRGenerator {
} else {
throw new IRGenerationException("Unknown function: " + identifierDecl.initializer);
}
- ir.add(new Alloc(identifierDecl.identifier, identifierDecl.type.type, identifierDecl.mutable,
+ ir.add(new Alloc(identifierDecl.identifier, identifierDecl.type.name(), identifierDecl.mutable,
value));
}
diff --git a/src/main/java/no/eliashaugsbakk/kompilator/parsing/Type.java b/src/main/java/no/eliashaugsbakk/kompilator/parsing/Type.java
index 6b1007c..d660ccd 100644
--- a/src/main/java/no/eliashaugsbakk/kompilator/parsing/Type.java
+++ b/src/main/java/no/eliashaugsbakk/kompilator/parsing/Type.java
@@ -1,28 +1,11 @@
package no.eliashaugsbakk.kompilator.parsing;
-import java.util.Objects;
-public class Type {
- public final String type;
- public final boolean nullable;
-
- public Type(String type, boolean nullable) {
- this.type = type;
- this.nullable = nullable;
- }
-
- @Override
- public boolean equals(Object o) {
- if (o instanceof Type t) {
- return this.type.equals(t.type) && this.nullable == t.nullable;
- } else {
- return false;
- }
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(type, nullable);
+public record Type(String name, boolean nullable) {
+ boolean isAssignableTo(Type target) {
+ if (!this.name.equals(target.name)) return false;
+ if (this.nullable && !target.nullable) return false;
+ return true;
}
}
diff --git a/src/main/java/no/eliashaugsbakk/kompilator/semanticAnalysis/Analyzer.java b/src/main/java/no/eliashaugsbakk/kompilator/semanticAnalysis/Analyzer.java
index a01c6bf..7d301ed 100644
--- a/src/main/java/no/eliashaugsbakk/kompilator/semanticAnalysis/Analyzer.java
+++ b/src/main/java/no/eliashaugsbakk/kompilator/semanticAnalysis/Analyzer.java
@@ -47,13 +47,13 @@ public class Analyzer {
*/
private void analyzeIdentifierDeclaration(IdentifierDeclaration decl) throws SemanticException {
boolean initialized = decl.initializer != null;
- boolean nullable = decl.type.nullable;
+ boolean nullable = decl.type.nullable();
Type declaredType = decl.type;
// Ensure the type exists
// string is the only type implemented, but should look in a type table or something in the future
- if (!declaredType.type.equals("string")) {
- throw new SemanticException("Unknown type declaration: " + declaredType.type);
+ if (!declaredType.name().equals("string")) {
+ throw new SemanticException("Unknown type declaration: " + declaredType.name());
}
// Immutable variables must always be initialized on declaration
@@ -137,24 +137,24 @@ public class Analyzer {
for (Expression argument : call.arguments) {
Type argType = typeOf(argument);
- if (!argType.type.equals("string")) {
+ if (!argType.name().equals("string")) {
throw new SemanticException("skriv() only supports string literals");
}
- if (argType.nullable) {
- throw new SemanticException("Cannot print nullable string: " + argType.type + "?.");
+ if (argType.nullable()) {
+ throw new SemanticException("Cannot print nullable string: " + argType.name() + "?.");
}
}
}
private void checkAssignable(Type target, Type value) throws SemanticException {
- if (!target.type.equals(value.type)) {
+ if (!target.name().equals(value.name())) {
throw new SemanticException(
- "Type mismatch: expected " + target.type + ", found " + value.type);
+ "Type mismatch: expected " + target.name() + ", found " + value.name());
}
- if (value.nullable && !target.nullable) {
+ if (value.nullable() && !target.nullable()) {
throw new SemanticException(
- "Cannot assign nullable " + value.type + " to non-nullable " + target.type);
+ "Cannot assign nullable " + value.name() + " to non-nullable " + target.name());
}
}