aboutsummaryrefslogtreecommitdiff
path: root/generate_error.go
blob: 88fe3f4ea715cf6b3c6301164e57f1cfa6f08189 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// SPDX-FileCopyrightText: 2024 Himbeer <himbeer@disroot.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later

package main

import "fmt"

type errAlreadyDeclared struct {
	name string
	line int
}

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
	kind undeclaredKind
	line int
}

func (e errUndeclared) Error() string {
	return fmt.Sprintf("%d: undeclared %s %q\n", e.line, e.kind, e.name)
}

type errImmutable struct {
	name string
	line int
}

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)
}