Skip to content

Commit 39eaaff

Browse files
committed
compiler: symtable - stringer and fix test
1 parent f1c2c69 commit 39eaaff

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

compile/symtable.go

+21-12
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77
"github.com/ncw/gpython/py"
88
)
99

10+
//go:generate stringer -type=Scope,BlockType -output stringer.go
11+
1012
// Scope
1113
type Scope uint8
1214

1315
// Scope definitions
1416
const (
15-
scopeInvalid = Scope(iota)
17+
scopeInvalid Scope = iota
1618
scopeLocal
1719
scopeGlobalExplicit
1820
scopeGlobalImplicit
@@ -25,14 +27,14 @@ type DefUse uint8
2527

2628
// Flags for def-use information
2729
const (
28-
defGlobal = DefUse(1 << iota) // global stmt
29-
defLocal // assignment in code block
30-
defParam // formal parameter
31-
defNonlocal // nonlocal stmt
32-
defUse // name is used
33-
defFree // name used but not defined in nested block
34-
defFreeClass // free variable from class's method
35-
defImport // assignment occurred via import
30+
defGlobal DefUse = 1 << iota // global stmt
31+
defLocal // assignment in code block
32+
defParam // formal parameter
33+
defNonlocal // nonlocal stmt
34+
defUse // name is used
35+
defFree // name used but not defined in nested block
36+
defFreeClass // free variable from class's method
37+
defImport // assignment occurred via import
3638

3739
defBound = (defLocal | defParam | defImport)
3840

@@ -47,7 +49,7 @@ type BlockType uint8
4749

4850
// BlockTypes
4951
const (
50-
FunctionBlock = BlockType(iota)
52+
FunctionBlock BlockType = iota
5153
ClassBlock
5254
ModuleBlock
5355
)
@@ -79,9 +81,16 @@ type SymTable struct {
7981
Varnames []string // list of function parameters
8082
}
8183

82-
// Make a new symbol table from the ast supplied
83-
func NewSymTable(Ast ast.Ast, parent *SymTable) *SymTable {
84+
// Make a new top symbol table from the ast supplied
85+
func NewSymTable(Ast ast.Ast) *SymTable {
86+
return newSymTable(Ast, ModuleBlock, "top", nil)
87+
}
88+
89+
// Make a new symbol table from the ast supplied of the given type
90+
func newSymTable(Ast ast.Ast, Type BlockType, Name string, parent *SymTable) *SymTable {
8491
st := &SymTable{
92+
Type: ModuleBlock,
93+
Name: "top",
8594
Parent: parent,
8695
Symbols: NewSymbols(),
8796
}

compile/symtable_test.go

+17-5
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,31 @@ import (
1111

1212
func EqInt(t *testing.T, name string, a, b int) {
1313
if a != b {
14-
t.Errorf("%s want %d, got %d", name, a, b)
14+
t.Errorf("%s want %v, got %v", name, a, b)
15+
}
16+
}
17+
18+
func EqScope(t *testing.T, name string, a, b Scope) {
19+
if a != b {
20+
t.Errorf("%s want %v, got %v", name, a, b)
21+
}
22+
}
23+
24+
func EqBlockType(t *testing.T, name string, a, b BlockType) {
25+
if a != b {
26+
t.Errorf("%s want %v, got %v", name, a, b)
1527
}
1628
}
1729

1830
func EqBool(t *testing.T, name string, a, b bool) {
1931
if a != b {
20-
t.Errorf("%s want %d, got %d", name, a, b)
32+
t.Errorf("%s want %v, got %v", name, a, b)
2133
}
2234
}
2335

2436
func EqSymbol(t *testing.T, name string, a, b Symbol) {
2537
EqString(t, name+".Name", a.Name, b.Name)
26-
EqInt(t, name+".Scope", int(a.Scope), int(b.Scope))
38+
EqScope(t, name+".Scope", a.Scope, b.Scope)
2739
EqInt(t, name+".Flags", int(a.Flags), int(b.Flags))
2840
if a.Namespace == nil {
2941
if b.Namespace == nil {
@@ -61,7 +73,7 @@ func EqSymbols(t *testing.T, name string, a, b Symbols) {
6173
}
6274

6375
func EqSymTable(t *testing.T, name string, a, b *SymTable) {
64-
EqInt(t, name+": Type", int(a.Type), int(b.Type))
76+
EqBlockType(t, name+": Type", a.Type, b.Type)
6577
EqString(t, name+": Name", a.Name, b.Name)
6678
EqInt(t, name+": Lineno", a.Lineno, b.Lineno)
6779
EqBool(t, name+": Optimized", a.Optimized, b.Optimized)
@@ -109,7 +121,7 @@ func TestSymTable(t *testing.T) {
109121
if err != nil {
110122
panic(err) // FIXME error handling!
111123
}
112-
symtab = NewSymTable(Ast, nil)
124+
symtab = NewSymTable(Ast)
113125
}()
114126
if test.out == nil {
115127
if symtab != nil {

0 commit comments

Comments
 (0)