Skip to content

built-ins.stx

pdmosses/webdsl-statix/webdslstatix/trans/static-semantics/types/built-ins.stx

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
231
232
233
234
235
236
237
238
module 

imports
  static-semantics/actions/functions

  static-semantics/webdsl-actions
  static-semantics/webdsl-built-ins
  static-semantics/webdsl-entities
  static-semantics/webdsl-native
  static-semantics/webdsl-types
  static-semantics/webdsl

rules

   : scope * string
  declareBuiltInType(, ) :- { }
    object(s) == BUILTINTYPE(_, s_object),
    new s_type, s_type -INHERIT-> s_object, s_type -DEF-> s,
    declareExtendScope(s, name, s_type), // declare s_type to be linked to type name
    extendScopes(resolveExtendScope(s, name), s_type),
    declareType(s, name, BUILTINTYPE(name, s_type)),
    declareVar(s, name, STATICBUILTINTYPE(name, s_type)). // declare class name as global variable that refers to the static scope

   : scope * string * string
  declareBuiltInSubType(, , ) :- { }
    builtInType(s, super) == BUILTINTYPE(super, s_super),
    new s_sub, s_sub -INHERIT-> s_super, s_sub -DEF-> s,
    noCircularInheritance(s_sub) | error $[Circular inheritance detected in built-in type [sub]],
    declareExtendScope(s, sub, s_sub), // declare s_type to be linked to type name
    extendScopes(resolveExtendScope(s, sub), s_sub),
    declareType(s, sub, BUILTINTYPE(sub, s_sub)),
    declareVar(s, sub, STATICBUILTINTYPE(sub, s_sub)).

   : scope * string -> TYPE
  builtInType(s, ) = t :- {}
    resolveType(s, name) == ts | error $[Built-in type [name] does not exist [ts]],
    ts == [(_, (_, t@BUILTINTYPE(name, _)))] | error $[Built-in type [name] does not exist [ts]].

rules

     : scope -> TYPE
  int(s)   = builtInType(s, "Int").
   : scope -> TYPE
  float(s) = builtInType(s, "Float").
    : scope -> TYPE
  long(s)  = builtInType(s, "Long").

        : scope -> TYPE
  string(s)       = builtInType(s, "String").
        : scope -> TYPE
  secret(s)       = builtInType(s, "Secret").
         : scope -> TYPE
  email(s)        = builtInType(s, "Email").
           : scope -> TYPE
  url(s)          = builtInType(s, "URL").
      : scope -> TYPE
  wikitext(s)     = builtInType(s, "WikiText").
          : scope -> TYPE
  text(s)         = builtInType(s, "Text").
         : scope -> TYPE
  patch(s)        = builtInType(s, "Patch").
   : scope -> TYPE
  placeholder(s)  = builtInType(s, "Placeholder").

       : scope -> TYPE
  date(s)     = builtInType(s, "Date").
   : scope -> TYPE
  datetime(s) = builtInType(s, "DateTime").
       : scope -> TYPE
  time(s)     = builtInType(s, "Time").

    : scope -> TYPE
  bool(s)  = builtInType(s, "Bool").
    : scope -> TYPE
  uuid(s)  = builtInType(s, "UUID").
    : scope -> TYPE
  file(s)  = builtInType(s, "File").
   : scope -> TYPE
  image(s) = builtInType(s, "Image").

   : scope -> TYPE
  entity(s) = definedTypeNoRef(s, "Entity").

   : scope -> TYPE
  object(s) = definedTypeNoRef(s, "Object").

   : scope
  declareTypeBuiltIns() :-
    // numeric types
    declareBuiltInType(s, "Int"),
    declareBuiltInType(s, "Float"),
    declareBuiltInType(s, "Long"),

    // string types
    declareBuiltInType(s, "String"),
    declareBuiltInSubType(s, "Secret", "String"),
    declareBuiltInSubType(s, "Email", "String"),
    declareBuiltInSubType(s, "URL", "String"),
    declareBuiltInSubType(s, "WikiText", "String"),
    declareBuiltInSubType(s, "Text", "String"),
    declareBuiltInSubType(s, "Patch", "String"),
    declareBuiltInSubType(s, "Placeholder", "String"),

    // date types
    declareBuiltInType(s, "DateTime"),
    declareBuiltInSubType(s, "Date", "DateTime"),
    declareBuiltInSubType(s, "Time", "DateTime"),

    // misc
    declareBuiltInType(s, "Bool"),
    declareBuiltInType(s, "UUID"),
    declareBuiltInType(s, "File"),
    declareBuiltInType(s, "Image").

   : scope
  declareTypeBuiltInFunctions() :- {      }
    int == int(s),
    string == string(s),
    float == float(s),

    new s_int, declareExtendScope(s, "Int", s_int),
    declBuiltInFunctionEntity(s_int, "floatValue", [], float, FALSE()),

    new s_file, declareExtendScope(s, "File", s_file),
    declBuiltInFunctionEntity(s_file, "download"           , []      , VOID(), FALSE()),
    declBuiltInFunctionEntity(s_file, "download"           , [string], VOID(), FALSE()),
    declBuiltInFunctionEntity(s_file, "fileName"           , []      , string, FALSE()),
    declBuiltInFunctionEntity(s_file, "fileNameForDownload", []      , string, FALSE()),

    declareExtendScope(s, "Image", s_file), // also expose file functions to image
    new s_image, declareExtendScope(s, "Image", s_image),
    declBuiltInFunctionEntity(s_image, "resize"   , [int, int]          , VOID()  , FALSE()),
    declBuiltInFunctionEntity(s_image, "crop"     , [int, int, int, int], VOID()  , FALSE()),
    declBuiltInFunctionEntity(s_image, "getWidth" , []                  , int     , FALSE()),
    declBuiltInFunctionEntity(s_image, "getHeight", []                  , int     , FALSE()),
    declBuiltInFunctionEntity(s_image, "clone"    , []                  , image(s), FALSE()),

    new s_float, declareExtendScope(s, "Float", s_float),
    declBuiltInFunctionEntity(s_float, "round" , [], int, FALSE()),
    declBuiltInFunctionEntity(s_float, "floor" , [], int, FALSE()),
    declBuiltInFunctionEntity(s_float, "ceil"  , [], int, FALSE()),
    declBuiltInFunctionEntity(s_float, "log"   , [], float, FALSE()),
    declBuiltInFunctionEntity(s_float, "log2"  , [], float, FALSE()).

