summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedEnumDefinitionNode.java37
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedEnumVariantDefinitionNode.java29
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java46
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java6
5 files changed, 118 insertions, 2 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedEnumDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedEnumDefinitionNode.java
new file mode 100644
index 0000000..42c28c1
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedEnumDefinitionNode.java
@@ -0,0 +1,37 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.untyped;
+
+public record UntypedEnumDefinitionNode(String name, java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode> variants, org.zwobble.hobgoblin.compiler.ast.DocComment docComment, org.zwobble.hobgoblin.compiler.sources.Source source) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceStatementNode {
+ public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode.Builder("", java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.Native.arbitraryDocComment(), org.zwobble.hobgoblin.compiler.sources.Native.arbitrarySource());
+ }
+
+ public record Builder(String name, java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode> variants, org.zwobble.hobgoblin.compiler.ast.DocComment docComment, org.zwobble.hobgoblin.compiler.sources.Source source) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceStatementNode.Builder {
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode build() {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode(name, variants, docComment, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode.Builder withName(String name) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode.Builder(name, variants, docComment, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode.Builder withVariants(java.util.List<org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode> variants) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode.Builder(name, variants, docComment, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode.Builder withDocComment(org.zwobble.hobgoblin.compiler.ast.DocComment docComment) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode.Builder(name, variants, docComment, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode.Builder(name, variants, docComment, source);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedEnumVariantDefinitionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedEnumVariantDefinitionNode.java
new file mode 100644
index 0000000..25ffe87
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedEnumVariantDefinitionNode.java
@@ -0,0 +1,29 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.untyped;
+
+public record UntypedEnumVariantDefinitionNode(String name, org.zwobble.hobgoblin.compiler.sources.Source source) {
+ public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode.Builder("", org.zwobble.hobgoblin.compiler.sources.Native.arbitrarySource());
+ }
+
+ public record Builder(String name, org.zwobble.hobgoblin.compiler.sources.Source source) {
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode build() {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode(name, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode.Builder withName(String name) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode.Builder(name, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode.Builder(name, source);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumVariantDefinitionNode body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java
index a2d3171..8cf12e1 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNamespaceStatementNode.java
@@ -2,7 +2,7 @@
package org.zwobble.hobgoblin.compiler.ast.untyped;
-public sealed interface UntypedNamespaceStatementNode permits org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumDefinitionNode {
+public sealed interface UntypedNamespaceStatementNode permits org.zwobble.hobgoblin.compiler.ast.untyped.UntypedEnumDefinitionNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNativeTypeDefinitionNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumDefinitionNode {
public interface Builder {
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNamespaceStatementNode build();
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
index f3cb123..a9e5947 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
@@ -103,7 +103,9 @@ public class Parser {
) {
var docComment = parseDocComment(tokens);
- if (tokens.isNext(TokenType.KEYWORD_NATIVE)) {
+ if (tokens.isNext(TokenType.KEYWORD_ENUM)) {
+ return parseEnumDefinition(tokens, docComment);
+ } else if (tokens.isNext(TokenType.KEYWORD_NATIVE)) {
return parseNativeTypeDefinition(tokens, docComment);
} else if (tokens.isNext(TokenType.KEYWORD_STRUCT)) {
return parseStructDefinition(tokens, docComment);
@@ -114,6 +116,48 @@ public class Parser {
}
}
+ private static UntypedEnumDefinitionNode parseEnumDefinition(
+ TokenIterator tokens,
+ DocComment docComment
+ ) {
+ var start = tokens.startPosition();
+
+ tokens.skip(TokenType.KEYWORD_ENUM);
+ var name = tokens.next(TokenType.IDENTIFIER);
+ tokens.skip(TokenType.SYMBOL_BRACE_OPEN);
+
+ var variants = parseMany(
+ () -> !tokens.isNext(TokenType.KEYWORD_VARIANT),
+ () -> parseEnumVariantDefinition(tokens),
+ () -> true
+ );
+
+ tokens.skip(TokenType.SYMBOL_BRACE_CLOSE);
+
+ var end = tokens.endPosition();
+ var source = createSource(start, end);
+
+ return new UntypedEnumDefinitionNode(
+ name.charSequence().toString(),
+ variants,
+ docComment,
+ source
+ );
+ }
+
+ private static UntypedEnumVariantDefinitionNode parseEnumVariantDefinition(TokenIterator tokens) {
+ var start = tokens.startPosition();
+
+ tokens.skip(TokenType.KEYWORD_VARIANT);
+ var name = parseIdentifier(tokens);
+ tokens.skip(TokenType.SYMBOL_SEMICOLON);
+
+ var end = tokens.endPosition();
+ var source = createSource(start, end);
+
+ return new UntypedEnumVariantDefinitionNode(name, source);
+ }
+
private static UntypedNativeTypeDefinitionNode parseNativeTypeDefinition(
TokenIterator tokens,
DocComment docComment
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
index 384ef19..f8fe696 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
@@ -69,6 +69,9 @@ public class TypeChecker {
TypeCheckerNamespaceContext context
) {
return switch (untyped) {
+ case UntypedEnumDefinitionNode untypedEnumDefinitionNode ->
+ throw new UnsupportedOperationException("TODO");
+
case UntypedNativeTypeDefinitionNode untypedNativeTypeDefinition ->
declareNativeTypeDefinition(untypedNativeTypeDefinition, context);
@@ -85,6 +88,9 @@ public class TypeChecker {
TypeCheckerNamespaceContext context
) {
return switch (untyped) {
+ case UntypedEnumDefinitionNode untypedEnumDefinitionNode ->
+ throw new UnsupportedOperationException("TODO");
+
case UntypedNativeTypeDefinitionNode untypedNativeTypeDefinition ->
defineNativeTypeDefinition(untypedNativeTypeDefinition, context);