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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
module
imports
static-semantics/actions/functions
static-semantics/entities/annotations
static-semantics/entities/built-ins
static-semantics/entities/generated-functions
static-semantics/types/built-ins
static-semantics/webdsl-actions
static-semantics/webdsl-types
static-semantics/webdsl
rules // unimplemented
stmtOk(_, _, ForeachStatementEntityDerive(_, _), _) :- try { false } | warning $[This statement is not yet implemented].
rules // entity declaration
// entity x : super { ... }
defOk(, e@Entity(, , bodydecs)) :- { }
resolveType(s, super) == [(_, (super', superType@ENTITY(_, s_super)))] | error $[Entity [super] is not defined] @super,
new s_entity, s_entity -INHERIT-> s_super, s_entity -DEF-> s,
noCircularInheritance(s_entity) | error $[Circular inhertitance detected] @e,
declEntity(s, s_entity, x, bodydecs),
declareVar(s, x, STATICENTITY(x, s_entity)), // declare entity_name as global variable that refers to the static scope
declSuperVar(s_entity, superType),
declareEntityAnnotation(s_entity, SUBENTITY()),
@super.ref := super'.
// entity x { ... }
defOk(, EntityNoSuper(, bodydecs)) :- { }
entity(s) == ENTITY(_, s_super),
new s_entity, s_entity -INHERIT-> s_super, s_entity -DEF-> s,
declEntity(s, s_entity, x, bodydecs),
declareVar(s, x, STATICENTITY(x, s_entity)). // declare entity_name as global variable that refers to the static scope
// session x { ... }
defOk(, SessionEntity(, bodydecs)) :- {}
new s_entity, s_entity -DEF-> s,
declEntity(s, s_entity, x, bodydecs),
declareVar(s, x, ENTITY(x, s_entity)), // session entities only have one instance and don't have a static scope
declareEntityAnnotation(s_entity, SESSIONENTITY()).
: scope * scope * string * list(EntityBodyDeclaration)
declEntity(, , , bodydecs) :- { }
declareExtendScope(s, entity_name, s_entity), // declare entity_scope to be linked to entity_name
extendScopes(resolveExtendScope(s, entity_name), s_entity),
entityType == ENTITY(entity_name, s_entity),
declareType(s, entity_name, entityType),
declThisVar(s_entity, entityType),
declEntityBody(s_entity, entity_name, bodydecs).
maps extendScope(list(*), *)
: (path * (string * scope)) * scope
extendScope((_, (_, s)), s). // Do not inherit own scope
extendScope((_, (_, s')), s) :-
s -EXTEND-> s'.
// extend entity x { ... }
defOk(s_global, ExtendEntity(x, bodydecs)) :- declExtendEntity(s_global, x, bodydecs).
// extend session { ... }
defOk(s_global, ExtendSessionEntity(x, bodydecs)) :- declExtendEntity(s_global, x, bodydecs).
: scope * string * list(EntityBodyDeclaration)
declExtendEntity(, , bodydecs) :- { entity_scopes}
resolveType(s, entity_name) == [(_, (_, ENTITY(_, _)))] | error $[Entity [entity_name] is not defined],
new s_extend_entity, s_extend_entity -DEF-> s,
declareExtendScope(s, entity_name, s_extend_entity), // declare entity_scope to be linked to entity_name
extendScopes(resolveExtendScope(s, entity_name), s_extend_entity),
declEntityBody(s_extend_entity, entity_name, bodydecs).
: scope
noCircularInheritance() :- {}
query () filter INHERIT+ and { s :- s == s_ent }
min $ < INHERIT
in s_ent |-> res,
res == [].
: scope * TYPE
declThisVar(, t) :-
declareVar(s_ent, "this", t),
declareAnnotation(s_ent, "this", DERIVED()). // prevent mutability of this var
: scope * TYPE
declSuperVar(, t) :-
declareVar(s_ent, "super", t),
declareAnnotation(s_ent, "super", DERIVED()). // prevent mutability of super var
defOk(, Enum(, values)) :- { }
entity(s) == ENTITY(_, s_super),
new s_entity, s_entity -INHERIT-> s_super, s_entity -DEF-> s,
declareVar(s_entity, "name", string(s)),
t == ENTITY(e, s_entity),
declareType(s, e, t),
declareEnumValues(s, values, t).
declareEnumValues maps declareEnumValue(*, list(*), *)
: scope * EnumValue * TYPE
declareEnumValue(s, EnumValue(v, _), t) :- declareVar(s, v, t).
rules // entity body declaration
: scope * string * EntityBodyDeclaration
declEntityBodyDeclaration(_, _, _) :- try { false } | warning $[This entity body declaration is not yet implemented].
maps declEntityBodyDeclaration(*, *, list(*))
// entity function
declEntityBodyDeclaration(s, _, EntityFunction(f)) :- entityFunctionOk(s, f, FALSE()).
declEntityBodyDeclaration(s, _, StaticEntityFunction(f)) :- entityFunctionOk(s, f, TRUE()).
declEntityBodyDeclaration(, _, CachedFunction()) :-
canBeCached(s, f),
entityFunctionOk(s, f, FALSE()).
// entity property
declEntityBodyDeclaration(, ent, Property(, propkind, , PropAnnos(annos))) :- {}
sortType == typeOfSort(s, sort),
inequalType(sortType, UNTYPED()) | error $[Cannot resolve type [sort]] @sort,
inequalType(sortType, VOID()) | error $[Property type 'Void' not allowed] @sort,
inequalType(sortType, REF(_)) | error $[Reference type is not allowed in property] @sort,
isValidTypeForPropKind(propkind, sort, sortType),
declProperty(s, ent, x, sortType),
annotationsOk(s, x, sortType, annos).
// derived entity property
declEntityBodyDeclaration(, ent, DerivedProperty(, propkind, sort, PropAnnos(annos), )) :- { }
sortType == typeOfSort(s, sort),
sortTypeString == ppType(sortType),
expType == typeOfExp(s, exp),
typeCompatible(expType, sortType) | error $[The expression of the derived property [x] should have type [sortTypeString]] @exp,
declProperty(s, ent, x, sortType),
declareAnnotation(s, x, DERIVED()),
annotationsOk(s, x, sortType, annos).
declProperties maps declProperty(*, *, list(*), list(*))
: scope * string * string * TYPE
declProperty(, ent, , sortType) :-
validPropertyName(x),
declareVar(s, x, sortType),
resolveLocalProperty(s, x) == [_] | error $[Property [x] of entity [ent] is defined multiple times] @x,
noDuplicateVarDefsInSuper(s, x) | error $[Cannot override existing entity property [x]] @x.
: string
validPropertyName() :-
x != "class" | error $[Cannot use the reserved property name [x]] @x.
: PropKind * Sort * TYPE
isValidTypeForPropKind(Simple(), sort, sortType) :-
isBuiltInType(sortType) | error $[Expected simple type] @sort.
isValidTypeForPropKind(Ref(), _, ENTITY(_, _)).
isValidTypeForPropKind(Ref(), _, LIST(ENTITY(_, _))).
isValidTypeForPropKind(Ref(), _, SET(ENTITY(_, _))).
isValidTypeForPropKind(Ref(), sort, _) :-
false | error $[Expected: Reference type or Composite type] @sort. // correct error message for tests
isValidTypeForPropKind(Comp(), _, ENTITY(_, _)).
isValidTypeForPropKind(Comp(), _, LIST(ENTITY(_, _))).
isValidTypeForPropKind(Comp(), _, SET(ENTITY(_, _))).
isValidTypeForPropKind(Comp(), sort, _) :-
false | error $[Expected: Reference type or Composite type] @sort. // correct error message for tests
isValidTypeForPropKind(AnyProp(), _, ENTITY(_, _)).
isValidTypeForPropKind(AnyProp(), _, LIST(ENTITY(_, _))).
isValidTypeForPropKind(AnyProp(), _, SET(ENTITY(_, _))).
isValidTypeForPropKind(AnyProp(), sort, sortType) :-
isBuiltInType(sortType) | error $[Expected simple type or entity type or a collection of an entity type] @sort.
// validation
declEntityBodyDeclaration(s, _, ValidateEntity(validateExp, messageExp)) :- validateOk(s, validateExp, messageExp).
rules // entity resolving
typeOfExp(, exp@ObjectCreation(x, prop_assignments)) = :-
definedType(s, x) == e,
e == ENTITY(_, _),
propAssignmentsOk(s, e, prop_assignments),
@exp.type := e.
: scope -> TYPE
typeOfThis(s_ent) = t :-
resolveVar(s_ent, "this") == [(_, (_, t))] | error $[Variable "this" not defined].
typeOfExp(s, This(_)) = typeOfThis(s).
typeOfProperty(_, _, "this") = UNTYPED() :-
false | error $[This is only accessible from within the entity].
// object creation properties
propAssignmentsOk maps propAssignmentOk(*, *, list(*))
: scope * TYPE * PropAssignment
propAssignmentOk(_, _, _) :- try { false } | warning $[This property assignment is not yet implemented].
propAssignmentOk(, ent@ENTITY(e, s_ent), PropAssignment(ObjectPropertyAssignment(, exp))) :- { }
typeOfProperty(s, ent, x) == propType | error $[Entity [e] does not contain a property [x]],
isMutable(s_ent, x) | error $[Assignment to derived property is not allowed.], // correct error message for tests
typeOfExp(s, exp) == expType,
typeCompatible(expType, propType) | error $[Type of expression [expType] is incompatible with type of entity property [propType]].
rules // subtyping
typeCompatibleB(ENTITY(_, s_sub), ENTITY(_, s_super)) = inherits(s_sub, s_super).
: scope * scope -> BOOL
inherits(s_sub, s_super) = nonEmptyPathScopeList(ps) :-
query () filter INHERIT*
and { s :- s == s_super }
min $ < INHERIT
in s_sub |-> ps.
: list((path * scope)) -> BOOL
nonEmptyPathScopeList(_) = FALSE().
nonEmptyPathScopeList([(_,_)]) = TRUE().
: scope * scope -> int
inheritEdgesAmount(s_sub, s_super) = pathLength(p) :-
query () filter INHERIT* and { s :- s == s_super }
min $ < INHERIT
in s_sub |-> [(p, _)].
rules // extension
extends : scope * scope -> BOOL
extends(s_extending, s) = nonEmptyPathScopeList(ps) :-
query () filter EXTEND? (INHERIT EXTEND?)*
and { s' :- s' == s }
min $ < EXTEND, $ < INHERIT, EXTEND < INHERIT
in s_extending |-> ps.
|