Skip to content

Commit b3b3992

Browse files
committed
next, any, callable, class.__module__, str.__new__, bool.__str__, float.__new__, int.__float__
1 parent 6f6bf6e commit b3b3992

14 files changed

Lines changed: 82 additions & 7 deletions

programs/testany.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
assert any(()) is False
2+
assert any((1,)) is True, any((1,))
3+
assert any((0,)) is False
4+
assert any((0, 0, 0, 0)) is False
5+
assert any((0, 0, 0, 1)) is True
6+
7+
class A:
8+
x = 0
9+
def __bool__(self): A.x += 1; return True
10+
11+
assert any((A(), A(), A(), A()))
12+
assert A.x == 1

programs/testcallable.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
assert not callable(5)
2+
def a(): pass
3+
assert callable(a)
4+
class A:
5+
def __call__(self): pass
6+
7+
assert callable(A())

programs/testclasses4.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
exec("""
2+
class A: pass
3+
4+
assert A.__module__ == "builtins"
5+
""", {})

programs/testiter1.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def a():
2+
yield 5
3+
yield 6
4+
5+
x = iter(a())
6+
assert next(x) == 5
7+
assert next(x) == 6
8+
try:
9+
next(x)
10+
assert False
11+
except StopIteration: pass
12+
13+
assert next(x, 7) == 7

programs/teststrings10.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class A:
2+
def __str__(self): return "foo"
3+
4+
assert str(A()) == "foo"
5+
assert str() == ""
6+
assert str(True) == "True"
7+
assert str(False) == "False"

python-semantics-boolean-ops.k

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ module PYTHON-SEMANTICS-BOOLEAN-OPS
1919
rule checkBool(.Obj) => .Obj
2020
rule checkInt(O:Object) => test(intvalue(O) ==Int 0, ref("False"), ref("True"))
2121
rule checkInt(.Obj) => .Obj
22-
rule invokeBuiltin(obj("new_bool",_), ListItem(O), .) => newHelper(O, ref("bool"), .) ~> ref("False")
23-
rule invokeBuiltin(obj("new_bool",_), ListItem(O:Object) ListItem(O2:Object), .) => newHelper(O, ref("bool"), .) ~> bool(O2)
22+
rule invokeBuiltin(obj("bool.__new__",_), ListItem(O), .) => newHelper(O, ref("bool"), .) ~> ref("False")
23+
rule invokeBuiltin(obj("bool.__new__",_), ListItem(O:Object) ListItem(O2:Object), .) => newHelper(O, ref("bool"), .) ~> bool(O2)
24+
rule invokeBuiltin(obj("bool.__str__",_), ListItem(O:Object), .) => if O: "True" else: "False"
2425

2526
rule len(O) => checkLen(index(getmember(O, "__len__", true, false, false) (.Arguments)))
2627
rule checkLen(O:Object) => test(intvalue(O) >=Int 0, O, raiseInternal("ValueError", "__len__() should return >= 0"))

python-semantics-builtin-functions.k

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@ module PYTHON-SEMANTICS-BUILTIN-FUNCTIONS
66
imports PYTHON-VISITOR
77

88
syntax K ::= doAll(Exp) [strict]
9+
| doAny(Exp) [strict]
10+
11+
rule invokeBuiltin(obj("abs",_), ListItem(O:Object), .) => getmember(O, "__abs__", true, false, false)(.Arguments) -> raiseInternal("TypeError", "bad operand type for abs()")
912

1013
rule invokeBuiltin(obj("all",_), ListItem(O:Object), .) => try: doAll(ref("iter")(O)) except ref("StopIteration"): ref("True") else: pass
1114
rule doAll(Iter:Object) => if not getmember(Iter, "__next__", true, false, true)(.Arguments) : ref("False") else: doAll(Iter)
1215

