summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2020-05-03 15:19:45 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2020-05-03 15:19:45 -0400
commit9661c9839d6635181b81e0a1061dfa566f7660fe (patch)
treec9298875e57c08985f376af36bf346bb048befbd
parent3fcabe8edbe4213ab1d5a65c584ee332c1b26baf (diff)
trying to make std/*.toc portable, fwrite not working for some reason
-rw-r--r--build.bat2
-rw-r--r--cgen.c18
-rw-r--r--main.c5
-rw-r--r--parse.c21
-rw-r--r--std/base.toc23
-rw-r--r--std/io.toc9
-rw-r--r--std/mem.toc6
-rw-r--r--test.toc16
-rw-r--r--toc.c19
-rw-r--r--types.c7
-rw-r--r--types.h17
11 files changed, 132 insertions, 11 deletions
diff --git a/build.bat b/build.bat
index 1d6a338..50f2fbc 100644
--- a/build.bat
+++ b/build.bat
@@ -1 +1 @@
-cl /DTOC_DEBUG /W3 /wd4146 /D_CRT_SECURE_NO_WARNINGS /Fe:toc.exe /DEBUG /Zi main.c
+cl /DTOC_DEBUG /W3 /wd4146 /D_CRT_SECURE_NO_WARNINGS /Od /Fe:toc.exe /DEBUG /Zi main.c
diff --git a/cgen.c b/cgen.c
index 6fc6cb6..607db80 100644
--- a/cgen.c
+++ b/cgen.c
@@ -1732,6 +1732,9 @@ static void cgen_expr(CGenerator *g, Expression *e) {
case BUILTIN_COMPILING:
cgen_write(g, "false");
break;
+ case BUILTIN_PLATFORM:
+ cgen_write(g, "platform__");
+ break;
}
break;
case EXPR_CAST: {
@@ -2270,6 +2273,21 @@ static void cgen_file(CGenerator *g, ParsedFile *f) {
"typedef struct { void *data; i64 n; } slice_;\n"
"#define false ((bool)0)\n"
"#define true ((bool)1)\n"
+ "#ifdef __linux__\n" /* see also toc.c */
+ "#define platform__ " stringify(PLATFORM_LINUX) "\n"
+ "#elif defined _WIN32\n"
+ "#define platform__ " stringify(PLATFORM_WINDOWS) "\n"
+ "#elif defined __APPLE__\n"
+ "#define platform__ " stringify(PLATFORM_OSX) "\n"
+ "#elif defined __FreeBSD__\n"
+ "#define platform__ " stringify(PLATFORM_FREEBSD) "\n"
+ "#elif defined __OpenBSD__\n"
+ "#define platform__ " stringify(PLATFORM_OPENBSD) "\n"
+ "#elif defined __unix__\n"
+ "#define platform__ " stringify(PLATFORM_MISC_UNIX) "\n"
+ "#else\n"
+ "#define platform__ " stringify(PLATFORM_OTHER) "\n"
+ "#endif\n"
"static slice_ mkslice_(void *data, i64 n) { slice_ ret; ret.data = data; ret.n = n; return ret; }\n");
cgen_sdecls_file(g, f);
diff --git a/main.c b/main.c
index 313267b..ba98e87 100644
--- a/main.c
+++ b/main.c
@@ -8,6 +8,11 @@
/*
@TODO:
+allow
+#include "foo.toc", foo;
+#include "foo.toc", foo;
+then do #include "std/base.toc", base; in std/io.toc and std/mem.toc
+does our use before declare thing work with #include?
#no_warn
start making a standard library... (printf; stringbuilder would be nice to have)
improve type_to_str:
diff --git a/parse.c b/parse.c
index b066708..c40e556 100644
--- a/parse.c
+++ b/parse.c
@@ -3051,3 +3051,24 @@ static inline void construct_resolved_builtin_type(Type *t, BuiltinType builtin)
t->builtin = builtin;
t->flags = TYPE_IS_RESOLVED;
}
+
+#ifndef TOC_DEBUG
+static
+#endif
+char *location_to_str(Location *where) {
+ File *file = where->file;
+ Token *tokens = file->tokens;
+ SourcePos pos = tokens[where->start].pos;
+ char *contents = file->contents;
+ char *s = contents + pos.start;
+ size_t nchars = 10;
+ char buf[64] = {0};
+ snprintf(buf, sizeof buf - 12, "Line %u of %s: ", (unsigned)pos.line, file->filename);
+ char *end = memchr(s, '\0', nchars);
+ if (!end) end = s + nchars;
+ char tmp = *end;
+ *end = '\0';
+ strcat(buf, s);
+ *end = tmp;
+ return str_dup(buf);
+}
diff --git a/std/base.toc b/std/base.toc
new file mode 100644
index 0000000..bdb91e0
--- /dev/null
+++ b/std/base.toc
@@ -0,0 +1,23 @@
+PLATFORM_OTHER ::= 0;
+PLATFORM_LINUX ::= 1;
+PLATFORM_WINDOWS ::= 2;
+PLATFORM_OSX ::= 3;
+PLATFORM_FREEBSD ::= 4;
+PLATFORM_OPENBSD ::= 5;
+PLATFORM_MISC_UNIX ::= 6;
+
+PLATFORM ::= #builtin("platform");
+#if PLATFORM == PLATFORM_LINUX {
+ libc ::= "libc.so.6";
+} elif PLATFORM == PLATFORM_WINDOWS {
+ libc ::= "msvcrt.dll";
+} elif PLATFORM == PLATFORM_OSX {
+ libc ::= "libc.dylib";
+} elif PLATFORM == PLATFORM_FREEBSD || PLATFORM == PLATFORM_OPENBSD {
+ libc ::= "libc.so";
+} else {
+ /* maybe it's non-linux gnu? */
+ libc ::= "libc.so.6";
+}
+
+
diff --git a/std/io.toc b/std/io.toc
index 2c82dc9..c3d8f71 100644
--- a/std/io.toc
+++ b/std/io.toc
@@ -1,12 +1,13 @@
-putchar ::= #foreign("putchar", "libc.so.6") fn(#C int) #C int;
+#include "std/base.toc";
+
+putchar ::= #foreign("putchar", libc) fn(#C int) #C int;
toc_putchar ::= fn(x: char) {
putchar(x as #C int);
}
-printf ::= #foreign("printf", "libc.so.6") fn(#C &"char const", #C ..) #C int;
+fwrite ::= #foreign("fwrite", libc) fn(#C &"const void", #C size_t, #C size_t, #C &"FILE") #C size_t;
writes ::= fn(x: []char) {
- printf_strfmt := "%s\0";
- printf(&printf_strfmt[0], &x[0]);
+ fwrite(&x[0], 1, x.len as #C size_t, #builtin("stdout"));
}
puts ::= fn(x: []char) {
diff --git a/std/mem.toc b/std/mem.toc
index 6d16895..e283364 100644
--- a/std/mem.toc
+++ b/std/mem.toc
@@ -1,6 +1,8 @@
+#include "std/base.toc";
+
// TODO: check for failed calloc
-calloc ::= #foreign("calloc", "libc.so.6") fn(#C size_t, #C size_t) #C &"void";
-free ::= #foreign("free", "libc.so.6") fn(#C &"void");
+calloc ::= #foreign("calloc", libc) fn(#C size_t, #C size_t) #C &"void";
+free ::= #foreign("free", libc) fn(#C &"void");
new ::= fn(t :: Type) &t {
calloc(1, (sizeof t) as #C size_t)
diff --git a/test.toc b/test.toc
index 581909b..a4b7bff 100644
--- a/test.toc
+++ b/test.toc
@@ -1,3 +1,18 @@
+#include "std/io.toc";
+#include "std/mem.toc";
+main ::= fn() {
+ p := #builtin("platform");
+ P ::= #builtin("platform");
+ puti(p);
+ puti(P);
+ s := news(char, 12);
+ for c, i := "hello, world" {
+ s[i] = c;
+ }
+ puts(s);
+}
+main();
+/*
stdc ::= "msvcrt.dll";
printf ::= #foreign("printf",stdc) fn (#C &"char const", #C ..) #C int;
puti ::= fn(i: u64) i32 {
@@ -16,3 +31,4 @@ main ::= fn() {
puti(foo(0x12345678cafebabe as u64, 0x76543210deadbeef as u64));
}
main();
+*/
diff --git a/toc.c b/toc.c
index 7069205..ed7443a 100644
--- a/toc.c
+++ b/toc.c
@@ -69,6 +69,25 @@ static void print_block_location(Block *b);
#define join3(a,b) a##b
#define join2(a,b) join3(a,b)
#define join(a,b) join2(a,b)
+#define stringify3(x) #x
+#define stringify2(x) stringify3(x)
+#define stringify(x) stringify2(x)
+
+#ifdef __linux__ /* see also cgen_file */
+#define platform__ PLATFORM_LINUX
+#elif defined _WIN32
+#define platform__ PLATFORM_WINDOWS
+#elif defined __APPLE__
+#define platform__ PLATFORM_OSX
+#elif defined __FreeBSD__
+#define platform__ PLATFORM_FREEBSD
+#elif defined __OpenBSD__
+#define platform__ PLATFORM_OPENBSD
+#elif defined __unix__
+#define platform__ PLATFORM_MISC_UNIX
+#else
+#define platform__ PLATFORM_OTHER
+#endif
static void fprint_char_literal(FILE *f, char c) {
if (isprint(c))
diff --git a/types.c b/types.c
index 7dbfff6..df6dd07 100644
--- a/types.c
+++ b/types.c
@@ -1468,6 +1468,9 @@ static Value get_builtin_val(BuiltinVal val) {
case BUILTIN_COMPILING:
v.boolv = true;
break;
+ case BUILTIN_PLATFORM:
+ v.i64 = platform__;
+ break;
}
return v;
}
@@ -1489,6 +1492,10 @@ static void get_builtin_val_type(Allocator *a, BuiltinVal val, Type *t) {
t->kind = TYPE_BUILTIN;
t->builtin = BUILTIN_BOOL;
break;
+ case BUILTIN_PLATFORM:
+ t->kind = TYPE_BUILTIN;
+ t->builtin = BUILTIN_I64;
+ break;
}
}
diff --git a/types.h b/types.h
index 22dfe56..448727a 100644
--- a/types.h
+++ b/types.h
@@ -366,7 +366,7 @@ typedef String StrLiteral;
typedef struct {
U32 line;
- U32 start; /* index in ctx->str */
+ U32 start; /* index in file->contents */
U32 end; /* exclusive */
} SourcePos;
@@ -741,16 +741,25 @@ typedef struct SliceExpr {
} c;
} SliceExpr;
+#define PLATFORM_OTHER 0
+#define PLATFORM_LINUX 1
+#define PLATFORM_WINDOWS 2
+#define PLATFORM_OSX 3
+#define PLATFORM_FREEBSD 4
+#define PLATFORM_OPENBSD 5
+#define PLATFORM_MISC_UNIX 6
+
typedef enum {
BUILTIN_STDOUT,
BUILTIN_STDERR,
BUILTIN_STDIN,
- BUILTIN_COMPILING
-#define BUILTIN_VAL_COUNT (BUILTIN_COMPILING+1)
+ BUILTIN_COMPILING,
+ BUILTIN_PLATFORM
+#define BUILTIN_VAL_COUNT (BUILTIN_PLATFORM+1)
} BuiltinVal;
const char *const builtin_val_names[BUILTIN_VAL_COUNT] =
- {"stdout", "stderr", "stdin", "compiling"};
+ {"stdout", "stderr", "stdin", "compiling", "platform"};
typedef struct Namespace {
Block body;