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
| func NewSymTable() *SymTable { sym := &SymTable{ Variants: []Variant{}, Fuctions: []Fuction{}, InFuctions: inFunction{}, } sym.initBuildInFunc() return sym }
func NewVariant(name string, value string, typ int) *Variant { return &Variant{name, value, typ} }
func (vl *SymTable) showVarAll() { fmt.Println("|-----------------SymTable-----------------------|") fmt.Println("|name \t\t type \t\t value \t\t |") fmt.Println("|------------------------------------------------|") for _, v := range vl.Variants { fmt.Printf("|%-15v %-15v %-15v |\n", v.name, v.typ, v.value) } fmt.Printf("|------------------------------------------------|\n\n") }
func (vl *SymTable) addVar(name string, value string, typ int) { vl.Variants = append(vl.Variants, *NewVariant(name, value, typ)) }
func (vl *SymTable) getVarByName(name string) (string, int) { for _, v := range vl.Variants { if v.name == name { return v.value, v.typ } } return "", -1 }
func (vl *SymTable) getvarByIndex(index int) (string, int) { return vl.Variants[index].value, vl.Variants[index].typ }
func (vl *SymTable) getVarlen() int { return len(vl.Variants) }
func (vl *SymTable) delVar() { vl.Variants = vl.Variants[:len(vl.Variants)-1] }
func NewFunction(name string, args []string, ctx *Jsp.FuncdefContext) *Fuction { return &Fuction{name, args, ctx} }
func (vl *SymTable) addFunc(name string, args []string, ctx *Jsp.FuncdefContext) { vl.Fuctions = append(vl.Fuctions, *NewFunction(name, args, ctx)) }
func (vl *SymTable) getFunc(name string) ([]string, *Jsp.FuncdefContext) { for _, f := range vl.Fuctions { if f.name == name { return f.args, f.fctx } } return nil, nil }
func (vl *SymTable) showFuncAll() { fmt.Println("|----------Function--------------|") fmt.Println("|name \t\t args \t\t |") for _, v := range vl.Fuctions { fmt.Printf("|%-15v[ ", v.name) for _, a := range v.args { fmt.Printf("%v ", a) } fmt.Println("]") } fmt.Printf("|--------------------------------|\n\n") }
func (vl *SymTable) initBuildInFunc() { vl.InFuctions.log = func(msg string) int { fmt.Printf("[+]log: %v\n", msg) return 0 } }
func (vl *SymTable) callInFunc(name string, args []Variant) string { if name == "log" { vl.InFuctions.log(args[0].value) } return "" }
|