Add floats
This commit is contained in:
37
new_lex.cpp
37
new_lex.cpp
@@ -103,7 +103,7 @@ struct Token{
|
||||
|
||||
union {
|
||||
U64 int_val;
|
||||
F64 float_val;
|
||||
F64 f64_val;
|
||||
String error_val;
|
||||
Intern_String intern_val;
|
||||
S64 indent;
|
||||
@@ -137,8 +137,6 @@ force_inline B32 token_is_assign(Token *token){return token_is_assign(token->kin
|
||||
force_inline B32 token_is_compare(Token_Kind token){return token >= TK_FirstCompare && token <= TK_LastCompare;}
|
||||
force_inline B32 token_is_compare(Token *token){return token_is_compare(token->kind);}
|
||||
|
||||
|
||||
|
||||
function U8
|
||||
lexc(Lex_Stream *s){
|
||||
return s->stream.str[s->iter];
|
||||
@@ -209,6 +207,7 @@ token_error(Token *t, String error_val){
|
||||
|
||||
function void
|
||||
lex_parse_u64(Token *t){
|
||||
t->kind = TK_Integer;
|
||||
U64 result = 0;
|
||||
U64 m = 1;
|
||||
for(S64 i = t->len - 1; i >= 0; --i){
|
||||
@@ -224,6 +223,16 @@ lex_parse_u64(Token *t){
|
||||
t->int_val = result;
|
||||
}
|
||||
|
||||
function void
|
||||
lex_parse_f64(Token *t){
|
||||
t->kind = TK_Float;
|
||||
char buffer[128];
|
||||
S64 len = clamp_top((int)t->len, 126);
|
||||
memory_copy(buffer, t->str, len);
|
||||
buffer[len] = 0;
|
||||
t->f64_val = strtod(buffer, 0);
|
||||
}
|
||||
|
||||
function void
|
||||
lex_advance(Lex_Stream *s){
|
||||
if(s->iter >= s->stream.len){
|
||||
@@ -574,11 +583,25 @@ lex__stream(Intern_Table *table, Array<Token> *array, Lex_Stream *s){
|
||||
|
||||
case '0':case '1':case '2':case '3':case '4':
|
||||
case '5':case '6':case '7':case '8':case '9':{
|
||||
t.kind = TK_Integer;
|
||||
while(lex_is_numeric(lexc(s)))
|
||||
B32 found_dot = false;
|
||||
for(;;){
|
||||
if(lex_is_numeric(lexc(s)))
|
||||
;
|
||||
else if(lexc(s) == '.'){
|
||||
if(found_dot){
|
||||
token_error(&t, "Multiple '.' in float literal"_s);
|
||||
goto end_of_switch;
|
||||
}
|
||||
found_dot = true;
|
||||
}
|
||||
else break;
|
||||
|
||||
lex_advance(s);
|
||||
}
|
||||
lex_set_len(s, &t);
|
||||
lex_parse_u64(&t);
|
||||
if(found_dot) lex_parse_f64(&t);
|
||||
else lex_parse_u64(&t);
|
||||
|
||||
} break;
|
||||
|
||||
case 'A':case 'a':case 'M':case 'm':case 'B':
|
||||
@@ -603,7 +626,7 @@ lex__stream(Intern_Table *table, Array<Token> *array, Lex_Stream *s){
|
||||
default: {
|
||||
token_error(&t, "Unknown token"_s);
|
||||
}
|
||||
}
|
||||
}end_of_switch:
|
||||
|
||||
if(t.len==0)
|
||||
lex_set_len(s,&t);
|
||||
|
||||
Reference in New Issue
Block a user