rules // built-in functions and properties for built-in generic type List

  typeOfProperty(s, LIST(t), "length") = int(s).
  typeOfProperty(s, LIST(t), "first") = t.
  typeOfProperty(s, LIST(t), "last") = t.

  typeOfCall(s, LIST(t), "add", [arg]) = VOID() :-
    typeCompatible(typeOfExp(s, arg), t) | error $[No function 'add' with compatible argument types found].

  typeOfCall(s, LIST(t), "remove", [arg]) = VOID() :-
    typeCompatible(typeOfExp(s, arg), t) | error $[No function 'remove' with compatible argument types found].

  typeOfCall(s, LIST(_), "clear", []) = VOID().

  typeOfCall(s, rt@LIST(), "addAll", [arg]) = rt :- {}
    argType == typeOfExp(s, arg),
    or(
      typeCompatibleB(argType, LIST(t)),
      typeCompatibleB(argType, SET(t))
    ).

  typeOfCall(s, LIST(t), "set", []) = SET(t).

  typeOfCall(s, LIST(t), "indexOf", [arg]) = int(s) :-
    typeCompatible(typeOfExp(s, arg), t).

  typeOfCall(, LIST(t), "get", [arg]) = t :-
    typeCompatible(typeOfExp(s, arg), int(s)).

  typeOfCall(, LIST(t), "set", [arg1, arg2]) = VOID() :-
    typeCompatible(typeOfExp(s, arg1), int(s)),
    typeCompatible(typeOfExp(s, arg2), t).

  typeOfCall(, LIST(t), "insert", [arg1, arg2]) = VOID() :-
    typeCompatible(typeOfExp(s, arg1), int(s)),
    typeCompatible(typeOfExp(s, arg2), t).

  typeOfCall(, LIST(t), "removeAt", [arg]) = VOID() :-
    typeCompatible(typeOfExp(s, arg), int(s)).

  typeOfCall(, lt@LIST(_), "subList", [arg1, arg2]) = lt :-
    typeCompatible(typeOfExp(s, arg1), int(s)),
    typeCompatible(typeOfExp(s, arg2), int(s)).

  typeOfCall(s, LIST(t), "random", []) = t.

rules // built-in functions specifically for lists of strings

  typeOfCall(s, LIST(BUILTINTYPE("String", _)), "concat", []) = string(s).

  typeOfCall(, LIST(BUILTINTYPE("String", _)), "concat", []) =  :- {  }
    string == string(s),
    t == typeOfExp(s, arg),
    typeCompatible(t, string) | error $[Concatenation separator type must be compatible with String, [t] given] @arg.

rules // built-in functions and properties for built-in generic type Set

  typeOfProperty(s, SET(t), "length") = int(s).
  typeOfProperty(s, SET(t), "first") = t.
  typeOfProperty(s, SET(t), "last") = t.

  typeOfCall(, SET(t), "get", [arg]) = t :-
    typeCompatible(typeOfExp(s, arg), int(s)).

  typeOfCall(s, SET(t), "add", [arg]) = VOID() :-
    typeCompatible(typeOfExp(s, arg), t).

  typeOfCall(s, SET(t), "remove", [arg]) = VOID() :-
    typeCompatible(typeOfExp(s, arg), t).

  typeOfCall(s, SET(_), "clear", []) = VOID().

  typeOfCall(s, rt@SET(), "addAll", [arg]) = rt :- {}
    argType == typeOfExp(s, arg),
    or(
      typeCompatibleB(argType, LIST(t)),
      typeCompatibleB(argType, SET(t))
    ).

  typeOfCall(s, SET(t), "list", []) = LIST(t).

  typeOfCall(s, SET(t), "random", []) = t.

rules // built-in properties for built-in generic type Ref

  typeOfCall(s, REF(t), "getEntity", []) = entity(s).

  typeOfCall(s, REF(t), "getValidationErrors", []) = LIST(string(s)).

  typeOfCall(s, REF(LIST(t)), "getAllowed", []) = LIST(t).
  typeOfCall(s, REF(SET(t)) , "getAllowed", []) = LIST(t).
  typeOfCall(s, REF(t)      , "getAllowed", []) = LIST(t).

  typeOfCall(s, REF(t), "getReflectionProperty", []) = definedTypeIfExists(s, "ReflectionProperty").