Skip to content

Commit 211a89b

Browse files
committed
add rough draft of setattr
1 parent 6c44358 commit 211a89b

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

python-semantics-attribute-ref.k

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
require "python-semantics-common.k"
1+
require "python-zsemantics-common.k"
22

33
module PYTHON-SEMANTICS-ATTRIBUTE-REF
44
imports PYTHON-SEMANTICS-COMMON
55

66
syntax ObjRef ::= "mapLookup" "(" Expression "," Expression ")" [strict]
7+
| "mapInsert" "(" Expression "," Expression "," Expression ")" [strict]
78
| "basesLookup" "(" Expression "," Expression ")" [strict]
89
| "basesLookup" "(" List "," Expression ")" [strict(2)]
910
| "descriptor" "(" Expression "," Expression "," Expression "," Test ")" [strict]
1011
| "checkData" "(" Expression "," Expression ")" [strict]
12+
| "checkDataSet" "(" Expression "," Expression "," Expression ")" [strict]
1113

1214
rule O . X:Id => try: (getmember(O, "__getattribute__", true, false, true) (Id2String(X))) except ref("AttributeError") : (getmember(O, "__getattr__", true, false, false) (Id2String(X)) -> raise) else: pass
1315

@@ -60,4 +62,28 @@ module PYTHON-SEMANTICS-ATTRIBUTE-REF
6062
rule <k> descriptor(O:Object, O2:Object, Type:Object, false) => (getmember(O, "__get__", false, false, false) (O, O2, O2, .Arguments)) -> O ...</k>
6163
<symbols>... "None" |-> None:Int ...</symbols> when id(O2) ==K None
6264

65+
rule setmember(O:Object, Name:Object, Value:Object) => checkDataSet(getmember(O, Name, false, false, false), Name, Value) -> mapInsert(O, Name, Value) -> raiseInternal("AttributeError", "object has no attribute '" +String strvalue(Name) +String "'")
66+
67+
rule checkDataSet(O:Object, Name, Value) => getmember(O, "__set__", false, false, false) (O, Name, Value, .Arguments) -> .Obj
68+
rule checkDataSet(.Obj, _, _) => .Obj
69+
70+
rule <k> mapInsert(obj(B,_), Name, Value) => ref(Dict) [Name] , .Expressions := Value ...</k>
71+
<object>
72+
<id> B </id>
73+
<oattrs>... "__dict__" |-> Dict ...</oattrs>
74+
</object>
75+
76+
rule <k> mapInsert(obj(B,_), _, _) => .Obj ...</k>
77+
<object>...
78+
<id> B </id>
79+
<oattrs> Env:Map </oattrs>
80+
...</object> when notBool("__dict__" in keys(Env))
81+
82+
rule <k> mapInsert(obj(B,_), _, _) => raiseInternal("TypeError", "can't set attributes of built-in/extension type") ...</k>
83+
<object>...
84+
<id> B </id>
85+
<oattrs>... "__dict__" |-> _ ...</oattrs>
86+
<oenv> _ </oenv>
87+
...</object>
88+
6389
endmodule

python-semantics-common.k

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module PYTHON-SEMANTICS-COMMON
5555
| "gettype" "(" Expression ")"
5656
| "getbases" "(" Expression ")"
5757
| "getmember" "(" Expression "," Expression "," Bool "," Bool "," Bool ")" [strict(1 2)]
58+
| "setmember" "(" Expression "," Expression ")" [strict]
5859
| "binaryOp" "(" Expression "," Expression "," String "," String "," K ")" [strict(1 2)]
5960
| "unaryOp" "(" Expression "," String "," K ")" [strict(1)]
6061
| "invoke" "(" Expression "," Map ")" [strict(1)]

python-semantics-objects.k

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,7 @@ module PYTHON-SEMANTICS-OBJECTS
4646

4747
rule invokeBuiltin(obj("mro_type",_), ListItem(Type), .) => mutable(getMro(id(Type), getref(Type, "__bases__")), ref("list"))
4848

49+
rule invokeBuiltin(obj("setattr_object",_), ListItem(O) ListItem(Name) ListItem(Value), .) => test(istype(O, ref("type")), raiseInternal("TypeError", "can't apply this __setattr__ to type object"), setmember(O, Name, Value))
50+
rule invokeBuiltin(obj("setattr_type",_), ListItem(Type) ListItem(Name) ListItem(Value), .) => setmember(Type, Name, Value)
51+
4952
endmodule

0 commit comments

Comments
 (0)