Introduce def as a trial. Fixup of timeit.

This commit is contained in:
Christoffer Lerno
2023-04-21 16:03:28 +02:00
parent edd2f1c717
commit c847650579
6 changed files with 50 additions and 9 deletions

View File

@@ -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());
}

View File

@@ -517,6 +517,7 @@ typedef enum
TOKEN_CATCH,
TOKEN_CONST,
TOKEN_CONTINUE,
TOKEN_DEF,
TOKEN_DEFINE,
TOKEN_DEFAULT,
TOKEN_DEFER,

View File

@@ -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);

View File

@@ -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:

View File

@@ -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:

View File

@@ -1 +1 @@
#define COMPILER_VERSION "0.4.501"
#define COMPILER_VERSION "0.4.502"