diff --git a/resources/lib/std/cinterop.c3 b/resources/lib/std/cinterop.c3 index e395abc72..693b1d45e 100644 --- a/resources/lib/std/cinterop.c3 +++ b/resources/lib/std/cinterop.c3 @@ -16,65 +16,71 @@ $assert (C_SHORT_SIZE <= C_INT_SIZE); $assert (C_INT_SIZE <= C_LONG_SIZE); $assert (C_LONG_SIZE <= C_LONG_LONG_SIZE); -$if C_INT_SIZE == 64: +$switch ($$C_INT_SIZE): +$case 64: define CInt = long; define CUInt = ulong; -$elif C_INT_SIZE == 32: +$case 32: define CInt = int; define CUInt = uint; -$elif C_INT_SIZE == 16: +$case 16: define CInt = short; define CUInt = ushort; -$else: +$default: $assert(false, "Invalid C int size"); -$endif; +$endswitch; -$if C_LONG_SIZE == 64: +$switch ($$C_LONG_SIZE): +$case 64: define CLong = long; define CULong = ulong; -$elif C_LONG_SIZE == 32: +$case 32: define CLong = int; define CULong = uint; -$elif C_LONG_SIZE == 16: +$case 16: define CLong = short; define CULong = ushort; -$else: +$default: $assert(false, "Invalid C long size"); -$endif; +$endswitch; -$if C_SHORT_SIZE == 32: +$switch ($$C_SHORT_SIZE): +$case 32: define CShort = int; define CUShort = uint; -$elif C_SHORT_SIZE == 16: +$case 16: define CShort = short; define CUShort = ushort; -$elif C_SHORT_SIZE == 8: +$case 8: define CShort = ichar; define CUShort = char; -$else: +$default: $assert(false, "Invalid C short size"); -$endif; +$endswitch; -$if C_LONG_LONG_SIZE == 128: +$switch ($$C_LONG_LONG_SIZE): +$case 128: define CLongLong = int128; define CULongLong = uint128; -$elif C_LONG_LONG_SIZE == 64: +$case 64: define CLongLong = long; define CULongLong = ulong; -$elif C_LONG_LONG_SIZE == 32: +$case 32: define CLongLong = int; define CULongLong = uint; -$elif C_LONG_LONG_SIZE == 16: +$case 16: define CLongLong = short; define CULongLong = ushort; -$else: +$default: $assert(false, "Invalid C long long size"); -$endif; +$endswitch; + + define CSChar = ichar; define CUChar = char; -$if $$C_CHAR_IS_SIGNED: +$if ($$C_CHAR_IS_SIGNED): define CChar = ichar; $else: define CChar = char; diff --git a/resources/lib/std/enumset.c3 b/resources/lib/std/enumset.c3 index 82077f3b8..a1ab046fb 100644 --- a/resources/lib/std/enumset.c3 +++ b/resources/lib/std/enumset.c3 @@ -5,29 +5,23 @@ $assert(Enum.min < Enum.max, "Only strictly increasing enums may be used with en $assert(Enum.max < 64, "Maximum value of an enum used as enum set is 63"); $assert(Enum.min >= 0, "Minimum value of an enum used as enum set is 0"); -$switch $$C_INT_SIZE: - +$switch ($$C_INT_SIZE): $case 64: -private define EnumSetType = ulong; - + private define EnumSetType = ulong; $case 32: - - $if Enum.max < 32: -private define EnumSetType = uint; + $if (Enum.max < 32): + private define EnumSetType = uint; $else: -private define EnumSetType = ulong; + private define EnumSetType = ulong; $endif; - $default: - - $if Enum.max < 16: -private define EnumSetType = ushort; - $elif Enum.max < 31: -private define EnumSetType = uint; + $if (Enum.max < 16): + private define EnumSetType = ushort; + $elif (Enum.max < 31): + private define EnumSetType = uint; $else: -private define EnumSetType = ulong; + private define EnumSetType = ulong; $endif; - $endswitch; define EnumSet = distinct EnumSetType; diff --git a/resources/lib/std/libc.c3 b/resources/lib/std/libc.c3 index 9d452e125..952cded6e 100644 --- a/resources/lib/std/libc.c3 +++ b/resources/lib/std/libc.c3 @@ -248,19 +248,8 @@ const int EOF = -1; const int FOPEN_MAX = 20; const int FILENAME_MAX = 1024; -$if $$C_INT_SIZE == 64: -define ErrnoType = long; -$elif $$C_INT_SIZE == 32: -define ErrnoType = int; -$else: -define ErrnoType = short; -$endif; - -$if $$C_LONG_SIZE == 64: -define SeekIndex = long; -$else: -define SeekIndex = int; -$endif; +define ErrnoType = CInt; +define SeekIndex = CLong; extern fn int fclose(CFile stream); extern fn void clearerr(CFile stream); @@ -302,11 +291,7 @@ extern fn void perror(char* str); // time.h -$if $$C_LONG_SIZE == 64: -define TimeOffset = long; -$else: -define TimeOffset = int; -$endif; +define TimeOffset = CLong; struct Tm { diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index c02250834..0b45b1209 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -143,7 +143,7 @@ static inline Decl *parse_ct_if_top_level(ParseContext *context) { advance_and_verify(context, TOKEN_CT_IF); Decl *ct = decl_new_ct(DECL_CT_IF, context->prev_tok); - ASSIGN_EXPR_ELSE(ct->ct_if_decl.expr, parse_constant_expr(context), poisoned_decl); + ASSIGN_EXPR_ELSE(ct->ct_if_decl.expr, parse_const_paren_expr(context), poisoned_decl); if (!parse_top_level_block(context, &ct->ct_if_decl.then, TOKEN_CT_ENDIF, TOKEN_CT_ELIF, TOKEN_CT_ELSE)) return poisoned_decl; @@ -152,7 +152,7 @@ static inline Decl *parse_ct_if_top_level(ParseContext *context) { advance_and_verify(context, TOKEN_CT_ELIF); Decl *ct_elif = decl_new_ct(DECL_CT_ELIF, context->prev_tok); - ASSIGN_EXPR_ELSE(ct_elif->ct_elif_decl.expr, parse_constant_expr(context), poisoned_decl); + ASSIGN_EXPR_ELSE(ct_elif->ct_elif_decl.expr, parse_const_paren_expr(context), poisoned_decl); if (!parse_top_level_block(context, &ct_elif->ct_elif_decl.then, TOKEN_CT_ENDIF, TOKEN_CT_ELIF, TOKEN_CT_ELSE)) return poisoned_decl; ct_if_decl->elif = ct_elif; @@ -214,7 +214,7 @@ static inline Decl *parse_ct_switch_top_level(ParseContext *context) { advance_and_verify(context, TOKEN_CT_SWITCH); Decl *ct = decl_new_ct(DECL_CT_SWITCH, context->prev_tok); - ASSIGN_EXPR_ELSE(ct->ct_switch_decl.expr, parse_constant_expr(context), poisoned_decl); + ASSIGN_EXPR_ELSE(ct->ct_switch_decl.expr, parse_const_paren_expr(context), poisoned_decl); CONSUME_OR(TOKEN_COLON, poisoned_decl); while (!try_consume(context, TOKEN_CT_ENDSWITCH)) diff --git a/src/compiler/parse_stmt.c b/src/compiler/parse_stmt.c index f8b35e44a..e7c455b47 100644 --- a/src/compiler/parse_stmt.c +++ b/src/compiler/parse_stmt.c @@ -713,7 +713,7 @@ static inline Ast* parse_ct_switch_stmt(ParseContext *context) { Ast *ast = AST_NEW_TOKEN(AST_CT_SWITCH_STMT, context->tok); advance_and_verify(context, TOKEN_CT_SWITCH); - ASSIGN_EXPR_ELSE(ast->ct_switch_stmt.cond, parse_constant_expr(context), poisoned_ast); + ASSIGN_EXPR_ELSE(ast->ct_switch_stmt.cond, parse_const_paren_expr(context), poisoned_ast); TRY_CONSUME(TOKEN_COLON, "Expected ':' after $switch expression, did you forget it?"); Ast **cases = NULL; while (!try_consume(context, TOKEN_CT_ENDSWITCH)) diff --git a/test/test_suite/compile_time/ct_if_fails.c3 b/test/test_suite/compile_time/ct_if_fails.c3 index d16363120..1d582b250 100644 --- a/test/test_suite/compile_time/ct_if_fails.c3 +++ b/test/test_suite/compile_time/ct_if_fails.c3 @@ -1,28 +1,28 @@ int x; -$if x > 0: +$if (x > 0): $endif; -$if 0: +$if (0): $assert(false); $endif; -$if 1: +$if (1): $else: $endif; -$if 1: +$if (1): $else: $else: // #error: Expected a top level declaration here. $endif; -$if 1: -$elif 2: +$if (1): +$elif (2): $else: $endif; -$if 1: -$elif 2: -$elif 3: +$if (1): +$elif (2): +$elif (3): $else: $endif; diff --git a/test/test_suite/compile_time/ct_switch_top_level.c3t b/test/test_suite/compile_time/ct_switch_top_level.c3t index 71e1a7f29..9cdd66701 100644 --- a/test/test_suite/compile_time/ct_switch_top_level.c3t +++ b/test/test_suite/compile_time/ct_switch_top_level.c3t @@ -6,20 +6,20 @@ extern fn void printf(char*, ...); macro tester() { $Type = int; - $switch $Type: + $switch ($Type): $case int: - printf("Hello\n"); - int z = 0; + printf("Hello\n"); + int z = 0; $default: - int j = 213; + int j = 213; $endswitch; } -$switch bool.typeid: +$switch (bool.typeid): $case int: -int oefke = 23; + int oefke = 23; $default: -int oeoekgokege = 343432; + int oeoekgokege = 343432; $endswitch; fn int main()