diff --git a/compiler.c b/compiler.c index 2c9e67d..3f768e6 100644 --- a/compiler.c +++ b/compiler.c @@ -2,6 +2,7 @@ #include "common.h" #include "emit.h" #include "lexer.h" +#include "list.h" Emitter emitter = {0}; Parser parser = {0}; @@ -289,6 +290,30 @@ void declaration(); ParseRule *get_rule(TokenType type); void parse_precedence(Precedence precedence); + +void +calculate_strides(i32 *dims, i32 num_dims, i32 *out_strides) +{ + i32 i; + i32 current_stride = 1; + + for(i = num_dims - 1; i >= 0; i--) { + out_strides[i] = current_stride; + current_stride *= dims[i]; + } +} + +void +define_array() +{ + /* example + i32 dims[3] = {3, 3, 3}; + i32 strides[3]; + + calculate_strides(dims, 3, strides); + */ +} + u32 variable_declaration(Symbol *def) { @@ -296,18 +321,18 @@ variable_declaration(Symbol *def) TokenType tt = parser.previous.type; SymbolType st = token_type_to_sym_type(tt); Symbol *variable; + bool is_array = false; + i32 current_stride = 1; + List *strides; if(parser.current.type == TOKEN_LBRACE) { is_array = true; + strides = List_init(arena); while(!check(TOKEN_RBRACKET)) { - if(is_type()) { - /* consume dimension if exists */ - - } else if (check(TOKEN_LBRACKET)) { - /* it is a unbounded array */ - + if(parser.previous.type == TOKEN_LITERAL_NAT) { + /* consume dimension if exists */ } advance(); if(check(TOKEN_LBRACKET)) break; @@ -697,29 +722,6 @@ grouping() consume(TOKEN_RPAREN); } -void -calculate_strides(i32 *dims, i32 num_dims, i32 *out_strides) -{ - i32 i; - i32 current_stride = 1; - - for(i = num_dims - 1; i >= 0; i--) { - out_strides[i] = current_stride; - current_stride *= dims[i]; - } -} - -void -define_array() -{ - /* example - i32 dims[3] = {3, 3, 3}; - i32 strides[3]; - - calculate_strides(dims, 3, strides); - */ -} - void number() { diff --git a/compiler.h b/compiler.h index 6c9e7d7..4c888e9 100644 --- a/compiler.h +++ b/compiler.h @@ -19,15 +19,15 @@ struct scope_s { typedef enum { PREC_NONE, PREC_ASSIGNMENT, /* = */ - PREC_OR, /* or */ - PREC_AND, /* and */ + PREC_OR, /* or */ + PREC_AND, /* and */ PREC_EQUALITY, /* == != */ PREC_COMPARISON, /* < > <= >= */ - PREC_TERM, /* + - */ - PREC_FACTOR, /* * / */ - PREC_UNARY, /* ! - */ - PREC_CAST, /* as */ - PREC_CALL, /* . () */ + PREC_TERM, /* + - */ + PREC_FACTOR, /* * / */ + PREC_UNARY, /* ! - */ + PREC_CAST, /* as */ + PREC_CALL, /* . () */ PREC_PRIMARY } Precedence; diff --git a/libc.h b/libc.h index 47cdf3b..e5a13b2 100644 --- a/libc.h +++ b/libc.h @@ -94,18 +94,24 @@ u32 afree(Arena *arena); char *ascpy(Arena *arena, const char *start, u32 length); void *amcpy(Arena *arena, void *from, u32 length); -#define ARENA_RETURN(arena, ckpt, src_ptr, type) \ +#define ARENA_RETURN(arena, ckpt, src_ptr, type) \ return (type *)areturn(arena, (ckpt), (src_ptr), sizeof(type)) -#define ARENA_RETURN_ARRAY(arena, ckpt, src_ptr, type, count) \ +#define ARENA_RETURN_ARRAY(arena, ckpt, src_ptr, type, count) \ return (type *)areturn(arena, (ckpt), (src_ptr), sizeof(type) * (count)) -#define ARENA_RETURN_STR(arena, ckpt, src_ptr) \ +#define ARENA_RETURN_STR(arena, ckpt, src_ptr) \ return (char *)areturn(arena, (ckpt), (src_ptr), slen(src_ptr)) -#define ARENA_RETURN_STRBUF(arena, ckpt, sbuf) \ - char *to_return = StrBuf_toS(arena, sbuf); \ - return (char *)areturn(arena, (ckpt), (to_return), slen(to_return)) +#define ARENA_RETURN_STRBUF(arena, ckpt, sbuf) \ + u32 __UNDAR__to_str_length = 0; \ + char *__UNDAR__to_return = StrBuf_toS(arena, sbuf, &__UNDAR__to_str_length); \ + return (char *)areturn(arena, (ckpt), (__UNDAR__to_return), __UNDAR__to_str_length) + +#define ARENA_RETURN_LIST(arena, ckpt, list) \ + u32 __UNDAR__list_collapse_length = 0; \ + void *__UNDAR__to_return = List_collapse(arena, list, &__UNDAR__list_collapse_length); \ + return areturn(arena, (ckpt), (__UNDAR__to_return), __UNDAR__list_collapse_length) r32 int_to_real(i32 i); i32 real_to_int(r32 f); diff --git a/list.c b/list.c index b6688df..63b9857 100644 --- a/list.c +++ b/list.c @@ -102,3 +102,25 @@ List_last(List *list) { return node_value(list->tail); } + + +void * +List_collapse(Arena *a, List *list, u32 *out_size) +{ + Node *curr; + void *tmp; + + void *ptr = aend(a); + + if (!list || !ptr) return nil; + + curr = list->head; + while (curr) { + tmp = node_value(curr); + amcpy(a, tmp, curr->size); + if (out_size) *out_size += curr->size; + curr = curr->next; + } + + return ptr; +} diff --git a/list.h b/list.h index 4dbd69c..37e2d6a 100644 --- a/list.h +++ b/list.h @@ -27,5 +27,6 @@ void *List_find(List *list, compare_fn compare, void *target); void *List_get(List *list, u32 index); void List_set(List *list, u32 index, void *data, u32 size); void *List_last(List *list); +void *List_collapse(Arena *a, List *list, u32 *out_size); #endif diff --git a/strbuf.c b/strbuf.c index 363f667..f50f8a8 100644 --- a/strbuf.c +++ b/strbuf.c @@ -45,7 +45,7 @@ StrBuf_append(Arena *a, StrBuf *buf, char *str) } char * -StrBuf_toS(Arena *a, StrBuf *buf) +StrBuf_toS(Arena *a, StrBuf *buf, u32 *out_length) { Node *curr; char *tmp_str; @@ -58,6 +58,7 @@ StrBuf_toS(Arena *a, StrBuf *buf) while (curr) { tmp_str = node_value(curr); amcpy(a, tmp_str, curr->size); + if (out_length) *out_length += curr->size; curr = curr->next; } diff --git a/strbuf.h b/strbuf.h index 8405cd5..5dc6933 100644 --- a/strbuf.h +++ b/strbuf.h @@ -12,6 +12,6 @@ struct strbuf_s { StrBuf *StrBuf_init(Arena *a); void *StrBuf_append(Arena *a, StrBuf *buf, char *str); -char *StrBuf_toS(Arena *a, StrBuf *buf); +char *StrBuf_toS(Arena *a, StrBuf *buf, u32 *out_length); #endif \ No newline at end of file