commit cbcd41da9f384500dc4eb6ad0b7e2a0dd41c3c8e
parent aa7cd3361db7f347bc117ca5d2ec07a9e5ad3e99
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Fri, 19 Nov 2021 09:42:27 -0600
util.[ch] and nooc.h
Diffstat:
M | Makefile | | | 4 | ++-- |
M | main.c | | | 126 | ++++--------------------------------------------------------------------------- |
A | nooc.h | | | 114 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | util.c | | | 11 | +++++++++++ |
A | util.h | | | 1 | + |
5 files changed, 134 insertions(+), 122 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,5 +1,5 @@
.c.o:
$(CC) -c $< -o $@
-nooc: main.o array.o x64.o
- $(CC) main.o array.o x64.o -o nooc
+nooc: main.o array.o util.o x64.o
+ $(CC) main.o array.o x64.o util.o -o nooc
diff --git a/main.c b/main.c
@@ -11,8 +11,8 @@
#include "array.h"
#include "x64.h"
-
-#define DATA_OFFSET 0x2000
+#include "util.h"
+#include "nooc.h"
int
elf(char *text, size_t len, char* data, size_t dlen, FILE *f)
@@ -75,40 +75,8 @@ elf(char *text, size_t len, char* data, size_t dlen, FILE *f)
}
fwrite(data, 1, dlen, f);
}
-
-enum tokentype {
- TOK_NONE = 0,
- TOK_NAME,
-
- TOK_LPAREN,
- TOK_RPAREN,
-
- TOK_PLUS,
-
- TOK_COMMA,
- TOK_EQUAL,
-
- TOK_NUM,
- TOK_STRING,
-};
-
-struct slice {
- char *ptr;
- size_t len;
-};
-
-struct token {
- enum tokentype type;
- struct slice slice;
- struct token *next;
-};
-
-void
-error(char *error)
-{
- fprintf(stderr, "%s\n", error);
- exit(1);
-}
+struct decls decls;
+struct exprs exprs;
struct token *
lex(struct slice start)
@@ -198,22 +166,7 @@ lex(struct slice start)
return head;
}
-struct fparams {
- size_t cap;
- size_t len;
- size_t *data;
-};
-
-struct fcall {
- struct slice s;
- struct fparams params;
-};
-
-struct decl {
- struct slice s;
- size_t val; // struct exprs
- size_t addr;
-};
+struct data data_seg;
void
expect(struct token *tok, enum tokentype type)
@@ -224,12 +177,6 @@ expect(struct token *tok, enum tokentype type)
error("mismatch");
}
-struct data {
- size_t cap;
- size_t len;
- char *data;
-} data_seg;
-
uint64_t
data_push(char *ptr, size_t len)
{
@@ -244,31 +191,8 @@ data_pushint(uint64_t i)
return DATA_OFFSET + data_seg.len - 8;
}
-struct item {
- enum {
- ITEM_DECL,
- ITEM_CALL
- } kind;
- union {
- struct decl decl;
- struct fcall call;
- } d;
-};
-
-struct items {
- size_t cap;
- size_t len;
- struct item *data;
-};
-
struct items *curitems;
-struct decls {
- size_t cap;
- size_t len;
- uint64_t *data;
-} decls;
-
struct decl *
finddecl(struct items *items, struct slice s)
{
@@ -283,29 +207,6 @@ finddecl(struct items *items, struct slice s)
return NULL;
}
-enum primitive {
- P_INT,
- P_STR,
-};
-
-enum binop {
- OP_PLUS,
-};
-
-struct value {
- enum primitive type;
- union {
- uint64_t val;
- struct slice s;
- } v;
-};
-
-enum exprkind {
- EXPR_LIT,
- EXPR_IDENT,
- EXPR_BINARY
-};
-
char *exprkind_str(enum exprkind kind)
{
switch (kind) {
@@ -318,22 +219,7 @@ char *exprkind_str(enum exprkind kind)
}
}
-struct expr {
- enum exprkind kind;
- union {
- struct value v;
- enum binop op;
- struct slice s;
- } d;
- size_t left;
- size_t right;
-};
-
-struct exprs {
- size_t cap;
- size_t len;
- struct expr *data;
-} exprs;
+struct exprs exprs;
void
dumpval(struct value v)
diff --git a/nooc.h b/nooc.h
@@ -0,0 +1,114 @@
+#define DATA_OFFSET 0x2000
+
+enum tokentype {
+ TOK_NONE = 0,
+ TOK_NAME,
+
+ TOK_LPAREN,
+ TOK_RPAREN,
+
+ TOK_PLUS,
+
+ TOK_COMMA,
+ TOK_EQUAL,
+
+ TOK_NUM,
+ TOK_STRING,
+};
+
+struct slice {
+ char *ptr;
+ size_t len;
+};
+
+struct token {
+ enum tokentype type;
+ struct slice slice;
+ struct token *next;
+};
+
+struct fparams {
+ size_t cap;
+ size_t len;
+ size_t *data;
+};
+
+struct fcall {
+ struct slice s;
+ struct fparams params;
+};
+
+struct decl {
+ struct slice s;
+ size_t val; // struct exprs
+ size_t addr;
+};
+
+struct data {
+ size_t cap;
+ size_t len;
+ char *data;
+};
+
+struct item {
+ enum {
+ ITEM_DECL,
+ ITEM_CALL
+ } kind;
+ union {
+ struct decl decl;
+ struct fcall call;
+ } d;
+};
+
+struct items {
+ size_t cap;
+ size_t len;
+ struct item *data;
+};
+
+struct decls {
+ size_t cap;
+ size_t len;
+ uint64_t *data;
+};
+
+enum primitive {
+ P_INT,
+ P_STR,
+};
+
+enum binop {
+ OP_PLUS,
+};
+
+struct value {
+ enum primitive type;
+ union {
+ uint64_t val;
+ struct slice s;
+ } v;
+};
+
+enum exprkind {
+ EXPR_LIT,
+ EXPR_IDENT,
+ EXPR_BINARY
+};
+
+struct expr {
+ enum exprkind kind;
+ union {
+ struct value v;
+ enum binop op;
+ struct slice s;
+ } d;
+ size_t left;
+ size_t right;
+};
+
+struct exprs {
+ size_t cap;
+ size_t len;
+ struct expr *data;
+};
diff --git a/util.c b/util.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "array.h"
+
+void
+error(char *error)
+{
+ fprintf(stderr, "%s\n", error);
+ exit(1);
+}
diff --git a/util.h b/util.h
@@ -0,0 +1 @@
+void error(char *error);