aboutsummaryrefslogtreecommitdiff
path: root/examples/staged.zc
diff options
context:
space:
mode:
Diffstat (limited to 'examples/staged.zc')
-rw-r--r--examples/staged.zc27
1 files changed, 15 insertions, 12 deletions
diff --git a/examples/staged.zc b/examples/staged.zc
index a180ae3..23453f9 100644
--- a/examples/staged.zc
+++ b/examples/staged.zc
@@ -1,9 +1,12 @@
+namespace;
+include rt, run;
+
// staged code: a program writes a specialized function as source, compiles it
// while running, and calls it. here, polynomials with their coefficients baked
// in as constants, so there's no loop and no array left at runtime.
let buf[2000], n;
-fn put(s) { let i = 0; while (s[i]) { buf[n] = s[i]; n++; i++; } return 0; }
-fn putnum(int v) { if (v >= 10) putnum(v / 10); buf[n] = '0' + v % 10; n++; return 0; }
+void put(s) { let i = 0; while (s[i]) { buf[n] = s[i]; n++; i++; } return; }
+void putnum(int v) { if (v >= 10) putnum(v / 10); buf[n] = '0' + v % 10; n++; return; }
// coefficients c[0] + c[1] x + ... in horner form: ((c3 x + c2) x + c1) x + c0
fn specialize(int* c, int deg) {
@@ -19,15 +22,15 @@ fn specialize(int* c, int deg) {
buf[n] = 0;
return buf;
}
-fn main() {
- [4]int c;
+void main() {
+ int[4] c;
c[0] = 7; c[1] = 0 - 3; c[2] = 0; c[3] = 2; // 2x^3 - 3x + 7
- let src = specialize(c, 3), q = tokens(src);
- put_s(src); put_c(10);
- load(q); // compiled now, into the running program
- free(d*q);
- let f = sval[lookups("poly")]; // and found by name in the compiler's symbol table
- for (int x = 0 - 2; x <= 3; x++) { put_n(f(x)); put_c(' '); }
- put_c(10);
- return 0;
+ let src = specialize(c, 3), q = run.tokens(src);
+ rt.put_s(src); rt.put_c(10);
+ run.load(q); // compiled now, into the running program
+ rt.free(q);
+ let f = run.sval[run.lookups("poly")]; // and found by name in the compiler's symbol table
+ for (int x = 0 - 2; x <= 3; x++) { rt.put_n(f(x)); rt.put_c(' '); }
+ rt.put_c(10);
+ return;
}