-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkpython-semantics-class.k
More file actions
108 lines (84 loc) · 4.27 KB
/
Copy pathkpython-semantics-class.k
File metadata and controls
108 lines (84 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require "kpython-semantics-assignment.k"
module KPYTHON-SEMANTICS-CLASS
imports KPYTHON-SEMANTICS-ASSIGNMENT
rule class C:Id S => class C (Object) S
syntax Val ::= objectClosure(Id, List)
syntax KItem ::= fstackFrame(Map,K,List,K)
rule <k> class Class1:Id (Class2:Id) { S } => declareStmts(Class1, S); ...</k>
<classes>... (.Bag => <classData>
<className> Class1 </className>
<baseClass> Class2 </baseClass>
<declarations> S </declarations>
</classData>)
...</classes>
<storeClass> ... .Map => Class1 |-> Class2 ... </storeClass>
<classStaticVars>... (.Bag => <classStaticData>
<classStaticName> Class1 </classStaticName>
<classStaticDeclaration> .Map </classStaticDeclaration>
</classStaticData>)
...</classStaticVars>
[structural]
syntax Stmt ::= "declareStmts" "(" Id "," Stmts ")" ";"
| "declareStmt" "(" Id "," Stmt ")" ";"
rule declareStmts(Class, S:Stmt); => declareStmt(Class, S); [structural]
rule declareStmts(Class, S:Stmt Es:Stmts); => declareStmt(Class, S);declareStmts(Class, Es); [structural]
rule <k> declareStmt(Class, def F:Id(Xs:Ids) S); => . ... </k>
<classStaticName> Class </classStaticName>
<classStaticDeclaration> Env => Env[F <- "function"] </classStaticDeclaration>
rule <k> declareStmt(Class, X:Id = V:Val ; ) ; => . ... </k>
<classStaticName> Class </classStaticName>
<classStaticDeclaration> Env => Env[X <- V] </classStaticDeclaration>
rule <k> Class:Id(Vs:Vals) ~> K => create(Class) ~> storeObj ~> __init__(Vs); return self; </k>
<env> Env => .Map </env>
<nextLoc> L:Int => L +Int 1 </nextLoc>
<control> <xstack> XS </xstack>
<crntObj> Obj
=> <crntClass> Object </crntClass>
<envStack> ListItem(envStackFrame(Object, .Map)) </envStack>
<location> L </location>
</crntObj>
<fstack> .List => ListItem(fstackFrame(Env, K, XS, <crntObj> Obj </crntObj>)) ...</fstack>
</control>
<storeClass> StoreClass </storeClass>
when Class in keys(StoreClass)
syntax KItem ::= create(Id)
rule <k> create(Class:Id)
=> create(Class1) ~> setCrntClass(Class) ~> S ~> addInit ~> addEnvLayer ...</k>
<className> Class </className>
<baseClass> Class1:Id </baseClass>
<declarations> S </declarations> [structural]
rule <k> create(Object) => . ...</k> [structural]
syntax KItem ::= setCrntClass(Id)
rule <k> setCrntClass(C) => . ...</k>
<crntClass> _ => C </crntClass> [structural]
syntax KItem ::= "addInit"
rule <k> addInit => def __init__(.Ids) {} ...</k>
<env> Env </env>
when notBool(__init__ in keys(Env))
rule <k> addInit => . ...</k>
<env>... __init__|-> _ ...</env>
syntax KItem ::= "addEnvLayer"
rule <k> addEnvLayer => . ...</k>
<env> Env => .Map </env>
<crntClass> Class:Id </crntClass>
<envStack> .List => ListItem(envStackFrame(Class, Env)) ...</envStack>
[structural]
syntax KItem ::= "storeObj"
rule <k> storeObj => . ...</k>
<crntObj> <crntClass> CC </crntClass> <envStack> ES </envStack> (<location> L:Int </location> => .Bag) </crntObj>
<store>... .Map => L |-> objectClosure(CC, ES) ...</store>
// isinstance
rule isinstance(objectClosure(_, ListItem(envStackFrame(C,_)) _),C) => true
rule isinstance(objectClosure(_, (ListItem(envStackFrame(C,_)) => .List) _),C') when C =/=K C' [structural]
rule isinstance(objectClosure(_, .List),_) => false
// static variables
syntax KItem ::= "lookupd" "(" Id "," Id ")"
syntax KItem ::= "lookupenv" "(" Id "," Map ")"
rule <k> C:Id . Y:Id => lookupd(C, Y) ... </k>
<storeClass> StoreClass </storeClass>
when C in keys(StoreClass) [structural]
rule <k> lookupd(C:Id, Y:Id) => lookupenv(Y, Env) ... </k>
<classStaticName> C </classStaticName>
<classStaticDeclaration> Env </classStaticDeclaration>
rule lookupenv(Y:Id, Y|->V:Val _) => V
endmodule