about summary refs log tree commit diff
path: root/pkg/lang/vm/errors.go
blob: 9859febcc4b3cd3d4987237f614a56ae6b73a184 (plain)
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
package vm

import (
	"errors"
	"fmt"
	"jinx/pkg/lang/vm/code"
	"jinx/pkg/lang/vm/text"
	"jinx/pkg/lang/vm/value"
)

type Error struct {
	Pc  int
	Err error
}

func (e Error) Error() string {
	return fmt.Sprintf("vm error at pc %d: %s", e.Pc, e.Err)
}

// Fatal errors

var (
	ErrCallStackOverflow   = errors.New("call stack overflow (max depth: 1000)")
	ErrLocalStackOverflow  = errors.New("local stack overflow (max depth: 1000)")
	ErrNoPreviousCallFrame = errors.New("no previous call frame")
	ErrCantPopRootFrame    = errors.New("cannot pop root frame")

	ErrCallFrameEmpty = errors.New("current call frame is empty")
)

type ErrLocalIndexOutOfBounds struct {
	Index int
	Len   int
}

func (e ErrLocalIndexOutOfBounds) Error() string {
	return fmt.Sprintf("local index out of bounds: %d (len: %d)", e.Index, e.Len)
}

type ErrInvalidOp struct {
	Op uint8
}

func (e ErrInvalidOp) Error() string {
	return fmt.Sprintf("invalid opcode: %d", e.Op)
}

// Non-fatal errors, which will later be implemented as catchable exceptions

type ErrInvalidOperandTypes struct {
	Op code.Op
	X  value.Type
	Y  value.Type
}

func (e ErrInvalidOperandTypes) Error() string {
	return fmt.Sprintf("invalid operand types for op %s: %v, %v", text.OpToString(e.Op), e.X, e.Y)
}

type ErrArrayIndexOutOfBounds struct {
	Index int
	Len   int
}

func (e ErrArrayIndexOutOfBounds) Error() string {
	return fmt.Sprintf("array index out of bounds: %d (len: %d)", e.Index, e.Len)
}