diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/alloc.ct | 4 | ||||
| -rw-r--r-- | examples/example.ct | 53 | ||||
| -rw-r--r-- | examples/main.ct | 3 | ||||
| -rw-r--r-- | examples/vec.ct | 32 |
4 files changed, 74 insertions, 18 deletions
diff --git a/examples/alloc.ct b/examples/alloc.ct index 5f11bb7..11eed78 100644 --- a/examples/alloc.ct +++ b/examples/alloc.ct @@ -1,8 +1,8 @@ /* from c bindings */ -pub alloc(s usize) *void +pub alloc(s: usize) *void { - p *u8 = malloc(s); + p: *u8 = malloc(s); assert(p, "memory allocation failed\n"); return p; } diff --git a/examples/example.ct b/examples/example.ct index ce3e4e7..b5d752f 100644 --- a/examples/example.ct +++ b/examples/example.ct @@ -70,6 +70,59 @@ pub add(r *string, a *string, b *string) r.len = len; } +/* example of hygienic and recursive macros. + In macros, the last statement of the block is taken as the "return" value, + so the following two definitions would be enough to get the smallest + value of any number of parameters, without affecting any variables in + the parent context. If recursive blocks, the last of the blocks is taken. + + In this case, the same thing could be done with functions of course. + + Internally, if one of these macros is passed as a function argument, all + statements except the last (which has to be a certain type, + have to check exactly which rules I want to apply to it) will be moved + into the nearest "regular" context, i.e. not a function call in a function + call or something like that. The last variable's context is kept as the block + in which it was defined, but accessed "anonymously". In C, something like + + // some counter to make sure we don't overlap some other variable + int min_res_12345; + { + const int _a = a; + const int _b = b; + + const int _res = _a < _b ? _a : _b; + min_res_12345 = _res; + } + call(min_res_12345); + + */ +define min(a, b) {{ + _a const = a; + _b const = b; + + _res const = if _a < _b { + _a + } else { + _b + }; + + _res; +}} + +define min(a, b ...args) {{ + _a const = a; + _b const = min(b, ...args); + + _res const = if _a < _b { + _a + } else { + _b + }; + + _res; +}} + pub define new(x ...args) { x = alloc(sizeof(x)); diff --git a/examples/main.ct b/examples/main.ct new file mode 100644 index 0000000..4ebe8b7 --- /dev/null +++ b/examples/main.ct @@ -0,0 +1,3 @@ +main() i32 { + return 0; +} diff --git a/examples/vec.ct b/examples/vec.ct index f44fec0..dee50ca 100644 --- a/examples/vec.ct +++ b/examples/vec.ct @@ -1,32 +1,32 @@ -typedef vectorable { +type vectorable { std; /* this would be needed for comparisons like find() or something */ comparable; } -typedef vec (t vectorable) { - void std_vector; - len usize; - buf *t; +type vec {t: vectorable} { + std_vector: void; + len: usize; + buf: 't; } -pub define vec(t) { struct (vector[t]) } +pub define vec(t) { struct (vec{t}) } -pub init(mut v *vector) +pub init(v: 'vector) { v.len = 0; v.buf = null; } -pub length(v *vec) usize { return v.len; } +pub length(v: *vec => usize) { return v.len; } -pub index(v *vec, i usize) *#(v.buf) +pub index(v: *vec, i: usize => '#v.buf) { assert(i < v.len, "index %zu out of bounds\n", i); return &v.buf[i]; } -pub index(v *vec, i isize) *#v.buf +pub index(v: mut *vec, i: const isize => '#v.buf) { if i < 0 { assert(-i < v.len, "reverse index %zi out of bounds\n", i); @@ -37,17 +37,17 @@ pub index(v *vec, i isize) *#v.buf return &v.buf[i]; } -pub prepend(mut *vec, e *#v.buf) { insert(v, e, 0uz); } -pub append(mut v *vec, e *#v.buf) { insert(v, e, v.len) } +pub prepend(v: mut 'vec, e: '#v.buf) { insert(v, e, 0); } +pub append(v: mut 'vec, e: '#v.buf) { insert(v, e, v.len); } -pub preplace(mut v *vec, e *#v.buf) { place(v, e, 0uz); } -pub applace(mut v *vec, e *#v.buf) { place(v, e, v.len - 1); } +pub preplace(v: mut 'vec, e: '#v.buf) { place(v, e, 0); } +pub applace(v: mut 'vec, e: '#v.buf) { place(v, e, v.len - 1); } -pub place(mut v *vec, e *#v.buf, i usize) +pub place(v: mut *vec, e: '#v.buf, i: usize) { } -pub insert(mut v *vec, e *#v.buf, i usize) +pub insert(v: mut *vec, e^ '#v.buf, i: usize) { } |
