Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cc sema #125

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions sources/kernel/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,9 @@ BrResult syscall_dispatch(BrSyscall syscall, BrArg args)
{
log$("Syscall: Task({}): {}({#p}) -> {}",
task_self()->id,
str$(br_syscall_to_string(syscall)),
str$(br_syscall_to_str(syscall)),
args,
str$(br_result_to_string(result)));
str$(br_result_to_str(result)));
}

task_end_syscall();
Expand Down
4 changes: 2 additions & 2 deletions sources/libs/bal/abi/helpers.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <bal/abi/helpers.h>
#include <brutal-debug/log.h>

char const *br_syscall_to_string(BrSyscall syscall)
char const *br_syscall_to_str(BrSyscall syscall)
{
static char const *SYSCALL_NAMES[] = {
#define ITER(SYSCALL) #SYSCALL,
Expand All @@ -17,7 +17,7 @@ char const *br_syscall_to_string(BrSyscall syscall)
return SYSCALL_NAMES[syscall];
}

char const *br_result_to_string(BrResult result)
char const *br_result_to_str(BrResult result)
{
static char const *RESULT_NAMES[] = {
#define ITER(RESULT) #RESULT,
Expand Down
4 changes: 2 additions & 2 deletions sources/libs/bal/abi/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include <bal/abi/types.h>

char const *br_syscall_to_string(BrSyscall syscall);
char const *br_syscall_to_str(BrSyscall syscall);

char const *br_result_to_string(BrResult result);
char const *br_result_to_str(BrResult result);

Error br_result_to_error(BrResult result);

Expand Down
28 changes: 17 additions & 11 deletions sources/libs/brutal-base/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,38 @@

typedef struct
{
void const *buf;
size_t len;

size_t size;
void const *buf;
} SliceImpl;

#define slice_impl$(SLICE) \
(SliceImpl) { .len = (SLICE).len, .size = sizeof(*(SLICE).buf), .buf = (SLICE).buf, }

#define InlineBuf(T) \
struct \
{ \
size_t len; \
T buf[]; \
}

#define Slice(T) \
struct \
{ \
size_t len; \
T const *buf; \
size_t len; \
}

typedef Slice(void) VoidSlice;

#define slice$(type, buffer, size) \
(type) { .len = size, .buf = buffer }
(type) { .buf = buffer, .len = size }

#define slice_array$(type, buffer) \
(type) { .len = sizeof(buffer) / sizeof(*buffer), .buf = &(buffer) }
(type) { .buf = (buffer), .len = sizeof(buffer) / sizeof(*buffer), }

#define slice_begin$(SLICE) ((SLICE).buf)

#define slice_end$(SLICE) ((SLICE).buf + (SLICE).len)

#define slice_foreach$(VAR, SELF) \
if ((SELF).len) \
for (typeof((SELF).buf) VAR = slice_begin$(SELF); VAR != slice_end$(SELF); VAR++)

#define slice_foreach_rev$(VAR, SELF) \
if ((SELF).len) \
for (typeof((SELF).buf) VAR = slice_end$(SELF) - 1; VAR >= slice_begin$(SELF); VAR--)
16 changes: 8 additions & 8 deletions sources/libs/brutal-debug/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
} \
})