16+
rule invokeBuiltin(obj("any",_), ListItem(O:Object), .) => try: doAny(ref("iter")(O)) except ref("StopIteration"): ref("False") else: pass
17+
rule doAny(Iter:Object) => if getmember(Iter, "__next__", true, false, true)(.Arguments) : ref("True") else: doAny(Iter)
18+
19+
rule invokeBuiltin(obj("callable",_), ListItem(O:Object), .) => bool(hasmember(O, "__call__", false))
20+
1321
rule invokeBuiltin(obj("super.__new__",_), ListItem(Cls) L:List, .) => newHelper(Cls, ref("super"), .) ~> mutable("__self__" |-> ref("None") "__self_class__" |-> ref("None") "__thisclass__" |-> ref("None"), Cls)
1422
rule <k> invokeBuiltin(obj("super.__init__",_), ListItem(O), .) => #if ArgCount >Int 0 #then if VarNames[0] in Locals: if "__class__" in Cells: test(getattr2(Cells["__class__"], "cell_contents") =/=Obj .Obj, test(istype(getref2(Cells["__class__"], "cell_contents"), ref("type")), ref("super.__init__")(O, getref2(Cells["__class__"], "cell_contents"), Locals[VarNames[0]]), raiseInternal("SystemError", "super(): __class__ is not a type")), raiseInternal("SystemError", "super(): empty __class__ cell")) else: raiseInternal("SystemError", "super(): __class__ cell not found") else: raiseInternal("SystemError", "super(): arg[0] deleted") #else raiseInternal("SystemError", "super(): no arguments") #fi ...</k>
1523
<frameObject> N </frameObject>

python-semantics-classes.k

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module PYTHON-SEMANTICS-CLASSES
1717

1818
rule invokeClass(Metaclass:Object, CO:Object, Bases:Object, M:Map) => executeClass(Metaclass, CO, Bases, ref("builtins"), ref("globals")(.Arguments), doCall(Metaclass . String2Id("__prepare__"), ListItem(CO . String2Id("co_name")) ListItem(Bases), M), M)
1919

20-
rule <k> executeClass(Metaclass:Object, CO:Object, Bases:Object, Builtins:Object, Globals:Object, Locals:Object, M:Map) => executeFrame(N, CO, ref(Back), Locals, Globals, Builtins, makeCells(CO . String2Id("co_freevars"), CO . String2Id("co_cellvars"), ref("tuple")(ref("map")(getref2(ref(Back), "f_cells") . String2Id("__getitem__"), CO . String2Id("co_freevars"))), N), .) ~> ((Locals["__module__"]) = ref("globals")(.Arguments)["__name__"]::Exp) ~> (if CO . String2Id("co_consts")[0] is not ref("None") : (Locals["__doc__"]) = CO . String2Id("co_consts")[0] else: pass) ~> restoreContext(LL, FL, Back) ~> setClass(doCall(Metaclass, ListItem(CO . String2Id("co_name")) ListItem(Bases) ListItem(Locals), M), CO, ref(N)) ...</k>
20+
rule <k> executeClass(Metaclass:Object, CO:Object, Bases:Object, Builtins:Object, Globals:Object, Locals:Object, M:Map) => executeFrame(N, CO, ref(Back), Locals, Globals, Builtins, makeCells(CO . String2Id("co_freevars"), CO . String2Id("co_cellvars"), ref("tuple")(ref("map")(getref2(ref(Back), "f_cells") . String2Id("__getitem__"), CO . String2Id("co_freevars"))), N), .) ~> ((Locals["__module__"]) = ref("globals")(.Arguments) . String2Id("get")("__name__", "builtins")) ~> (if CO . String2Id("co_consts")[0] is not ref("None") : (Locals["__doc__"]) = CO . String2Id("co_consts")[0] else: pass) ~> restoreContext(LL, FL, Back) ~> setClass(doCall(Metaclass, ListItem(CO . String2Id("co_name")) ListItem(Bases) ListItem(Locals), M), CO, ref(N)) ...</k>
2121
<nextLoc> N => N +Int 1 </nextLoc>
2222
<frameObject> Back:Int </frameObject>
2323
<fstack> FL => . </fstack>

python-semantics-common.k

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ module PYTHON-SEMANTICS-COMMON
204204
syntax Float ::= "floatvalue" "(" Exp ")" [function]
205205
rule floatvalue(obj(_,<oattrs>... "__value__" |-> F:Float ...</oattrs>)) => F
206206
207-
syntax String ::= "strvalue" "(" Object ")" [function]
207+
syntax String ::= "strvalue" "(" Exp ")" [function]
208208
rule strvalue(obj(_,<oattrs>... "__value__" |-> S:String ...</oattrs>)) => S
209209
210210
syntax List ::= "listvalue" "(" Object ")" [function]

