-
Notifications
You must be signed in to change notification settings - Fork 6
/
dot.go
62 lines (53 loc) · 1.35 KB
/
dot.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
60
61
62
package ast
import (
"errors"
"fmt"
"github.com/NicoNex/tau/internal/code"
"github.com/NicoNex/tau/internal/compiler"
"github.com/NicoNex/tau/internal/obj"
)
type Dot struct {
l Node
r Node
pos int
}
func NewDot(l, r Node, pos int) Node {
return Dot{
l: l,
r: r,
pos: pos,
}
}
func (d Dot) Eval() (obj.Object, error) {
return obj.NullObj, errors.New("ast.Dot: not a constant expression")
}
func (d Dot) String() string {
return fmt.Sprintf("%v.%v", d.l, d.r)
}
func (d Dot) Compile(c *compiler.Compiler) (position int, err error) {
if position, err = d.l.Compile(c); err != nil {
return
}
if _, ok := d.r.(Identifier); !ok {
return position, fmt.Errorf("expected identifier with dot operator, got %T", d.r)
}
position = c.Emit(code.OpConstant, c.AddConstant(obj.NewString(d.r.String())))
position = c.Emit(code.OpDot)
c.Bookmark(d.pos)
return
}
// CompileDefine assumes the dot operation is for defining a value.
func (d Dot) CompileDefine(c *compiler.Compiler) (position int, err error) {
if position, err = d.l.Compile(c); err != nil {
return
}
if _, ok := d.r.(Identifier); !ok {
return position, fmt.Errorf("expected identifier with dot operator, got %T", d.r)
}
position = c.Emit(code.OpConstant, c.AddConstant(obj.NewString(d.r.String())))
c.Bookmark(d.pos)
return
}
func (d Dot) IsConstExpression() bool {
return false
}