Skip to content

Commit eb115a9

Browse files
HyeockJinKimcorona10
authored andcommitted
Generate SyntaxError of global declaration (#74)
* Generate SyntaxError of global declaration Generate SyntaxError instead of SyntaxWarning if global declaration is for priviously used variable Fixes #72 * Modify symtable test
1 parent 7bc4005 commit eb115a9

File tree

2 files changed

+9
-265
lines changed

2 files changed

+9
-265
lines changed

symtable/symtable.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package symtable
1717

1818
import (
1919
"fmt"
20-
"log"
2120
"sort"
2221
"strings"
2322

@@ -229,13 +228,10 @@ func (st *SymTable) Parse(Ast ast.Ast) {
229228
cur, ok := st.Symbols[string(name)]
230229
if ok {
231230
if (cur.Flags & DefLocal) != 0 {
232-
// FIXME this should be a warning
233-
log.Printf("name '%s' is assigned to before nonlocal declaration", name)
234-
231+
st.panicSyntaxErrorf(node, "name '%s' is assigned to before nonlocal declaration", name)
235232
}
236233
if (cur.Flags & DefUse) != 0 {
237-
// FIXME this should be a warning
238-
log.Printf("name '%s' is used prior to nonlocal declaration", name)
234+
st.panicSyntaxErrorf(node, "name '%s' is used prior to nonlocal declaration", name)
239235
}
240236
}
241237
st.AddDef(node, name, DefNonlocal)
@@ -245,13 +241,11 @@ func (st *SymTable) Parse(Ast ast.Ast) {
245241
cur, ok := st.Symbols[string(name)]
246242
if ok {
247243
if (cur.Flags & DefLocal) != 0 {
248-
// FIXME this should be a warning
249-
log.Printf("name '%s' is assigned to before global declaration", name)
244+
st.panicSyntaxErrorf(node, "name '%s' is assigned to before global declaration", name)
250245

251246
}
252247
if (cur.Flags & DefUse) != 0 {
253-
// FIXME this should be a warning
254-
log.Printf("name '%s' is used prior to global declaration", name)
248+
st.panicSyntaxErrorf(node, "name '%s' is used prior to global declaration", name)
255249
}
256250
}
257251
st.AddDef(node, name, DefGlobal)

symtable/symtable_data_test.go

Lines changed: 5 additions & 255 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ var symtableTestData = []struct {
514514
Children: Children{},
515515
},
516516
},
517-
}, nil, ""},
517+
}, nil,""},
518518
{"def fn(a):\n global b\n global b\n return b", "exec", &SymTable{
519519
Type: ModuleBlock,
520520
Name: "top",
@@ -568,112 +568,8 @@ var symtableTestData = []struct {
568568
},
569569
},
570570
}, nil, ""},
571-
{"def inner():\n print(x)\n global x\n", "exec", &SymTable{
572-
Type: ModuleBlock,
573-
Name: "top",
574-
Lineno: 0,
575-
Unoptimized: optTopLevel,
576-
Nested: false,
577-
Free: false,
578-
ChildFree: false,
579-
Generator: false,
580-
Varargs: false,
581-
Varkeywords: false,
582-
ReturnsValue: false,
583-
NeedsClassClosure: false,
584-
Varnames: []string{},
585-
Symbols: Symbols{
586-
"inner": Symbol{
587-
Flags: DefLocal,
588-
Scope: ScopeLocal,
589-
},
590-
"x": Symbol{
591-
Flags: DefGlobal,
592-
Scope: ScopeGlobalExplicit,
593-
},
594-
},
595-
Children: Children{
596-
&SymTable{
597-
Type: FunctionBlock,
598-
Name: "inner",
599-
Lineno: 1,
600-
Unoptimized: 0,
601-
Nested: false,
602-
Free: false,
603-
ChildFree: false,
604-
Generator: false,
605-
Varargs: false,
606-
Varkeywords: false,
607-
ReturnsValue: false,
608-
NeedsClassClosure: false,
609-
Varnames: []string{},
610-
Symbols: Symbols{
611-
"print": Symbol{
612-
Flags: DefUse,
613-
Scope: ScopeGlobalImplicit,
614-
},
615-
"x": Symbol{
616-
Flags: DefGlobal | DefUse,
617-
Scope: ScopeGlobalExplicit,
618-
},
619-
},
620-
Children: Children{},
621-
},
622-
},
623-
}, nil, ""},
624-
{"def fn(a):\n b = 6\n global b\n b = a", "exec", &SymTable{
625-
Type: ModuleBlock,
626-
Name: "top",
627-
Lineno: 0,
628-
Unoptimized: optTopLevel,
629-
Nested: false,
630-
Free: false,
631-
ChildFree: false,
632-
Generator: false,
633-
Varargs: false,
634-
Varkeywords: false,
635-
ReturnsValue: false,
636-
NeedsClassClosure: false,
637-
Varnames: []string{},
638-
Symbols: Symbols{
639-
"b": Symbol{
640-
Flags: DefGlobal,
641-
Scope: ScopeGlobalExplicit,
642-
},
643-
"fn": Symbol{
644-
Flags: DefLocal,
645-
Scope: ScopeLocal,
646-
},
647-
},
648-
Children: Children{
649-
&SymTable{
650-
Type: FunctionBlock,
651-
Name: "fn",
652-
Lineno: 1,
653-
Unoptimized: 0,
654-
Nested: false,
655-
Free: false,
656-
ChildFree: false,
657-
Generator: false,
658-
Varargs: false,
659-
Varkeywords: false,
660-
ReturnsValue: false,
661-
NeedsClassClosure: false,
662-
Varnames: []string{"a"},
663-
Symbols: Symbols{
664-
"a": Symbol{
665-
Flags: DefParam | DefUse,
666-
Scope: ScopeLocal,
667-
},
668-
"b": Symbol{
669-
Flags: DefGlobal | DefLocal,
670-
Scope: ScopeGlobalExplicit,
671-
},
672-
},
673-
Children: Children{},
674-
},
675-
},
676-
}, nil, ""},
571+
{"def inner():\n print(x)\n global x\n", "exec", nil, py.SyntaxError, "name 'x' is used prior to global declaration"},
572+
{"def fn(a):\n b = 6\n global b\n b = a", "exec", nil, py.SyntaxError, "name 'b' is assigned to before global declaration"},
677573
{"def fn(a=b,c=1):\n return a+b", "exec", &SymTable{
678574
Type: ModuleBlock,
679575
Name: "top",
@@ -817,154 +713,8 @@ var symtableTestData = []struct {
817713
}, nil, ""},
818714
{"def fn(a):\n nonlocal b\n ", "exec", nil, py.SyntaxError, "no binding for nonlocal 'b' found"},
819715
{"def outer():\n def inner():\n nonlocal x\n x = 2", "exec", nil, py.SyntaxError, "no binding for nonlocal 'x' found"},
820-
{"def outer():\n x = 1\n def inner():\n print(x)\n nonlocal x\n", "exec", &SymTable{
821-
Type: ModuleBlock,
822-
Name: "top",
823-
Lineno: 0,
824-
Unoptimized: optTopLevel,
825-
Nested: false,
826-
Free: false,
827-
ChildFree: true,
828-
Generator: false,
829-
Varargs: false,
830-
Varkeywords: false,
831-
ReturnsValue: false,
832-
NeedsClassClosure: false,
833-
Varnames: []string{},
834-
Symbols: Symbols{
835-
"outer": Symbol{
836-
Flags: DefLocal,
837-
Scope: ScopeLocal,
838-
},
839-
},
840-
Children: Children{
841-
&SymTable{
842-
Type: FunctionBlock,
843-
Name: "outer",
844-
Lineno: 1,
845-
Unoptimized: 0,
846-
Nested: false,
847-
Free: false,
848-
ChildFree: true,
849-
Generator: false,
850-
Varargs: false,
851-
Varkeywords: false,
852-
ReturnsValue: false,
853-
NeedsClassClosure: false,
854-
Varnames: []string{},
855-
Symbols: Symbols{
856-
"inner": Symbol{
857-
Flags: DefLocal,
858-
Scope: ScopeLocal,
859-
},
860-
"x": Symbol{
861-
Flags: DefLocal,
862-
Scope: ScopeCell,
863-
},
864-
},
865-
Children: Children{
866-
&SymTable{
867-
Type: FunctionBlock,
868-
Name: "inner",
869-
Lineno: 3,
870-
Unoptimized: 0,
871-
Nested: true,
872-
Free: true,
873-
ChildFree: false,
874-
Generator: false,
875-
Varargs: false,
876-
Varkeywords: false,
877-
ReturnsValue: false,
878-
NeedsClassClosure: false,
879-
Varnames: []string{},
880-
Symbols: Symbols{
881-
"print": Symbol{
882-
Flags: DefUse,
883-
Scope: ScopeGlobalImplicit,
884-
},
885-
"x": Symbol{
886-
Flags: DefNonlocal | DefUse,
887-
Scope: ScopeFree,
888-
},
889-
},
890-
Children: Children{},
891-
},
892-
},
893-
},
894-
},
895-
}, nil, ""},
896-
{"def outer():\n x = 1\n def inner():\n x = 2\n nonlocal x", "exec", &SymTable{
897-
Type: ModuleBlock,
898-
Name: "top",
899-
Lineno: 0,
900-
Unoptimized: optTopLevel,
901-
Nested: false,
902-
Free: false,
903-
ChildFree: true,
904-
Generator: false,
905-
Varargs: false,
906-
Varkeywords: false,
907-
ReturnsValue: false,
908-
NeedsClassClosure: false,
909-
Varnames: []string{},
910-
Symbols: Symbols{
911-
"outer": Symbol{
912-
Flags: DefLocal,
913-
Scope: ScopeLocal,
914-
},
915-
},
916-
Children: Children{
917-
&SymTable{
918-
Type: FunctionBlock,
919-
Name: "outer",
920-
Lineno: 1,
921-
Unoptimized: 0,
922-
Nested: false,
923-
Free: false,
924-
ChildFree: true,
925-
Generator: false,
926-
Varargs: false,
927-
Varkeywords: false,
928-
ReturnsValue: false,
929-
NeedsClassClosure: false,
930-
Varnames: []string{},
931-
Symbols: Symbols{
932-
"inner": Symbol{
933-
Flags: DefLocal,
934-
Scope: ScopeLocal,
935-
},
936-
"x": Symbol{
937-
Flags: DefLocal,
938-
Scope: ScopeCell,
939-
},
940-
},
941-
Children: Children{
942-
&SymTable{
943-
Type: FunctionBlock,
944-
Name: "inner",
945-
Lineno: 3,
946-
Unoptimized: 0,
947-
Nested: true,
948-
Free: true,
949-
ChildFree: false,
950-
Generator: false,
951-
Varargs: false,
952-
Varkeywords: false,
953-
ReturnsValue: false,
954-
NeedsClassClosure: false,
955-
Varnames: []string{},
956-
Symbols: Symbols{
957-
"x": Symbol{
958-
Flags: DefLocal | DefNonlocal,
959-
Scope: ScopeFree,
960-
},
961-
},
962-
Children: Children{},
963-
},
964-
},
965-
},
966-
},
967-
}, nil, ""},
716+
{"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"},
717+
{"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"},
968718
{"def outer():\n x = 1\n def inner(x):\n nonlocal x", "exec", nil, py.SyntaxError, "name 'x' is parameter and nonlocal"},
969719
{"def outer():\n x = 1\n def inner(x):\n global x", "exec", nil, py.SyntaxError, "name 'x' is parameter and global"},
970720
{"def outer():\n def inner():\n global x\n nonlocal x\n ", "exec", nil, py.SyntaxError, "name 'x' is nonlocal and global"},

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy