summaryrefslogtreecommitdiff
path: root/nqasm.c
diff options
context:
space:
mode:
Diffstat (limited to 'nqasm.c')
-rw-r--r--nqasm.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/nqasm.c b/nqasm.c
index ed49a48..138407e 100644
--- a/nqasm.c
+++ b/nqasm.c
@@ -6,6 +6,32 @@
#include "lexer.h"
#include "parser.h"
+#include "vector.h"
+
+struct label {
+ const char *name;
+ uint16_t addr;
+};
+
+static struct labels {
+ struct vector v;
+ struct label l[];
+} *labels;
+
+uint16_t addr;
+
+static int labels_init(void)
+{
+ return !(labels = vector_init(8, sizeof labels->l[0], sizeof *labels));
+}
+
+static struct label *labels_append(void)
+{
+ struct vector *v = &labels->v;
+ struct label *l = vector_append(&v);
+ labels = (struct labels *)v;
+ return l;
+}
void add_instruction(const struct instruction *inst)
{
@@ -19,10 +45,27 @@ void add_instruction(const struct instruction *inst)
}
printf("%04x\n", bits);
+ addr += 2;
+}
+
+void add_label(const char *name)
+{
+ struct label *l = labels_append();
+ if (!l) {
+ fprintf(stderr, "unable to allocate label\n");
+ exit(1);
+ }
+
+ *l = (struct label) {
+ .name = name,
+ .addr = addr
+ };
}
int main(int argc, char **argv)
{
+ labels_init();
+
yyparse();
return 0;
}