Skip to content

Commit

Permalink
flamingo: Bring out common.h file
Browse files Browse the repository at this point in the history
  • Loading branch information
obiwac committed Aug 12, 2024
1 parent ce76d90 commit 65051a1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
45 changes: 45 additions & 0 deletions flamingo/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// This Source Form is subject to the terms of the AQUA Software License,
// v. 1.0. Copyright (c) 2024 Aymeric Wibo

#pragma once

#include "flamingo.h"
#include "runtime/tree_sitter/api.h"

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

__attribute__((format(printf, 2, 3))) static int error(flamingo_t* flamingo, char const* fmt, ...) {
va_list args;
va_start(args, fmt);

// TODO validate the size of the program name

// format caller's error message

char formatted[sizeof flamingo->err];
size_t res = vsnprintf(formatted, sizeof formatted, fmt, args);
assert(res < sizeof formatted);

// format the new error message and concatenate to the previous one if there were still errors outstanding
// TODO truncate the number of errors that we show at once (just do this by seeing how much longer we have)

if (flamingo->errors_outstanding) {
size_t const prev_len = strlen(flamingo->err);
res = snprintf(flamingo->err + prev_len, sizeof flamingo->err - prev_len, ", %s:%d:%d: %s", flamingo->progname, 0, 0, formatted);
}

else {
res = snprintf(flamingo->err, sizeof flamingo->err, "%s:%d:%d: %s", flamingo->progname, 0, 0, formatted);
}

assert(res < sizeof flamingo->err);

va_end(args);
flamingo->errors_outstanding = true;

return -1;
}
42 changes: 1 addition & 41 deletions flamingo/flamingo.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@
#include "parser.c"
#include "runtime/lib.c"

#include "flamingo.h"
#include "runtime/tree_sitter/api.h"
#include "common.h"
#include "scope.c"
#include "val.c"

#include <assert.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
TSParser* parser;
TSTree* tree;
Expand All @@ -27,38 +19,6 @@ extern TSLanguage const* tree_sitter_flamingo(void);
static int parse_expr(flamingo_t* flamingo, TSNode node, flamingo_val_t** val);
static int parse_statement(flamingo_t* flamingo, TSNode node);

__attribute__((format(printf, 2, 3))) static int error(flamingo_t* flamingo, char const* fmt, ...) {
va_list args;
va_start(args, fmt);

// TODO validate the size of the program name

// format caller's error message

char formatted[sizeof flamingo->err];
size_t res = vsnprintf(formatted, sizeof formatted, fmt, args);
assert(res < sizeof formatted);

// format the new error message and concatenate to the previous one if there were still errors outstanding
// TODO truncate the number of errors that we show at once (just do this by seeing how much longer we have)

if (flamingo->errors_outstanding) {
size_t const prev_len = strlen(flamingo->err);
res = snprintf(flamingo->err + prev_len, sizeof flamingo->err - prev_len, ", %s:%d:%d: %s", flamingo->progname, 0, 0, formatted);
}

else {
res = snprintf(flamingo->err, sizeof flamingo->err, "%s:%d:%d: %s", flamingo->progname, 0, 0, formatted);
}

assert(res < sizeof flamingo->err);

va_end(args);
flamingo->errors_outstanding = true;

return -1;
}

int flamingo_create(flamingo_t* flamingo, char const* progname, char* src, size_t src_size) {
flamingo->progname = progname;
flamingo->errors_outstanding = false;
Expand Down
2 changes: 1 addition & 1 deletion flamingo/val.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <assert.h>
#include <stdlib.h>

static flamingo_val_t* val_incref(flamingo_val_t* val) {
static flamingo_val_t* val_incref(flamingo_val_t* val) {
assert(val->ref_count > 0); // value has already been freed
val->ref_count++;

Expand Down

0 comments on commit 65051a1

Please sign in to comment.