summaryrefslogtreecommitdiff
path: root/src/main/java/no/eliashaugsbakk/kompilator/semanticAnalysis/Analyzer.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/no/eliashaugsbakk/kompilator/semanticAnalysis/Analyzer.java
parentb8ccfae5f782aa09343b885225dca2b1d36052aa (diff)
refactor Type to be a record class
- now with method to check type compatability
Diffstat (limited to 'src/main/java/no/eliashaugsbakk/kompilator/semanticAnalysis/Analyzer.java')
-rw-r--r--src/main/java/no/eliashaugsbakk/kompilator/semanticAnalysis/Analyzer.java20
1 files changed, 10 insertions, 10 deletions
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());
}
}