Skip to content

Commit 72b4fba

Browse files
committed
more fixes for python3.3
1 parent 8737e6b commit 72b4fba

7 files changed

Lines changed: 43 additions & 26 deletions

parse.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,13 @@ def visit_If(self, node):
109109
+ self.visit_list(node.body,"'_newline_") + ",," + (self.visit_list(node.orelse,"'_newline_") if node.orelse else "'pass(.List{K})") + ")")
110110

111111
def visit_With(self, node):
112+
return "'with_:_(" + self.visit_list(node.items, "'_`,_", "`,") + ",," + self.visit_list(node.body, "'_newline_") + ")"
113+
114+
def visit_withitem(self, node):
112115
if node.optional_vars:
113-
return "'with_as_:_(" + self.visit(node.context_expr) + ",," + self.visit(node.optional_vars) + ",," + self.visit_list(node.body, "'_newline_") + ")"
116+
return "'_as_(" + self.visit(node.context_expr) + ",," + self.visit(node.optional_vars) + ")"
114117
else:
115-
return "'with_:_(" + self.visit(node.context_expr) + ",," + self.visit_list(node.body, "'_newline_") + ")"
118+
return self.visit(node.context_expr)
116119

117120
def visit_Raise(self, node):
118121
if not node.exc:

programs/testwith2.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
class A:
22
def __enter__(self):
3-
self.x = 0
3+
global x
4+
x = 0
45
def __exit__(self, type, value, traceback):
5-
assert self.x == 0
6-
self.x = 1
6+
global x
7+
assert x == 1
8+
x = 2
9+
10+
class B:
11+
def __enter__(self):
12+
global x
13+
def __exit__(self, type, value, traceback):
14+
global x
15+
assert x == 0
16+
x = 1
717
assert type is value.__class__ is RuntimeError
818
assert traceback.tb_next is None
919
return True
1020

11-
with A(): y = 5; raise
21+
with A(), B(): y = 5; raise
1222
assert y == 5

