-
Notifications
You must be signed in to change notification settings - Fork 0
/
st.go
133 lines (118 loc) · 2.91 KB
/
st.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
122
123
124
125
126
127
128
129
130
131
132
133
package st
//
// S T - Symbol table
//
// (C) Philip Schlump, 2013-2015.
// Version: 1.0.0
// BuildNo: 28
//
// Special Thanks to 2C-Why, LLC for supporting this project.
//
import (
"errors"
"fmt"
"io"
"sync"
"github.com/pschlump/lexie/com"
"github.com/pschlump/lexie/mt"
"github.com/pschlump/lexie/gen"
)
// Add to this Fx - pointer to function for eval of builtins?
type SymbolType struct {
Name string //
Body string //
SymType int // new - Macro / Item-Name / Begnning-Block-Name / Ending Block Name? / If/Eif/Else block name?
FxId int // new
AnyData interface{} //
NParam int //
ParamName []string //
DefVal []string // new - Default value for params in param order
Prev *SymbolType //
}
type SymbolTable struct {
Symbols map[string]*SymbolType
mutex sync.RWMutex
}
func NewSymbolTable() (st *SymbolTable) {
st = &SymbolTable{
Symbols: make(map[string]*SymbolType),
}
return
}
var NotFoundError = errors.New("Not Found")
func (st *SymbolTable) LookupSymbol(name string) (as *SymbolType, err error) {
ok := false
err = nil
st.mutex.RLock()
if as, ok = st.Symbols[name]; ok {
st.mutex.RUnlock()
return
}
st.mutex.RUnlock()
as = nil
err = NotFoundError
return
}
func (st *SymbolTable) DefineSymbol(name, body string, plist []string) (ss *SymbolType) {
as, err := st.LookupSymbol(name)
x := &SymbolType{
Name: name, //
Body: body, //
ParamName: plist, //
SymType: gen.Tok_Tree_Macro, // Tok_Tree_Macro
FxId: 0, // not a builtin
NParam: 0, // no params defined
}
if err == nil {
x.Prev = as
}
st.mutex.Lock()
st.Symbols[name] = x
st.mutex.Unlock()
ss = x
return
}
func (st *SymbolTable) DefineReservedWord(name string, fxid int) (ss *SymbolType) {
as, err := st.LookupSymbol(name)
x := &SymbolType{
Name: name, //
SymType: gen.Tok_Tree_Macro, // Tok_Tree_Macro
FxId: fxid, //
}
if err == nil {
x.Prev = as
}
st.mutex.Lock()
st.Symbols[name] = x
st.mutex.Unlock()
ss = x
return
}
func (st *SymbolTable) UnDefineSymbol(name string) {
as, err := st.LookupSymbol(name)
if err == nil {
st.mutex.Lock()
if as.Prev != nil {
st.Symbols[name] = as.Prev
} else {
delete(st.Symbols, name)
}
st.mutex.Unlock()
}
}
func (st *SymbolTable) Dump01(fo io.Writer) {
fmt.Fprintf(fo, "Dump of symbol table\n")
for ii, vv := range st.Symbols {
fmt.Fprintf(fo, "[%s] %s \n", ii, vv.Body)
}
}
func (st *SymbolTable) DumpSymbolTable(fo io.Writer) {
for ii, vv := range st.Symbols {
fmt.Fprintf(fo, "\t[%s] Body=%s SymType=%d FxId=%d\n", ii, vv.Body, vv.SymType, vv.FxId)
if vv.SymType == gen.Tok_Template {
fmt.Fprintf(fo, "\t\tTemplate\n")
mtv := vv.AnyData.(*mt.MtType)
fmt.Fprintf(fo, "AnyData = %s\n", com.SVarI(mtv))
}
}
}