summaryrefslogtreecommitdiff
path: root/notes/syntax_types.txt
blob: 4316844fc7fb50c41170cf2408333082fc5d9648 (plain)
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


Type declaration, may occur at top level or inside functions:

    type NameOfType<T,U> = DefinitionOfType


Builtin elementary types
see syntax.txt

Enum types

    enum (
        E1=v1
        E2
        E3
    )
    enum (E1=v1,E2,E3 ...)

    values are optional. The first value defaults to 0, the following default to previous+1

    Should enums allow a none value also?

    enum (
        none=-1
        E1
        E2
    )

    Require the "none" value to come first or last?
    Should enums have a base type? (defaulting to e.g. int)

    enum byte (
        E1
        E2
    )

Disjoint/error types

    ref X or enum Y
    # allowed if enum Y has only small values ( -127..128 ?)
    # should "none" values be allowed?

    T or enum Z
    # allowed if T is unsigned (or a "neither signed/unsigned type") AND enum Z has no non-negative values
    # should "none" values be allowed?

    which syntax to use to check if it is an enum value (usually an error) or a reference/integer?
    (multiple alternatives possible... also, this may be used for optional types also!)
    alternative 1:
        rwref File f   # no var on variable -> works like "final" in Java
        if f = open("blabla") {     # i.e. = on a disjoint type returns true if it was assigned
        } else {
            # ... but how do we access the error code?
        }
    alternative 2:
        # overloaded switch syntax
        switch open("blabla") {
            case enum any as error_code {
            }
            case File f as file {
            }
        }
    alternative 3:
        rwref File f = open("blabla") else error_code {
            
        }
    alternative 4:
        try {
            rwref File f = open("blabla")
        } error {
            # ... but how do we access the error code AND source of error?
        }
    alternative 5:
        try {
            rwref File f = open("blabla") throw open error_code
        } catch open, read {  # multi catch can be supported
            # error is available in error_code
        }
    alternative 6:
        rwref File or enum Error f = open("blabla")
        if f is enum Error {
        
        }
        
    

Struct types

    struct (
        T1 i1
        T2 i2
        ...
    )
    struct (T1 i1, T2 i2)

Class struct types (tagged union)

    class struct (
        SUBCLASS1 (
            T1 i1
            T1 i2
            ...
        )
        SUBCLASS2 (
            T1 i1
            ...
        )
        ...
    )

Array type

    [LEN]T

    LEN may be a constant expression, or an immutable variable

Optional type

    QUALIFIER?

    Only applicable on refrences. Maybe on enums also?

Reference type

    ref
    mergedref
    ownref
    (takeown, alloc, etc?)
    
    Should we have rwref, woref etc as separate keywords?

Type reference

    enum I
    struct I
    QUALIFIER I
    
    Qualifiers are things like "ref" etc.
    If I is a type parameter, then it must follow the rules for generic types.

Generic type reference

    QUALIFIERS I<T1,T2...>

    Qualifiers are things like "ref" etc.
    Parametricized types cannot be stack-allocated or merged
    - Should we allow enums? E.g. I<enum T1>
    - Should we have syntax handling for function pointers? E.g. I<func T>
    -- This is needed because data-pointers and function pointers may have different size.
    -- Generic function types could happen, but is probably not that common.

Function types (if we should have function pointers?)
- Should pointers be implicit or explicit(and required)?

    func(T1 arg1...) returns T2
    func(T1 arg1...) noreturn

Any type (should we have this?)

    any

    Only meaningful in pointers (perhaps mainly as "addr any").
    Should not accept function pointers, unless written as
    follows, in which case it only allows function pointers:

    any func




Type qualifiers:

    var       - variable may be modified by us
    writeonly - we may write but not read from this variable
    aliased   - access in the same thread may occur
    threaded  - other threads may access this variable

    When should qualifiers affect nested references?

    Should we have separate keywords for reads and writes for aliased/threaded keywords?
    This is only needed for "var" data, otherwise it is obvious.
    And it should mainly be needed when passing a aliased/threaded variable to
    a function that has less access (either read-only or write-only)
    - Skip this distinction? And loose some performance, but hopefully not much.

Type states, definition:

    type [STATE1,STATE2...] T = private

    A type with type states will always be in exactly one of the given type states.

Type states, accepted type states for parameter/variable/struct member/etc:

    T [STATE1,STATE2...] t
    T [not STATE1,STATE2...] t

    # leaving a space inbetween (and enforcing it in the compiler)
    # gives a visual distinction between type states and arrays

Type states, required type states to access struct member:

    MEMBERTYPE STRUCTMEMBER when [STATE1,STATE2...]
    MEMBERTYPE STRUCTMEMBER when [not STATE1,STATE2...]

    If the struct type is not in an accepted type state,
    then the struct member may not be accessed.

    This can be used for:
    - uninitialized data
    - invalidated data
    - data that may only be accessed when a lock is held(?)
      (how to validate this? perhaps we can return a new type to access the locked data?)

    How to distinguish between uninitialized/invalidated data that needs initialization, and unlocked data which just needs to be locked?
    shared when [Unlocked] int i   # access always allowed, and exclusive access when locked
    int i locked when [Locked]     # access only allowed when locked. keeps state between typestate changes

Type states, change (expression)

    EXPR typestate STATE

    Can we restrict where this should be allowed?

Type states, change (function)

    alternative 1:
        func work(ref var A[closed->open] a) return xxx
    alternative 2:
        func work(ref var A a) return xxx
        typestate a [STATE1,STATE2...] -> [STATE3] when return == 1
        # or something like that...