From 10e0580ca3e920a5c7c126abdd70683c6fe2188f Mon Sep 17 00:00:00 2001 From: Elias Haugsbakk Date: Sun, 20 Sep 2026 23:25:08 +0200 Subject: refactor Type to be a record class - now with method to check type compatability --- .../kompilator/IRGeneration/IRGenerator.java | 2 +- .../no/eliashaugsbakk/kompilator/parsing/Type.java | 27 ++++------------------ .../kompilator/semanticAnalysis/Analyzer.java | 20 ++++++++-------- 3 files changed, 16 insertions(+), 33 deletions(-) (limited to 'src') 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()); } } -- cgit v1.2.3