python-semantics-functions.k

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ module PYTHON-SEMANTICS-FUNCTIONS
233233
rule compileVisitor('_|=_,0,K) => visit(K, 'bindVisitor)
234234
235235
rule compileVisitor('for_in_:_,0,K) => visit(K, 'bindVisitor)
236-
rule compileVisitor('with_as_:_,1,K) => visit(K, 'bindVisitor)
236+
rule compileVisitor('with_:_,0,K) => visit(K, 'bindVisitor)
237237
rule compileVisitor('except_as_:_,1,K) => visit(K, 'bindVisitor)
238238
rule compileVisitor('del_,0,K) => visit(K, 'bindVisitor)
239239
@@ -247,11 +247,13 @@ module PYTHON-SEMANTICS-FUNCTIONS
247247
rule bindVisitor('_._, 1, K) => visited(K)
248248
rule bindVisitor('_`[_`], 0, K) => visit(K, 'compileVisitor)
249249
rule bindVisitor('_`[_`], 1, K) => visited(K)
250-
rule bindVisitor(Lbl, N, K) => visit(K, 'bindVisitor) when notBool(Lbl ==KLabel '_._ orBool Lbl ==KLabel '_`[_`]) andBool N =/=Int -1
250+
rule bindVisitor('_as_, 0, K) => visit(K, 'compileVisitor)
251+
rule bindVisitor('_as_, 1, K) => visited(K)
252+
rule bindVisitor(Lbl, N, K) => visit(K, 'bindVisitor) when notBool(Lbl ==KLabel '_as_ orBool Lbl ==KLabel '_._ orBool Lbl ==KLabel '_`[_`]) andBool N =/=Int -1
251253
rule bindVisitor(returner, -1, X:Id) => addDef(X) ~> visited(X)
252254
rule bindVisitor(returner, -1, K) => visited(K) when isId(K) =/=K true
253255
254-
rule compileVisitor(Lbl, N, K) => visit(K, 'compileVisitor) when notBool ((Lbl ==KLabel 'def_`(_`)->_:_ andBool N =/=Int 2) orElseBool (Lbl ==KLabel 'class_`(_`):_ andBool N =/=Int 1) orElseBool (N ==Int 0 andBool (Lbl ==KLabel '_:=_ orElseBool Lbl ==KLabel '_+=_ orElseBool Lbl ==KLabel '_-=_ orElseBool Lbl ==KLabel '_*=_ orElseBool Lbl ==KLabel '_/=_ orElseBool Lbl ==KLabel '_floor/=_ orElseBool Lbl ==KLabel '_%=_ orElseBool Lbl ==KLabel '_**=_ orElseBool Lbl ==KLabel '_>>=_ orElseBool Lbl ==KLabel '_<<=_ orElseBool Lbl ==KLabel '_&=_ orElseBool Lbl ==KLabel '_^=_ orElseBool Lbl ==KLabel '_|=_)) orElseBool (Lbl ==KLabel 'for_in_:_ andBool N ==Int 0) orElseBool (Lbl ==KLabel 'with_as_:_ andBool N ==Int 1) orElseBool (Lbl ==KLabel 'except_as_:_ andBool N ==Int 1) orElseBool Lbl ==KLabel 'del_ orElseBool Lbl ==KLabel 'global_ orElseBool Lbl ==KLabel 'nonlocal_ orElseBool (Lbl ==KLabel '_=_ andBool N ==Int 0) orElseBool Lbl ==KLabel 'import_ orElseBool Lbl ==KLabel 'from_import_ orElseBool Lbl ==KLabel 'def_`(_`):_ orElseBool Lbl ==KLabel 'lambda_:_) andBool N =/=Int -1
256+
rule compileVisitor(Lbl, N, K) => visit(K, 'compileVisitor) when notBool ((Lbl ==KLabel 'def_`(_`)->_:_ andBool N =/=Int 2) orElseBool (Lbl ==KLabel 'class_`(_`):_ andBool N =/=Int 1) orElseBool (N ==Int 0 andBool (Lbl ==KLabel '_:=_ orElseBool Lbl ==KLabel '_+=_ orElseBool Lbl ==KLabel '_-=_ orElseBool Lbl ==KLabel '_*=_ orElseBool Lbl ==KLabel '_/=_ orElseBool Lbl ==KLabel '_floor/=_ orElseBool Lbl ==KLabel '_%=_ orElseBool Lbl ==KLabel '_**=_ orElseBool Lbl ==KLabel '_>>=_ orElseBool Lbl ==KLabel '_<<=_ orElseBool Lbl ==KLabel '_&=_ orElseBool Lbl ==KLabel '_^=_ orElseBool Lbl ==KLabel '_|=_)) orElseBool (Lbl ==KLabel 'for_in_:_ andBool N ==Int 0) orElseBool (Lbl ==KLabel 'with_:_ andBool N ==Int 0) orElseBool (Lbl ==KLabel 'except_as_:_ andBool N ==Int 1) orElseBool Lbl ==KLabel 'del_ orElseBool Lbl ==KLabel 'global_ orElseBool Lbl ==KLabel 'nonlocal_ orElseBool (Lbl ==KLabel '_=_ andBool N ==Int 0) orElseBool Lbl ==KLabel 'import_ orElseBool Lbl ==KLabel 'from_import_ orElseBool Lbl ==KLabel 'def_`(_`):_ orElseBool Lbl ==KLabel 'lambda_:_) andBool N =/=Int -1
255257
256258
rule compileVisitor(returner, -1, K) => visited(K) when getKLabel K =/=KLabel 'def_`(_`)->_:_ andBool getKLabel K =/=KLabel 'def_`(_`):_ andBool getKLabel K =/=KLabel 'class_`(_`):_ andBool isId(K) =/=K true
257259
rule compileVisitor(returner, -1, .) => visited(.)

python-semantics-import.k

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ module PYTHON-SEMANTICS-IMPORT
3737

3838
// import statement
3939

40-
rule (. => moduleTarget(Module), .Expressions := doImport(Module, .ModuleAliases) ) ~> import (Module , Rest => Rest)
41-
rule import .ModuleAliases => .
40+
rule (. => moduleTarget(Module), .Expressions := doImport(Module, .Aliases) ) ~> import (Module , Rest => Rest)
41+
rule import .Aliases => .
4242

43-
rule from Module:RelativeModule import Fromlist:ModuleAliases => moduleTargets(Fromlist) := tuple(importFrom(doImport(Module, Fromlist), moduleNameIds(Fromlist)))
43+
rule from Module:RelativeModule import Fromlist:Aliases => moduleTargets(Fromlist) := tuple(importFrom(doImport(Module, Fromlist), moduleNameIds(Fromlist)))
4444

4545
syntax ObjRef ::= doImport(K, K)
46-
rule doImport(Module, Fromlist) => ref("builtins") . String2Id("__import__") (moduleName(Module), ref("globals")(.Arguments), ref("locals")(.Arguments), test(Fromlist ==K .ModuleAliases, ref("None"), tuple(moduleNames(Fromlist))), moduleLevel(Module), .Arguments)
46+
rule doImport(Module, Fromlist) => ref("builtins") . String2Id("__import__") (moduleName(Module), ref("globals")(.Arguments), ref("locals")(.Arguments), test(Fromlist ==K .Aliases, ref("None"), tuple(moduleNames(Fromlist))), moduleLevel(Module), .Arguments)
4747

