diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-12-05 19:57:34 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-12-05 19:57:34 +0000 |
| commit | 6be55e7252f1e9b03345ef23cfada39621fc817b (patch) | |
| tree | 3a617de3a7556de4c167a289a6dc46e93f4a55be /src/main/java/org/zwobble | |
| parent | 27e05b517d85b6286b259a1925886fc76b944c8a (diff) | |
Remove Interpreter
Diffstat (limited to 'src/main/java/org/zwobble')
3 files changed, 51 insertions, 90 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/interpreter/Interpreter.java b/src/main/java/org/zwobble/hobgoblin/compiler/interpreter/Interpreter.java deleted file mode 100644 index 5014d07..0000000 --- a/src/main/java/org/zwobble/hobgoblin/compiler/interpreter/Interpreter.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.zwobble.hobgoblin.compiler.interpreter; - -import org.zwobble.hobgoblin.compiler.ast.typed.*; -import org.zwobble.hobgoblin.compiler.types.TypeLevelValueType; - -import java.util.List; - -public class Interpreter<TValue> { - private final InterpreterConfig<TValue> config; - - public Interpreter(InterpreterConfig<TValue> config) { - this.config = config; - } - - public TValue evalStatements( - List<TypedStatementNode> statements - ) { - for (var statement : statements) { - switch (statement) { - case TypedReturnNode return_ -> { - return evalExpression(return_.expression()); - } - } - } - - throw new UnsupportedOperationException("TODO: missing return"); - } - - public TValue evalExpression( - TypedExpressionNode expression - ) { - if (expression.type() instanceof TypeLevelValueType typeLevelValue) { - return config.typeLevelValueToValue(typeLevelValue.value()); - } - - throw new UnsupportedOperationException("TODO"); - } -} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/interpreter/InterpreterConfig.java b/src/main/java/org/zwobble/hobgoblin/compiler/interpreter/InterpreterConfig.java deleted file mode 100644 index 835cb31..0000000 --- a/src/main/java/org/zwobble/hobgoblin/compiler/interpreter/InterpreterConfig.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.zwobble.hobgoblin.compiler.interpreter; - -import org.zwobble.hobgoblin.compiler.types.TypeLevelValue; - -public interface InterpreterConfig<TValue> { - TValue typeLevelValueToValue(TypeLevelValue typeLevelValue); -} diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGenerator.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGenerator.java index fd4ca0a..bf22b71 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGenerator.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGenerator.java @@ -1,16 +1,17 @@ package org.zwobble.hobgoblin.compiler.output.generators.sql; -import org.zwobble.hobgoblin.compiler.ast.typed.*; +import org.zwobble.hobgoblin.compiler.ast.typed.TypedExpressionNode; +import org.zwobble.hobgoblin.compiler.ast.typed.TypedQueryDefinitionNode; +import org.zwobble.hobgoblin.compiler.ast.typed.TypedReturnNode; +import org.zwobble.hobgoblin.compiler.ast.typed.TypedTableDefinitionNode; import org.zwobble.hobgoblin.compiler.builtins.NativeTypes; import org.zwobble.hobgoblin.compiler.errors.InternalError; -import org.zwobble.hobgoblin.compiler.interpreter.Interpreter; -import org.zwobble.hobgoblin.compiler.interpreter.InterpreterConfig; import org.zwobble.hobgoblin.compiler.output.lang.sql.ast.*; import org.zwobble.hobgoblin.compiler.typechecker.TypesInfo; import org.zwobble.hobgoblin.compiler.types.SimpleStructType; import org.zwobble.hobgoblin.compiler.types.Table; import org.zwobble.hobgoblin.compiler.types.Type; -import org.zwobble.hobgoblin.compiler.types.TypeLevelValue; +import org.zwobble.hobgoblin.compiler.types.TypeLevelValueType; import org.zwobble.hobgoblin.compiler.util.Casing; import java.util.List; @@ -59,55 +60,60 @@ public class SqlGenerator { TypedQueryDefinitionNode queryDefinition, TypesInfo typesInfo ) { - var interpreter = new Interpreter<>(new InterpreterConfig<SqlStatement>() { - @Override - public SqlStatement typeLevelValueToValue(TypeLevelValue typeLevelValue) { - return generateRelation(new RelationTable((Table) typeLevelValue), typesInfo); + for (var statement : queryDefinition.statements()) { + switch (statement) { + case TypedReturnNode return_ -> { + return generateExpression(return_.expression(), typesInfo); + } } - }); - return interpreter.evalStatements(queryDefinition.statements()); + } + + throw new UnsupportedOperationException("TODO"); } - SqlSelectStatement generateRelation(Relation relation, TypesInfo typesInfo) { - switch (relation) { - case RelationTable relationTable -> { - var table = relationTable.table(); - var schemaInfo = typesInfo.databaseSchemaInfo(table.databaseSchema()); - var tableInfo = schemaInfo.tables().get(table.name()); + private SqlStatement generateExpression(TypedExpressionNode expression, TypesInfo typesInfo) { + if (expression.type() instanceof TypeLevelValueType typeLevelValueType && typeLevelValueType.value() instanceof Table table) { + return generateSelectForTable(table, typesInfo); + } - var fields = typesInfo.fieldsOf(tableInfo.rowType()) - .orElse(List.of()); + throw new UnsupportedOperationException("TODO"); + } - // The type-checker should have verified that the row type has - // fields, so don't try to handle gracefully here. - if (fields.isEmpty()) { - throw new InternalError("row type has no fields"); - } + SqlSelectStatement generateSelectForTable(Table table, TypesInfo typesInfo) { + var schemaInfo = typesInfo.databaseSchemaInfo(table.databaseSchema()); + var tableInfo = schemaInfo.tables().get(table.name()); - var columns = fields.stream() - .map(field -> { - var columnName = generateColumnName(field.name()); - return new SqlResultColumn( - new SqlRef(columnName), - columnName - ); - }) - .toList(); - - // Order by the primary key to ensure stable ordering. - var orderBy = List.of( - new SqlOrderingTerm(new SqlRef( - generateColumnName(tableInfo.primaryKey().name()) - )) - ); + var fields = typesInfo.fieldsOf(tableInfo.rowType()) + .orElse(List.of()); - return new SqlSelectStatement( - columns, - generateTableName(table.name()), - orderBy - ); - } + // The type-checker should have verified that the row type has + // fields, so don't try to handle gracefully here. + if (fields.isEmpty()) { + throw new InternalError("row type has no fields"); } + + var columns = fields.stream() + .map(field -> { + var columnName = generateColumnName(field.name()); + return new SqlResultColumn( + new SqlRef(columnName), + columnName + ); + }) + .toList(); + + // Order by the primary key to ensure stable ordering. + var orderBy = List.of( + new SqlOrderingTerm(new SqlRef( + generateColumnName(tableInfo.primaryKey().name()) + )) + ); + + return new SqlSelectStatement( + columns, + generateTableName(table.name()), + orderBy + ); } private SqlIdentifier generateColumnName(String fieldName) { |
