-
Notifications
You must be signed in to change notification settings - Fork 6
/
bitwisenot.go
61 lines (50 loc) · 1.16 KB
/
bitwisenot.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
package ast
import (
"fmt"
"github.com/NicoNex/tau/internal/code"
"github.com/NicoNex/tau/internal/compiler"
"github.com/NicoNex/tau/internal/obj"
)
type BitwiseNot struct {
n Node
pos int
}
func NewBitwiseNot(n Node, pos int) Node {
return BitwiseNot{
n: n,
pos: pos,
}
}
func (b BitwiseNot) Eval() (obj.Object, error) {
value, err := b.n.Eval()
if err != nil {
return obj.NullObj, err
}
if !obj.AssertTypes(value, obj.IntType) {
return obj.NullObj, fmt.Errorf("unsupported operator '~' for type %v", value.Type())
}
return obj.NewInteger(^int64(value.(obj.Integer))), nil
}
func (b BitwiseNot) String() string {
return fmt.Sprintf("~%v", b.n)
}
func (b BitwiseNot) Compile(c *compiler.Compiler) (position int, err error) {
if b.IsConstExpression() {
o, err := b.Eval()
if err != nil {
return 0, c.NewError(b.pos, err.Error())
}
position = c.Emit(code.OpConstant, c.AddConstant(o))
c.Bookmark(b.pos)
return position, err
}
if position, err = b.n.Compile(c); err != nil {
return
}
position = c.Emit(code.OpBwNot)
c.Bookmark(b.pos)
return
}
func (b BitwiseNot) IsConstExpression() bool {
return b.n.IsConstExpression()
}