4848
syntax NAME ::= moduleTarget(K) [function]
4949
rule moduleTarget(_ as Name) => Name
@@ -63,18 +63,18 @@ module PYTHON-SEMANTICS-IMPORT
6363
rule moduleLevel(_ as _) => 0
6464
rule moduleLevel(_:Expression) => 0
6565

66-
syntax Expressions ::= moduleNames(ModuleAliases) [function]
66+
syntax Expressions ::= moduleNames(Aliases) [function]
6767
rule moduleNames(Module, Rest) => moduleName(Module) , moduleNames(Rest)
68-
rule moduleNames(.ModuleAliases) => .Expressions
68+
rule moduleNames(.Aliases) => .Expressions
6969

70-
syntax Expressions ::= moduleTargets(ModuleAliases) [function]
70+
syntax Expressions ::= moduleTargets(Aliases) [function]
7171
rule moduleTargets(Module, Rest) => moduleName(Module) , moduleTargets(Rest)
72-
rule moduleTargets(.ModuleAliases) => .Expressions
72+
rule moduleTargets(.Aliases) => .Expressions
7373

74-
syntax Expressions ::= moduleNameIds(ModuleAliases) [function]
74+
syntax Expressions ::= moduleNameIds(Aliases) [function]
7575
rule moduleNameIds(Name:Id, Rest) => Name , moduleNameIds(Rest)
7676
rule moduleNameIds(Name:Id as _, Rest) => Name , moduleNameIds(Rest)
77-
rule moduleNameIds(.ModuleAliases) => .Expressions
77+
rule moduleNameIds(.Aliases) => .Expressions
7878

7979
syntax Expressions ::= importFrom (Expression, Expressions) [strict(1)]
8080
rule importFrom(O:Object, Name , Rest) => O . Name , importFrom(O, Rest)

python-semantics-macros.k

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ module PYTHON-SEMANTICS-MACROS
3131

3232
rule try: Try ExceptClauses else: Else finally: Finally => try: try: Try ExceptClauses else: Else finally: Finally [macro, anywhere]
3333

34+
rule with W, W2, W3 : Body => with W, .Aliases : with W2, W3: Body [macro, anywhere]
35+
3436
endmodule

python-semantics-try.k

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ module PYTHON-SEMANTICS-TRY
113113
rule <k> doExcept(Type:Object, Except, X) => test(istype(B, Type), test(Id2String(X) ==String "", except: Except, (X, .Expressions := B ~> except: (try: Except finally: unbind(X)))), .) ...</k>
114114
<xcontext> B </xcontext>
115115

116-
rule <k> invokeBuiltin(obj("sys_exc_info",_), ., .) => tuple(#if Ex ==K ref("None") #then ref("None") #else Ex . String2Id("__class__") #fi, Ex, Ex . String2Id("__traceback__"), .Expressions) ...</k>
116+
rule <k> invokeBuiltin(obj("sys_exc_info",_), ., .) => tuple(#if Ex ==K ref("None") #then ref("None") #else Ex . String2Id("__class__") #fi, Ex, #if Ex ==K ref("None")
117+
#then ref("None") #else Ex . String2Id("__traceback__") #fi, .Expressions) ...</k>
117118
<xcontext> Ex </xcontext>
118119

119120
endmodule

python-syntax.k

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ module PYTHON-SYNTAX imports PYTHON-TOKENS
108108
syntax Targets ::= Expressions
109109
syntax Target ::= Expression
110110

111-
syntax ModuleAliases ::= List{ModuleAlias,","}
112-
syntax ModuleAlias ::= Expression
111+
syntax Aliases ::= List{Alias,","}
112+
syntax Alias ::= Expression
113113
| Expression "as" NAME
114114
syntax RelativeModule ::= ".dir"
115115
| Expression
@@ -141,8 +141,8 @@ module PYTHON-SYNTAX imports PYTHON-TOKENS
141141
| "raise" Expression "from" Expression [seqstrict]
142142
| "break"
143143
| "continue"
144-
| "import" ModuleAliases
145-
| "from" RelativeModule "import" ModuleAliases
144+
| "import" Aliases
145+
| "from" RelativeModule "import" Aliases
146146
| "from" Expression "import" "*"
147147
| "global" NAMES
148148
| "nonlocal" NAMES
@@ -152,8 +152,7 @@ module PYTHON-SYNTAX imports PYTHON-TOKENS
152152
| "try" ":" Statements ExceptClauses "else" ":" Statements //"finally" ":" Statements
153153
| "try" ":" Statements ExceptClauses "else" ":" Statements "finally" ":" Statements
154154
| "try" ":" Statements "finally" ":" Statements
155-
| "with" Expression "as" Target ":" Statements [strict(1)]
156-
| "with" Expression ":" Statements [strict(1)]
155+
| "with" Aliases ":" Statements [strict(1)]
157156
| Decorated
158157

159158
syntax Statements ::= Statement

0 commit comments

Comments
 (0)