From c8476505797f968af2ab1ecc2249323c3fd5c692 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 21 Apr 2023 16:03:28 +0200 Subject: [PATCH] Introduce `def` as a trial. Fixup of timeit. --- resources/examples/timeit.c3 | 25 ++++++++++++++++++++----- src/compiler/enums.h | 1 + src/compiler/parse_global.c | 28 +++++++++++++++++++++++++--- src/compiler/parse_stmt.c | 1 + src/compiler/tokens.c | 2 ++ src/version.h | 2 +- 6 files changed, 50 insertions(+), 9 deletions(-) diff --git a/resources/examples/timeit.c3 b/resources/examples/timeit.c3 index cd0e62ba1..b3a0e1e94 100644 --- a/resources/examples/timeit.c3 +++ b/resources/examples/timeit.c3 @@ -1,12 +1,27 @@ module test; import std::time; +import std::io; -public macro timeit(#call) +def foo = write; + +def Int = int; + +macro @timeit(#call) { - Time t = time::current(); - typeof(#call) result = #call; - TimeDiff diff = time::current() - t; - libc::printf("'%s' took %f ms\n", $stringify(#call), diff * 1000); + Clock t = clock::now(); + var result = #call; + io::printfn("'%s' took %d ns", $stringify(#call), t.mark()); return result; } +fn int write() +{ + for (int i = 0; i < 1000; i++) io::printf("%d", i); + io::printn(); + return 0; +} + +fn void main() +{ + @timeit(write()); +} \ No newline at end of file diff --git a/src/compiler/enums.h b/src/compiler/enums.h index 167d82f92..2e3d2e61c 100644 --- a/src/compiler/enums.h +++ b/src/compiler/enums.h @@ -517,6 +517,7 @@ typedef enum TOKEN_CATCH, TOKEN_CONST, TOKEN_CONTINUE, + TOKEN_DEF, TOKEN_DEFINE, TOKEN_DEFAULT, TOKEN_DEFER, diff --git a/src/compiler/parse_global.c b/src/compiler/parse_global.c index c570b5926..8d670d144 100644 --- a/src/compiler/parse_global.c +++ b/src/compiler/parse_global.c @@ -46,6 +46,7 @@ void recover_top_level(ParseContext *c) case TOKEN_EXTERN: case TOKEN_ENUM: case TOKEN_DEFINE: + case TOKEN_DEF: case TOKEN_TYPEDEF: case TOKEN_FAULT: return; @@ -1734,7 +1735,7 @@ static inline void decl_add_type(Decl *decl, TypeKind kind) */ static inline Decl *parse_typedef_declaration(ParseContext *c) { - advance_and_verify(c, TOKEN_TYPEDEF); + if (!try_consume(c, TOKEN_DEF)) advance_and_verify(c, TOKEN_TYPEDEF); Decl *decl = decl_new(DECL_POISONED, symstr(c), c->span); DEBUG_LOG("Parse typedef %s", decl->name); @@ -1846,7 +1847,7 @@ static inline Decl *parse_typedef_declaration(ParseContext *c) static inline Decl *parse_define_ident(ParseContext *c) { // 1. Store the beginning of the "define". - advance_and_verify(c, TOKEN_DEFINE); + if (!try_consume(c, TOKEN_DEF)) advance_and_verify(c, TOKEN_DEFINE); // 2. At this point we expect an ident or a const token. // since the Type is handled. @@ -1937,7 +1938,7 @@ static inline Decl *parse_define_ident(ParseContext *c) static inline Decl *parse_define_attribute(ParseContext *c) { // 1. Store the beginning of the "define". - advance_and_verify(c, TOKEN_DEFINE); + if (!try_consume(c, TOKEN_DEF)) advance_and_verify(c, TOKEN_DEFINE); Decl *decl = decl_new(DECL_ATTRIBUTE, symstr(c), c->span); @@ -1982,6 +1983,23 @@ static inline Decl *parse_define(ParseContext *c) } } +/** + * define_decl ::= DEFINE define_type_body | + */ +static inline Decl *parse_def(ParseContext *c) +{ + switch (peek(c)) + { + case TOKEN_TYPE_IDENT: + return parse_typedef_declaration(c); + case TOKEN_AT_TYPE_IDENT: + // define @Foo = @inline, @noreturn + return parse_define_attribute(c); + default: + return parse_define_ident(c); + } +} + static inline bool parse_is_macro_name(ParseContext *c) { return (tok_is(c, TOKEN_IDENT) && peek(c) != TOKEN_SCOPE) || tok_is(c, TOKEN_AT_IDENT); @@ -2782,6 +2800,10 @@ Decl *parse_top_level_statement(ParseContext *c, ParseContext **c_ref) if (contracts) goto CONTRACT_NOT_ALLOWED; decl = parse_typedef_declaration(c); break; + case TOKEN_DEF: + if (contracts) goto CONTRACT_NOT_ALLOWED; + decl = parse_def(c); + break; case TOKEN_DEFINE: if (contracts) goto CONTRACT_NOT_ALLOWED; decl = parse_define(c); diff --git a/src/compiler/parse_stmt.c b/src/compiler/parse_stmt.c index 3edb75aa9..4c136d1d1 100644 --- a/src/compiler/parse_stmt.c +++ b/src/compiler/parse_stmt.c @@ -1303,6 +1303,7 @@ Ast *parse_stmt(ParseContext *c) case TOKEN_FAULT: case TOKEN_UNION: case TOKEN_DEFINE: + case TOKEN_DEF: case TOKEN_TYPEDEF: case TOKEN_DOCS_START: case TOKEN_DOCS_END: diff --git a/src/compiler/tokens.c b/src/compiler/tokens.c index 4a929d010..e62bf7c6f 100644 --- a/src/compiler/tokens.c +++ b/src/compiler/tokens.c @@ -203,6 +203,8 @@ const char *token_type_to_string(TokenType type) return "const"; case TOKEN_CONTINUE: return "continue"; + case TOKEN_DEF: + return "def"; case TOKEN_DEFAULT: return "default"; case TOKEN_DEFER: diff --git a/src/version.h b/src/version.h index 93ea1a6ab..c0a043bac 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "0.4.501" \ No newline at end of file +#define COMPILER_VERSION "0.4.502" \ No newline at end of file