-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzc.go
121 lines (107 loc) · 1.8 KB
/
zc.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package zc
import (
"strings"
"github.com/blackchip-org/scan"
"github.com/blackchip-org/zc/v6/msg"
"github.com/blackchip-org/zc/v6/pkg/coll"
)
type Type interface {
Name() string
AppName() string
GoName() string
Parse(coll.State, string) (any, bool, error)
Format(coll.State, any) string
Dup(any) any
}
type Item struct {
TypeVal any
Type Type
val string
Unit string
Label string
}
func (i Item) Val() string {
var str string
if i.val == "" {
str = i.Type.Format(coll.NullState, i.TypeVal)
} else {
str = i.val
}
return str
}
func (i Item) Dup() Item {
return Item{
TypeVal: i.Type.Dup(i.TypeVal),
Type: i.Type,
Unit: i.Unit,
Label: i.Label,
}
}
func (i Item) String() string {
var s strings.Builder
if i.Label != "" {
s.WriteString(i.Label)
s.WriteString(": ")
}
s.WriteString(msg.EscapeString(i.Val()))
if i.Unit != "" {
s.WriteRune(' ')
s.WriteString(i.Unit)
}
return s.String()
}
func DupItems(items []Item) []Item {
items2 := make([]Item, len(items))
for i, item := range items {
items2[i] = item.Dup()
}
return items2
}
type State interface {
State(string) (any, bool)
NewState(string, any)
}
type Calc interface {
Push(...Item)
Pop() Item
Stack() []Item
SetStack([]Item)
Len() int
String() string
Var(string) (any, bool)
NewVar(string, any)
Notify(string, ...any)
Raise(error)
Label() string
Unit() string
SetLabel(string)
SetUnit(string)
Eval(string) error
New() Calc
Temp() []Item
SetTemp([]Item)
Store(string)
Load(string)
Reset()
}
type Op struct {
Name string
Funcs []Func
Macro []scan.Token
}
type Func struct {
Params []Type
VarParam Type
Returns []Type
VarReturn Type
Eval func(Calc)
}
type Macro struct {
Name string
Expr string
}
type Vol struct {
Name string
Ops []Op
Macros []Macro
}