-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.go
59 lines (50 loc) · 1.13 KB
/
index.go
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
package ast
import (
"errors"
"fmt"
"github.com/NicoNex/tau/internal/code"
"github.com/NicoNex/tau/internal/compiler"
"github.com/NicoNex/tau/internal/obj"
)
type Index struct {
left Node
index Node
pos int
}
func NewIndex(l, i Node, pos int) Node {
return Index{left: l,
index: i,
pos: pos,
}
}
func (i Index) Eval() (obj.Object, error) {
return obj.NullObj, errors.New("ast.Index: not a constant expression")
}
func (i Index) String() string {
return fmt.Sprintf("%v[%v]", i.left, i.index)
}
func (i Index) Compile(c *compiler.Compiler) (position int, err error) {
if position, err = i.left.Compile(c); err != nil {
return
}
if position, err = i.index.Compile(c); err != nil {
return
}
position = c.Emit(code.OpIndex)
c.Bookmark(i.pos)
return
}
// CompileDefine assumes the index operation is for defining a value.
func (i Index) CompileDefine(c *compiler.Compiler) (position int, err error) {
if position, err = i.left.Compile(c); err != nil {
return
}
if position, err = i.index.Compile(c); err != nil {
return
}
c.Bookmark(i.pos)
return
}
func (i Index) IsConstExpression() bool {
return false
}