python-semantics-dunder-builtin.k

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,12 @@ module PYTHON-SEMANTICS-DUNDER-BUILTIN
6565
object "ValueError"("type",ListItem("Exception"),.,.) ~>
6666
object "Warning"("type",ListItem("Exception"),.,.) ~>
6767
object "ZeroDivisionError"("type", ListItem("ArithmeticError"),.,.) ~>
68+
(def "abs"(1, 1, false) ;) ~>
6869
(def "all"(1, 1, false) ;) ~>
70+
(def "any"(1, 1, false) ;) ~>
6971
object "bool"("type",ListItem("int"),
70-
"__new__" |-> def "new_bool" (1, 2, false)
72+
"__new__" |-> def "bool.__new__" (1, 2, false)
73+
"__str__" |-> def "bool.__str__" (0, 0, true)
7174
,.) ~>
7275
object "bytearray"("type",ListItem("object"),
7376
"__new__" |-> def "bytearray.__new__"(1, -1, false)
@@ -87,6 +90,7 @@ module PYTHON-SEMANTICS-DUNDER-BUILTIN
8790
"__radd__" |-> def "radd_bytes"(1, 1, true)
8891
"__rmul__" |-> def "rmul_bytes" (1, 1, true)
8992
,.) ~>
93+
(def "callable"(1, 1, false) ;) ~>
9094
object "classmethod"("type", ListItem("object"),
9195
"__get__" |-> def "get_classmethod"(1, 2, true)
9296
"__init__" |-> def "init_classmethod"(1, 1, true)
@@ -121,6 +125,7 @@ module PYTHON-SEMANTICS-DUNDER-BUILTIN
121125
"__le__" |-> def "float.__le__" (1, 1, true)
122126
"__lt__" |-> def "float.__lt__" (1, 1, true)
123127
"__ne__" |-> def "float.__ne__" (1, 1, true)
128+
"__new__" |-> def "float.__new__" (1, 2, false)
124129
,.) ~>
125130
(def "format" (1, 2, false) ;) ~>
126131
(def "getattr" (2, 3, false) ;) ~>
@@ -137,7 +142,7 @@ module PYTHON-SEMANTICS-DUNDER-BUILTIN
137142
"__ceil__" |-> def "ceil_int" (0, 0, true)
138143
//TODO: divmod, doc
139144
"__eq__" |-> def "eq_int"(1, 1, true)
140-
//TODO: float
145+
"__float__" |-> def "int.__float__"(0, 0, true)
141146
"__floor__" |-> def "floor_int" (0, 0, true)
142147
"__floordiv__" |-> def "floordiv_int" (1, 1, true)
143148
//TODO: format
@@ -218,6 +223,7 @@ module PYTHON-SEMANTICS-DUNDER-BUILTIN
218223
"__setattr__" |-> def "module.__setattr__" (2, 2, true)
219224
,.) ~>
220225
object "module.__dict__"("attribute",.,., "__get__" |-> "__dict__") ~>
226+
(def "next" (1, 2, false) ;) ~>
221227
object "object"("type",.,
222228
"__class__" |-> ref("__class__")
223229
"__getattribute__" |-> def "getattribute_object" (1, 1, true)
@@ -285,6 +291,7 @@ module PYTHON-SEMANTICS-DUNDER-BUILTIN
285291
"__mod__" |-> def "mod_str"(1, 1, true)
286292
"__mul__" |-> def "mul_str"(1, 1, true)
287293
"__ne__" |-> def "ne_str" (1, 1, true)
294+
"__new__" |-> def "str.__new__" (1, 4, false)
288295
"__repr__" |-> def "str.__repr__" (0, 0, true)
289296
,.) ~>
290297
object "super"("type",ListItem("object"),
@@ -606,9 +613,11 @@ module PYTHON-SEMANTICS-DUNDER-BUILTIN
606613
, "__debug__" : #if Constants("PYTHONOPTIMIZE") ==Int 0 #then ref("True") #else ref("False") #fi
607614
, "__doc__" : "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices."
608615
, "all" : ref("all")
616+
, "any" : ref("any")
609617
, "bool" : ref("bool")
610618
, "bytearray" : ref("bytearray")
611619
, "bytes" : ref("bytes")
620+
, "callable" : ref("callable")
612621
, "classmethod" : ref("classmethod")
613622
, "compile" : ref("compile")
614623
, "complex" : ref("complex")
@@ -630,6 +639,7 @@ module PYTHON-SEMANTICS-DUNDER-BUILTIN
630639
, "list" : ref("list")
631640
, "locals" : ref("locals")
632641
, "map" : ref("map")
642+
, "next" : ref("next")
633643
, "object" : ref("object")
634644
, "ord" : ref("ord")
635645
, "range" : ref("range")

0 commit comments

Comments
 (0)