nooc

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/nooc
Log | Files | Refs | LICENSE

commit 6e5fb6c210e7fe49dba0d27f12d737fed4f2f218
parent 54e7c2410d1228329fce62d50e7e6f3e9745dd60
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Sat,  4 Dec 2021 19:01:04 -0600

add function to compare literals to slices

Diffstat:
Mmain.c | 8++++----
Mutil.c | 14++++++++++++++
Mutil.h | 1+
Mx64.c | 1+
4 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/main.c b/main.c @@ -13,8 +13,8 @@ #include "array.h" #include "x64.h" -#include "util.h" #include "nooc.h" +#include "util.h" #include "elf.h" struct decls decls; @@ -29,11 +29,11 @@ lex(struct slice start) struct token *cur = head; while (start.len) { - if (start.len >= 2 && memcmp(start.ptr, "if", 2) == 0) { + if (slice_cmplit(&start, "if") == 0) { cur->type = TOK_IF; start.ptr += 2; start.len -= 2; - } else if (start.len >= 4 && memcmp(start.ptr, "else", 4) == 0) { + } else if (slice_cmplit(&start, "else") == 0) { cur->type = TOK_ELSE; start.ptr += 4; start.len -= 4; @@ -517,7 +517,7 @@ genblock(char *buf, struct block *block) struct expr expr = exprs.data[item->idx]; // FIXME: 7 should not be hardcoded here if (expr.kind == EXPR_FCALL) { - if (expr.d.call.name.len == 7 && memcmp(exprs.data[item->idx].d.call.name.ptr, "syscall", 7) == 0) { + if (slice_cmplit(&exprs.data[item->idx].d.call.name, "syscall") == 0) { total += gensyscall(buf ? buf + total : NULL, &(exprs.data[item->idx].d.call.params)); } else { error("unknown function!"); diff --git a/util.c b/util.c @@ -1,7 +1,21 @@ #include <stdio.h> +#include <stdint.h> #include <stdlib.h> +#include <string.h> +#include "nooc.h" #include "array.h" +#include "util.h" + +int +slice_cmplit(struct slice *s1, char *s2) +{ + size_t len = strlen(s2); + if (s1->len < len) + return 1; + + return memcmp(s1->ptr, s2, len); +} void error(char *error) diff --git a/util.h b/util.h @@ -1 +1,2 @@ +int slice_cmplit(struct slice *s1, char *s2); void error(char *error); diff --git a/x64.c b/x64.c @@ -3,6 +3,7 @@ #include <stdint.h> #include <string.h> +#include "nooc.h" #include "x64.h" #include "util.h"