Skip to content

Generate SyntaxError of global declaration #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions symtable/symtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package symtable

import (
"fmt"
"log"
"sort"
"strings"

Expand Down Expand Up @@ -229,13 +228,10 @@ func (st *SymTable) Parse(Ast ast.Ast) {
cur, ok := st.Symbols[string(name)]
if ok {
if (cur.Flags & DefLocal) != 0 {
// FIXME this should be a warning
log.Printf("name '%s' is assigned to before nonlocal declaration", name)

st.panicSyntaxErrorf(node, "name '%s' is assigned to before nonlocal declaration", name)
}
if (cur.Flags & DefUse) != 0 {
// FIXME this should be a warning
log.Printf("name '%s' is used prior to nonlocal declaration", name)
st.panicSyntaxErrorf(node, "name '%s' is used prior to nonlocal declaration", name)
}
}
st.AddDef(node, name, DefNonlocal)
Expand All @@ -245,13 +241,11 @@ func (st *SymTable) Parse(Ast ast.Ast) {
cur, ok := st.Symbols[string(name)]
if ok {
if (cur.Flags & DefLocal) != 0 {
// FIXME this should be a warning
log.Printf("name '%s' is assigned to before global declaration", name)
st.panicSyntaxErrorf(node, "name '%s' is assigned to before global declaration", name)

}
if (cur.Flags & DefUse) != 0 {
// FIXME this should be a warning
log.Printf("name '%s' is used prior to global declaration", name)
st.panicSyntaxErrorf(node, "name '%s' is used prior to global declaration", name)
}
}
st.AddDef(node, name, DefGlobal)
Expand Down
260 changes: 5 additions & 255 deletions symtable/symtable_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ var symtableTestData = []struct {
Children: Children{},
},
},
}, nil, ""},
}, nil,""},
{"def fn(a):\n global b\n global b\n return b", "exec", &SymTable{
Type: ModuleBlock,
Name: "top",
Expand Down Expand Up @@ -568,112 +568,8 @@ var symtableTestData = []struct {
},
},
}, nil, ""},
{"def inner():\n print(x)\n global x\n", "exec", &SymTable{
Type: ModuleBlock,
Name: "top",
Lineno: 0,
Unoptimized: optTopLevel,
Nested: false,
Free: false,
ChildFree: false,
Generator: false,
Varargs: false,
Varkeywords: false,
ReturnsValue: false,
NeedsClassClosure: false,
Varnames: []string{},
Symbols: Symbols{
"inner": Symbol{
Flags: DefLocal,
Scope: ScopeLocal,
},
"x": Symbol{
Flags: DefGlobal,
Scope: ScopeGlobalExplicit,
},
},
Children: Children{
&SymTable{
Type: FunctionBlock,
Name: "inner",
Lineno: 1,
Unoptimized: 0,
Nested: false,
Free: false,
ChildFree: false,
Generator: false,
Varargs: false,
Varkeywords: false,
ReturnsValue: false,
NeedsClassClosure: false,
Varnames: []string{},
Symbols: Symbols{
"print": Symbol{
Flags: DefUse,
Scope: ScopeGlobalImplicit,
},
"x": Symbol{
Flags: DefGlobal | DefUse,
Scope: ScopeGlobalExplicit,
},
},
Children: Children{},
},
},
}, nil, ""},
{"def fn(a):\n b = 6\n global b\n b = a", "exec", &SymTable{
Type: ModuleBlock,
Name: "top",
Lineno: 0,
Unoptimized: optTopLevel,
Nested: false,
Free: false,
ChildFree: false,
Generator: false,
Varargs: false,
Varkeywords: false,
ReturnsValue: false,
NeedsClassClosure: false,
Varnames: []string{},
Symbols: Symbols{
"b": Symbol{
Flags: DefGlobal,
Scope: ScopeGlobalExplicit,
},
"fn": Symbol{
Flags: DefLocal,
Scope: ScopeLocal,
},
},
Children: Children{
&SymTable{
Type: FunctionBlock,
Name: "fn",
Lineno: 1,
Unoptimized: 0,
Nested: false,
Free: false,
ChildFree: false,
Generator: false,
Varargs: false,
Varkeywords: false,
ReturnsValue: false,
NeedsClassClosure: false,
Varnames: []string{"a"},
Symbols: Symbols{
"a": Symbol{
Flags: DefParam | DefUse,
Scope: ScopeLocal,
},
"b": Symbol{
Flags: DefGlobal | DefLocal,
Scope: ScopeGlobalExplicit,
},
},
Children: Children{},
},
},
}, nil, ""},
{"def inner():\n print(x)\n global x\n", "exec", nil, py.SyntaxError, "name 'x' is used prior to global declaration"},
{"def fn(a):\n b = 6\n global b\n b = a", "exec", nil, py.SyntaxError, "name 'b' is assigned to before global declaration"},
{"def fn(a=b,c=1):\n return a+b", "exec", &SymTable{
Type: ModuleBlock,
Name: "top",
Expand Down Expand Up @@ -817,154 +713,8 @@ var symtableTestData = []struct {
}, nil, ""},
{"def fn(a):\n nonlocal b\n ", "exec", nil, py.SyntaxError, "no binding for nonlocal 'b' found"},
{"def outer():\n def inner():\n nonlocal x\n x = 2", "exec", nil, py.SyntaxError, "no binding for nonlocal 'x' found"},
{"def outer():\n x = 1\n def inner():\n print(x)\n nonlocal x\n", "exec", &SymTable{
Type: ModuleBlock,
Name: "top",
Lineno: 0,
Unoptimized: optTopLevel,
Nested: false,
Free: false,
ChildFree: true,
Generator: false,
Varargs: false,
Varkeywords: false,
ReturnsValue: false,
NeedsClassClosure: false,
Varnames: []string{},
Symbols: Symbols{
"outer": Symbol{
Flags: DefLocal,
Scope: ScopeLocal,
},
},
Children: Children{
&SymTable{
Type: FunctionBlock,
Name: "outer",
Lineno: 1,
Unoptimized: 0,
Nested: false,
Free: false,
ChildFree: true,
Generator: false,
Varargs: false,
Varkeywords: false,
ReturnsValue: false,
NeedsClassClosure: false,
Varnames: []string{},
Symbols: Symbols{
"inner": Symbol{
Flags: DefLocal,
Scope: ScopeLocal,
},
"x": Symbol{
Flags: DefLocal,
Scope: ScopeCell,
},
},
Children: Children{
&SymTable{
Type: FunctionBlock,
Name: "inner",
Lineno: 3,
Unoptimized: 0,
Nested: true,
Free: true,
ChildFree: false,
Generator: false,
Varargs: false,
Varkeywords: false,
ReturnsValue: false,
NeedsClassClosure: false,
Varnames: []string{},
Symbols: Symbols{
"print": Symbol{
Flags: DefUse,
Scope: ScopeGlobalImplicit,
},
"x": Symbol{
Flags: DefNonlocal | DefUse,
Scope: ScopeFree,
},
},
Children: Children{},
},
},
},
},
}, nil, ""},
{"def outer():\n x = 1\n def inner():\n x = 2\n nonlocal x", "exec", &SymTable{
Type: ModuleBlock,
Name: "top",
Lineno: 0,
Unoptimized: optTopLevel,
Nested: false,
Free: false,
ChildFree: true,
Generator: false,
Varargs: false,
Varkeywords: false,
ReturnsValue: false,
NeedsClassClosure: false,
Varnames: []string{},
Symbols: Symbols{
"outer": Symbol{
Flags: DefLocal,
Scope: ScopeLocal,
},
},
Children: Children{
&SymTable{
Type: FunctionBlock,
Name: "outer",
Lineno: 1,
Unoptimized: 0,
Nested: false,
Free: false,
ChildFree: true,
Generator: false,
Varargs: false,
Varkeywords: false,
ReturnsValue: false,
NeedsClassClosure: false,
Varnames: []string{},
Symbols: Symbols{
"inner": Symbol{
Flags: DefLocal,
Scope: ScopeLocal,
},
"x": Symbol{
Flags: DefLocal,
Scope: ScopeCell,
},
},
Children: Children{
&SymTable{
Type: FunctionBlock,
Name: "inner",
Lineno: 3,
Unoptimized: 0,
Nested: true,
Free: true,
ChildFree: false,
Generator: false,
Varargs: false,
Varkeywords: false,
ReturnsValue: false,
NeedsClassClosure: false,
Varnames: []string{},
Symbols: Symbols{
"x": Symbol{
Flags: DefLocal | DefNonlocal,
Scope: ScopeFree,
},
},
Children: Children{},
},
},
},
},
}, nil, ""},
{"def outer():\n x = 1\n def inner():\n print(x)\n nonlocal x\n", "exec", nil, py.SyntaxError, "name 'x' is used prior to nonlocal declaration"},
{"def outer():\n x = 1\n def inner():\n x = 2\n nonlocal x", "exec", nil, py.SyntaxError, "name 'x' is assigned to before nonlocal declaration"},
{"def outer():\n x = 1\n def inner(x):\n nonlocal x", "exec", nil, py.SyntaxError, "name 'x' is parameter and nonlocal"},
{"def outer():\n x = 1\n def inner(x):\n global x", "exec", nil, py.SyntaxError, "name 'x' is parameter and global"},
{"def outer():\n def inner():\n global x\n nonlocal x\n ", "exec", nil, py.SyntaxError, "name 'x' is nonlocal and global"},
Expand Down