Clang format
This commit is contained in:
42
base.cpp
42
base.cpp
@@ -120,11 +120,20 @@ typedef double F64;
|
||||
#define api
|
||||
#define CORE_Static static
|
||||
#define global static
|
||||
#define assert(x) do{if(!(x))Breakpoint;}while(0)
|
||||
#define assert(x) \
|
||||
do { \
|
||||
if (!(x)) Breakpoint; \
|
||||
} while (0)
|
||||
#define assert_message(x, ...) assert(x)
|
||||
#define invalid_codepath assert_message(0, "Invalid codepath")
|
||||
#define invalid_return do{assert_message(0, "Invalid codepath"); return {};}while(0)
|
||||
#define invalid_default_case default: invalid_codepath
|
||||
#define invalid_return \
|
||||
do { \
|
||||
assert_message(0, "Invalid codepath"); \
|
||||
return {}; \
|
||||
} while (0)
|
||||
#define invalid_default_case \
|
||||
default: \
|
||||
invalid_codepath
|
||||
#define not_implemented assert_message(0, "Not implemented")
|
||||
#define unused(x) ((void)x)
|
||||
#define buff_cap(x) (sizeof(x) / sizeof((x)[0]))
|
||||
@@ -147,7 +156,10 @@ global String string_null = {(U8 *)"null", 4};
|
||||
|
||||
union Intern_String { // Basically just String
|
||||
String s;
|
||||
struct{ U8 *str; S64 len; };
|
||||
struct {
|
||||
U8 *str;
|
||||
S64 len;
|
||||
};
|
||||
};
|
||||
|
||||
struct Allocator {
|
||||
@@ -176,7 +188,6 @@ CORE_Static void deallocate(Allocator *allocator, void *p) {
|
||||
allocator->deallocate(allocator, p);
|
||||
}
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Utilities
|
||||
//-----------------------------------------------------------------------------
|
||||
@@ -330,7 +341,8 @@ operator!=(Intern_String a, Intern_String b){
|
||||
do { \
|
||||
if ((f) == 0) { \
|
||||
(f) = (l) = (n); \
|
||||
} else { \
|
||||
} \
|
||||
else { \
|
||||
(l) = (l)->next = (n); \
|
||||
} \
|
||||
} while (0)
|
||||
@@ -340,7 +352,8 @@ operator!=(Intern_String a, Intern_String b){
|
||||
do { \
|
||||
if ((f) == (l)) { \
|
||||
(f) = (l) = 0; \
|
||||
} else { \
|
||||
} \
|
||||
else { \
|
||||
(f) = (f)->next; \
|
||||
} \
|
||||
} while (0)
|
||||
@@ -368,7 +381,8 @@ operator!=(Intern_String a, Intern_String b){
|
||||
(f) = (l) = (node); \
|
||||
(node)->prev = 0; \
|
||||
(node)->next = 0; \
|
||||
} else { \
|
||||
} \
|
||||
else { \
|
||||
(l)->next = (node); \
|
||||
(node)->prev = (l); \
|
||||
(node)->next = 0; \
|
||||
@@ -382,13 +396,16 @@ operator!=(Intern_String a, Intern_String b){
|
||||
if ((first) == (last)) { \
|
||||
assert_message((node) == (first), "Macro assert failed"); \
|
||||
(first) = (last) = 0; \
|
||||
} else if ((last) == (node)) { \
|
||||
} \
|
||||
else if ((last) == (node)) { \
|
||||
(last) = (last)->prev; \
|
||||
(last)->next = 0; \
|
||||
} else if ((first) == (node)) { \
|
||||
} \
|
||||
else if ((first) == (node)) { \
|
||||
(first) = (first)->next; \
|
||||
(first)->prev = 0; \
|
||||
} else { \
|
||||
} \
|
||||
else { \
|
||||
(node)->prev->next = (node)->next; \
|
||||
(node)->next->prev = (node)->prev; \
|
||||
} \
|
||||
@@ -410,7 +427,8 @@ operator!=(Intern_String a, Intern_String b){
|
||||
(first) = (first)->next; \
|
||||
if ((first)) \
|
||||
(first)->prev = 0; \
|
||||
} else { \
|
||||
} \
|
||||
else { \
|
||||
(node)->prev->next = (node)->next; \
|
||||
if ((node)->next) \
|
||||
(node)->next->prev = (node)->prev; \
|
||||
|
||||
@@ -124,6 +124,9 @@ arena_from_buffer(void *buffer, size_t size) {
|
||||
struct Scratch_Scope {
|
||||
Arena *arena;
|
||||
int pos;
|
||||
Scratch_Scope(Arena *arena) { this->arena = arena; this->pos = arena->len; }
|
||||
Scratch_Scope(Arena *arena) {
|
||||
this->arena = arena;
|
||||
this->pos = arena->len;
|
||||
}
|
||||
~Scratch_Scope() { this->arena->len = this->pos; }
|
||||
};
|
||||
|
||||
@@ -101,10 +101,12 @@ struct Array{
|
||||
force_inline T *last() { return data + len - 1; }
|
||||
force_inline T *begin() { return data; }
|
||||
force_inline T *end() { return data + len; }
|
||||
force_inline T &operator[](S64 i){ assert(i >= 0 && i < cap); return data[i]; }
|
||||
force_inline T &operator[](S64 i) {
|
||||
assert(i >= 0 && i < cap);
|
||||
return data[i];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <class T>
|
||||
CORE_Static Array<T>
|
||||
array_make(Allocator *a, S64 size = 16) {
|
||||
@@ -276,7 +278,9 @@ CORE_Static Intern_String
|
||||
U8 *slot = (U8 *)map_get(&t->map, hash);
|
||||
if (slot) {
|
||||
// @todo: Is this a cast bug: *(slot-sizeof(S64))? slot is u8 so truncates?
|
||||
Intern_String result = {{slot, *(slot-sizeof(S64))}};
|
||||
Intern_String result = {
|
||||
{slot, *(slot - sizeof(S64))}
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -288,7 +292,9 @@ CORE_Static Intern_String
|
||||
string_address[string.len] = 0;
|
||||
|
||||
map_insert(&t->map, hash, string_address);
|
||||
Intern_String result = {{string_address, *len_address}};
|
||||
Intern_String result = {
|
||||
{string_address, *len_address}
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -492,7 +498,8 @@ void free_all_nodes(List<T> *list){
|
||||
|
||||
template <class T>
|
||||
T ordered_remove(List<T> *list, int index) {
|
||||
List_Node<T> *node; int in_block_index;
|
||||
List_Node<T> *node;
|
||||
int in_block_index;
|
||||
T *data = list_get(list, index, &node, &in_block_index);
|
||||
|
||||
assert_message(data, "Trying to unordered_remove element that's outside of the List");
|
||||
|
||||
@@ -146,7 +146,8 @@ struct String_Builder{
|
||||
if (first_free) {
|
||||
block = first_free;
|
||||
first_free = first_free->next;
|
||||
} else{
|
||||
}
|
||||
else {
|
||||
block = (String_Builder_Block *)allocate_size(allocator, sizeof(String_Builder_Block) + size, false);
|
||||
}
|
||||
memory_zero(block, sizeof(String_Builder_Block) + 1); // Also clear first byte of character data
|
||||
@@ -307,7 +308,6 @@ CORE_Static String
|
||||
string_trim(String string) {
|
||||
if (string.len == 0) return string;
|
||||
|
||||
|
||||
S64 whitespace_begin = 0;
|
||||
for (; whitespace_begin < string.len; whitespace_begin++) {
|
||||
if (!is_whitespace(string.str[whitespace_begin])) {
|
||||
|
||||
925
c3_big_int.cpp
925
c3_big_int.cpp
File diff suppressed because it is too large
Load Diff
@@ -1,8 +1,7 @@
|
||||
struct Token;
|
||||
|
||||
#include <inttypes.h>
|
||||
enum CmpRes
|
||||
{
|
||||
enum CmpRes {
|
||||
CMP_LT,
|
||||
CMP_GT,
|
||||
CMP_EQ,
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
|
||||
#define gen(...) pctx->gen.addf(__VA_ARGS__)
|
||||
#define genln(...) do{gen("\n"); gen_indent(); gen(__VA_ARGS__); }while(0)
|
||||
#define genln(...) \
|
||||
do { \
|
||||
gen("\n"); \
|
||||
gen_indent(); \
|
||||
gen(__VA_ARGS__); \
|
||||
} while (0)
|
||||
global S32 global_indent;
|
||||
global S32 is_inside_struct;
|
||||
|
||||
@@ -142,8 +147,11 @@ string_simple_decl_postfix(Allocator *a, Ast_Type *ast){
|
||||
return string;
|
||||
} break;
|
||||
case TYPE_LAMBDA: break;
|
||||
case TYPE_SLICE: case TYPE_ENUM: case TYPE_STRUCT:break;
|
||||
default:break;
|
||||
case TYPE_SLICE:
|
||||
case TYPE_ENUM:
|
||||
case TYPE_STRUCT: break;
|
||||
default:
|
||||
break;
|
||||
// default: return string_from_cstring((char *)name(ast));
|
||||
}
|
||||
return {};
|
||||
@@ -189,9 +197,15 @@ get_type_postfix(Ast_Type *type){
|
||||
case TYPE_F32: return "f"_s; break;
|
||||
case TYPE_U64: return "ULL"_s; break;
|
||||
case TYPE_S64: return "LL"_s; break;
|
||||
case TYPE_F64:case TYPE_S8:case TYPE_S16:
|
||||
case TYPE_S32:case TYPE_U8:case TYPE_U16:
|
||||
case TYPE_INT:case TYPE_U32:case TYPE_CHAR:
|
||||
case TYPE_F64:
|
||||
case TYPE_S8:
|
||||
case TYPE_S16:
|
||||
case TYPE_S32:
|
||||
case TYPE_U8:
|
||||
case TYPE_U16:
|
||||
case TYPE_INT:
|
||||
case TYPE_U32:
|
||||
case TYPE_CHAR:
|
||||
return ""_s;
|
||||
break;
|
||||
invalid_default_case;
|
||||
@@ -243,10 +257,18 @@ gen_value(Token *pos, Value a){
|
||||
int length = 0;
|
||||
gen("(%QString){(uint8_t *)\"", pctx->symbol_prefix);
|
||||
for (int i = 0; i < a.intern_val.len; i++) {
|
||||
if(a.intern_val.str[i] == '\n'){length += 2; gen("\\n");}
|
||||
else if(a.intern_val.str[i] == '\r'){length += 2; gen("\\r");}
|
||||
else{length += 1; gen("%c", a.intern_val.str[i]);}
|
||||
|
||||
if (a.intern_val.str[i] == '\n') {
|
||||
length += 2;
|
||||
gen("\\n");
|
||||
}
|
||||
else if (a.intern_val.str[i] == '\r') {
|
||||
length += 2;
|
||||
gen("\\r");
|
||||
}
|
||||
else {
|
||||
length += 1;
|
||||
gen("%c", a.intern_val.str[i]);
|
||||
}
|
||||
}
|
||||
gen("\", %d}", length);
|
||||
}
|
||||
@@ -327,10 +349,12 @@ gen_var(Ast_Decl *decl, B32 emit_value, B32 scope_names){
|
||||
if (decl->expr) {
|
||||
gen(" = ");
|
||||
gen_try_any_or_slice(decl->expr, decl->type);
|
||||
} else { // Default zero
|
||||
}
|
||||
else { // Default zero
|
||||
if (is_numeric(decl->type)) {
|
||||
gen(" = 0");
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
gen(" = {}");
|
||||
}
|
||||
}
|
||||
@@ -368,7 +392,6 @@ gen_expr(Ast_Expr *ast){
|
||||
gen("%Q", node->intern_val);
|
||||
}
|
||||
|
||||
|
||||
BREAK();
|
||||
}
|
||||
|
||||
@@ -442,7 +465,6 @@ gen_expr(Ast_Expr *ast){
|
||||
if (!token_is_assign(node->op)) gen(")");
|
||||
}
|
||||
|
||||
|
||||
BREAK();
|
||||
}
|
||||
|
||||
@@ -636,14 +658,12 @@ gen_ast(Ast *ast){
|
||||
BREAK();
|
||||
}
|
||||
|
||||
|
||||
CASE(BREAK, Break) {
|
||||
unused(node);
|
||||
gen("break;");
|
||||
BREAK();
|
||||
}
|
||||
|
||||
|
||||
CASE(PASS, Pass) {
|
||||
unused(node);
|
||||
gen("//pass");
|
||||
@@ -665,13 +685,13 @@ gen_ast(Ast *ast){
|
||||
gen("%QBufferSize(", pctx->symbol_prefix);
|
||||
gen_expr(node->cond);
|
||||
gen(")");
|
||||
} else{
|
||||
}
|
||||
else {
|
||||
assert(is_slice(node->cond->resolved_type));
|
||||
gen_expr(node->cond);
|
||||
gen(".len");
|
||||
}
|
||||
|
||||
|
||||
gen("; _i%d+=1)", node->pos->line);
|
||||
gen("{");
|
||||
global_indent++;
|
||||
@@ -749,7 +769,10 @@ gen_ast(Ast *ast){
|
||||
}
|
||||
|
||||
case AST_TYPE:
|
||||
CASE(CONST, Decl){unused(node);BREAK();}
|
||||
CASE(CONST, Decl) {
|
||||
unused(node);
|
||||
BREAK();
|
||||
}
|
||||
|
||||
CASE(SWITCH, Switch) {
|
||||
gen("switch(");
|
||||
@@ -835,7 +858,8 @@ compile_to_c_code(){
|
||||
You can #define %QAssertMessage(x) to get more comprehensive error info
|
||||
You can #define %QMemoryCopy(x) to avoid using default memory copy
|
||||
*/
|
||||
)", pctx->single_header_library_name, pctx->single_header_library_name, pctx->single_header_library_name,
|
||||
)",
|
||||
pctx->single_header_library_name, pctx->single_header_library_name, pctx->single_header_library_name,
|
||||
pctx->symbol_prefix, pctx->symbol_prefix, pctx->symbol_prefix);
|
||||
genln("#ifndef %Q_LIBRARY_HEADER ", pctx->single_header_library_name);
|
||||
genln("#define %Q_LIBRARY_HEADER ", pctx->single_header_library_name);
|
||||
@@ -939,7 +963,6 @@ compile_to_c_code(){
|
||||
compiler_error(0, "Entry point is not defined! Try main or WinMain");
|
||||
}
|
||||
|
||||
|
||||
if (pctx->emit_type_info) {
|
||||
// Generate language.core
|
||||
for (S32 i = 0; i < pctx->base_language_ordered_decl_len; i++) {
|
||||
@@ -978,18 +1001,17 @@ compile_to_c_code(){
|
||||
gen(".struct_members = (Type_Info_Struct_Member[]){");
|
||||
For_Named(t->agg.members, m) {
|
||||
gen("{.name = (%Q){(uint8_t *)\"%Q\", %d}, .type = %d, .offset = %d}, ", prefixed_string_type, m.name, m.name.len, m.type->type_id, m.offset);
|
||||
|
||||
}
|
||||
gen("}");
|
||||
} break;
|
||||
default: {}
|
||||
default: {
|
||||
}
|
||||
// invalid_default_case;
|
||||
}
|
||||
gen("},");
|
||||
}
|
||||
global_indent--;
|
||||
gen("};");
|
||||
|
||||
}
|
||||
|
||||
if (pctx->single_header_library_mode) {
|
||||
@@ -1007,12 +1029,10 @@ compile_to_c_code(){
|
||||
index += 1;
|
||||
}
|
||||
|
||||
|
||||
if (pctx->single_header_library_mode) {
|
||||
genln("#endif");
|
||||
}
|
||||
|
||||
|
||||
String string_result = string_flatten(pctx->perm, &pctx->gen);
|
||||
pctx->time.code_generation = os_time() - pctx->time.code_generation;
|
||||
return string_result;
|
||||
|
||||
@@ -48,13 +48,15 @@ get_operator_info(Token_Kind op){
|
||||
case TK_Or: return pctx->op_info_table + 17;
|
||||
case TK_Neg: return pctx->op_info_table + 18;
|
||||
case TK_Not: return pctx->op_info_table + 19;
|
||||
default: {}
|
||||
default: {
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
CORE_Static Ast_Operator_Info *
|
||||
get_operator_info(Intern_String op) {
|
||||
if(0){}
|
||||
if (0) {
|
||||
}
|
||||
else if (pctx->op_info_table[0].op.str == op.str) return pctx->op_info_table + 0;
|
||||
else if (pctx->op_info_table[1].op.str == op.str) return pctx->op_info_table + 1;
|
||||
else if (pctx->op_info_table[2].op.str == op.str) return pctx->op_info_table + 2;
|
||||
|
||||
@@ -5,6 +5,3 @@ global S64 bigint_allocation_count;
|
||||
|
||||
const uintptr_t pointer_size = sizeof(uintptr_t);
|
||||
const uintptr_t pointer_align = __alignof(uintptr_t);
|
||||
|
||||
|
||||
|
||||
|
||||
144
core_lexing.cpp
144
core_lexing.cpp
@@ -157,7 +157,8 @@ lex_parse_ident(Intern_Table *table, Lex_Stream *s, Token *t){
|
||||
if (lexc(s) == '=') { \
|
||||
lex_advance(s); \
|
||||
t.kind = Assign; \
|
||||
} else { \
|
||||
} \
|
||||
else { \
|
||||
t.kind = OpName; \
|
||||
} \
|
||||
break
|
||||
@@ -166,10 +167,12 @@ lex_parse_ident(Intern_Table *table, Lex_Stream *s, Token *t){
|
||||
if (lexc(s) == '=') { \
|
||||
lex_advance(s); \
|
||||
t.kind = Assign; \
|
||||
} else if (lexc(s) == op) { \
|
||||
} \
|
||||
else if (lexc(s) == op) { \
|
||||
lex_advance(s); \
|
||||
t.kind = Incr; \
|
||||
} else { \
|
||||
} \
|
||||
else { \
|
||||
t.kind = OpName; \
|
||||
} \
|
||||
break
|
||||
@@ -272,11 +275,16 @@ lex__stream(Core_Ctx *lexer){
|
||||
for (;;) {
|
||||
switch (lexc(s)) {
|
||||
case 0: goto end_of_stream; break;
|
||||
case '\t': case ' ': lex_advance(s); t.indent++; break;
|
||||
case '\t':
|
||||
case ' ':
|
||||
lex_advance(s);
|
||||
t.indent++;
|
||||
break;
|
||||
case '\r': lex_advance(s); break;
|
||||
case '/': {
|
||||
if (lexci(s, 1) == '/') {
|
||||
lex_advance(s); lex_advance(s);
|
||||
lex_advance(s);
|
||||
lex_advance(s);
|
||||
t.kind = TK_Comment;
|
||||
for (;;) {
|
||||
if (lexc(s) == '\n' || lexc(s) == 0) break;
|
||||
@@ -284,11 +292,13 @@ lex__stream(Core_Ctx *lexer){
|
||||
}
|
||||
}
|
||||
else if (lexci(s, 1) == '*') {
|
||||
lex_advance(s); lex_advance(s);
|
||||
lex_advance(s);
|
||||
lex_advance(s);
|
||||
t.kind = TK_Comment;
|
||||
for (;;) {
|
||||
if (lexc(s) == '*' && lexci(s, 1) == '/') {
|
||||
lex_advance(s); lex_advance(s);
|
||||
lex_advance(s);
|
||||
lex_advance(s);
|
||||
break;
|
||||
}
|
||||
else if (lexc(s) == 0) {
|
||||
@@ -317,7 +327,8 @@ lex__stream(Core_Ctx *lexer){
|
||||
semi.indent = last->indent + 2; // @todo: proper detection of indentation
|
||||
lex_add_token(lexer, &semi);
|
||||
s->indent_stack.add(lexer->tokens.last());
|
||||
} else{
|
||||
}
|
||||
else {
|
||||
semi.kind = SAME_SCOPE;
|
||||
lex_add_token(lexer, &semi);
|
||||
}
|
||||
@@ -351,7 +362,8 @@ lex__stream(Core_Ctx *lexer){
|
||||
goto indent_loop_break;
|
||||
}
|
||||
}
|
||||
} indent_loop_break:
|
||||
}
|
||||
indent_loop_break:
|
||||
beginning = false;
|
||||
|
||||
// @note: handle the indented token
|
||||
@@ -360,16 +372,36 @@ lex__stream(Core_Ctx *lexer){
|
||||
switch (*t.str) {
|
||||
case 0: goto end_of_stream; break;
|
||||
case '@': t.kind = TK_At; break;
|
||||
case '(': s->inside_brace_paren++; t.kind = TK_OpenParen; break;
|
||||
case ')': s->inside_brace_paren--; t.kind = TK_CloseParen; break;
|
||||
case '{': s->inside_brace_paren++; t.kind = TK_OpenBrace; break;
|
||||
case '}': s->inside_brace_paren--; t.kind = TK_CloseBrace; break;
|
||||
case '[': s->inside_brace_paren++; t.kind = TK_OpenBracket; break;
|
||||
case ']': s->inside_brace_paren--; t.kind = TK_CloseBracket; break;
|
||||
case '(':
|
||||
s->inside_brace_paren++;
|
||||
t.kind = TK_OpenParen;
|
||||
break;
|
||||
case ')':
|
||||
s->inside_brace_paren--;
|
||||
t.kind = TK_CloseParen;
|
||||
break;
|
||||
case '{':
|
||||
s->inside_brace_paren++;
|
||||
t.kind = TK_OpenBrace;
|
||||
break;
|
||||
case '}':
|
||||
s->inside_brace_paren--;
|
||||
t.kind = TK_CloseBrace;
|
||||
break;
|
||||
case '[':
|
||||
s->inside_brace_paren++;
|
||||
t.kind = TK_OpenBracket;
|
||||
break;
|
||||
case ']':
|
||||
s->inside_brace_paren--;
|
||||
t.kind = TK_CloseBracket;
|
||||
break;
|
||||
case ',': t.kind = TK_Comma; break;
|
||||
case '~': t.kind = TK_Neg; break;
|
||||
case '?': t.kind = TK_Question; break;
|
||||
case '^': t.kind = TK_BitXor; break;
|
||||
case '^':
|
||||
t.kind = TK_BitXor;
|
||||
break;
|
||||
CASE2('!', TK_Not, TK_NotEquals);
|
||||
CASE2('=', TK_Assign, TK_Equals);
|
||||
CASE2('*', TK_Mul, TK_MulAssign);
|
||||
@@ -537,10 +569,16 @@ lex__stream(Core_Ctx *lexer){
|
||||
lex_parse_u64(lexer, &t, 2);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
case '1':case '2':case '3':case '4':
|
||||
case '5':case '6':case '7':case '8':case '9':{
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9': {
|
||||
B32 found_dot = false;
|
||||
for (;;) {
|
||||
if (lex_is_numeric(lexc(s)))
|
||||
@@ -562,15 +600,59 @@ lex__stream(Core_Ctx *lexer){
|
||||
|
||||
} break;
|
||||
|
||||
case 'A':case 'a':case 'M':case 'm':case 'B':
|
||||
case 'b':case 'N':case 'n':case 'C':case 'c':case 'O':
|
||||
case 'o':case 'D':case 'd':case 'P':case 'p':case 'E':
|
||||
case 'e':case 'Q':case 'q':case 'F':case 'f':case 'R':
|
||||
case 'r':case 'G':case 'g':case 'S':case 's':case 'H':
|
||||
case 'h':case 'T':case 't':case 'I':case 'i':case 'U':
|
||||
case 'u':case 'J':case 'j':case 'V':case 'v':case 'K':
|
||||
case 'k':case 'W':case 'w':case 'L':case 'X':case 'l':
|
||||
case 'x':case 'Z':case 'z':case 'Y':case 'y':case '_': {
|
||||
case 'A':
|
||||
case 'a':
|
||||
case 'M':
|
||||
case 'm':
|
||||
case 'B':
|
||||
case 'b':
|
||||
case 'N':
|
||||
case 'n':
|
||||
case 'C':
|
||||
case 'c':
|
||||
case 'O':
|
||||
case 'o':
|
||||
case 'D':
|
||||
case 'd':
|
||||
case 'P':
|
||||
case 'p':
|
||||
case 'E':
|
||||
case 'e':
|
||||
case 'Q':
|
||||
case 'q':
|
||||
case 'F':
|
||||
case 'f':
|
||||
case 'R':
|
||||
case 'r':
|
||||
case 'G':
|
||||
case 'g':
|
||||
case 'S':
|
||||
case 's':
|
||||
case 'H':
|
||||
case 'h':
|
||||
case 'T':
|
||||
case 't':
|
||||
case 'I':
|
||||
case 'i':
|
||||
case 'U':
|
||||
case 'u':
|
||||
case 'J':
|
||||
case 'j':
|
||||
case 'V':
|
||||
case 'v':
|
||||
case 'K':
|
||||
case 'k':
|
||||
case 'W':
|
||||
case 'w':
|
||||
case 'L':
|
||||
case 'X':
|
||||
case 'l':
|
||||
case 'x':
|
||||
case 'Z':
|
||||
case 'z':
|
||||
case 'Y':
|
||||
case 'y':
|
||||
case '_': {
|
||||
t.kind = TK_Identifier;
|
||||
lex_parse_ident(table, s, &t);
|
||||
t.intern_val = intern_string(table, t.string);
|
||||
@@ -582,7 +664,8 @@ lex__stream(Core_Ctx *lexer){
|
||||
default: {
|
||||
token_error(&t, "Unknown token"_s);
|
||||
}
|
||||
}end_of_switch:
|
||||
}
|
||||
end_of_switch:
|
||||
|
||||
if (t.len == 0)
|
||||
lex_set_len(s, &t);
|
||||
@@ -683,7 +766,8 @@ case TK_StringLit: return "[String]";
|
||||
case TK_Error: return "[Error]";
|
||||
case TK_Float: return "[Float]";
|
||||
case TK_Integer: return "[Int]";
|
||||
case TK_Keyword: return "[Keyword]";
|
||||
case TK_Keyword:
|
||||
return "[Keyword]";
|
||||
/*END*/
|
||||
case CLOSE_SCOPE: return "Close_Scope";
|
||||
case OPEN_SCOPE: return "Open_Scope";
|
||||
|
||||
@@ -223,7 +223,6 @@ int main(int argument_count, char **arguments){
|
||||
String program_name = string_from_cstring(arguments[1]);
|
||||
compile_file(&arena, program_name, COMPILE_PRINT_STATS);
|
||||
}
|
||||
|
||||
}
|
||||
printf("End of program\n");
|
||||
return 0;
|
||||
|
||||
@@ -54,7 +54,8 @@ String core_stringify_message(Core_Ctx *pctx, Allocator *allocator, Core_Message
|
||||
// Print token part
|
||||
if (color_codes_enabled) {
|
||||
b.addf(PRINTF_RED "%.*s" PRINTF_RESET, (S64)token->len, token->str);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
b.addf("%.*s", (S64)token->len, token->str);
|
||||
}
|
||||
|
||||
@@ -170,7 +171,8 @@ token_match(Token_Kind a, Token_Kind b){
|
||||
Token *ta = token_get();
|
||||
Token *tb = token_get(1);
|
||||
if (ta->kind == a && tb->kind == b) {
|
||||
token_next(); token_next();
|
||||
token_next();
|
||||
token_next();
|
||||
return ta;
|
||||
}
|
||||
return 0;
|
||||
@@ -210,7 +212,8 @@ parse_init_stmt(Ast_Expr *expr){
|
||||
Ast_Atom *name = (Ast_Atom *)expr;
|
||||
result = (Ast_Expr *)ast_var(token, 0, name->intern_val, value);
|
||||
set_flag(result->flags, AST_EXPR);
|
||||
} else{
|
||||
}
|
||||
else {
|
||||
result = ast_expr_binary((Ast_Atom *)expr, value, token);
|
||||
}
|
||||
set_flag(result->flags, AST_STMT);
|
||||
@@ -416,7 +419,6 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){
|
||||
}
|
||||
Ast_If *result_if = ast_if(token, if_nodes);
|
||||
scope->stmts.add(result_if);
|
||||
|
||||
}
|
||||
else if (token_is(TK_Identifier) && token_is(TK_Comma, 1)) {
|
||||
Array<Ast_Decl *> decls = {scratch};
|
||||
@@ -448,7 +450,6 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){
|
||||
else {
|
||||
compiler_error(token, "Unexpected token [%s] while parsing statement", name(token->kind));
|
||||
}
|
||||
|
||||
}
|
||||
} while (token_match(SAME_SCOPE));
|
||||
token_expect(CLOSE_SCOPE);
|
||||
@@ -504,8 +505,13 @@ parse_lambda(Token *token){
|
||||
// Pratt expression parser
|
||||
// Based on this really good article: https://matklad.github.io/2020/04/13/simple-but-powerful-pratt-parsing.html
|
||||
//-----------------------------------------------------------------------------
|
||||
struct Binding_Power{S64 left;S64 right;};
|
||||
enum Binding{Binding_Prefix,Binding_Infix,Binding_Postfix};
|
||||
struct Binding_Power {
|
||||
S64 left;
|
||||
S64 right;
|
||||
};
|
||||
enum Binding { Binding_Prefix,
|
||||
Binding_Infix,
|
||||
Binding_Postfix };
|
||||
|
||||
CORE_Static Binding_Power
|
||||
binding_power(Binding binding, Token_Kind kind) {
|
||||
@@ -514,7 +520,8 @@ binding_power(Binding binding, Token_Kind kind){
|
||||
if (binding == Binding_Postfix) goto Postfix;
|
||||
else invalid_codepath;
|
||||
|
||||
Prefix: switch(kind){
|
||||
Prefix:
|
||||
switch (kind) {
|
||||
case TK_OpenBracket:
|
||||
return {-2, 22};
|
||||
case TK_Increment:
|
||||
@@ -531,7 +538,8 @@ binding_power(Binding binding, Token_Kind kind){
|
||||
return {-2, 20};
|
||||
default: return {-1, -1};
|
||||
}
|
||||
Infix: switch(kind){
|
||||
Infix:
|
||||
switch (kind) {
|
||||
case TK_Or:
|
||||
return {9, 10};
|
||||
case TK_And:
|
||||
@@ -561,7 +569,8 @@ binding_power(Binding binding, Token_Kind kind){
|
||||
return {20, 19};
|
||||
default: return {};
|
||||
}
|
||||
Postfix: switch(kind){
|
||||
Postfix:
|
||||
switch (kind) {
|
||||
case TK_Increment:
|
||||
case TK_Decrement:
|
||||
case TK_OpenBracket:
|
||||
@@ -674,7 +683,6 @@ parse_expr(S64 min_bp){
|
||||
}
|
||||
|
||||
else break;
|
||||
|
||||
}
|
||||
return left;
|
||||
}
|
||||
@@ -845,7 +853,8 @@ parse_decl(B32 is_global){
|
||||
|
||||
if (token_match_pound(pctx->intern_foreign)) {
|
||||
set_flag(flags, AST_FOREIGN);
|
||||
} else if(token_match_pound(pctx->intern_strict)){
|
||||
}
|
||||
else if (token_match_pound(pctx->intern_strict)) {
|
||||
set_flag(flags, AST_STRICT);
|
||||
}
|
||||
|
||||
@@ -955,10 +964,12 @@ parse_file(Ast_File *file){
|
||||
if (token_match_pound(pctx->intern_load)) {
|
||||
parse_load(true);
|
||||
continue;
|
||||
} else if (token_match_pound(pctx->intern_import)) {
|
||||
}
|
||||
else if (token_match_pound(pctx->intern_import)) {
|
||||
parse_import(true);
|
||||
continue;
|
||||
} else if (token_match_pound(pctx->intern_link)) {
|
||||
}
|
||||
else if (token_match_pound(pctx->intern_link)) {
|
||||
Token *file = token_expect(TK_StringLit);
|
||||
add(pctx->perm, &pctx->files_to_link, file);
|
||||
continue;
|
||||
|
||||
@@ -199,7 +199,8 @@ compare_values(Token *pos, Token_Kind op, Value a, Value b, bool is_const){
|
||||
|
||||
make_sure_types_are_compatible_for_constant_evaluation(pos, &a, &b);
|
||||
|
||||
skip_eval: B32 result = 0;
|
||||
skip_eval:
|
||||
B32 result = 0;
|
||||
if (is_const) {
|
||||
switch (a.type->kind) {
|
||||
case TYPE_TYPE: {
|
||||
@@ -245,12 +246,12 @@ compare_values(Token *pos, Token_Kind op, Value a, Value b, bool is_const){
|
||||
} break;
|
||||
|
||||
default: {
|
||||
failure: compiler_error(pos, "Constant application of binary %s on values of type %Q and %Q is not allowed", name(op), typestring(a.type), typestring(b.type));
|
||||
failure:
|
||||
compiler_error(pos, "Constant application of binary %s on values of type %Q and %Q is not allowed", name(op), typestring(a.type), typestring(b.type));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return value_bool(result);
|
||||
}
|
||||
|
||||
@@ -280,14 +281,18 @@ eval_binary(Token *pos, Token_Kind op, Value a, Value b, bool is_const){
|
||||
case TK_Div: bigint_div_trunc(&result.big_int_val, &a.big_int_val, &b.big_int_val); break;
|
||||
case TK_Mod: bigint_mod(&result.big_int_val, &a.big_int_val, &b.big_int_val); break;
|
||||
case TK_LeftShift: bigint_shl(&result.big_int_val, &a.big_int_val, &b.big_int_val); break;
|
||||
case TK_RightShift: bigint_shr(&result.big_int_val, &a.big_int_val, &b.big_int_val); break;
|
||||
case TK_RightShift:
|
||||
bigint_shr(&result.big_int_val, &a.big_int_val, &b.big_int_val);
|
||||
break;
|
||||
invalid_default_case;
|
||||
}
|
||||
} break;
|
||||
CASE_BOOL : {
|
||||
switch (op) {
|
||||
case TK_And: result.bool_val = a.bool_val && b.bool_val; break;
|
||||
case TK_Or: result.bool_val = a.bool_val || b.bool_val; break;
|
||||
case TK_Or:
|
||||
result.bool_val = a.bool_val || b.bool_val;
|
||||
break;
|
||||
invalid_default_case;
|
||||
}
|
||||
} break;
|
||||
@@ -296,7 +301,9 @@ eval_binary(Token *pos, Token_Kind op, Value a, Value b, bool is_const){
|
||||
case TK_Add: result.f64_val = a.f64_val + b.f64_val; break;
|
||||
case TK_Sub: result.f64_val = a.f64_val - b.f64_val; break;
|
||||
case TK_Mul: result.f64_val = a.f64_val * b.f64_val; break;
|
||||
case TK_Div: result.f64_val = a.f64_val / b.f64_val; break;
|
||||
case TK_Div:
|
||||
result.f64_val = a.f64_val / b.f64_val;
|
||||
break;
|
||||
invalid_default_case;
|
||||
}
|
||||
} break;
|
||||
@@ -338,7 +345,8 @@ eval_unary(Token *pos, Token_Kind op, Operand *operand){
|
||||
|
||||
BigInt result = {};
|
||||
switch (op) {
|
||||
case TK_Add:{} break;
|
||||
case TK_Add: {
|
||||
} break;
|
||||
case TK_Sub: {
|
||||
switch (type->kind) {
|
||||
CASE_INT : {
|
||||
@@ -353,10 +361,13 @@ eval_unary(Token *pos, Token_Kind op, Operand *operand){
|
||||
} break;
|
||||
case TK_Neg: {
|
||||
switch (type->kind) {
|
||||
CASE_SINT: case TYPE_UNTYPED_INT:
|
||||
bigint_not(&result, &a->big_int_val, digit_count(a), 1); break;
|
||||
CASE_SINT:
|
||||
case TYPE_UNTYPED_INT:
|
||||
bigint_not(&result, &a->big_int_val, digit_count(a), 1);
|
||||
break;
|
||||
CASE_UINT:
|
||||
bigint_not(&result, &a->big_int_val, digit_count(a), 0); break;
|
||||
bigint_not(&result, &a->big_int_val, digit_count(a), 0);
|
||||
break;
|
||||
default: goto failure;
|
||||
}
|
||||
a->big_int_val = result;
|
||||
@@ -368,12 +379,18 @@ eval_unary(Token *pos, Token_Kind op, Operand *operand){
|
||||
a->bool_val = 1;
|
||||
a->bool_val = 0;
|
||||
} break;
|
||||
CASE_FLOAT: a->bool_val = !a->f64_val; break;
|
||||
CASE_BOOL: a->bool_val = !a->bool_val; break;
|
||||
CASE_FLOAT:
|
||||
a->bool_val = !a->f64_val;
|
||||
break;
|
||||
CASE_BOOL:
|
||||
a->bool_val = !a->bool_val;
|
||||
break;
|
||||
default: goto failure;
|
||||
}
|
||||
} break;
|
||||
default: failure: compiler_error(pos, "Constant application of unary %s on values of type %Q is not allowed", name(op), typestring(a->type));
|
||||
default:
|
||||
failure:
|
||||
compiler_error(pos, "Constant application of unary %s on values of type %Q is not allowed", name(op), typestring(a->type));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -667,7 +684,8 @@ try_propagating_resolved_type_to_untyped_literals(Ast *ast, Ast_Type *type, Ast_
|
||||
BREAK();
|
||||
}
|
||||
|
||||
default:{}
|
||||
default: {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -728,7 +746,6 @@ resolve_stmt(Ast *ast, Ast_Type *ret){
|
||||
if (!ast) return;
|
||||
assert(ast->parent_scope->kind == AST_SCOPE);
|
||||
|
||||
|
||||
switch (ast->kind) {
|
||||
CASE(RETURN, Return) { // @todo: need to check if all paths return a value
|
||||
Arena *scratch = pctx->scratch;
|
||||
@@ -949,7 +966,8 @@ resolve_cast(Ast_Binary *node){
|
||||
case TYPE_TYPE: {
|
||||
if (is_int(type)) {
|
||||
expr.type = type;
|
||||
} else goto failure;
|
||||
}
|
||||
else goto failure;
|
||||
} break;
|
||||
case TYPE_ENUM: {
|
||||
if (is_int(type))
|
||||
@@ -971,18 +989,24 @@ resolve_cast(Ast_Binary *node){
|
||||
else if (is_float(type)) {
|
||||
expr.value.type = type;
|
||||
if (expr.is_const) expr.value.f64_val = bigint_as_float(&expr.big_int_val); // @leak
|
||||
} else goto failure;
|
||||
}
|
||||
else goto failure;
|
||||
} break;
|
||||
case TYPE_F32: case TYPE_F64: {
|
||||
case TYPE_F32:
|
||||
case TYPE_F64: {
|
||||
if (is_float(type)) {
|
||||
expr.type = type;
|
||||
}
|
||||
else if (is_int(type)) {
|
||||
if (expr.is_const) expr.value.big_int_val = bigint_s64((S64)expr.value.f64_val); // @todo: What to do here???
|
||||
expr.type = type;
|
||||
} else goto failure;
|
||||
}
|
||||
else goto failure;
|
||||
} break;
|
||||
default: failure: compiler_error(node->pos, "Failed to cast from %Q to %Q", typestring(expr.type), typestring(type));;
|
||||
default:
|
||||
failure:
|
||||
compiler_error(node->pos, "Failed to cast from %Q to %Q", typestring(expr.type), typestring(type));
|
||||
;
|
||||
}
|
||||
|
||||
assert(original_type != type ? expr.type == type : 1);
|
||||
@@ -1052,7 +1076,8 @@ resolve_compound_struct(Ast_Call *node, Ast_Type *type){
|
||||
|
||||
if (m.visited) {
|
||||
compiler_error(it->pos, "Field already initialized");
|
||||
} else m.visited = true;
|
||||
}
|
||||
else m.visited = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1156,7 +1181,8 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
|
||||
|
||||
if (node->expr) {
|
||||
type.type_val = type_array(type.type_val, bigint_as_unsigned(&expr.big_int_val));
|
||||
} else{
|
||||
}
|
||||
else {
|
||||
type.type_val = type_slice(type.type_val, node);
|
||||
}
|
||||
|
||||
@@ -1382,7 +1408,10 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
|
||||
node->resolved_type = type_pointer(value.type_val);
|
||||
return operand_type(node->resolved_type);
|
||||
}
|
||||
else{ compiler_error(node->pos, "Dereferencing expression %Q that is not a [Pointer] or [Type]", typestring(value.type)); return {}; }
|
||||
else {
|
||||
compiler_error(node->pos, "Dereferencing expression %Q that is not a [Pointer] or [Type]", typestring(value.type));
|
||||
return {};
|
||||
}
|
||||
}
|
||||
else if (node->op == TK_Dereference) {
|
||||
if (!value.is_lvalue) {
|
||||
@@ -1599,7 +1628,6 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
|
||||
set_flag(call_item->call_flags, CALL_INCLUDED);
|
||||
items.add(call_item);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// @note: check if all arguments are included and cleanup
|
||||
@@ -1612,7 +1640,6 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
|
||||
node->exprs = items.tight_copy(pctx->perm);
|
||||
node->resolved_type = name.type->func.ret;
|
||||
|
||||
|
||||
return operand_rvalue(name.type->func.ret);
|
||||
//
|
||||
// CALL End
|
||||
@@ -1630,7 +1657,9 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
|
||||
CORE_Static Ast_Type *
|
||||
get_type_base(Ast_Type *type) {
|
||||
switch (type->kind) {
|
||||
case TYPE_POINTER: case TYPE_SLICE: case TYPE_ARRAY: return get_type_base(type->base);
|
||||
case TYPE_POINTER:
|
||||
case TYPE_SLICE:
|
||||
case TYPE_ARRAY: return get_type_base(type->base);
|
||||
default: return type;
|
||||
}
|
||||
}
|
||||
@@ -1721,7 +1750,8 @@ resolve_decl(Ast_Decl *ast){
|
||||
BREAK();
|
||||
}
|
||||
|
||||
case AST_NAMESPACE: break;
|
||||
case AST_NAMESPACE:
|
||||
break;
|
||||
|
||||
CASE(ENUM, Decl) {
|
||||
Ast_Type *type_of_enum = resolve_typespec(node->typespec, AST_CAN_BE_NULL);
|
||||
@@ -1735,13 +1765,15 @@ resolve_decl(Ast_Decl *ast){
|
||||
if (decl->expr) {
|
||||
op = require_const_int(decl->expr, AST_CANT_BE_NULL);
|
||||
value = bigint_as_signed(&op.big_int_val) + 1;
|
||||
} else{
|
||||
}
|
||||
else {
|
||||
decl->state = DECL_RESOLVED;
|
||||
op.type = node->type_val;
|
||||
bigint_init_signed(&op.big_int_val, value);
|
||||
if (is_flag_set(node->flags, AST_FLAG)) {
|
||||
value = value << 1;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
value += 1;
|
||||
}
|
||||
}
|
||||
@@ -1753,7 +1785,6 @@ resolve_decl(Ast_Decl *ast){
|
||||
|
||||
invalid_default_case;
|
||||
}
|
||||
|
||||
}
|
||||
ast->state = DECL_RESOLVED;
|
||||
|
||||
|
||||
@@ -60,5 +60,9 @@ enum{
|
||||
CORE_Static Operand resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_context, Ast_Scope *field_access_scope);
|
||||
CORE_Static void resolve_decl(Ast_Decl *ast);
|
||||
CORE_Static Ast_Decl *resolve_name(Ast_Scope *parent_scope, Token *pos, Intern_String name, Search_Flag search_flags = 0);
|
||||
#define CASE(kind,type) case AST_##kind: { Ast_##type *node = (Ast_##type *)ast;
|
||||
#define BREAK() } break
|
||||
#define CASE(kind, type) \
|
||||
case AST_##kind: { \
|
||||
Ast_##type *node = (Ast_##type *)ast;
|
||||
#define BREAK() \
|
||||
} \
|
||||
break
|
||||
|
||||
@@ -17,7 +17,8 @@ get_name_of_type(Ast_Type *type){
|
||||
case TYPE_U32: return "U32";
|
||||
case TYPE_U64: return "U64";
|
||||
case TYPE_TUPLE: return "Tuple";
|
||||
case TYPE_TYPE: return "Type";
|
||||
case TYPE_TYPE:
|
||||
return "Type";
|
||||
invalid_default_case;
|
||||
}
|
||||
return "<unknown_type>";
|
||||
@@ -101,7 +102,10 @@ type_slice(Ast_Type *base, Ast *ast){
|
||||
return result;
|
||||
}
|
||||
|
||||
struct Slice{void *p; S64 len;};
|
||||
struct Slice {
|
||||
void *p;
|
||||
S64 len;
|
||||
};
|
||||
result = type_new(pctx->perm, TYPE_SLICE, sizeof(Slice), alignof(Slice));
|
||||
result->arr.base = base;
|
||||
result->arr.slice_hash = hash;
|
||||
|
||||
40
core_types.h
40
core_types.h
@@ -2,12 +2,36 @@
|
||||
// Resolved Types
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
#define CASE_SINT case TYPE_S8:case TYPE_S16:case TYPE_S32:case TYPE_S64: case TYPE_CHAR: case TYPE_INT
|
||||
#define CASE_UINT case TYPE_U8:case TYPE_U16:case TYPE_U32:case TYPE_U64
|
||||
#define CASE_INT case TYPE_UNTYPED_INT: CASE_SINT: CASE_UINT
|
||||
#define CASE_BOOL case TYPE_UNTYPED_BOOL: case TYPE_BOOL
|
||||
#define CASE_FLOAT case TYPE_UNTYPED_FLOAT: case TYPE_F32: case TYPE_F64
|
||||
#define CASE_STRING case TYPE_UNTYPED_STRING: case TYPE_STRUCT: case TYPE_POINTER
|
||||
#define CASE_UNTYPED case TYPE_UNTYPED_INT: case TYPE_UNTYPED_BOOL: case TYPE_UNTYPED_FLOAT: case TYPE_UNTYPED_STRING
|
||||
#define CASE_SINT \
|
||||
case TYPE_S8: \
|
||||
case TYPE_S16: \
|
||||
case TYPE_S32: \
|
||||
case TYPE_S64: \
|
||||
case TYPE_CHAR: \
|
||||
case TYPE_INT
|
||||
#define CASE_UINT \
|
||||
case TYPE_U8: \
|
||||
case TYPE_U16: \
|
||||
case TYPE_U32: \
|
||||
case TYPE_U64
|
||||
#define CASE_INT \
|
||||
case TYPE_UNTYPED_INT: \
|
||||
CASE_SINT: \
|
||||
CASE_UINT
|
||||
#define CASE_BOOL \
|
||||
case TYPE_UNTYPED_BOOL: \
|
||||
case TYPE_BOOL
|
||||
#define CASE_FLOAT \
|
||||
case TYPE_UNTYPED_FLOAT: \
|
||||
case TYPE_F32: \
|
||||
case TYPE_F64
|
||||
#define CASE_STRING \
|
||||
case TYPE_UNTYPED_STRING: \
|
||||
case TYPE_STRUCT: \
|
||||
case TYPE_POINTER
|
||||
#define CASE_UNTYPED \
|
||||
case TYPE_UNTYPED_INT: \
|
||||
case TYPE_UNTYPED_BOOL: \
|
||||
case TYPE_UNTYPED_FLOAT: \
|
||||
case TYPE_UNTYPED_STRING
|
||||
#define ARRAY_SIZE_SLICE (-1)
|
||||
|
||||
@@ -220,7 +220,6 @@ os_list_dir(Arena *scratch, Allocator *a, String dir, U32 flags = LIST_NO_FLAGS)
|
||||
String full_file_path = string_fmt(a, "%Q/%Q", dir, filename);
|
||||
OS_File_Info listing = {full_file_path, os_get_absolute_path(a, full_file_path)};
|
||||
|
||||
|
||||
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
listing.is_directory = true;
|
||||
|
||||
|
||||
156
stb_sprintf.h
156
stb_sprintf.h
@@ -268,11 +268,9 @@ static struct
|
||||
"00010203040506070809101112131415161718192021222324"
|
||||
"25262728293031323334353637383940414243444546474849"
|
||||
"50515253545556575859606162636465666768697071727374"
|
||||
"75767778798081828384858687888990919293949596979899"
|
||||
};
|
||||
"75767778798081828384858687888990919293949596979899"};
|
||||
|
||||
STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char pcomma, char pperiod)
|
||||
{
|
||||
STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char pcomma, char pperiod) {
|
||||
stbsp__period = pperiod;
|
||||
stbsp__comma = pcomma;
|
||||
}
|
||||
@@ -291,23 +289,23 @@ STBSP__PUBLICDEF void STB_SPRINTF_DECORATE(set_separators)(char pcomma, char ppe
|
||||
#define STBSP__METRIC_1024 2048
|
||||
#define STBSP__METRIC_JEDEC 4096
|
||||
|
||||
static void stbsp__lead_sign(stbsp__uint32 fl, char *sign)
|
||||
{
|
||||
static void stbsp__lead_sign(stbsp__uint32 fl, char *sign) {
|
||||
sign[0] = 0;
|
||||
if (fl & STBSP__NEGATIVE) {
|
||||
sign[0] = 1;
|
||||
sign[1] = '-';
|
||||
} else if (fl & STBSP__LEADINGSPACE) {
|
||||
}
|
||||
else if (fl & STBSP__LEADINGSPACE) {
|
||||
sign[0] = 1;
|
||||
sign[1] = ' ';
|
||||
} else if (fl & STBSP__LEADINGPLUS) {
|
||||
}
|
||||
else if (fl & STBSP__LEADINGPLUS) {
|
||||
sign[0] = 1;
|
||||
sign[1] = '+';
|
||||
}
|
||||
}
|
||||
|
||||
static STBSP__ASAN stbsp__uint32 stbsp__strlen_limited(char const *s, stbsp__uint32 limit)
|
||||
{
|
||||
static STBSP__ASAN stbsp__uint32 stbsp__strlen_limited(char const *s, stbsp__uint32 limit) {
|
||||
char const *sn = s;
|
||||
|
||||
// get up to 4-byte alignment
|
||||
@@ -346,8 +344,7 @@ static STBSP__ASAN stbsp__uint32 stbsp__strlen_limited(char const *s, stbsp__uin
|
||||
return (stbsp__uint32)(sn - s);
|
||||
}
|
||||
|
||||
STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va)
|
||||
{
|
||||
STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintfcb)(STBSP_SPRINTFCB *callback, void *user, char *buf, char const *fmt, va_list va) {
|
||||
static char hex[] = "0123456789abcdefxp";
|
||||
static char hexu[] = "0123456789ABCDEFXP";
|
||||
char *bf;
|
||||
@@ -421,7 +418,8 @@ cl = lg; \
|
||||
bf[1] = f[1];
|
||||
bf[2] = f[2];
|
||||
bf[3] = f[3];
|
||||
} else
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
*(stbsp__uint32 *)bf = v;
|
||||
@@ -473,10 +471,12 @@ cl = lg; \
|
||||
if (fl & STBSP__METRIC_SUFFIX) {
|
||||
if (fl & STBSP__METRIC_1024) {
|
||||
fl |= STBSP__METRIC_JEDEC;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
fl |= STBSP__METRIC_1024;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
fl |= STBSP__METRIC_SUFFIX;
|
||||
}
|
||||
++f;
|
||||
@@ -500,7 +500,8 @@ cl = lg; \
|
||||
if (f[0] == '*') {
|
||||
fw = va_arg(va, stbsp__uint32);
|
||||
++f;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
while ((f[0] >= '0') && (f[0] <= '9')) {
|
||||
fw = fw * 10 + f[0] - '0';
|
||||
f++;
|
||||
@@ -512,7 +513,8 @@ cl = lg; \
|
||||
if (f[0] == '*') {
|
||||
pr = va_arg(va, stbsp__uint32);
|
||||
++f;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
pr = 0;
|
||||
while ((f[0] >= '0') && (f[0] <= '9')) {
|
||||
pr = pr * 10 + f[0] - '0';
|
||||
@@ -558,9 +560,11 @@ cl = lg; \
|
||||
if ((f[1] == '6') && (f[2] == '4')) {
|
||||
fl |= STBSP__INTMAX;
|
||||
f += 3;
|
||||
} else if ((f[1] == '3') && (f[2] == '2')) {
|
||||
}
|
||||
else if ((f[1] == '3') && (f[2] == '2')) {
|
||||
f += 3;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
fl |= ((sizeof(void *) == 8) ? STBSP__INTMAX : 0);
|
||||
++f;
|
||||
}
|
||||
@@ -705,7 +709,8 @@ cl = lg; \
|
||||
if (dp < 0) {
|
||||
tail[2] = '-';
|
||||
dp = -dp;
|
||||
} else
|
||||
}
|
||||
else
|
||||
tail[2] = '+';
|
||||
n = (dp >= 1000) ? 6 : ((dp >= 100) ? 5 : ((dp >= 10) ? 4 : 3));
|
||||
tail[0] = (char)n;
|
||||
@@ -755,7 +760,8 @@ cl = lg; \
|
||||
// this is the insane action to get the pr to match %g semantics for %f
|
||||
if (dp > 0) {
|
||||
pr = (dp < (stbsp__int32)l) ? l - dp : 0;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
pr = -dp + ((pr > (stbsp__int32)l) ? (stbsp__int32)l : pr);
|
||||
}
|
||||
goto dofloatfromg;
|
||||
@@ -799,7 +805,8 @@ cl = lg; \
|
||||
if (dp < 0) {
|
||||
tail[2] = '-';
|
||||
dp = -dp;
|
||||
} else
|
||||
}
|
||||
else
|
||||
tail[2] = '+';
|
||||
#ifdef STB_SPRINTF_MSVC_MODE
|
||||
n = 5;
|
||||
@@ -884,7 +891,8 @@ cl = lg; \
|
||||
}
|
||||
tz = pr - (n + l);
|
||||
cs = 1 + (3 << 24); // how many tens did we write (for commas below)
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
cs = (fl & STBSP__TRIPLET_COMMA) ? ((600 - (stbsp__uint32)dp) % 3) : 0;
|
||||
if ((stbsp__uint32)dp >= l) {
|
||||
// handle xxxx000*000.0
|
||||
@@ -893,7 +901,8 @@ cl = lg; \
|
||||
if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {
|
||||
cs = 0;
|
||||
*s++ = stbsp__comma;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
*s++ = sn[n];
|
||||
++n;
|
||||
if (n >= l)
|
||||
@@ -919,7 +928,8 @@ cl = lg; \
|
||||
if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {
|
||||
cs = 0;
|
||||
*s++ = stbsp__comma;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
*s++ = '0';
|
||||
--n;
|
||||
}
|
||||
@@ -930,14 +940,16 @@ cl = lg; \
|
||||
*s++ = stbsp__period;
|
||||
tz = pr;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
// handle xxxxx.xxxx000*000
|
||||
n = 0;
|
||||
for (;;) {
|
||||
if ((fl & STBSP__TRIPLET_COMMA) && (++cs == 4)) {
|
||||
cs = 0;
|
||||
*s++ = stbsp__comma;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
*s++ = sn[n];
|
||||
++n;
|
||||
if (n >= (stbsp__uint32)dp)
|
||||
@@ -1079,7 +1091,8 @@ cl = lg; \
|
||||
n64 = (stbsp__uint64)-i64;
|
||||
fl |= STBSP__NEGATIVE;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
stbsp__int32 i = va_arg(va, stbsp__int32);
|
||||
n64 = (stbsp__uint32)i;
|
||||
if ((f[0] != 'u') && (i < 0)) {
|
||||
@@ -1109,7 +1122,8 @@ cl = lg; \
|
||||
if (n64 >= 100000000) {
|
||||
n = (stbsp__uint32)(n64 % 100000000);
|
||||
n64 /= 100000000;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
n = (stbsp__uint32)n64;
|
||||
n64 = 0;
|
||||
}
|
||||
@@ -1125,7 +1139,8 @@ cl = lg; \
|
||||
l = 0;
|
||||
*--s = stbsp__comma;
|
||||
--o;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
*--s = (char)(n % 10) + '0';
|
||||
n /= 10;
|
||||
}
|
||||
@@ -1140,7 +1155,8 @@ cl = lg; \
|
||||
l = 0;
|
||||
*--s = stbsp__comma;
|
||||
--o;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
*--s = '0';
|
||||
}
|
||||
}
|
||||
@@ -1174,7 +1190,8 @@ cl = lg; \
|
||||
{
|
||||
pr = (fw > pr) ? fw : pr;
|
||||
fw = 0;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
fl &= ~STBSP__TRIPLET_COMMA; // if no leading zeros, then no commas
|
||||
}
|
||||
}
|
||||
@@ -1243,7 +1260,8 @@ cl = lg; \
|
||||
if ((fl & STBSP__TRIPLET_COMMA) && (cs++ == c)) {
|
||||
cs = 0;
|
||||
*bf++ = stbsp__comma;
|
||||
} else
|
||||
}
|
||||
else
|
||||
*bf++ = '0';
|
||||
--i;
|
||||
}
|
||||
@@ -1388,8 +1406,7 @@ cl = lg; \
|
||||
// ============================================================================
|
||||
// wrapper functions
|
||||
|
||||
STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...)
|
||||
{
|
||||
STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(sprintf)(char *buf, char const *fmt, ...) {
|
||||
int result;
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
@@ -1405,8 +1422,7 @@ typedef struct stbsp__context {
|
||||
char tmp[STB_SPRINTF_MIN];
|
||||
} stbsp__context;
|
||||
|
||||
static char *stbsp__clamp_callback(const char *buf, void *user, int len)
|
||||
{
|
||||
static char *stbsp__clamp_callback(const char *buf, void *user, int len) {
|
||||
stbsp__context *c = (stbsp__context *)user;
|
||||
c->length += len;
|
||||
|
||||
@@ -1433,8 +1449,7 @@ static char *stbsp__clamp_callback(const char *buf, void *user, int len)
|
||||
return (c->count >= STB_SPRINTF_MIN) ? c->buf : c->tmp; // go direct into buffer if you can
|
||||
}
|
||||
|
||||
static char * stbsp__count_clamp_callback( const char * buf, void * user, int len )
|
||||
{
|
||||
static char *stbsp__count_clamp_callback(const char *buf, void *user, int len) {
|
||||
stbsp__context *c = (stbsp__context *)user;
|
||||
(void)sizeof(buf);
|
||||
|
||||
@@ -1442,25 +1457,24 @@ static char * stbsp__count_clamp_callback( const char * buf, void * user, int le
|
||||
return c->tmp; // go direct into buffer if you can
|
||||
}
|
||||
|
||||
STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, char const * fmt, va_list va )
|
||||
{
|
||||
STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsnprintf)(char *buf, int count, char const *fmt, va_list va) {
|
||||
stbsp__context c;
|
||||
|
||||
if ( (count == 0) && !buf )
|
||||
{
|
||||
if ((count == 0) && !buf) {
|
||||
c.length = 0;
|
||||
|
||||
STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__count_clamp_callback, &c, c.tmp, fmt, va );
|
||||
STB_SPRINTF_DECORATE(vsprintfcb)
|
||||
(stbsp__count_clamp_callback, &c, c.tmp, fmt, va);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
int l;
|
||||
|
||||
c.buf = buf;
|
||||
c.count = count;
|
||||
c.length = 0;
|
||||
|
||||
STB_SPRINTF_DECORATE( vsprintfcb )( stbsp__clamp_callback, &c, stbsp__clamp_callback(0,&c,0), fmt, va );
|
||||
STB_SPRINTF_DECORATE(vsprintfcb)
|
||||
(stbsp__clamp_callback, &c, stbsp__clamp_callback(0, &c, 0), fmt, va);
|
||||
|
||||
// zero-terminate
|
||||
l = (int)(c.buf - buf);
|
||||
@@ -1472,8 +1486,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE( vsnprintf )( char * buf, int count, c
|
||||
return c.length;
|
||||
}
|
||||
|
||||
STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...)
|
||||
{
|
||||
STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char const *fmt, ...) {
|
||||
int result;
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
@@ -1484,8 +1497,7 @@ STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(snprintf)(char *buf, int count, char c
|
||||
return result;
|
||||
}
|
||||
|
||||
STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va)
|
||||
{
|
||||
STBSP__PUBLICDEF int STB_SPRINTF_DECORATE(vsprintf)(char *buf, char const *fmt, va_list va) {
|
||||
return STB_SPRINTF_DECORATE(vsprintfcb)(0, 0, buf, fmt, va);
|
||||
}
|
||||
|
||||
@@ -1503,8 +1515,7 @@ for (cn = 0; cn < 8; cn++) \
|
||||
}
|
||||
|
||||
// get float info
|
||||
static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value)
|
||||
{
|
||||
static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo, double value) {
|
||||
double d;
|
||||
stbsp__int64 b = 0;
|
||||
|
||||
@@ -1521,24 +1532,19 @@ static stbsp__int32 stbsp__real_to_parts(stbsp__int64 *bits, stbsp__int32 *expo,
|
||||
|
||||
static double const stbsp__bot[23] = {
|
||||
1e+000, 1e+001, 1e+002, 1e+003, 1e+004, 1e+005, 1e+006, 1e+007, 1e+008, 1e+009, 1e+010, 1e+011,
|
||||
1e+012, 1e+013, 1e+014, 1e+015, 1e+016, 1e+017, 1e+018, 1e+019, 1e+020, 1e+021, 1e+022
|
||||
};
|
||||
1e+012, 1e+013, 1e+014, 1e+015, 1e+016, 1e+017, 1e+018, 1e+019, 1e+020, 1e+021, 1e+022};
|
||||
static double const stbsp__negbot[22] = {
|
||||
1e-001, 1e-002, 1e-003, 1e-004, 1e-005, 1e-006, 1e-007, 1e-008, 1e-009, 1e-010, 1e-011,
|
||||
1e-012, 1e-013, 1e-014, 1e-015, 1e-016, 1e-017, 1e-018, 1e-019, 1e-020, 1e-021, 1e-022
|
||||
};
|
||||
1e-012, 1e-013, 1e-014, 1e-015, 1e-016, 1e-017, 1e-018, 1e-019, 1e-020, 1e-021, 1e-022};
|
||||
static double const stbsp__negboterr[22] = {
|
||||
-5.551115123125783e-018, -2.0816681711721684e-019, -2.0816681711721686e-020, -4.7921736023859299e-021, -8.1803053914031305e-022, 4.5251888174113741e-023,
|
||||
4.5251888174113739e-024, -2.0922560830128471e-025, -6.2281591457779853e-026, -3.6432197315497743e-027, 6.0503030718060191e-028, 2.0113352370744385e-029,
|
||||
-3.0373745563400371e-030, 1.1806906454401013e-032, -7.7705399876661076e-032, 2.0902213275965398e-033, -7.1542424054621921e-034, -7.1542424054621926e-035,
|
||||
2.4754073164739869e-036, 5.4846728545790429e-037, 9.2462547772103625e-038, -4.8596774326570872e-039
|
||||
};
|
||||
2.4754073164739869e-036, 5.4846728545790429e-037, 9.2462547772103625e-038, -4.8596774326570872e-039};
|
||||
static double const stbsp__top[13] = {
|
||||
1e+023, 1e+046, 1e+069, 1e+092, 1e+115, 1e+138, 1e+161, 1e+184, 1e+207, 1e+230, 1e+253, 1e+276, 1e+299
|
||||
};
|
||||
1e+023, 1e+046, 1e+069, 1e+092, 1e+115, 1e+138, 1e+161, 1e+184, 1e+207, 1e+230, 1e+253, 1e+276, 1e+299};
|
||||
static double const stbsp__negtop[13] = {
|
||||
1e-023, 1e-046, 1e-069, 1e-092, 1e-115, 1e-138, 1e-161, 1e-184, 1e-207, 1e-230, 1e-253, 1e-276, 1e-299
|
||||
};
|
||||
1e-023, 1e-046, 1e-069, 1e-092, 1e-115, 1e-138, 1e-161, 1e-184, 1e-207, 1e-230, 1e-253, 1e-276, 1e-299};
|
||||
static double const stbsp__toperr[13] = {
|
||||
8388608,
|
||||
6.8601809640529717e+028,
|
||||
@@ -1552,14 +1558,12 @@ static double const stbsp__toperr[13] = {
|
||||
-9.9566444326005119e+213,
|
||||
6.3641293062232429e+236,
|
||||
-5.2069140800249813e+259,
|
||||
-5.2504760255204387e+282
|
||||
};
|
||||
-5.2504760255204387e+282};
|
||||
static double const stbsp__negtoperr[13] = {
|
||||
3.9565301985100693e-040, -2.299904345391321e-063, 3.6506201437945798e-086, 1.1875228833981544e-109,
|
||||
-5.0644902316928607e-132, -6.7156837247865426e-155, -2.812077463003139e-178, -5.7778912386589953e-201,
|
||||
7.4997100559334532e-224, -4.6439668915134491e-247, -6.3691100762962136e-270, -9.436808465446358e-293,
|
||||
8.0970921678014997e-317
|
||||
};
|
||||
8.0970921678014997e-317};
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER <= 1200)
|
||||
static stbsp__uint64 const stbsp__powten[20] = {
|
||||
@@ -1582,8 +1586,7 @@ static stbsp__uint64 const stbsp__powten[20] = {
|
||||
10000000000000000,
|
||||
100000000000000000,
|
||||
1000000000000000000,
|
||||
10000000000000000000U
|
||||
};
|
||||
10000000000000000000U};
|
||||
#define stbsp__tento19th ((stbsp__uint64)1000000000000000000)
|
||||
#else
|
||||
static stbsp__uint64 const stbsp__powten[20] = {
|
||||
@@ -1606,8 +1609,7 @@ static stbsp__uint64 const stbsp__powten[20] = {
|
||||
10000000000000000ULL,
|
||||
100000000000000000ULL,
|
||||
1000000000000000000ULL,
|
||||
10000000000000000000ULL
|
||||
};
|
||||
10000000000000000000ULL};
|
||||
#define stbsp__tento19th (1000000000000000000ULL)
|
||||
#endif
|
||||
|
||||
@@ -1655,7 +1657,8 @@ static void stbsp__raise_to_power10(double *ohi, double *olo, double d, stbsp__i
|
||||
double ph, pl;
|
||||
if ((power >= 0) && (power <= 22)) {
|
||||
stbsp__ddmulthi(ph, pl, d, stbsp__bot[power]);
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
stbsp__int32 e, et, eb;
|
||||
double p2h, p2l;
|
||||
|
||||
@@ -1683,7 +1686,8 @@ static void stbsp__raise_to_power10(double *ohi, double *olo, double d, stbsp__i
|
||||
ph = p2h;
|
||||
pl = p2l;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
if (eb) {
|
||||
e = eb;
|
||||
if (eb > 22)
|
||||
@@ -1717,8 +1721,7 @@ static void stbsp__raise_to_power10(double *ohi, double *olo, double d, stbsp__i
|
||||
// decimal point in decimal_pos. +/-INF and NAN are specified by special values
|
||||
// returned in the decimal_pos parameter.
|
||||
// frac_digits is absolute normally, but if you want from first significant digits (got %g and %e), or in 0x80000000
|
||||
static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits)
|
||||
{
|
||||
static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, char *out, stbsp__int32 *decimal_pos, double value, stbsp__uint32 frac_digits) {
|
||||
double d;
|
||||
stbsp__int64 bits = 0;
|
||||
stbsp__int32 expo, e, ng, tens;
|
||||
@@ -1830,7 +1833,8 @@ static stbsp__int32 stbsp__real_to_str(char const **start, stbsp__uint32 *len, c
|
||||
if (bits >= 100000000) {
|
||||
n = (stbsp__uint32)(bits % 100000000);
|
||||
bits /= 100000000;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
n = (stbsp__uint32)bits;
|
||||
bits = 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user