aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/all.hff51
-rw-r--r--src/common.hff12
-rw-r--r--src/libc.hff7
-rw-r--r--src/main.cff12
-rw-r--r--src/parse.cff2
-rw-r--r--src/parse.hff27
-rw-r--r--src/util.cff14
-rw-r--r--src/vec.hff38
8 files changed, 119 insertions, 44 deletions
diff --git a/src/all.hff b/src/all.hff
new file mode 100644
index 0000000..89f2b71
--- /dev/null
+++ b/src/all.hff
@@ -0,0 +1,51 @@
+import "libc.hff";
+
+/// Macros
+
+defmacro assert {
+(ex, s) [
+ (do
+ if not (ex) {
+ fprintf(stderr, "%s:%d: assertion failed: ", #FILE, #LINE);
+ fprintf(stderr, "`%s'", (s));
+ fprintf(stderr, "\n");
+ abort();
+ }
+ )
+]
+}
+
+/// Types
+
+struct Loc {
+ fileid u16,
+ idx isize,
+ col int,
+ line int,
+}
+
+#[lax]
+enum TokT {
+ kw_or
+}
+
+struct Tok {
+ t int,
+ u union {
+ i i64
+ },
+}
+
+struct Decl {
+}
+
+struct Parser {
+
+}
+
+// parse.cff
+extern fn parse(P *Parser, path *const u8) [#]Decl;
+
+// util.cff
+extern fn xmalloc(n usize) *void;
+extern fn xrealloc(p *void, n usize) *void;
diff --git a/src/common.hff b/src/common.hff
deleted file mode 100644
index 71e3be0..0000000
--- a/src/common.hff
+++ /dev/null
@@ -1,12 +0,0 @@
-import "libc.hff";
-
-defmacro assert {
-(ex,s) [ (do
- if not (ex) {
- fprintf(stderr, "%s:%d: assertion failed: ", #FILE, #LINE);
- fprintf(stderr, "`%s'", (s));
- fprintf(stderr, "\n");
- abort();
- }
-) ]
-}
diff --git a/src/libc.hff b/src/libc.hff
index f06f4dd..d686905 100644
--- a/src/libc.hff
+++ b/src/libc.hff
@@ -1,4 +1,4 @@
-! stdio.h
+// stdio.h
struct FILE;
extern static stdin *FILE,
stdout *FILE,
@@ -6,6 +6,9 @@ extern static stdin *FILE,
extern fn printf(fmt *const u8, ...) void;
extern fn fprintf(fp *FILE, fmt *const u8, ...) void;
-! stdlib.h
+// stdlib.h
extern fn abort() void;
extern fn exit(c int) void;
+extern fn malloc(n usize) *void;
+extern fn realloc(p *void, n usize) *void;
+extern fn free(p *void) void;
diff --git a/src/main.cff b/src/main.cff
index 9392a7a..4315ce1 100644
--- a/src/main.cff
+++ b/src/main.cff
@@ -1,11 +1,19 @@
+import "all.hff";
import "libc.hff";
-import "common.hff";
-import "parse.hff";
+import "vec.hff";
extern fn main(argc int, argv **u8) int {
assert(argc > 1, "args?");
let p = Parser {};
parse(&p, argv[1]);
+
+ let args Vec<*u8> = {};
+ for let i = 1; i < argc; ++i {
+ args->push(argv[i]);
+ }
+ vec_each(s, i, args,
+ printf("%d: %s\n", i, s);
+ )
}
diff --git a/src/parse.cff b/src/parse.cff
index 35cccdb..089d647 100644
--- a/src/parse.cff
+++ b/src/parse.cff
@@ -1,4 +1,4 @@
-import "parse.hff";
+import "all.hff";
extern fn parse(P *Parser, path *const u8) [#]Decl {
return {};
diff --git a/src/parse.hff b/src/parse.hff
deleted file mode 100644
index 9976df3..0000000
--- a/src/parse.hff
+++ /dev/null
@@ -1,27 +0,0 @@
-struct Loc {
- fileid u16,
- idx isize,
- col int,
- line int,
-}
-
-#[lax]
-enum TokT {
- kw_or
-}
-
-struct Tok {
- t int,
- u union {
- i i64
- },
-}
-
-struct Decl {
-}
-
-struct Parser {
-
-}
-
-extern fn parse(P *Parser, path *const u8) [#]Decl;
diff --git a/src/util.cff b/src/util.cff
new file mode 100644
index 0000000..36a0c13
--- /dev/null
+++ b/src/util.cff
@@ -0,0 +1,14 @@
+import "all.hff";
+
+extern fn xmalloc(n usize) *void {
+ let p = malloc(n);
+ assert(p != #null, "malloc");
+ return p;
+}
+
+extern fn xrealloc(p *void, n usize) *void {
+ let p = realloc(p, n);
+ assert(p != #null, "realloc");
+ return p;
+}
+
diff --git a/src/vec.hff b/src/vec.hff
new file mode 100644
index 0000000..8f794c9
--- /dev/null
+++ b/src/vec.hff
@@ -0,0 +1,38 @@
+import "all.hff";
+
+extern fn malloc(n usize) *void;
+struct Vec<T> {
+ dat *T,
+ len uint,
+ cap uint,
+
+ fn push(vec *Vec, x T) void {
+ if vec.len >= vec.cap {
+ vec.cap = (vec.len + 1) > 8 ? (vec.len + 1) * 2 : 8;
+ if vec.dat == #null {
+ vec.dat = malloc(vec.cap * sizeof T);
+ assert(vec.dat != #null, "malloc");
+ } else {
+ vec.dat = realloc(vec.dat, vec.cap * sizeof T);
+ assert(vec.dat != #null, "realloc");
+ }
+ }
+ vec.dat[vec.len++] = x;
+ }
+
+ fn clear(vec *Vec) void {
+ free(vec.dat);
+ vec.len = 0;
+ vec.cap = 0;
+ }
+}
+
+defmacro vec_each(x, i, v, ...body) [
+ {
+ let $v = v;
+ for let i = 0; i < $v.len; ++i {
+ let x = v.dat[i];
+ { body }
+ }
+ }
+]