diff options
| author | Marc Vertes <mvertes@free.fr> | 2024-03-08 19:29:34 +0100 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2024-03-08 19:29:34 +0100 |
| commit | 8c4fa9d85cd274439dbd7d0a5c699fe1cea557dc (patch) | |
| tree | a8c910cf13aa06e61ee2db40b5dd2f1f63a25b9d /parser/symbol.go | |
| parent | 828cfd1c8da5e243fd0f34530fb1a7faab49784e (diff) | |
feat: add type representation in vm package
Type and Value types in vm package are now used in place of reflect.Type
and reflect.Value. It allows to remove the dependency on reflect for
parser and compiler packages.
The main purpose of Type is to provide a solution to implement recursive
structs, named types, interfaces and methods, despite the limitations of
Go reflect. The goal is to provide the thinnest layer around reflect.
Diffstat (limited to 'parser/symbol.go')
| -rw-r--r-- | parser/symbol.go | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/parser/symbol.go b/parser/symbol.go index 6c5ab9b..c75f241 100644 --- a/parser/symbol.go +++ b/parser/symbol.go @@ -3,8 +3,9 @@ package parser import ( "fmt" "go/constant" - "reflect" "strings" + + "github.com/mvertes/parscan/vm" ) type symKind int @@ -23,25 +24,25 @@ const unsetAddr = -65535 type symbol struct { kind symKind index int // address of symbol in frame - value reflect.Value // + Type *vm.Type // + value vm.Value // cval constant.Value // - Type reflect.Type // local bool // if true address is relative to local frame, otherwise global used bool // } -func symtype(s *symbol) reflect.Type { +func symtype(s *symbol) *vm.Type { if s.Type != nil { return s.Type } - return reflect.TypeOf(s.value) + return vm.TypeOf(s.value) } -func (p *Parser) AddSym(i int, name string, v reflect.Value) { +func (p *Parser) AddSym(i int, name string, v vm.Value) { p.addSym(i, name, v, symValue, nil, false) } -func (p *Parser) addSym(i int, name string, v reflect.Value, k symKind, t reflect.Type, local bool) { +func (p *Parser) addSym(i int, name string, v vm.Value, k symKind, t *vm.Type, local bool) { name = strings.TrimPrefix(name, "/") p.symbols[name] = &symbol{kind: k, index: i, local: local, value: v, Type: t} } @@ -66,17 +67,17 @@ func (p *Parser) getSym(name, scope string) (sym *symbol, sc string, ok bool) { func initUniverse() map[string]*symbol { return map[string]*symbol{ - "any": {kind: symType, index: unsetAddr, Type: reflect.TypeOf((*any)(nil)).Elem()}, - "bool": {kind: symType, index: unsetAddr, Type: reflect.TypeOf((*bool)(nil)).Elem()}, - "error": {kind: symType, index: unsetAddr, Type: reflect.TypeOf((*error)(nil)).Elem()}, - "int": {kind: symType, index: unsetAddr, Type: reflect.TypeOf((*int)(nil)).Elem()}, - "string": {kind: symType, index: unsetAddr, Type: reflect.TypeOf((*string)(nil)).Elem()}, + "any": {kind: symType, index: unsetAddr, Type: vm.TypeOf((*any)(nil)).Elem()}, + "bool": {kind: symType, index: unsetAddr, Type: vm.TypeOf((*bool)(nil)).Elem()}, + "error": {kind: symType, index: unsetAddr, Type: vm.TypeOf((*error)(nil)).Elem()}, + "int": {kind: symType, index: unsetAddr, Type: vm.TypeOf((*int)(nil)).Elem()}, + "string": {kind: symType, index: unsetAddr, Type: vm.TypeOf((*string)(nil)).Elem()}, "nil": {index: unsetAddr}, "iota": {kind: symConst, index: unsetAddr}, - "true": {index: unsetAddr, value: reflect.ValueOf(true), Type: reflect.TypeOf(true)}, - "false": {index: unsetAddr, value: reflect.ValueOf(false), Type: reflect.TypeOf(false)}, + "true": {index: unsetAddr, value: vm.ValueOf(true), Type: vm.TypeOf(true)}, + "false": {index: unsetAddr, value: vm.ValueOf(false), Type: vm.TypeOf(false)}, - "println": {index: unsetAddr, value: reflect.ValueOf(func(v ...any) { fmt.Println(v...) })}, + "println": {index: unsetAddr, value: vm.ValueOf(func(v ...any) { fmt.Println(v...) })}, } } |