#define assert_br_success(EXPR) ( \
{ \
BrResult __value = (EXPR); \
\
if (UNLIKELY(__value != BR_SUCCESS)) \
{ \
panic$("{} is not equal to BR_SUCCESS (but is equal to: '{}') ", #EXPR, br_result_to_string(__value)); \
} \
#define assert_br_success(EXPR) ( \
{ \
BrResult __value = (EXPR); \
\
if (UNLIKELY(__value != BR_SUCCESS)) \
{ \
panic$("{} is not equal to BR_SUCCESS (but is equal to: '{}') ", #EXPR, br_result_to_str(__value)); \
} \
})

#define assert_falsity(EXPR) ( \
Expand Down
4 changes: 4 additions & 0 deletions sources/libs/brutal-ds/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ void vec_swap_impl(VecImpl *impl, int idx1, int idx2);
if ((SELF)->len) \
for (typeof((SELF)->data) VAR = vec_begin(SELF); VAR != vec_end(SELF); VAR++)

#define vec_foreach_rev(VAR, SELF) \
if ((SELF)->len) \
for (typeof((SELF)->data) VAR = vec_end(SELF) - 1; VAR >= vec_begin(SELF); VAR--)

#define vec_foreach_idx(IDX, VAR, SELF) \
if ((SELF)->len) \
for (int IDX = 0; IDX < (SELF)->len; IDX++) \
Expand Down
1 change: 0 additions & 1 deletion sources/libs/brutal-fmt/fmt.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <brutal-io>
#include <brutal-parse>
#include <brutal-text>

#include "case.h"
Expand Down
2 changes: 2 additions & 0 deletions sources/libs/brutal-fmt/funcs.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <brutal-base>

#include "fmt.h"

IoResult fmt_format(IoWriter writer, Str format, AnyVa args);
Expand Down
37 changes: 35 additions & 2 deletions sources/libs/brutal-parse/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,29 @@
#include "lex.h"

Lex lex(Scan *scan, LexFn *fn, Alloc *alloc)
{
return lex_tu(scan, fn, alloc, -1);
}

Lex lex_tu(Scan *scan, LexFn *fn, Alloc *alloc, int translation_unit)
{
Lex self = {};
vec_init(&self.lexemes, alloc);

while (!scan_ended(scan))
{
scan_begin(scan);

Lexeme l = {fn(scan), scan_end(scan)};
int begin = scan->head;

Lexeme l = {
.type = fn(scan),
.str = scan_end(scan),
(SrcRef){
.translation_unit = translation_unit,
.begin = begin,
.end = scan->head,
},
};

if (l.type == LEXEME_INVALID)
{
Expand Down Expand Up @@ -50,6 +64,11 @@ Lexeme lex_peek(Lex *self, int offset)
return (Lexeme){
LEXEME_EOF,
str$(""),
(SrcRef){
.begin = self->head + offset,
.end = self->head + offset,
.translation_unit = -1,
},
};
}

Expand Down Expand Up @@ -146,3 +165,17 @@ bool lex_ok(Lex *self)
{
return !self->has_error;
}

SrcRef lex_src_ref(Lex *lex, int begin, int end)
{
Lexeme first = lex->lexemes.data[begin];
Lexeme last = lex->lexemes.data[end];

SrcRef cref = {
.begin = first.pos.begin,
.end = last.pos.end,
.translation_unit = first.pos.translation_unit,
};

return cref;
}
4 changes: 4 additions & 0 deletions sources/libs/brutal-parse/lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ typedef Str LexToStrFn(LexemeType type);

Lex lex(Scan *scan, LexFn *fn, Alloc *alloc);

Lex lex_tu(Scan *scan, LexFn *fn, Alloc *alloc, int translation_unit);

Lex lex_from_lexeme(Lexemes *from);

void lex_deinit(Lex *self);
Expand Down Expand Up @@ -65,3 +67,5 @@ void lex_throw(Lex *self, Str message);
bool lex_expect(Lex *self, LexemeType type);

bool lex_ok(Lex *self);

SrcRef lex_src_ref(Lex *lex, int begin, int end);
2 changes: 2 additions & 0 deletions sources/libs/brutal-parse/lexeme.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <brutal-text>

#include "ref.h"
#define LEXEME_EOF (-1)
#define LEXEME_INVALID (-2)

Expand All @@ -11,6 +12,7 @@ typedef struct
{
LexemeType type;
Str str;
SrcRef pos;
} Lexeme;

#define lexeme$(TYPE, STR) \
Expand Down
12 changes: 6 additions & 6 deletions sources/libs/brutal-parse/nums.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,26 +146,26 @@ bool scan_next_double(Scan *self, double *value)

#endif

bool str_to_uint(Str string, unsigned long *value)
bool str_to_uint(Str str, unsigned long *value)
{
Scan scan = {0};
scan_init(&scan, string);
scan_init(&scan, str);
return scan_next_uint(&scan, value);
}

bool str_to_int(Str string, long *value)
bool str_to_int(Str str, long *value)
{
Scan scan = {0};
scan_init(&scan, string);
scan_init(&scan, str);
return scan_next_int(&scan, value);
}

#ifndef __freestanding__

bool str_to_float(Str string, double *value)
bool str_to_float(Str str, double *value)
{
Scan scan = {0};
scan_init(&scan, string);
scan_init(&scan, str);
return scan_next_double(&scan, value);
}

Expand Down
6 changes: 3 additions & 3 deletions sources/libs/brutal-parse/nums.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ bool scan_next_double(Scan *self, double *value);

#endif

bool str_to_uint(Str string, unsigned long *value);
bool str_to_uint(Str str, unsigned long *value);

bool str_to_int(Str string, long *value);
bool str_to_int(Str str, long *value);

#ifndef __freestanding__

bool str_to_float(Str string, double *value);
bool str_to_float(Str str, double *value);

#endif
37 changes: 37 additions & 0 deletions sources/libs/brutal-parse/ref.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "ref.h"

void scan_srcref_info(SrcRef ref, SrcFilePosInfo *info, Str const src)
{
int begin = ref.begin;
int end = ref.end;
int line_len;

int line_number_start = str_count_chr(str_sub(src, 0, begin), '\n') + 1;
int line_number_end = str_count_chr(str_sub(src, 0, end), '\n') + 1;
int line_start = str_last_chr(str_sub(src, 0, begin), '\n') + 1;
int line_end = str_first_chr(str_sub(src, end, src.len), '\n') + end;
bool is_multiline = line_number_start != line_number_end;

if (!is_multiline)
{
line_len = str_first_chr(str_sub(src, line_start, src.len), '\n');
}
else
{
line_len = str_last_chr(str_sub(src, line_start, line_end + 1), '\n');
}

if (line_len == -1)
{
line_len = src.len;
}

*info = (SrcFilePosInfo){
.is_multiline = is_multiline,
.char_line_len = line_len,
.char_line_end = line_end,
.char_line_start = line_start,
.line_number_end = line_number_end,
.line_number_start = line_number_start,
};
}
23 changes: 23 additions & 0 deletions sources/libs/brutal-parse/ref.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <brutal-text>
#include <stdint.h>

typedef struct
{
int translation_unit;
int begin;
int end;
} SrcRef;

typedef struct
{
int line_number_start; // the number of the line at the start
int line_number_end; // the number of the line at the end
int char_line_start; // the starting character index aligned by a line
int char_line_end; // the last character index aligned by a line
int char_line_len; // the length of the line(s)
bool is_multiline; // if the range is represented by multiple or a single line
} SrcFilePosInfo;

void scan_srcref_info(SrcRef ref, SrcFilePosInfo *info, Str const src);
Loading