-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.c
100 lines (76 loc) · 1.92 KB
/
test.c
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
// Pyrite BASIC compiler test
#include <stdio.h>
#include "pyrite.h"
void func(int a) { printf("func: %d\n", a); }
BASIC(
type Vec3
field x as float, y as float
field z as float
endtype
declare(bar as () of void,
baz as (int) of int)
functype(IntF, ((int), void))
function foo as (i as int, j as int) of int do
let
o as Vec3 equal new(Vec3), a as stringArray equal array(string, 3),
s as string equal str("123"),
c as charArray equal array(char, 5),
f as IntF equal func,
fa as IntFArray equal array(IntF, 5)
end
elem(a, 0) = s ; elem(a, 1) = str("_456_") ; elem(a, 2) = str("789");
print(join(str("ABC"), str("DEF"))); bar()
var(x as int) // 'var' doesn't link variables to the GC system
for x in 0 to 2 do
print(elem(a, x))
end
return 2 * (i + j) end
end
type myEx
field code as int, msg as string
endtype
function handle as (e as myEx) of void do
printf("Handled exception with code %d and message %s\n",
e->code, e->msg->data)
end
function bar as (none) of void do
try
printf("Trying baz...\n");
baz(45);
printf("Tried baz!\n")
catch(myEx)
printf("Caught a myEx!\n"); handle((myEx)exception)
end
end
function baz as (x as int) of int do
printf("baz got: %d\n", x)
let e as myEx equal new(myEx) end
e->code = 67 ; e->msg = str("'a message'") ; throw(e)
return x + 1 end
end
function printSA as (a as stringArray) of void do
print(str("Printing string array:"))
var(i as int)
for i in 0 upto arraylen(a) do
print(join(str(" "), elem(a, i)))
end
end
)
// Undefine BASIC keywords so C code will work
#include "pyrite-undefs.h"
int main(int argc, char ** argv) {
GC_Init();
GC_SetMode(GC_OFF);
stringArray avec = array(string, argc);
for (int i = 0; i < argc; i++) {
elem(avec, i) = str(argv[i]);
}
GC_SetMode(GC_ON);
int r = foo(42, 47);
printf("foo: %d\n", r);
printSA(avec);
r = GC_Collect();
printf("Freed %d bytes\n", r);
printf("All works fine..?\n");
return 0;
}