aboutsummaryrefslogtreecommitdiff
path: root/generate_error.go
diff options
context:
space:
mode:
authorHimbeer <himbeer@disroot.org>2024-09-06 13:19:22 +0200
committerHimbeer <himbeer@disroot.org>2024-09-06 13:56:07 +0200
commit959599b7bf803a91595375f650245609bf7a338f (patch)
tree47aafa8d49e00e45c0cb25255c8486eeec763692 /generate_error.go
parent05c620c4d637b73f6b267ed57d5750587ccfd7a3 (diff)
Add basic type system with only integers
This commit adds static typing with signed and unsigned integers of 8, 16, 32 and 64 bits.
Diffstat (limited to 'generate_error.go')
-rw-r--r--generate_error.go52
1 files changed, 43 insertions, 9 deletions
diff --git a/generate_error.go b/generate_error.go
index e89add2..88fe3f4 100644
--- a/generate_error.go
+++ b/generate_error.go
@@ -15,19 +15,35 @@ func (e errAlreadyDeclared) Error() string {
return fmt.Sprintf("%d: redeclaration of %q\n", e.line, e.name)
}
+type undeclaredKind int
+
+const (
+ undeclaredVariable undeclaredKind = iota
+ undeclaredFunction
+ undeclaredType
+)
+
+func (u undeclaredKind) String() string {
+ switch u {
+ case undeclaredVariable:
+ return "variable"
+ case undeclaredFunction:
+ return "function"
+ case undeclaredType:
+ return "type"
+ default:
+ return "identifier"
+ }
+}
+
type errUndeclared struct {
- name string
- isFunc bool
- line int
+ name string
+ kind undeclaredKind
+ line int
}
func (e errUndeclared) Error() string {
- kind := "variable"
- if e.isFunc {
- kind = "function"
- }
-
- return fmt.Sprintf("%d: undeclared %s %q\n", e.line, kind, e.name)
+ return fmt.Sprintf("%d: undeclared %s %q\n", e.line, e.kind, e.name)
}
type errImmutable struct {
@@ -38,3 +54,21 @@ type errImmutable struct {
func (e errImmutable) Error() string {
return fmt.Sprintf("%d: cannot assign to constant %q\n", e.line, e.name)
}
+
+type errTypeMismatch struct {
+ expected, got cerType
+ line int
+}
+
+func (e errTypeMismatch) Error() string {
+ return fmt.Sprintf("%d: expected type %s, got %s", e.line, e.expected, e.got)
+}
+
+type errArgNumMismatch struct {
+ expected, got int
+ line int
+}
+
+func (e errArgNumMismatch) Error() string {
+ return fmt.Sprintf("%d: function expects %d arguments, got %d", e.line, e.expected, e.got)
+}