From 3cb5df5639b21aed270b2cb046b9a1fcf429c3f9 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 4 Apr 2025 18:14:16 +0200 Subject: [PATCH] 0.7 fixes. Improving the yacc grammar. --- lib/std/core/array.c3 | 2 +- lib/std/math/math_nolibc/atanh.c3 | 6 +- lib/std/math/math_nolibc/log1p.c3 | 6 +- resources/examples/retry.c3 | 2 +- resources/grammar/c3.l | 7 ++- resources/grammar/grammar.y | 62 +++++++++++-------- resources/testfragments/allocators_testing.c3 | 2 +- resources/testfragments/demo1.c3 | 2 +- 8 files changed, 51 insertions(+), 38 deletions(-) diff --git a/lib/std/core/array.c3 b/lib/std/core/array.c3 index dca56a592..4175301c3 100644 --- a/lib/std/core/array.c3 +++ b/lib/std/core/array.c3 @@ -26,7 +26,7 @@ macro slice2d(array_ptr, x = 0, xlen = 0, y = 0, ylen = 0) if (xlen < 1) xlen = $typeof((*array_ptr)[0]).len + xlen; if (ylen < 1) ylen = $typeof((*array_ptr)).len + ylen; var $ElementType = $typeof((*array_ptr)[0][0]); - return Slice2d{$ElementType} { ($ElementType*)array_ptr, $typeof((*array_ptr)[0]).len, y, ylen, x, xlen }; + return (Slice2d{$ElementType}) { ($ElementType*)array_ptr, $typeof((*array_ptr)[0]).len, y, ylen, x, xlen }; } diff --git a/lib/std/math/math_nolibc/atanh.c3 b/lib/std/math/math_nolibc/atanh.c3 index 5f7383569..ac2cabc0e 100644 --- a/lib/std/math/math_nolibc/atanh.c3 +++ b/lib/std/math/math_nolibc/atanh.c3 @@ -29,7 +29,7 @@ fn double _atanh(double x) @weak @extern("atanh") @nostrip } return double.nan; /* x<2**-28 */ - case ix < 0x3e300000 && (1e300 + x) > 0.: + case ix < 0x3e300000 && (1e300 + x) > 0.0: return x; } x.set_high_word(ix); @@ -37,11 +37,11 @@ fn double _atanh(double x) @weak @extern("atanh") @nostrip if (ix < 0x3fe00000) { t = x + x; - t = 0.5 * _log1p(t + t * x / (1. - x)); + t = 0.5 * _log1p(t + t * x / (1.0 - x)); } else { - t = 0.5 * _log1p((x + x) / (1. - x)); + t = 0.5 * _log1p((x + x) / (1.0 - x)); } return sign ? -t : t; } diff --git a/lib/std/math/math_nolibc/log1p.c3 b/lib/std/math/math_nolibc/log1p.c3 index 9fe0984a5..7268160c5 100644 --- a/lib/std/math/math_nolibc/log1p.c3 +++ b/lib/std/math/math_nolibc/log1p.c3 @@ -90,7 +90,7 @@ fn double _log1p(double x) @weak @extern("log1p") @nostrip /* correction term ~ log(1+x)-log(u), avoid underflow in c/u */ if (k < 54) { - c = (k >= 2) ? 1. - (u - x) : x - (u - 1.); + c = (k >= 2) ? 1.0 - (u - x) : x - (u - 1.0); c /= u; } else @@ -100,10 +100,10 @@ fn double _log1p(double x) @weak @extern("log1p") @nostrip /* reduce u into [sqrt(2)/2, sqrt(2)] */ hu = (hu & 0x000fffff) + 0x3fe6a09e; u = bitcast(((ulong)hu << 32) | (bitcast(u, ulong) & 0xffffffff) , double); - f = u - 1.; + f = u - 1.0; } double hfsq = 0.5 * f * f; - double s = f / (2. + f); + double s = f / (2.0 + f); double z = s * s; double w = z * z; double t1 = w * (LG2 + w * (LG4 + w * LG6)); diff --git a/resources/examples/retry.c3 b/resources/examples/retry.c3 index 9b8693067..f1fe04edc 100644 --- a/resources/examples/retry.c3 +++ b/resources/examples/retry.c3 @@ -1,7 +1,7 @@ module test; import libc; -faultde NOPE; +faultdef NOPE; fn int? eventually_succeed() { diff --git a/resources/grammar/c3.l b/resources/grammar/c3.l index b6f86518a..724ce27f8 100644 --- a/resources/grammar/c3.l +++ b/resources/grammar/c3.l @@ -112,10 +112,11 @@ typedef struct { [^*]+ { } } \/\/.* { } +"alias" { return(ALIAS); } "any" { return(ANY); } -"anyfault" { return(ANYFAULT); } "asm" { return(ASM); } "assert" { return(ASSERT); } +"attrdef" { return(ATTRDEF); } "bitstruct" { return(BITSTRUCT); } "bool" { return(BOOL); } "break" { return(BREAK); } @@ -124,10 +125,8 @@ typedef struct { "char" { return(CHAR); } "const" { return(CONST); } "continue" { return(CONTINUE); } -"def" { return(DEF); } "default" { return(DEFAULT); } "defer" { return(DEFER); } -"distinct" { return(DISTINCT); } "do" { return(DO); } "double" { return(DOUBLE); } "else" { return(ELSE); } @@ -135,6 +134,7 @@ typedef struct { "extern" { return(EXTERN); } "false" { return(FALSE); } "fault" { return(FAULT); } +"faultdef" { return(FAULTDEF); } "float" { return(FLOAT); } "bfloat16" { return(BFLOAT16); } "float16" { return(FLOAT16); } @@ -165,6 +165,7 @@ typedef struct { "tlocal" { return(TLOCAL); } "true" { return(TRUE); } "try" { return(TRY); } +"typedef" { return(TYPEDEF); } "typeid" { return(TYPEID); } "uint" { return(UINT); } "uint128" { return(UINT128); } diff --git a/resources/grammar/grammar.y b/resources/grammar/grammar.y index 0ff48f06c..57f544359 100644 --- a/resources/grammar/grammar.y +++ b/resources/grammar/grammar.y @@ -14,13 +14,13 @@ void yyerror(YYLTYPE * yylloc_param , yyscan_t yyscanner, const char *yymsgp); %token IDENT HASH_IDENT CT_IDENT CONST_IDENT %token TYPE_IDENT CT_TYPE_IDENT %token AT_TYPE_IDENT AT_IDENT CT_INCLUDE -%token STRING_LITERAL INTEGER +%token STRING_LITERAL INTEGER ALIAS %token CT_AND_OP CT_OR_OP CT_CONCAT_OP CT_EXEC %token INC_OP DEC_OP SHL_OP SHR_OP LE_OP GE_OP EQ_OP NE_OP %token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN %token SUB_ASSIGN SHL_ASSIGN SHR_ASSIGN AND_ASSIGN -%token XOR_ASSIGN OR_ASSIGN VAR NUL ELVIS NEXTCASE ANYFAULT -%token MODULE IMPORT DEF EXTERN +%token XOR_ASSIGN OR_ASSIGN VAR NUL ELVIS NEXTCASE +%token MODULE IMPORT TYPEDEF ATTRDEF FAULTDEF EXTERN %token CHAR SHORT INT LONG FLOAT DOUBLE CONST VOID USZ ISZ UPTR IPTR ANY %token ICHAR USHORT UINT ULONG BOOL INT128 UINT128 FLOAT16 FLOAT128 BFLOAT16 %token TYPEID BITSTRUCT STATIC BANGBANG AT_CONST_IDENT HASH_TYPE_IDENT @@ -536,7 +536,7 @@ base_type_no_user_defined | UPTR | ISZ | USZ - | ANYFAULT + | FAULT | ANY | TYPEID | CT_TYPE_IDENT @@ -567,10 +567,8 @@ type_suffix | '[' constant_expr ']' | '[' ']' | '[' '*' ']' - | '[' '?' ']' | LVEC constant_expr RVEC | LVEC '*' RVEC - | LVEC '?' RVEC ; type : base_type @@ -623,17 +621,17 @@ ct_switch_body ; ct_for_stmt - : CT_FOR '(' for_cond ')' opt_stmt_list CT_ENDFOR + : CT_FOR for_cond ':' opt_stmt_list CT_ENDFOR ; ct_foreach_stmt - : CT_FOREACH '(' CT_IDENT ':' expr ')' opt_stmt_list CT_ENDFOREACH - | CT_FOREACH '(' CT_IDENT ',' CT_IDENT ':' expr ')' opt_stmt_list CT_ENDFOREACH + : CT_FOREACH CT_IDENT ':' expr ':' opt_stmt_list CT_ENDFOREACH + | CT_FOREACH CT_IDENT ',' CT_IDENT ':' expr ':' opt_stmt_list CT_ENDFOREACH ; ct_switch - : CT_SWITCH '(' constant_expr ')' - | CT_SWITCH '(' type ')' - | CT_SWITCH + : CT_SWITCH constant_expr ':' + | CT_SWITCH type ':' + | CT_SWITCH ':' ; ct_switch_stmt @@ -1076,13 +1074,12 @@ enum_declaration ; faults - : CONST_IDENT - | faults ',' CONST_IDENT + : CONST_IDENT opt_attributes + | faults ',' CONST_IDENT opt_attributes ; fault_declaration - : FAULT TYPE_IDENT opt_interface_impl opt_attributes '{' faults '}' - | FAULT TYPE_IDENT opt_interface_impl opt_attributes '{' faults ',' '}' + : FAULTDEF faults ';' ; func_macro_name @@ -1188,9 +1185,24 @@ global_declaration | global_storage optional_type IDENT opt_attributes '=' expr ';' ; +attribute_comma_list + : attribute + | attribute_comma_list ',' attribute + ; + +opt_comma + : ',' + | empty + ; + +define_attribute_body + : empty + | '=' attribute_comma_list opt_comma + ; + define_attribute - : AT_TYPE_IDENT '(' parameters ')' opt_attributes '=' '{' opt_attributes '}' - | AT_TYPE_IDENT opt_attributes '=' '{' opt_attributes '}' + : AT_TYPE_IDENT '(' parameters ')' opt_attributes define_attribute_body + | AT_TYPE_IDENT opt_attributes define_attribute_body ; generic_expr @@ -1203,15 +1215,15 @@ opt_generic_parameters ; define_ident - : IDENT '=' path_ident opt_generic_parameters - | CONST_IDENT '=' path_const opt_generic_parameters - | AT_IDENT '=' path_at_ident opt_generic_parameters + : IDENT opt_attributes '=' path_ident opt_generic_parameters + | CONST_IDENT opt_attributes '=' path_const opt_generic_parameters + | AT_IDENT opt_attributes '=' path_at_ident opt_generic_parameters ; define_declaration - : DEF define_ident opt_attributes ';' - | DEF define_attribute opt_attributes ';' - | DEF TYPE_IDENT opt_attributes '=' typedef_type opt_attributes ';' + : ALIAS define_ident ';' + | ATTRDEF define_attribute ';' + | ALIAS TYPE_IDENT opt_attributes '=' typedef_type opt_attributes ';' ; interface_body @@ -1235,7 +1247,7 @@ interface_declaration_name ; distinct_declaration - : DISTINCT TYPE_IDENT opt_interface_impl opt_attributes '=' opt_inline type ';' + : TYPEDEF TYPE_IDENT opt_interface_impl opt_attributes '=' opt_inline type ';' ; module_param diff --git a/resources/testfragments/allocators_testing.c3 b/resources/testfragments/allocators_testing.c3 index 60fc088f8..d4a956221 100644 --- a/resources/testfragments/allocators_testing.c3 +++ b/resources/testfragments/allocators_testing.c3 @@ -76,7 +76,7 @@ fn void main() testAllocator(tmem, 126); testAllocator(tmem, 12600); ArenaAllocator aa; - aa.init(&&char[1024] {}); + aa.init(&&(char[1024]){}); testAllocator(&aa, 126); io::printn("Test dynamic arena"); DynamicArenaAllocator dynamic_arena; diff --git a/resources/testfragments/demo1.c3 b/resources/testfragments/demo1.c3 index b3fc51529..6b490000f 100644 --- a/resources/testfragments/demo1.c3 +++ b/resources/testfragments/demo1.c3 @@ -13,7 +13,7 @@ struct Foo fn Foo createFoo(int a, int b = 10) { // Compound initializer - return Foo {a, b}; + return (Foo) {a, b}; } struct Bar