eval expressions: unary +, correctly lex exponentiated numerics

This commit is contained in:
Ryan Fleury
2024-10-21 09:23:31 -07:00
parent 52245a8e5a
commit d0915ec9a7
6 changed files with 25 additions and 9 deletions
+5 -4
View File
@@ -696,27 +696,28 @@ f64_from_str8(String8 string)
{ {
// rjf: find starting pos of numeric string, as well as sign // rjf: find starting pos of numeric string, as well as sign
F64 sign = +1.0; F64 sign = +1.0;
//U64 first_numeric = 0;
if(string.str[0] == '-') if(string.str[0] == '-')
{ {
//first_numeric = 1;
sign = -1.0; sign = -1.0;
} }
else if(string.str[0] == '+') else if(string.str[0] == '+')
{ {
//first_numeric = 1;
sign = 1.0; sign = 1.0;
} }
// rjf: gather numerics // rjf: gather numerics
U64 num_valid_chars = 0; U64 num_valid_chars = 0;
char buffer[64]; char buffer[64];
B32 exp = 0;
for(U64 idx = 0; idx < string.size && num_valid_chars < sizeof(buffer)-1; idx += 1) for(U64 idx = 0; idx < string.size && num_valid_chars < sizeof(buffer)-1; idx += 1)
{ {
if(char_is_digit(string.str[idx], 10) || string.str[idx] == '.') if(char_is_digit(string.str[idx], 10) || string.str[idx] == '.' || string.str[idx] == 'e' ||
(exp && (string.str[idx] == '+' || string.str[idx] == '-')))
{ {
buffer[num_valid_chars] = string.str[idx]; buffer[num_valid_chars] = string.str[idx];
num_valid_chars += 1; num_valid_chars += 1;
exp = 0;
exp = (string.str[idx] == 'e');
} }
} }
+1
View File
@@ -90,6 +90,7 @@ E_ExprKindTable:
{ Typeof UnaryPrefix 1 "typeof" "typeof" "(" ")"} { Typeof UnaryPrefix 1 "typeof" "typeof" "(" ")"}
{ ByteSwap UnaryPrefix 1 "bswap" "bswap" "(" ")"} { ByteSwap UnaryPrefix 1 "bswap" "bswap" "(" ")"}
{ Pos UnaryPrefix 2 "+" "+" "" "" }
{ Neg UnaryPrefix 2 "-" "-" "" "" } { Neg UnaryPrefix 2 "-" "-" "" "" }
{ LogNot UnaryPrefix 2 "!" "!" "" "" } { LogNot UnaryPrefix 2 "!" "!" "" "" }
{ BitNot UnaryPrefix 2 "~" "~" "" "" } { BitNot UnaryPrefix 2 "~" "~" "" "" }
+4
View File
@@ -846,6 +846,10 @@ e_irtree_and_type_from_expr(Arena *arena, E_Expr *expr)
}break; }break;
//- rjf: unary operations //- rjf: unary operations
case E_ExprKind_Pos:
{
result = e_irtree_and_type_from_expr(arena, expr->first);
}break;
case E_ExprKind_Neg: case E_ExprKind_Neg:
case E_ExprKind_LogNot: case E_ExprKind_LogNot:
case E_ExprKind_BitNot: case E_ExprKind_BitNot:
+8 -1
View File
@@ -389,6 +389,7 @@ e_token_array_from_text(Arena *arena, String8 text)
E_TokenKind active_token_kind = E_TokenKind_Null; E_TokenKind active_token_kind = E_TokenKind_Null;
B32 active_token_kind_started_with_tick = 0; B32 active_token_kind_started_with_tick = 0;
B32 escaped = 0; B32 escaped = 0;
B32 exp = 0;
for(U64 idx = 0, advance = 0; idx <= text.size; idx += advance) for(U64 idx = 0, advance = 0; idx <= text.size; idx += advance)
{ {
U8 byte = (idx+0 < text.size) ? text.str[idx+0] : 0; U8 byte = (idx+0 < text.size) ? text.str[idx+0] : 0;
@@ -486,11 +487,17 @@ e_token_array_from_text(Arena *arena, String8 text)
}break; }break;
case E_TokenKind_Numeric: case E_TokenKind_Numeric:
{ {
if(!char_is_alpha(byte) && !char_is_digit(byte, 10) && byte != '.' && byte != ':') if(exp && byte == '+' || byte == '-'){}
else if(!char_is_alpha(byte) && !char_is_digit(byte, 10) && byte != '.' && byte != ':')
{ {
advance = 0; advance = 0;
token_formed = 1; token_formed = 1;
} }
else
{
exp = 0;
exp = (byte == 'e');
}
}break; }break;
case E_TokenKind_StringLiteral: case E_TokenKind_StringLiteral:
{ {
+4 -2
View File
@@ -14,7 +14,7 @@ str8_lit_comp("CharLiteral"),
str8_lit_comp("Symbol"), str8_lit_comp("Symbol"),
}; };
String8 e_expr_kind_strings[47] = String8 e_expr_kind_strings[48] =
{ {
str8_lit_comp("Nil"), str8_lit_comp("Nil"),
str8_lit_comp("Ref"), str8_lit_comp("Ref"),
@@ -26,6 +26,7 @@ str8_lit_comp("Cast"),
str8_lit_comp("Sizeof"), str8_lit_comp("Sizeof"),
str8_lit_comp("Typeof"), str8_lit_comp("Typeof"),
str8_lit_comp("ByteSwap"), str8_lit_comp("ByteSwap"),
str8_lit_comp("Pos"),
str8_lit_comp("Neg"), str8_lit_comp("Neg"),
str8_lit_comp("LogNot"), str8_lit_comp("LogNot"),
str8_lit_comp("BitNot"), str8_lit_comp("BitNot"),
@@ -80,7 +81,7 @@ str8_lit_comp("Insufficient evaluation machine stack space."),
str8_lit_comp("Malformed bytecode."), str8_lit_comp("Malformed bytecode."),
}; };
E_OpInfo e_expr_kind_op_info_table[47] = E_OpInfo e_expr_kind_op_info_table[48] =
{ {
{ E_OpKind_Null, 0, str8_lit_comp(""), str8_lit_comp(""), str8_lit_comp("") }, { E_OpKind_Null, 0, str8_lit_comp(""), str8_lit_comp(""), str8_lit_comp("") },
{ E_OpKind_Null, 0, str8_lit_comp(""), str8_lit_comp(""), str8_lit_comp("") }, { E_OpKind_Null, 0, str8_lit_comp(""), str8_lit_comp(""), str8_lit_comp("") },
@@ -92,6 +93,7 @@ E_OpInfo e_expr_kind_op_info_table[47] =
{ E_OpKind_UnaryPrefix, 1, str8_lit_comp("sizeof"), str8_lit_comp("("), str8_lit_comp(")") }, { E_OpKind_UnaryPrefix, 1, str8_lit_comp("sizeof"), str8_lit_comp("("), str8_lit_comp(")") },
{ E_OpKind_UnaryPrefix, 1, str8_lit_comp("typeof"), str8_lit_comp("("), str8_lit_comp(")") }, { E_OpKind_UnaryPrefix, 1, str8_lit_comp("typeof"), str8_lit_comp("("), str8_lit_comp(")") },
{ E_OpKind_UnaryPrefix, 1, str8_lit_comp("bswap"), str8_lit_comp("("), str8_lit_comp(")") }, { E_OpKind_UnaryPrefix, 1, str8_lit_comp("bswap"), str8_lit_comp("("), str8_lit_comp(")") },
{ E_OpKind_UnaryPrefix, 2, str8_lit_comp("+"), str8_lit_comp(""), str8_lit_comp("") },
{ E_OpKind_UnaryPrefix, 2, str8_lit_comp("-"), str8_lit_comp(""), str8_lit_comp("") }, { E_OpKind_UnaryPrefix, 2, str8_lit_comp("-"), str8_lit_comp(""), str8_lit_comp("") },
{ E_OpKind_UnaryPrefix, 2, str8_lit_comp("!"), str8_lit_comp(""), str8_lit_comp("") }, { E_OpKind_UnaryPrefix, 2, str8_lit_comp("!"), str8_lit_comp(""), str8_lit_comp("") },
{ E_OpKind_UnaryPrefix, 2, str8_lit_comp("~"), str8_lit_comp(""), str8_lit_comp("") }, { E_OpKind_UnaryPrefix, 2, str8_lit_comp("~"), str8_lit_comp(""), str8_lit_comp("") },
+3 -2
View File
@@ -101,6 +101,7 @@ E_ExprKind_Cast,
E_ExprKind_Sizeof, E_ExprKind_Sizeof,
E_ExprKind_Typeof, E_ExprKind_Typeof,
E_ExprKind_ByteSwap, E_ExprKind_ByteSwap,
E_ExprKind_Pos,
E_ExprKind_Neg, E_ExprKind_Neg,
E_ExprKind_LogNot, E_ExprKind_LogNot,
E_ExprKind_BitNot, E_ExprKind_BitNot,
@@ -159,9 +160,9 @@ E_InterpretationCode_COUNT,
C_LINKAGE_BEGIN C_LINKAGE_BEGIN
extern String8 e_token_kind_strings[6]; extern String8 e_token_kind_strings[6];
extern String8 e_expr_kind_strings[47]; extern String8 e_expr_kind_strings[48];
extern String8 e_interpretation_code_display_strings[11]; extern String8 e_interpretation_code_display_strings[11];
extern E_OpInfo e_expr_kind_op_info_table[47]; extern E_OpInfo e_expr_kind_op_info_table[48];
extern U8 e_kind_basic_byte_size_table[56]; extern U8 e_kind_basic_byte_size_table[56];
extern String8 e_kind_basic_string_table[56]; extern String8 e_kind_basic_string_table[56];