-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiments.kek
199 lines (148 loc) · 4.27 KB
/
experiments.kek
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
struct :: A {
a: Int
b: Float
c: String
}
# c struct
struct :: A {
a: Int
b: Float
c: String
}
struct :: B {
a: A # this is a block of memory with the size of A
b: *A # simple ref to A
b: ?*A # "nullable" ref to A -> really an option
c: !*A # this is a ref that is owned by the struct
# so if the struct gets freed the ref gets freed
}
OnTheStack :: A(1, 2.0, "hello")
OnTheHeap :: *A(1, 2.0, "hello")
# the scope in which sth is passed in is the owner
# you can pass an const once around
# local returns
# you can set a return type to local
# which means, that only the direct caller scope can edit
# the return value
# and can only pass const version to other functions
# if you say "local const", it cannot even edit it
# AND cannot pass it into other functions
# a receiving function can only set this to a local variable
foo :: fn() -> local User {
...
}
foo_const :: fn() -> local const User {
...
}
foo_const2 :: const fn() -> local User {
...
}
# local in a definition is a promise that this field does not leave the scope
# local in a return type is the requirement that the receiver scope
# does not pass it to another function
baz :: fn(const User){..}
bar :: fn() {
local user := foo()
user.name = "John"
user.age = 20
baz(user) # okay since it is const
local user :: foo_const()
user.name = "John" # error
baz(user) # error
myUContainer := List(User)[]
myUContainer.push(user) # error, since it is local
local myUContainer2 := List(User)[]
myUContainer2.push(user) # okay, since it is local
# which means you cannot set myUContainer2 to a field
# which is not local
# and you cannot pass it to a function that is not local
# and cou cannot return it
# you need to discard return values ...
_ = foo()
}
union_consumer :: fn(arg: Union(Int Float)){
}
union_consumer(Union(Int Float).fromInt(1))
union_consumer(->.fromInt(1))
union_consumer :: fn(arg: Int|Float){
if(number := arg|Int){
print(number)
}
elif(number := arg|Float){
print(number)
}
}
my_optionals :: fn(arg: ?Int){
if(number := arg?){
print(number)
}
}
my_optionals(1)
my_optionals(nil)
union_consumer(1)
union_consumer(1)
# todo: we want to use | for unions and ? for optionals
# since we need to build them into the language
# no matter what
? # option resolve operator + option make operator
# we then also don need to induce the type
# we can pass in a int where a union is needed and
# "expand" the code to the right type
# create a list type -> template
# just generates a list of type of code
# list and map is built in
# basically type generators
# we can have the basic types as templates
# the type is generated at compile time
# and get the name
# TemplateContainer
List(User)
Map(String,User)
#MemoryArena(User,InitSize)
Union(User,Error)
# will be called
Option(User)
TreeNode(Node)
Queue(Message)
{
Position :: struct{
x: Int
y: Int
}
MyGenericStruct :: struct(T1 T2)(Position){
a: T1
b: T2
c: Position
}
}
# comp time operator
@ruleset()
#Comp time scope
# executed at comp time with a tree-walk-interpreter
# can create a name space?
# a comp time scope acts on the parent scope
# the idea of comptime is to be a more strict syntax checked c preprocessor
# comptime if allows for compile time branching of code
# @ -> the compile time operator
# the next ast node will be interpreted by the tree walk interpreter
# at compile time
@{ # all code on this scope is comp time scope
ruleset("only_const", true)
@if WINDOWS {
out("compiling on windows")
}elif(UNIX){
out("compiling on unix")
}else{
out("compiling on unknown")
}
}
i :: MyGenericStruct(Int Float){
a=1
b=2.0
c=Position(1,2)
}
MyStruct :: struct{
}
List(User)()
Map(String,User)
Union()