summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-04-12 15:01:45 +0200
committermetamuffin <metamuffin@disroot.org>2025-04-12 15:01:45 +0200
commitde93325a308cd71e251c697b99b8fa2f27f6623c (patch)
tree89e5d9aa59635e545fb11a2388fa022d8a0b6234
parent5008891f9dc1fa194a4db0e8c05772a7749dad39 (diff)
downloadattocc-de93325a308cd71e251c697b99b8fa2f27f6623c.tar
attocc-de93325a308cd71e251c697b99b8fa2f27f6623c.tar.bz2
attocc-de93325a308cd71e251c697b99b8fa2f27f6623c.tar.zst
small refactor
-rw-r--r--attocc.c22
1 files changed, 9 insertions, 13 deletions
diff --git a/attocc.c b/attocc.c
index f2035ef..ae805ce 100644
--- a/attocc.c
+++ b/attocc.c
@@ -16,7 +16,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <fcntl.h>
-#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/cdefs.h>
@@ -440,10 +439,9 @@ struct token token_iter_next(struct token_iter *iter) {
char *op = OPERATORS[i];
if (remaining >= strlen(op)) {
if (strncmp(op, p, strlen(op)) == 0) {
+ p += strlen(op);
tok.kind = TOK_OPERATOR;
tok.data.operator= i;
-
- p += strlen(op);
match = 1;
break;
}
@@ -458,10 +456,9 @@ struct token token_iter_next(struct token_iter *iter) {
char *kw = KEYWORDS[i];
if (remaining >= strlen(kw) + 1) {
if (strncmp(kw, p, strlen(kw)) == 0 && !is_ident(p[strlen(kw)])) {
-
+ p += strlen(kw);
tok.kind = TOK_KEYWORD;
tok.data.keyword = i;
- p += strlen(kw);
match = 1;
break;
}
@@ -492,7 +489,6 @@ struct token token_iter_next(struct token_iter *iter) {
}
}
p--;
-
tok.kind = TOK_CONSTANT;
tok.data.constant_kind = CONST_INT;
tok.data.constant_int_value = value;
@@ -659,11 +655,11 @@ int enum_decl(struct global_context *gcx, struct token_iter *iter,
int value = 0;
while (1) {
if (a->kind == TOK_IDENTIFIER) {
- char *name = strdup(a->data.identifier);
+ char *name = strdup_failsafe(a->data.identifier);
if (token_step(iter, a))
return 1;
- if (a->kind == TOK_OPERATOR && a->kind == OP_ASSIGN) {
+ if (a->kind == TOK_OPERATOR && a->data.operator== OP_ASSIGN) {
if (token_step(iter, a))
return 1;
if (a->kind == TOK_CONSTANT && a->data.constant_kind == CONST_INT) {
@@ -680,7 +676,6 @@ int enum_decl(struct global_context *gcx, struct token_iter *iter,
fprintf(stderr, "expected assign or comma after enum ident\n");
return 1;
}
-
value += 1;
} else if (a->kind == TOK_SEPERATOR && a->data.seperator == SEP_RCURLY) {
@@ -696,6 +691,8 @@ int enum_decl(struct global_context *gcx, struct token_iter *iter,
int toplevel(struct token_iter *iter) {
struct token a = token_iter_next(iter);
+ struct global_context gcx;
+ gcx.num_constants = 0;
if (a.kind == TOK_KEYWORD) {
switch (a.data.keyword) {
@@ -703,14 +700,14 @@ int toplevel(struct token_iter *iter) {
if (token_step(iter, &a))
return 1;
if (a.kind == TOK_IDENTIFIER) {
- char *type_name = strdup(a.data.identifier);
+ char *type_name = strdup_failsafe(a.data.identifier);
if (token_step(iter, &a))
return 1;
}
if (a.kind == TOK_SEPERATOR && a.data.seperator == SEP_LCURLY) {
if (token_step(iter, &a))
return 1;
- if (enum_decl(iter, &a))
+ if (enum_decl(&gcx, iter, &a))
return 1;
}
break;
@@ -740,7 +737,6 @@ void assert_eq(int a, int b, char *label) {
void test() {
printf("sizeof(struct token) = %li\n", sizeof(struct token));
printf("sizeof(struct token_iter) = %li\n", sizeof(struct token_iter));
- printf("sizeof(struct token_buffer) = %li\n", sizeof(struct token_buffer));
assert_eq(strlen("sizeof"), 6, "strlen 1");
assert_eq(strlen(""), 0, "strlen 2");
assert_eq(strlen("a"), 1, "strlen 3");
@@ -771,7 +767,7 @@ int main(int argc, char **argv) {
}
int output_fd = open(output, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0640);
if (output_fd < 0) {
- perror("cannot open input");
+ perror("cannot open output");
return 1;
}