diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-01-16 20:34:41 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-01-16 20:34:41 -0500 |
commit | dd3c2aa388026afcbcb987d9944c6f7207fb2d2c (patch) | |
tree | 5c9eb31e80917879cbb95a2a0c40d8060dd21614 /std | |
parent | 2ca96f5c91395771a37ad6707b2b28c60e6a8873 (diff) |
better packages. also started std/io pkg (wrote hello world in toc!)
Diffstat (limited to 'std')
-rw-r--r-- | std/arr.c | 23 | ||||
-rw-r--r-- | std/arr.toc | 27 | ||||
-rw-r--r-- | std/io.c | 42 | ||||
-rw-r--r-- | std/io.toc | 14 |
4 files changed, 106 insertions, 0 deletions
diff --git a/std/arr.c b/std/arr.c new file mode 100644 index 0000000..2d2c03a --- /dev/null +++ b/std/arr.c @@ -0,0 +1,23 @@ +#include <stdint.h> +#include <stdio.h> +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef float f32; +typedef double f64; +typedef u8 bool; +typedef struct { void *data; i64 n; } slice_; +#define false ((bool)0) +#define true ((bool)1) +static slice_ mkslice_(void *data, i64 n) { slice_ ret; ret.data = data; ret.n = n; return ret; } +static void free_(void *data) { extern void free(void *data); free(data); } +static void *e__calloc(size_t n, size_t sz) { extern void *calloc(size_t n, size_t size); extern void abort(void); void *ret = calloc(n, sz); if (n && sz && !ret) { fprintf(stderr, "Out of memory.\n"); abort(); } return ret; } + + +/* declarations */ +/* code */ diff --git a/std/arr.toc b/std/arr.toc new file mode 100644 index 0000000..fcf6a45 --- /dev/null +++ b/std/arr.toc @@ -0,0 +1,27 @@ +// array package +pkg "arr"; + +#export Arr ::= fn (t :: Type) Type { + struct { + data : []t; + len, cap : int; + } +}; +#export arr_add ::= fn(t ::=, a : &Arr(t), x : t) { + if a.len >= a.cap { + a.cap = a.cap * 2 + 2; + new_data := new(t, a.cap); + each i := 0..a.len-1 { + new_data[i] = a.data[i]; + } + a.data = new_data; + } + a.data[a.len] = x; + a.len += 1; +}; + +#export arr_foreach ::= fn(t ::=, a : Arr(t), f : fn(&t)) { + each i := 0..a.len-1 { + f(&a.data[i]); + } +}; diff --git a/std/io.c b/std/io.c new file mode 100644 index 0000000..97d5d43 --- /dev/null +++ b/std/io.c @@ -0,0 +1,42 @@ +#include <stdint.h> +#include <stdio.h> +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef float f32; +typedef double f64; +typedef u8 bool; +typedef struct { void *data; i64 n; } slice_; +#define false ((bool)0) +#define true ((bool)1) +static slice_ mkslice_(void *data, i64 n) { slice_ ret; ret.data = data; ret.n = n; return ret; } +static void free_(void *data) { extern void free(void *data); free(data); } +static void *e__calloc(size_t n, size_t sz) { extern void *calloc(size_t n, size_t size); extern void abort(void); void *ret = calloc(n, sz); if (n && sz && !ret) { fprintf(stderr, "Out of memory.\n"); abort(); } return ret; } + + +/* declarations */ +void io__puti(i64 x); +void io__putf(f32 x); +void io__puts(slice_ x); +/* code */ +void io__puti(i64 x) { + printf("%ld\n", (long)x); +} + + +void io__putf(f32 x) { + printf("%f\n", (double)x); +} + + +void io__puts(slice_ x) { + fwrite(x.data, 1, x.n, stdout); + printf("\n"); +} + + diff --git a/std/io.toc b/std/io.toc new file mode 100644 index 0000000..68e9744 --- /dev/null +++ b/std/io.toc @@ -0,0 +1,14 @@ +pkg "io"; + +#export puti ::= fn(x: int) { + #C("printf(\"%ld\\n\", (long)x)"); +}; + +#export putf ::= fn(x: float) { + #C("printf(\"%f\\n\", (double)x)"); +}; + +#export puts ::= fn(x: []char) { + #C("fwrite(x.data, 1, x.n, stdout)"); + #C("printf(\"\\n\")"); +};
\ No newline at end of file |