aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/test.cff
blob: 6a2b736bf285a18a303561500f3e2ccafed85fcb (plain) (blame)
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
import "libc.hff";

union Val {
   u u32,
   f f32,
}


enum Color : i16 {
   Red, Green, Blue
}

defmacro each(i, x, arr, &body) [
   for let i = 0z; i < arr.#len; ++i {
      let x = arr[i];
      body
   }
]

static xs *void = {};

fn isort(xs *int, n usize) void {
   fn icmp(lhs *const void, rhs *const void, _ *void) int {
      let lhs = *as(*int)lhs,
          rhs = *as(*int)rhs;
      return lhs - rhs;
   }
   qsort(xs, n, 4, &icmp);

   fn foo() void {}
   let x= &foo;
   x();
}

struct List;
struct List {
   next *List,
   val int,
   
   fn ok(l List) void;

   fn length(l *List) usize {
      let n = 0z;
      ok(*l);
      for ; l; l = l.next { ++n; }
      return n;
   }

   fn ok(l List) void {}
}

struct Vec2f;
struct Vec2f {
   x f32,
   y f32,

   fn mag(v Vec2f) f32 {
      extern fn sqrtf(_ f32) f32;
      return sqrtf((v.x * v.x) + (v.y * v.y));
   }
   fn zero(v *Vec2f) void {
      v.x = 0;
      v.y = 0;
   }
}

fn spanz(cstr *const u8) [#]const u8 {
   extern fn strlen(s *const u8) usize;
   return cstr[0 :: strlen(cstr)];
}

defmacro transmute(Type, x) [
   (do
      union T { from typeof(x), to Type };
      (T{x}).to;
   )
]

enum union Number {
   none,
   int i64,
   flo f32,
}

extern fn main (argc int, argv **u8) int {

   let colors [3]Color = { :Red, :Green, :Blue } ;
   let x = Vec2f { .y: 1, .x: 2.4 };
   let p *const Vec2f = &x;
   printf("v = { %g, %g }\n", x.x, p.y);
   printf("mag = %g\n", x->mag());
   Vec2f:zero(&x);
   printf("mag = %g\n", (&x)->mag());

   let x = Number:none;
   let x = Number:int{};

   let is []int = { [4] = -1, 2, [1 - 1] = 3 };
   isort(is, is.#len);
   each(i, x, is,
      printf("%d\n", x);
   )
   each(i, x, is) {
      printf("%d\n", x);
   }
   let slice [#]int = is[3::5];

   printf("sl %d\n", slice[0]);
   slice = slice[1::4];
   printf("sl %d\n", slice[0]);
   slice.#len;
   printf("%c\n", "ABCD"[1::][0]);

   let const v Vec2f = {};
   // v->zero();
   // v.x += 1;

   printf("sizeof(is) = %zu\n", sizeof(is));
   printf("sizeof *void = %zu\n", sizeof *void);
   printf("alignof f64= %zu\n", alignof f64);
   printf("1.2 -> %#.8x\n", transmute(u32, 1.2f));

   switch is.#len {
   case 0, 1;
      is.#len < 2;
   case 3;
      3;
   case else;
      "idk";
   }
   
   switch colors[0] {
   case :Red;
   }


   return 0;
}