Switched to bigint in lexer

This commit is contained in:
Krzosa Karol
2022-06-06 10:00:53 +02:00
parent 960523b443
commit 4f876a36a4
4 changed files with 46 additions and 17 deletions

View File

@@ -103,7 +103,7 @@ struct Token{
union {
U32 unicode;
U64 int_val;
BigInt int_val;
F64 f64_val;
String error_val;
Intern_String intern_val;
@@ -209,19 +209,15 @@ token_error(Token *t, String error_val){
function void
lex_parse_u64(Token *t){
t->kind = TK_Integer;
U64 result = 0;
U64 m = 1;
BigInt m = bigint_u64(1); // @leak, it accumulates and potentially needs allocation
BigInt val10 = bigint_u64(10);
for(S64 i = t->len - 1; i >= 0; --i){
U64 val = t->str[i] - '0';
U64 new_val = val * m;
if((result + new_val) < result){
token_error(t, "Integer overflow"_s);
return;
}
result+=new_val;
m *= 10;
BigInt val = bigint_u64(t->str[i] - '0'); // I dont think this is a leak, too small
BigInt new_val = bigint_mul(&val, &m); // @leak
bigint_add(&t->int_val, &t->int_val, &new_val);
bigint_mul(&m, &m, &val10);
}
t->int_val = result;
}
function void