-
Notifications
You must be signed in to change notification settings - Fork 6
/
list.go
54 lines (44 loc) · 1.04 KB
/
list.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
package ast
import (
"errors"
"fmt"
"strings"
"github.com/NicoNex/tau/internal/code"
"github.com/NicoNex/tau/internal/compiler"
"github.com/NicoNex/tau/internal/obj"
)
type List []Node
func NewList(elements ...Node) Node {
var ret List
for _, e := range elements {
ret = append(ret, e)
}
return ret
}
// TODO: optimise this for the case where all the list elements are constant expressions.
func (l List) Eval() (obj.Object, error) {
return obj.NullObj, errors.New("ast.Index: not a constant expression")
}
func (l List) String() string {
var elements []string
for _, e := range l {
if s, ok := e.(String); ok {
elements = append(elements, s.Quoted())
} else {
elements = append(elements, e.String())
}
}
return fmt.Sprintf("[%s]", strings.Join(elements, ", "))
}
func (l List) Compile(c *compiler.Compiler) (position int, err error) {
for _, n := range l {
if position, err = n.Compile(c); err != nil {
return
}
}
position = c.Emit(code.OpList, len(l))
return
}
func (l List) IsConstExpression() bool {
return false
}