Skip to content

webdsl-native.stx

pdmosses/webdsl-statix/webdslstatix/trans/static-semantics/webdsl-native.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
module 

imports
  static-semantics/actions/functions

  static-semantics/types/built-ins

  static-semantics/webdsl-actions
  static-semantics/webdsl-entities
  static-semantics/webdsl-types
  static-semantics/webdsl

rules // subtype compatiblity

  typeCompatibleB(NATIVECLASS(_, s_sub), NATIVECLASS(_, s_super)) = inherits(s_sub, s_super).

  // two native classes with identical names are compatible due to redefinitions of built-in native classes
  typeCompatibleB(NATIVECLASS(nc, _), NATIVECLASS(nc, _)) = TRUE().

rules // declarations

  defOk(, NativeClass(c, NCAlias(alias), NCSuperNone(), elems)) :- {  }
    object(s) == BUILTINTYPE(_, s_super),
    nativeClassOk(s, c, alias, s_super, elems).

  defOk(, NativeClass(, NCAlias(alias), NCSuper(), elems)) :- {    }
    resolveType(s, super) == [(_, (super', superType))] | error $[Native class [super] is not defined] @c,
    s_super == scopeOfNativeClassSuper(superType),
    nativeClassOk(s, c, alias, s_super, elems),
    @super.ref := super'.

   : TYPE -> scope
  scopeOfNativeClassSuper(t) = new :- false | error $[Native classes cannot extend something of type [t]].
  scopeOfNativeClassSuper(NATIVECLASS(_, s)) = s.
  scopeOfNativeClassSuper(BUILTINTYPE(_, s)) = s.

   : scope * string * string * scope * list(NCElement)
  nativeClassOk(, c, , s_super, ) :- {  }
    new s_class, s_class -INHERIT-> s_super, s_class -DEF-> s,
    declareExtendScope(s, alias, s_class),
    extendScopes(resolveExtendScope(s, alias), s_class),
    declareType(s, alias, NATIVECLASS(alias, s_class)),
    declareVar(s, alias, STATICNATIVECLASS(alias, s_class)), // declare class name as global variable that refers to the static scope
    noCircularInheritance(s_class) | error $[Circular inhertitance detected] @c,
    declareNativeClassConstructors(s, s_class, alias, elems),
    nativeClassElementsOk(s, s_class, elems).

rules // native class extending

  defOk(, ExtendNativeClass(c, NCAlias(), elems)) :- {  class_scopes }
    resolveType(s, alias) == [(_, (_, NATIVECLASS(_, _)))] | error $[Native class [alias] is not defined],
    new s_extend_class, s_extend_class -DEF-> s,
    declareExtendScope(s, alias, s_extend_class), // declare entity_scope to be linked to entity_name
    extendScopes(resolveExtendScope(s, alias), s_extend_class),
    nativeClassElementsOk(s, s_extend_class, elems).

   : list((path * (string * TYPE))) -> BOOL
  nativeClassDeclaredB(_) = FALSE().
  nativeClassDeclaredB([(_, (_, NATIVECLASS(_, _)))]) = TRUE().

rules // native class elements

   maps nativeClassElementOk(*, *, list(*))
   : scope * scope * NCElement

  // constructors are declared in their own rule declareNativeClassConstructors
  nativeClassElementOk(_, _, NCConstructor(_)).
  nativeClassElementOk(_, _, NCConstructorFromStatic(_, _)).

  nativeClassElementOk(s, , NCProperty(, nt)) :- {}
    t == typeOfNativeType(s, nt),
    declareVar(s_class, x, t),
    noDuplicateVarDefsInSuper(s_class, x) | error $[Cannot override existing property [x]] @x.

  nativeClassElementOk(s, s_class, NCFunctionElement(function)) :-
    nativeClassFunctionOk(s, s_class, function).

  nativeClassElementOk(s, s_class, NCFunctionFromStaticElement(NCFunctionFromStatic(_, function))) :-
    nativeClassFunctionOk(s, s_class, function).

rules // native class functions

   : scope * scope * NCFunction
  nativeClassFunctionOk(, s_class, NCFunction(NCFunctionStatic(), f, , NCFunctionReturn(return))) :- { }
    argTypes   == typesOfNativeTypes(s, args),
    returnType == typeOfNativeType(s, return),
    declareStaticFunction(s_class, f, NATIVE_FUNCTION_ORIGIN(args), argTypes, returnType).

  nativeClassFunctionOk(, s_class, NCFunction(NCFunctionStaticNone(), f, , NCFunctionReturn(return))) :- { }
    argTypes   == typesOfNativeTypes(s, args),
    returnType == typeOfNativeType(s, return),
    declareFunction(s_class, f, NATIVE_FUNCTION_ORIGIN(args), argTypes, returnType).

rules // constructors

  declareNativeClassConstructors maps declareNativeClassConstructor(*, *, *, list(*))
   : scope * scope * string * NCElement
  declareNativeClassConstructor(_, _, _, _).
  declareNativeClassConstructor(, s_class, , NCConstructor()) :- {}
    argTypes == typesOfNativeTypes(s, args),
    declFunctionGlobal(s, name, NATIVE_FUNCTION_ORIGIN(args), argTypes, NATIVECLASS(name, s_class)).

  declareNativeClassConstructor(, s_class, , NCConstructorFromStatic(_, )) :- {}
    argTypes == typesOfNativeTypes(s, args),
    declFunctionGlobal(s, name, NATIVE_FUNCTION_ORIGIN(args), argTypes, NATIVECLASS(name, s_class)).

rules // typing of expressions

  typeOfCall(s, NATIVECLASS(_, s_class), f, args) = typeOfFunctionCallInternal(s, f, args, funSigs) :-
    funSigs == resolveEntityFunction(s_class, f).

  typeOfCall(s, STATICNATIVECLASS(_, s_class), f, args) = typeOfFunctionCallInternal(s, f, args, funSigs) :-
    funSigs == resolveStaticEntityFunction(s_class, f).

rules // typing of native types

   maps typeOfNativeType(*, list(*)) = list(*)
   : scope * NativeType -> TYPE
  typeOfNativeType(s, NativeSimpleType(t)) = typeOfSort(s, SimpleSort(t)).
  typeOfNativeType(s, NativeGenericType("List", [t])) =  LIST(typeOfNativeType(s, t)).
  typeOfNativeType(s, NativeGenericType("Set", [t])) = SET(typeOfNativeType(s, t)).
  typeOfNativeType(s, t@NativeGenericType(_, _)) = UNTYPED() :- false | error $[Only lists and sets with one type parameter are supported] @t.