diff options
Diffstat (limited to 'symbol')
| -rw-r--r-- | symbol/symbol.go | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/symbol/symbol.go b/symbol/symbol.go index 31869fd..e00b26d 100644 --- a/symbol/symbol.go +++ b/symbol/symbol.go @@ -31,6 +31,7 @@ const UnsetAddr = -65535 // Symbol structure used in parser and compiler. type Symbol struct { Kind Kind + Name string // Index int // address of symbol in frame PkgPath string // Type *vm.Type // @@ -40,6 +41,19 @@ type Symbol struct { Used bool // } +// func (s *Symbol) String() string { +// return fmt.Sprintf("{Kind: %v, Name: %v, Index: %v, Type: %v}\n", s.Kind, s.Name, s.Index, s.Type) +//} + +// IsConst return true if symbol is a constant. +func (s *Symbol) IsConst() bool { return s.Kind == Const } + +// IsType return true if symbol is a type. +func (s *Symbol) IsType() bool { return s.Kind == Type } + +// IsFunc return true if symbol is a function. +func (s *Symbol) IsFunc() bool { return s.Kind == Func } + // Vtype returns the VM type of a symbol. func Vtype(s *Symbol) *vm.Type { if s.Type != nil { @@ -54,7 +68,7 @@ type SymMap map[string]*Symbol // Add adds a new named symbol value at memory position i. func (sm SymMap) Add(i int, name string, v vm.Value, k Kind, t *vm.Type, local bool) { name = strings.TrimPrefix(name, "/") - sm[name] = &Symbol{Kind: k, Index: i, Local: local, Value: v, Type: t} + sm[name] = &Symbol{Kind: k, Name: name, Index: i, Local: local, Value: v, Type: t} } // Get searches for an existing symbol starting from the deepest scope. @@ -77,16 +91,16 @@ func (sm SymMap) Get(name, scope string) (sym *Symbol, sc string, ok bool) { // Init fills the symbol map with default Go symbols. func (sm SymMap) Init() { - sm["any"] = &Symbol{Kind: Type, Index: UnsetAddr, Type: vm.TypeOf((*any)(nil)).Elem()} - sm["bool"] = &Symbol{Kind: Type, Index: UnsetAddr, Type: vm.TypeOf((*bool)(nil)).Elem()} - sm["error"] = &Symbol{Kind: Type, Index: UnsetAddr, Type: vm.TypeOf((*error)(nil)).Elem()} - sm["int"] = &Symbol{Kind: Type, Index: UnsetAddr, Type: vm.TypeOf((*int)(nil)).Elem()} - sm["string"] = &Symbol{Kind: Type, Index: UnsetAddr, Type: vm.TypeOf((*string)(nil)).Elem()} - - sm["nil"] = &Symbol{Index: UnsetAddr} - sm["iota"] = &Symbol{Kind: Const, Index: UnsetAddr} - sm["true"] = &Symbol{Index: UnsetAddr, Value: vm.ValueOf(true), Type: vm.TypeOf(true)} - sm["false"] = &Symbol{Index: UnsetAddr, Value: vm.ValueOf(false), Type: vm.TypeOf(false)} - - sm["println"] = &Symbol{Index: UnsetAddr, Value: vm.ValueOf(func(v ...any) { fmt.Println(v...) })} + sm["any"] = &Symbol{Name: "any", Kind: Type, Index: UnsetAddr, Type: vm.TypeOf((*any)(nil)).Elem()} + sm["bool"] = &Symbol{Name: "bool", Kind: Type, Index: UnsetAddr, Type: vm.TypeOf((*bool)(nil)).Elem()} + sm["error"] = &Symbol{Name: "error", Kind: Type, Index: UnsetAddr, Type: vm.TypeOf((*error)(nil)).Elem()} + sm["int"] = &Symbol{Name: "int", Kind: Type, Index: UnsetAddr, Type: vm.TypeOf((*int)(nil)).Elem()} + sm["string"] = &Symbol{Name: "string", Kind: Type, Index: UnsetAddr, Type: vm.TypeOf((*string)(nil)).Elem()} + + sm["nil"] = &Symbol{Name: "nil", Index: UnsetAddr} + sm["iota"] = &Symbol{Name: "iota", Kind: Const, Index: UnsetAddr} + sm["true"] = &Symbol{Name: "true", Index: UnsetAddr, Value: vm.ValueOf(true), Type: vm.TypeOf(true)} + sm["false"] = &Symbol{Name: "false", Index: UnsetAddr, Value: vm.ValueOf(false), Type: vm.TypeOf(false)} + + sm["println"] = &Symbol{Name: "println", Index: UnsetAddr, Value: vm.ValueOf(func(v ...any) { fmt.Println(v...) })} } |
