summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/18-database/src/data.hob8
-rw-r--r--hobgoblin/hobgoblin.json56
-rw-r--r--hobgoblin/src/ast/untyped.hob19
-rw-r--r--hobgoblin/src/math.hob1
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedBinaryOperationNode.java58
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedBinaryOperator.java13
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedCallNode.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedExpressionNode.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedFunctionExpressionNode.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedMemberAccessNode.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNumberLiteralNode.java38
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedReturnNode.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/math/HobgoblinNativeMath.java11
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/parser/OperatorPrecedence.java1
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java60
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/parser/TokenType.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/parser/Tokenizer.java33
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java27
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/types/ConstantNumber.java12
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/types/RelationMethod.java10
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/types/TypeLevelValue.java2
-rw-r--r--src/main/java/org/zwobble/hobgoblin/compiler/types/Types.java4
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedBinaryOperationNodeMatcher.java59
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNumberLiteralNodeMatcher.java47
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserBinaryOperationTests.java26
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNumberTests.java24
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/parser/TokenizerTests.java44
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerEvalExpressionTests.java90
28 files changed, 572 insertions, 33 deletions
diff --git a/examples/18-database/src/data.hob b/examples/18-database/src/data.hob
index 1ef2ece..927f4f1 100644
--- a/examples/18-database/src/data.hob
+++ b/examples/18-database/src/data.hob
@@ -14,7 +14,11 @@ query allAuthors() {
return Data.authors;
}
-//query authorsByName(name: String) on Data {
+//query authorsByFixedId() {
+// return Data.authors.where($(author) => author.id == 42);
+//}
+
+//query authorsByName(name: String) {
// return Data.authors
-// .where($(author) => author.name = name);
+// .where($(author) => author.name == name);
//}
diff --git a/hobgoblin/hobgoblin.json5 b/hobgoblin/hobgoblin.json5
index 3de67b4..1b5c3ee 100644
--- a/hobgoblin/hobgoblin.json5
+++ b/hobgoblin/hobgoblin.json5
@@ -3,6 +3,12 @@
langs: {
java: {
packageName: "org.zwobble.hobgoblin.compiler",
+ nativeTypes: [
+ {
+ hobgoblin: "math.BigDecimal",
+ java: "java.math.BigDecimal",
+ },
+ ],
},
},
generators: [
diff --git a/hobgoblin/src/ast/untyped.hob b/hobgoblin/src/ast/untyped.hob
index 0acdfbc..fb0116b 100644
--- a/hobgoblin/src/ast/untyped.hob
+++ b/hobgoblin/src/ast/untyped.hob
@@ -1,4 +1,5 @@
import {DocComment} from ast;
+import {BigDecimal} from math;
import {Source} from sources;
import {NamespaceName} from types;
@@ -154,12 +155,25 @@ struct UntypedReturnNode {
// == Expressions ==
sum UntypedExpressionNode {
+ variant UntypedBinaryOperationNode;
variant UntypedCallNode;
variant UntypedFunctionExpressionNode;
variant UntypedMemberAccessNode;
+ variant UntypedNumberLiteralNode;
variant UntypedReferenceNode;
}
+struct UntypedBinaryOperationNode {
+ field operator: UntypedBinaryOperator;
+ field left: UntypedExpressionNode;
+ field right: UntypedExpressionNode;
+ field source: Source;
+}
+
+enum UntypedBinaryOperator {
+ variant equals;
+}
+
struct UntypedCallNode {
field receiver: UntypedExpressionNode;
field args: List[UntypedExpressionNode];
@@ -178,6 +192,11 @@ struct UntypedMemberAccessNode {
field source: Source;
}
+struct UntypedNumberLiteralNode {
+ field value: BigDecimal;
+ field source: Source;
+}
+
struct UntypedReferenceNode {
field name: String;
field source: Source;
diff --git a/hobgoblin/src/math.hob b/hobgoblin/src/math.hob
new file mode 100644
index 0000000..9560ae3
--- /dev/null
+++ b/hobgoblin/src/math.hob
@@ -0,0 +1 @@
+native BigDecimal {}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedBinaryOperationNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedBinaryOperationNode.java
new file mode 100644
index 0000000..b6fb024
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedBinaryOperationNode.java
@@ -0,0 +1,58 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.untyped;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode imports
+// Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode imports
+
+public record UntypedBinaryOperationNode(
+ org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperator operator,
+ org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode left,
+ org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode right,
+ org.zwobble.hobgoblin.compiler.sources.Source source
+) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode {
+ public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperator.EQUALS, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.arbitrary().build(), org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.arbitrary().build(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
+ }
+
+ public record Builder(
+ org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperator operator,
+ org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode left,
+ org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode right,
+ org.zwobble.hobgoblin.compiler.sources.Source source
+ ) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode.Builder {
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode build() {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode(operator, left, right, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder withOperator(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperator operator) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder(operator, left, right, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder withLeft(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode left) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder(operator, left, right, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder withLeft(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode.Builder left) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder(operator, left.build(), right, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder withRight(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode right) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder(operator, left, right, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder withRight(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode.Builder right) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder(operator, left, right.build(), source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder(operator, left, right, source);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedBinaryOperator.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedBinaryOperator.java
new file mode 100644
index 0000000..b479e9b
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedBinaryOperator.java
@@ -0,0 +1,13 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.untyped;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperator imports
+// Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperator imports
+
+public enum UntypedBinaryOperator {
+ EQUALS
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperator body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperator body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedCallNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedCallNode.java
index 2c19adb..8da37c1 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedCallNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedCallNode.java
@@ -11,7 +11,7 @@ public record UntypedCallNode(
org.zwobble.hobgoblin.compiler.sources.Source source
) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode {
public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedCallNode.Builder arbitrary() {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedCallNode.Builder(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReferenceNode.arbitrary().build(), java.util.List.of(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedCallNode.Builder(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.arbitrary().build(), java.util.List.of(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
}
public record Builder(
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedExpressionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedExpressionNode.java
index a5c54a0..2fd499e 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedExpressionNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedExpressionNode.java
@@ -5,7 +5,7 @@ package org.zwobble.hobgoblin.compiler.ast.untyped;
// Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode imports
// Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode imports
-public sealed interface UntypedExpressionNode permits org.zwobble.hobgoblin.compiler.ast.untyped.UntypedCallNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedFunctionExpressionNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedMemberAccessNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReferenceNode {
+public sealed interface UntypedExpressionNode permits org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedCallNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedFunctionExpressionNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedMemberAccessNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode, org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReferenceNode {
public interface Builder {
public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode build();
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedFunctionExpressionNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedFunctionExpressionNode.java
index 4e410f3..d4a6034 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedFunctionExpressionNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedFunctionExpressionNode.java
@@ -11,7 +11,7 @@ public record UntypedFunctionExpressionNode(
org.zwobble.hobgoblin.compiler.sources.Source source
) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode {
public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedFunctionExpressionNode.Builder arbitrary() {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedFunctionExpressionNode.Builder(java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReferenceNode.arbitrary().build(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedFunctionExpressionNode.Builder(java.util.List.of(), org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.arbitrary().build(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
}
public record Builder(
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedMemberAccessNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedMemberAccessNode.java
index 4434858..4fef83e 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedMemberAccessNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedMemberAccessNode.java
@@ -11,7 +11,7 @@ public record UntypedMemberAccessNode(
org.zwobble.hobgoblin.compiler.sources.Source source
) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode {
public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedMemberAccessNode.Builder arbitrary() {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedMemberAccessNode.Builder(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReferenceNode.arbitrary().build(), "", org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedMemberAccessNode.Builder(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.arbitrary().build(), "", org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
}
public record Builder(
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNumberLiteralNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNumberLiteralNode.java
new file mode 100644
index 0000000..1e041ea
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNumberLiteralNode.java
@@ -0,0 +1,38 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.untyped;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode imports
+// Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode imports
+
+public record UntypedNumberLiteralNode(
+ java.math.BigDecimal value,
+ org.zwobble.hobgoblin.compiler.sources.Source source
+) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode {
+ public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.Builder arbitrary() {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.Builder(org.zwobble.hobgoblin.compiler.math.HobgoblinNativeMath.arbitraryBigDecimal(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
+ }
+
+ public record Builder(
+ java.math.BigDecimal value,
+ org.zwobble.hobgoblin.compiler.sources.Source source
+ ) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode.Builder {
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode build() {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode(value, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.Builder withValue(java.math.BigDecimal value) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.Builder(value, source);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.Builder withSource(org.zwobble.hobgoblin.compiler.sources.Source source) {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.Builder(value, source);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.Builder body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.Builder body
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode body
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedReturnNode.java b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedReturnNode.java
index f115b24..03c3446 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedReturnNode.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedReturnNode.java
@@ -10,7 +10,7 @@ public record UntypedReturnNode(
org.zwobble.hobgoblin.compiler.sources.Source source
) implements org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStatementNode {
public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReturnNode.Builder arbitrary() {
- return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReturnNode.Builder(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReferenceNode.arbitrary().build(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReturnNode.Builder(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.arbitrary().build(), org.zwobble.hobgoblin.compiler.sources.HobgoblinNativeSources.arbitrarySource());
}
public record Builder(
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/math/HobgoblinNativeMath.java b/src/main/java/org/zwobble/hobgoblin/compiler/math/HobgoblinNativeMath.java
new file mode 100644
index 0000000..1592b50
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/math/HobgoblinNativeMath.java
@@ -0,0 +1,11 @@
+package org.zwobble.hobgoblin.compiler.math;
+
+import java.math.BigDecimal;
+
+public class HobgoblinNativeMath {
+ private HobgoblinNativeMath() {}
+
+ public static BigDecimal arbitraryBigDecimal() {
+ return BigDecimal.ZERO;
+ }
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/parser/OperatorPrecedence.java b/src/main/java/org/zwobble/hobgoblin/compiler/parser/OperatorPrecedence.java
index b63ca86..13c38db 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/parser/OperatorPrecedence.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/parser/OperatorPrecedence.java
@@ -2,4 +2,5 @@ package org.zwobble.hobgoblin.compiler.parser;
enum OperatorPrecedence {
CALL,
+ COMPARE,
}
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 508ab70..b4bb07d 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Parser.java
@@ -9,6 +9,7 @@ import org.zwobble.hobgoblin.compiler.types.QualifiedTypeName;
import org.zwobble.sourcetext.SourcePosition;
import org.zwobble.sourcetext.SourceText;
+import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -560,6 +561,8 @@ public class Parser {
) {
if (tokens.isNext(TokenType.SYMBOL_DOLLAR)) {
return parseFunctionExpression(tokens);
+ } else if (tokens.isNext(TokenType.NUMBER)) {
+ return parseNumberLiteral(tokens);
} else if (tokens.isNext(TokenType.IDENTIFIER)) {
return parseReference(tokens);
} else {
@@ -594,6 +597,18 @@ public class Parser {
);
}
+ private static UntypedNumberLiteralNode parseNumberLiteral(TokenIterator tokens) {
+ var start = tokens.startPosition();
+
+ var valueToken = tokens.next(TokenType.NUMBER);
+ var value = new BigDecimal(valueToken.charSequence().toString());
+
+ var end = tokens.endPosition();
+ var source = createSource(start, end);
+
+ return new UntypedNumberLiteralNode(value, source);
+ }
+
private static UntypedReferenceNode parseReference(TokenIterator tokens) {
var start = tokens.startPosition();
@@ -605,6 +620,44 @@ public class Parser {
return new UntypedReferenceNode(name, source);
}
+ private static class BinaryOperationParser implements OperationParser<OperatorPrecedence, UntypedExpressionNode> {
+ private final UntypedBinaryOperator operator;
+ private final OperatorPrecedence precedence;
+ private final TokenType operatorTokenType;
+
+ public BinaryOperationParser(
+ UntypedBinaryOperator operator,
+ OperatorPrecedence precedence,
+ TokenType operatorTokenType
+ ) {
+ this.operator = operator;
+ this.precedence = precedence;
+ this.operatorTokenType = operatorTokenType;
+ }
+
+ @Override
+ public OperatorPrecedence precedence() {
+ return precedence;
+ }
+
+ @Override
+ public List<TokenType> operatorTokenTypes() {
+ return List.of(operatorTokenType);
+ }
+
+ @Override
+ public UntypedExpressionNode parse(SourcePosition start, UntypedExpressionNode left, TokenIterator tokens) {
+ tokens.skip(operatorTokenType);
+
+ var right = parseExpression(tokens, Optional.of(precedence));
+
+ var end = tokens.endPosition();
+ var source = createSource(start, end);
+
+ return new UntypedBinaryOperationNode(operator, left, right, source);
+ }
+ }
+
private static final OperationParser<OperatorPrecedence, UntypedExpressionNode> CALL_PARSER = new OperationParser<>() {
@Override
public OperatorPrecedence precedence() {
@@ -666,8 +719,15 @@ public class Parser {
}
};
+ private static final BinaryOperationParser EQUALS_OPERATION_PARSER = new BinaryOperationParser(
+ UntypedBinaryOperator.EQUALS,
+ OperatorPrecedence.COMPARE,
+ TokenType.SYMBOL_EQUALS_EQUALS
+ );
+
private static final Map<TokenType, OperationParser<OperatorPrecedence, UntypedExpressionNode>> OPERATION_PARSERS = Stream.of(
CALL_PARSER,
+ EQUALS_OPERATION_PARSER,
MEMBER_ACCESS_PARSER
)
.flatMap(parser ->
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/parser/TokenType.java b/src/main/java/org/zwobble/hobgoblin/compiler/parser/TokenType.java
index e38f3c7..5896d6f 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/parser/TokenType.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/parser/TokenType.java
@@ -4,6 +4,7 @@ enum TokenType {
DOC_COMMENT,
END,
IDENTIFIER,
+ NUMBER,
WHITESPACE,
KEYWORD_ENUM,
@@ -21,6 +22,7 @@ enum TokenType {
SYMBOL_DOLLAR,
SYMBOL_DOT,
SYMBOL_DOT_DOT,
+ SYMBOL_EQUALS_EQUALS,
SYMBOL_FAT_ARROW,
SYMBOL_FORWARD_SLASH,
SYMBOL_PAREN_OPEN,
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Tokenizer.java b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Tokenizer.java
index 0b88b5c..b36ca12 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/parser/Tokenizer.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/parser/Tokenizer.java
@@ -55,6 +55,11 @@ class Tokenizer {
return identifierOrKeyword;
}
+ var number = tokenizeNumber(iterator);
+ if (number.isPresent()) {
+ return number;
+ }
+
var docComment = tokenizeDocComment(iterator);
if (docComment.isPresent()) {
return docComment;
@@ -144,6 +149,30 @@ class Tokenizer {
return Optional.of(token);
}
+ private static Optional<Token> tokenizeNumber(CharacterIterator iterator) {
+ var firstChar = iterator.peek();
+ if (!(firstChar >= '1' && firstChar <= '9')) {
+ return Optional.empty();
+ }
+
+ var start = iterator.position();
+
+ while (true) {
+ var nextChar = iterator.peek();
+ if (!(nextChar >= '0' && nextChar <= '9')) {
+ if (Character.isAlphabetic(nextChar)) {
+ throw new ParseError("number must contain only digits", iterator.characterSourceRange());
+ }
+
+ var end = iterator.position();
+ var token = new Token(TokenType.NUMBER, start.to(end));
+ return Optional.of(token);
+ }
+
+ iterator.skip();
+ }
+ }
+
private static Optional<Token> tokenizeSymbol(CharacterIterator iterator) {
TokenType tokenType;
var start = iterator.position();
@@ -156,6 +185,10 @@ class Tokenizer {
tokenType = TokenType.SYMBOL_FAT_ARROW;
iterator.skip();
iterator.skip();
+ } else if ("==".contentEquals(iterator.peekSequence(2))) {
+ tokenType = TokenType.SYMBOL_EQUALS_EQUALS;
+ iterator.skip();
+ iterator.skip();
} else {
switch (iterator.peek()) {
case '{':
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 d7fb222..c3f664c 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/typechecker/TypeChecker.java
@@ -543,7 +543,10 @@ public class TypeChecker {
);
}
+ // In principle, we should check that fields is non-empty. In practice,
+ // we implicitly check this by requiring a primary key.
var fields = context.fieldsOf(rowType).orElse(List.of());
+
var primaryKeyField = fields.stream()
.filter(field -> field.name().equals(untyped.primaryKey().name()))
.findFirst();
@@ -702,6 +705,9 @@ public class TypeChecker {
TypeCheckerNamespaceContext context
) {
switch (expression) {
+ case UntypedBinaryOperationNode _ -> {
+ throw new UnsupportedOperationException("TODO");
+ }
case UntypedCallNode _ -> {
throw new UnsupportedOperationException("TODO");
}
@@ -711,6 +717,9 @@ public class TypeChecker {
case UntypedMemberAccessNode memberAccess -> {
return evalMemberAccess(memberAccess, context);
}
+ case UntypedNumberLiteralNode numberLiteral -> {
+ return evalNumberLiteral(numberLiteral);
+ }
case UntypedReferenceNode reference -> {
return lookupTypeLevelValue(reference.name(), reference.source(), context);
}
@@ -731,7 +740,17 @@ public class TypeChecker {
}
}
- case Table _, Type _, TypeConstructor<?> _ -> {
+ case Table table -> {
+ switch (memberAccess.memberName()) {
+ case "where":
+ return new RelationMethod(new RelationTable(table), "where");
+
+ default:
+ break;
+ }
+ }
+
+ case ConstantNumber _, Type _, TypeConstructor<?> _, RelationMethod _ -> {
}
}
@@ -741,4 +760,10 @@ public class TypeChecker {
memberAccess.source()
);
}
+
+ private static TypeLevelValue evalNumberLiteral(
+ UntypedNumberLiteralNode numberLiteral
+ ) {
+ return new ConstantNumber(numberLiteral.value());
+ }
}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/ConstantNumber.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/ConstantNumber.java
new file mode 100644
index 0000000..bd16d81
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/ConstantNumber.java
@@ -0,0 +1,12 @@
+package org.zwobble.hobgoblin.compiler.types;
+
+import java.math.BigDecimal;
+
+public record ConstantNumber(
+ BigDecimal value
+) implements TypeLevelValue {
+ @Override
+ public String describe() {
+ return value.toString();
+ }
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/RelationMethod.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/RelationMethod.java
new file mode 100644
index 0000000..87ba7fa
--- /dev/null
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/RelationMethod.java
@@ -0,0 +1,10 @@
+package org.zwobble.hobgoblin.compiler.types;
+
+import org.zwobble.hobgoblin.compiler.ast.typed.Relation;
+
+public record RelationMethod(Relation relation, String name) implements TypeLevelValue {
+ @Override
+ public String describe() {
+ return name;
+ }
+}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeLevelValue.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeLevelValue.java
index 8d12c2e..e0f7042 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeLevelValue.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/TypeLevelValue.java
@@ -1,5 +1,5 @@
package org.zwobble.hobgoblin.compiler.types;
-public sealed interface TypeLevelValue permits DatabaseSchema, Table, Type, TypeConstructor {
+public sealed interface TypeLevelValue permits ConstantNumber, DatabaseSchema, RelationMethod, Table, Type, TypeConstructor {
String describe();
}
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/types/Types.java b/src/main/java/org/zwobble/hobgoblin/compiler/types/Types.java
index 0d321a0..756f3bf 100644
--- a/src/main/java/org/zwobble/hobgoblin/compiler/types/Types.java
+++ b/src/main/java/org/zwobble/hobgoblin/compiler/types/Types.java
@@ -31,6 +31,10 @@ public class Types {
return new TypeLevelValueType(databaseSchema);
}
+ public static Type relationMethod(RelationMethod method) {
+ return new TypeLevelValueType(method);
+ }
+
public static Type table(Table table) {
return new TypeLevelValueType(table);
}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedBinaryOperationNodeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedBinaryOperationNodeMatcher.java
new file mode 100644
index 0000000..63cf1d8
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedBinaryOperationNodeMatcher.java
@@ -0,0 +1,59 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.untyped;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher imports
+
+public final class UntypedBinaryOperationNodeMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher isUntypedBinaryOperationNode() {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode>> submatchers;
+
+ private UntypedBinaryOperationNodeMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode>> submatchers
+ ) {
+ this.submatchers = submatchers;
+ }
+
+ public org.zwobble.precisely.MatchResult match(java.lang.Object actual) {
+ return this.toMatcher().match(actual);
+ }
+
+ public org.zwobble.precisely.TextTree describe() {
+ return this.toMatcher().describe();
+ }
+
+ private org.zwobble.precisely.Matcher<java.lang.Object> toMatcher() {
+ return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher withOperator(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperator> operator) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("operator", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode::operator, (org.zwobble.precisely.Matcher) operator));
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher withLeft(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode> left) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("left", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode::left, (org.zwobble.precisely.Matcher) left));
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher withRight(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedExpressionNode> right) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("right", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode::right, (org.zwobble.precisely.Matcher) right));
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher withSource(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.sources.Source> source) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("source", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNode::source, (org.zwobble.precisely.Matcher) source));
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher body
+}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNumberLiteralNodeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNumberLiteralNodeMatcher.java
new file mode 100644
index 0000000..b6b0f86
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedNumberLiteralNodeMatcher.java
@@ -0,0 +1,47 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.ast.untyped;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNodeMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNodeMatcher imports
+
+public final class UntypedNumberLiteralNodeMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNodeMatcher isUntypedNumberLiteralNode() {
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNodeMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode>> submatchers;
+
+ private UntypedNumberLiteralNodeMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode>> submatchers
+ ) {
+ this.submatchers = submatchers;
+ }
+
+ public org.zwobble.precisely.MatchResult match(java.lang.Object actual) {
+ return this.toMatcher().match(actual);
+ }
+
+ public org.zwobble.precisely.TextTree describe() {
+ return this.toMatcher().describe();
+ }
+
+ private org.zwobble.precisely.Matcher<java.lang.Object> toMatcher() {
+ return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNodeMatcher withValue(org.zwobble.precisely.Matcher<? super java.math.BigDecimal> value) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("value", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode::value, (org.zwobble.precisely.Matcher) value));
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNodeMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNodeMatcher withSource(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.sources.Source> source) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("source", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode::source, (org.zwobble.precisely.Matcher) source));
+ return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNodeMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNodeMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNodeMatcher body
+}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserBinaryOperationTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserBinaryOperationTests.java
new file mode 100644
index 0000000..ea2f673
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserBinaryOperationTests.java
@@ -0,0 +1,26 @@
+package org.zwobble.hobgoblin.compiler.parser;
+
+import org.junit.jupiter.api.Test;
+import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperator;
+
+import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedBinaryOperationNodeMatcher.isUntypedBinaryOperationNode;
+import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReferenceNodeMatcher.isUntypedReferenceNode;
+import static org.zwobble.hobgoblin.compiler.parser.ParserTesting.parseString;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.equalTo;
+
+public class ParserBinaryOperationTests {
+ @Test
+ public void canParseEquality() {
+ var source = "a == b";
+
+ var node = parseString(source, Parser::parseTopLevelExpression);
+
+ assertThat(node, isUntypedBinaryOperationNode()
+ .withOperator(equalTo(UntypedBinaryOperator.EQUALS))
+ .withLeft(isUntypedReferenceNode().withName(equalTo("a")))
+ .withRight(isUntypedReferenceNode().withName(equalTo("b")))
+ );
+ }
+}
+
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNumberTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNumberTests.java
new file mode 100644
index 0000000..92f11ee
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserNumberTests.java
@@ -0,0 +1,24 @@
+package org.zwobble.hobgoblin.compiler.parser;
+
+import org.junit.jupiter.api.Test;
+
+import java.math.BigDecimal;
+
+import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNodeMatcher.isUntypedNumberLiteralNode;
+import static org.zwobble.hobgoblin.compiler.parser.ParserTesting.parseString;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.equalTo;
+
+public class ParserNumberTests {
+ @Test
+ public void canParseNumber() {
+ var source = "42";
+
+ var node = parseString(source, Parser::parseTopLevelExpression);
+
+ assertThat(node, isUntypedNumberLiteralNode()
+ .withValue(equalTo(BigDecimal.valueOf(42)))
+ );
+ }
+}
+
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/parser/TokenizerTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/parser/TokenizerTests.java
index 86fb1f9..b434ac0 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/parser/TokenizerTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/parser/TokenizerTests.java
@@ -4,6 +4,7 @@ import org.junit.jupiter.api.Test;
import org.zwobble.sourcetext.SourceRange;
import org.zwobble.sourcetext.SourceText;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.equalTo;
import static org.zwobble.precisely.Matchers.isSequence;
@@ -118,6 +119,38 @@ public class TokenizerTests {
}
@Test
+ public void digitsAreTokenizedAsNumber() {
+ var sourceText = SourceText.fromString("<string>", "42");
+
+ var tokens = Tokenizer.tokenize(sourceText);
+
+ assertThat(tokens.tokens(), isSequence(
+ equalTo(new Token(TokenType.NUMBER, sourceRange(sourceText, 0, 2)))
+ ));
+ }
+
+ @Test
+ public void digitsFollowedByNonDigitIdentifierCharacterProducesError() {
+ var sourceText = SourceText.fromString("<string>", "42a");
+
+ var error = assertThrows(ParseError.class, () -> Tokenizer.tokenize(sourceText));
+
+ assertThat(error.getMessage(), equalTo("number must contain only digits"));
+ }
+
+ @Test
+ public void digitsMayBeFollowedByNonDigitNonIdentifierCharacter() {
+ var sourceText = SourceText.fromString("<string>", "42==");
+
+ var tokens = Tokenizer.tokenize(sourceText);
+
+ assertThat(tokens.tokens(), isSequence(
+ equalTo(new Token(TokenType.NUMBER, sourceRange(sourceText, 0, 2))),
+ equalTo(new Token(TokenType.SYMBOL_EQUALS_EQUALS, sourceRange(sourceText, 2, 4)))
+ ));
+ }
+
+ @Test
public void symbolBraceOpenIsTokenized() {
var sourceText = SourceText.fromString("<string>", "{");
@@ -195,6 +228,17 @@ public class TokenizerTests {
}
@Test
+ public void symbolEqualsEqualsIsTokenized() {
+ var sourceText = SourceText.fromString("<string>", "==");
+
+ var tokens = Tokenizer.tokenize(sourceText);
+
+ assertThat(tokens.tokens(), isSequence(
+ equalTo(new Token(TokenType.SYMBOL_EQUALS_EQUALS, sourceRange(sourceText, 0, 2)))
+ ));
+ }
+
+ @Test
public void symbolFatArrowIsTokenized() {
var sourceText = SourceText.fromString("<string>", "=>");
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerEvalExpressionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerEvalExpressionTests.java
index cf1e73c..48dde6e 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerEvalExpressionTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerEvalExpressionTests.java
@@ -1,7 +1,9 @@
package org.zwobble.hobgoblin.compiler.typechecker;
import org.junit.jupiter.api.Test;
+import org.zwobble.hobgoblin.compiler.ast.typed.RelationTable;
import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedMemberAccessNode;
+import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode;
import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReferenceNode;
import org.zwobble.hobgoblin.compiler.builtins.NativeTypes;
import org.zwobble.hobgoblin.compiler.sources.NullSource;
@@ -10,6 +12,7 @@ import org.zwobble.hobgoblin.compiler.typechecker.errors.UnexpectedTypeError;
import org.zwobble.hobgoblin.compiler.types.*;
import org.zwobble.hobgoblin.compiler.util.SequencedMaps;
+import java.math.BigDecimal;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -17,6 +20,69 @@ import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.equalTo;
public class TypeCheckerEvalExpressionTests {
+ // == Member access ==
+
+ @Test
+ public void memberAccessCanReadFieldOnStruct() {
+ var untyped = UntypedMemberAccessNode.arbitrary()
+ .withReceiver(UntypedReferenceNode.arbitrary().withName("blogApp"))
+ .withMemberName("authors")
+ .build();
+ var globalContext = TypeCheckerContextArb.globalContext();
+ var namespaceName = NamespaceName.of("App");
+ var namespaceContext = globalContext.enterNamespace(namespaceName);
+ var databaseSchema = new DatabaseSchema(namespaceName, "blogApp");
+ namespaceContext.declare("blogApp", Types.databaseSchema(databaseSchema), NullSource.INSTANCE);
+ var table = new Table(databaseSchema, "authors");
+ var rowType = new SimpleStructType(namespaceName, "Author");
+ var idField = new Field("id", NativeTypes.INT_32, NullSource.INSTANCE);
+ var databaseSchemaInfo = new DatabaseSchemaInfo(SequencedMaps.ofEntries(
+ Map.entry("authors", new TableInfo(table, rowType, idField))
+ ));
+ namespaceContext.defineDatabaseSchema(databaseSchema, databaseSchemaInfo);
+
+ var typed = TypeChecker.evalExpression(untyped, namespaceContext);
+
+ assertThat(typed, equalTo(table));
+ }
+
+ @Test
+ public void memberAccessOnTableCanReadRelationMethod() {
+ var untyped = UntypedMemberAccessNode.arbitrary()
+ .withReceiver(UntypedReferenceNode.arbitrary().withName("authors"))
+ .withMemberName("where")
+ .build();
+ var globalContext = TypeCheckerContextArb.globalContext();
+ var namespaceName = NamespaceName.of("App");
+ var namespaceContext = globalContext.enterNamespace(namespaceName);
+ var databaseSchema = new DatabaseSchema(namespaceName, "blogApp");
+ var table = new Table(databaseSchema, "authors");
+ namespaceContext.declare("authors", Types.table(table), NullSource.INSTANCE);
+
+ var typed = TypeChecker.evalExpression(untyped, namespaceContext);
+
+ assertThat(typed, equalTo(new RelationMethod(new RelationTable(table), "where")));
+ }
+
+ // == Number literal ==
+
+ @Test
+ public void canEvaluateNumber() {
+ // TODO: infer more precise type for number
+ var untyped = UntypedNumberLiteralNode.arbitrary()
+ .withValue(BigDecimal.valueOf(42))
+ .build();
+ var globalContext = TypeCheckerContextArb.globalContext();
+ var namespaceName = NamespaceName.of("App");
+ var namespaceContext = globalContext.enterNamespace(namespaceName);
+
+ var typed = TypeChecker.evalExpression(untyped, namespaceContext);
+
+ assertThat(typed, equalTo(new ConstantNumber(BigDecimal.valueOf(42))));
+ }
+
+ // == References ==
+
@Test
public void whenVariableIsTypeLevelValueThenCanEvaluateReference() {
var untyped = UntypedReferenceNode.arbitrary()
@@ -68,28 +134,4 @@ public class TypeCheckerEvalExpressionTests {
assertThat(error.variableName(), equalTo("count"));
}
-
- @Test
- public void canReadMemberField() {
- var untyped = UntypedMemberAccessNode.arbitrary()
- .withReceiver(UntypedReferenceNode.arbitrary().withName("blogApp"))
- .withMemberName("authors")
- .build();
- var globalContext = TypeCheckerContextArb.globalContext();
- var namespaceName = NamespaceName.of("App");
- var namespaceContext = globalContext.enterNamespace(namespaceName);
- var databaseSchema = new DatabaseSchema(namespaceName, "blogApp");
- namespaceContext.declare("blogApp", Types.databaseSchema(databaseSchema), NullSource.INSTANCE);
- var table = new Table(databaseSchema, "authors");
- var rowType = new SimpleStructType(namespaceName, "Author");
- var idField = new Field("id", NativeTypes.INT_32, NullSource.INSTANCE);
- var databaseSchemaInfo = new DatabaseSchemaInfo(SequencedMaps.ofEntries(
- Map.entry("authors", new TableInfo(table, rowType, idField))
- ));
- namespaceContext.defineDatabaseSchema(databaseSchema, databaseSchemaInfo);
-
- var typed = TypeChecker.evalExpression(untyped, namespaceContext);
-
- assertThat(typed, equalTo(table));
- }
}