|
10 | 10 | package compile |
11 | 11 |
|
12 | 12 | import ( |
| 13 | + "log" |
13 | 14 | "strings" |
14 | 15 |
|
15 | 16 | "github.com/ncw/gpython/ast" |
@@ -153,10 +154,34 @@ func (st *SymTable) Parse(Ast ast.Ast) { |
153 | 154 | switch node := Ast.(type) { |
154 | 155 | case *ast.Nonlocal: |
155 | 156 | for _, name := range node.Names { |
| 157 | + cur, ok := st.Symbols[string(name)] |
| 158 | + if ok { |
| 159 | + if (cur.Flags & defLocal) != 0 { |
| 160 | + // FIXME this should be a warning |
| 161 | + log.Printf("name '%s' is assigned to before nonlocal declaration", name) |
| 162 | + |
| 163 | + } |
| 164 | + if (cur.Flags & defUse) != 0 { |
| 165 | + // FIXME this should be a warning |
| 166 | + log.Printf("name '%s' is used prior to nonlocal declaration", name) |
| 167 | + } |
| 168 | + } |
156 | 169 | st.AddDef(name, defNonlocal) |
157 | 170 | } |
158 | 171 | case *ast.Global: |
159 | 172 | for _, name := range node.Names { |
| 173 | + cur, ok := st.Symbols[string(name)] |
| 174 | + if ok { |
| 175 | + if (cur.Flags & defLocal) != 0 { |
| 176 | + // FIXME this should be a warning |
| 177 | + log.Printf("name '%s' is assigned to before global declaration", name) |
| 178 | + |
| 179 | + } |
| 180 | + if (cur.Flags & defUse) != 0 { |
| 181 | + // FIXME this should be a warning |
| 182 | + log.Printf("name '%s' is used prior to global declaration", name) |
| 183 | + } |
| 184 | + } |
160 | 185 | st.AddDef(name, defGlobal) |
161 | 186 | } |
162 | 187 | case *ast.Name: |
@@ -449,7 +474,7 @@ func (st *SymTable) AnalyzeName(scopes Scopes, name string, flags DefUse, bound, |
449 | 474 | if bound == nil { |
450 | 475 | panic(py.ExceptionNewf(py.SyntaxError, "nonlocal declaration not allowed at module level")) |
451 | 476 | } |
452 | | - if bound.Contains(name) { |
| 477 | + if !bound.Contains(name) { |
453 | 478 | panic(py.ExceptionNewf(py.SyntaxError, "no binding for nonlocal '%s' found", name)) |
454 | 479 | } |
455 | 480 | scopes[name] = scopeFree |
|
0 commit comments