-
Notifications
You must be signed in to change notification settings - Fork 76
/
can_comp.pe
334 lines (332 loc) · 8.74 KB
/
can_comp.pe
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#Note:-This file is ment for tests so it is a bit verbose and hard to read
"Multiline comment
Hello"
"Another Multiline comment
Hello"
def peregrine1()->int,int:
static def peregrine2()->int,int:
return 2,2
return 1,1
#error()
type a_type=def()->int,int
class decorate:
def __init__(self):...
def dec(self,x:a_type)->a_type:
static a:int=9
return x
ooo:decorate=decorate()
ooo_ptr:*decorate=&ooo
@ooo_ptr->dec()
@ooo_ptr->dec
@ooo.dec()
@ooo.dec
def some_random_name()->int,int:
return 4,4
def dec(x:a_type,i:int=5)->a_type:return x
@dec(8)
def peregrine3()->int,int:
def peregrine2()->int,int:
return 3,3
return 4,4
"class x.x
class x.x:
xx:f128
union x.y
union x.y:
xx:int
def x.b(int,...)->int"
def unknown_type(var):
var.__add__(1)
for x in var:
printf("Item is %lld\n",x)
for x in var:
printf("Item is %lld\n",x)
with var as y:
printf("Y is %lld\n",y)
with var:
...
with var as y,var as x:
printf("Y is %lld\n",y)
printf("X is %lld\n",x)
printf("5 in var = %lld\n",5 in var)
printf("55 in var = %lld\n",55 in var)
printf("5 not in var = %lld\n",5 not in var)
printf("55 not in var = %lld\n",55 not in var)
printf("var[6] = %lld\n",var[6])
printf("var.__getitem__(7,8) = %lld\n",var.__getitem__(7,index2=8))
def peregrine4():printf("Global function\n")
self:int=7
class inherit:
def __init__(self):...
virtual def __type__(self):printf("\e[1m\e[91mInherit\n\e[0m\e[0m")
class iterate_test(inherit):
i:int=0
x:int
i_ref:&int=i
class nested_class:
def __init__(self):...
enum nested_enum:x,y,z
enum nested_enum2:x,y,z=nested_enum.z
xyz:nested_enum=nested_enum.x
union nested_union:
x:int
y:int
def __init__(self):...
def __type__(self):printf("\e[1m\e[91mIterate_test\n\e[0m\e[0m")
def __init__(self,c):
self.i=c
self.x=self.i
printf("i_ref is %lld\n",self.i_ref)
def peregrine4(self)->int,int:
printf("Local func\n")
return 6,6
def __add__(self,item)->int:
peregrine4()
self.peregrine4()
return 0
def __neg__(self)->int:
return 0
def __iter__(self)->int:
return self.x-1
def __iterate__(self)->int:
self.i=self.i-1
return self.i
def __enter__(self)->int:
return 6
def __contains__(self,y:int)->bool:
if y<self.x:
return True
else:
return False
def __end__(self):
printf("Context ended\n")
def __getitem__(self,index1:int,index2:int)->int:
return index1+index2
def __getitem__(self,index1:int)->int:
return index1
def __del__(self):
printf("Deleted\n")
switch:iterate_test=iterate_test(5)
switch2:int=switch.__add__(2)
enum colours:
RED = 144,
GREEN,
BLUE = 59,Another=GREEN+9
type a = def(int)->int
type g = def(int)
static var_static:int = 8
static def staticfunc():
printf("Hello from static function\n")
printf("Static var is %lld\n",var_static)
item1:int=0
union name:
item1:int
item2:float
def divide(num1:int ,num2:int )->int:
match num2:
case 0:
raise error.ZeroDivisionError
case _:
return num1/num2
def decorator(func:a )->g:
h:int =9
def value(c:int):
h=8
printf("%lld\n",h)
printf("Answer is %lld\n",func(c))
return value
ggggggg:int=9
@decorator
def dec_test(x:int)->int:
ggggggg=7#mutability test
printf("%lld\n",ggggggg)
@decorator
def nested_test(x:int)->int:
ggggggg=7#mutability test
printf("%lld\n",ggggggg)
def nested_test2(x:int)->int:
ggggggg=7#mutability test
printf("%lld\n",ggggggg)
return x*x
nested_test2(7)
return x*x
nested_test(7)
return x*x
@decorator
static def static_dec_test(x:int)->int:
printf("Static decorator\n")
ggggggg=7#mutability test
printf("%lld\n",ggggggg)
@decorator
def nested_test(x:int)->int:
ggggggg=7#mutability test
printf("%lld\n",ggggggg)
def nested_test2(x:int)->int:
ggggggg=7#mutability test
printf("%lld\n",ggggggg)
return x*x
nested_test2(7)
return x*x
nested_test(7)
return x*x
def test(x:int)->int:#this is comment
return x
def lambda_test(x:a):
printf("The value is %lld\n",x(5))
def func(g:int)->int:
return g
static inline def static_inline_func():
printf("Hello from static inline function\n")
inline def inline_func():
printf("Hello from inline function\n")
export def exported_func():
printf("Hello from peregrine\n")
union union_name:
f:float
i:int
def (y:int)multiply(x:int)->int:
return x*y
def (const y:int)multiply2(const x:int)->int:
return x*y
private def private_func():
printf("Hello from private function\n")
#platform dependent stuff
'
def inline_asm():
arg1:int=45
arg2:int=50
add:int=0
__asm__:
add="addl %%ebx,%%eax"
"a"=arg1
"b"=arg2
printf("%lld + %lld = %lld\n",arg1,arg2,add)
'
type lambda_type=def(int)->int
def main():
"
inline_asm()
"
private_func()
square:lambda_type=def(x:int):x*x
printf("Square of 4 is %lld\n",square(4))
printf("Square of 4 is %lld\n",4|>square)
switch:iterate_test=iterate_test(5)
virtual_test:*inherit=&switch
virtual_test->__type__()
_in:inherit=inherit()
virtual_test=&_in
virtual_test->__type__()
switch2:int=switch.__add__(test(5))
switch3:*iterate_test=&switch
switch2=switch3->__add__(test(5))
peregrine1()
cc1:union_name
arrow_test:*union_name=&cc1
arrow_test->i=7
printf("arrow_test->i=%lld\n",arrow_test->i)
exported_func()
unknown_type(iterate_test(6))
for x in iterate_test(6):
printf("Item is %lld\n",x)
enum_test:colours=colours.RED
static_inline_func()
inline_func()
staticfunc()
dec_test(4)
static_dec_test(4)
test_var:int=9
test_pointer:*int=&test_var
test_ref:int=test_var
printf("Value of test_pointer is %lld is at location %p\n",*test_pointer,test_pointer)
printf("Value of test_ref is %lld is at location %p\n",test_ref,&test_ref)
def z()->int:
printf("hello\n")
return 5
printf("value of z() is %lld\n",z())
scope:
printf("Hello from new scope\n")
x:bool =False
const cc:bool=False
x=True
y:bool
y=True
if x==y:
...
printf("%lld\n",test(0))
a:int =0
b:int =7
c:int =7
match a,b,c:
case 5,7,8:
printf("a is 5,b is 7 and c is 8")
case 4,7,_:#c can be anything
printf("a is 4 but b is 7")
break #we dont want default to execute
case 4,_,7:#b can be anything
printf("a is 4 but c is 7")
break #we dont want default to execute
case 8,_:#b and c can be anything
printf("a is 8")
case _:
printf("idk")
#optional
default:#will be executed at the end if no break
printf("\nHello\n")
lambda_test(func)
assert 8==8
var1:name
var1.item1=8
printf("item1 of var1 is %lld\n",var1.item1)
var1.item2=6.8
printf("item2 of var1 is %lf\n",var1.item2)
#should throw an error
#assert 8==6
cast_test:int =9
printf("Value is %lf\n",cast<float>(cast_test))
printf("%lld\n",divide(6,2))
TernaryIf:int=5 if True else 6
assert TernaryIf==5
TernaryIf=5 if False else 6
assert TernaryIf==6
r:int=3
r--
assert r==2
r++
try:
assert r==0
except error.AssertionError:
printf("Assertion error\n")
try:
printf("%lld",divide(6,0))
except error.AssertionError,error.ZeroDivisionError:...
except:
printf("Exception caught\n")
try:assert False
except error.AssertionError,error.ZeroDivisionError as e:
printf("Exception caught %lld\n",e)
#TODO: Make it work with iterable and multiple function return
variable_a:int=5
variable_b:int=1
printf("variable_a is %lld and variable_b is %lld\n",variable_a,variable_b)
variable_b,variable_a=variable_a,variable_b
printf("variable_a is %lld and variable_b is %lld\n",variable_a,variable_b)
assert variable_a==1
assert variable_b==5
variable_b += variable_a
assert variable_b==6
printf("variable_b is %lld\n",variable_b)
a=1&2
try:
try:divide(1,0)
except:
printf("Nested try\n")
divide(1,0)
except:
printf("Global try\n")
def print_int(i:i32):
printf("Square is %d\n",i)
#long chained function call
print_int(square(4))
#the above is same as folows
4|>square|>print_int