aboutsummaryrefslogtreecommitdiff
path: root/src/tokens.c
blob: c90cfd96c2f1d85089504840f60e8716ccc3a684 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "tokens.h"
#include <assert.h>
#include <stdio.h>

Token Token_from_type(TokenType type, Location loc)
{
    Token token = {0};
    token.type = type;
    token.loc = loc;
    return token;
}

Token Token_from_int(i64 value, Location loc)
{
    Token token = {0};
    token.type = TOKEN_INTLIT;
    token.value.as_int = value;
    token.loc = loc;
    return token;
}

Token Token_from_string(char *value, Location loc)
{
    Token token = {0};
    token.type = TOKEN_STRINGLIT;
    token.value.as_string = value;
    token.loc = loc;
    return token;
}

Token Token_from_char(char value, Location loc)
{
    Token token = {0};
    token.type = TOKEN_CHARLIT;
    token.value.as_char = value;
    token.loc = loc;
    return token;
}

Token Token_from_identifier(char *value, Location loc)
{
    Token token = {0};
    token.type = TOKEN_IDENTIFIER;
    token.value.as_string = value;
    token.loc = loc;
    return token;
}

void Location_print(FILE *f, Location loc)
{
    fprintf(f, "%s:%d:%d", loc.filename, loc.line+1, loc.col+1);
}

char *token_type_to_str(TokenType type)
{
    // Otherwise, just print the token type
    switch (type)
    {
    #define HANDLE_ITEM(name, str) case name: return str;
        ENUM_TOKENS(HANDLE_ITEM)
        ENUM_KEYWORDS(HANDLE_ITEM)
    #undef HANDLE_ITEM
    default: assert(false && "Unreachable");
    }
}


void Token_print(FILE *f, Token *token)
{
    // Handle the different literals manually
    switch (token->type)
    {
        case TOKEN_INTLIT:      fprintf(f, "%lld", token->value.as_int); return;
        case TOKEN_STRINGLIT:   fprintf(f, "\"%s\"", token->value.as_string); return;
        case TOKEN_IDENTIFIER:  fprintf(f, "%s", token->value.as_string); return;
        default: break;
    }

    fprintf(f, "%s", token_type_to_str(token->type));
}

bool is_literal_token(TokenType type)
{
    switch (type)
    {
        case TOKEN_INTLIT:
        case TOKEN_STRINGLIT:
        case TOKEN_CHARLIT:
            return true;
        default:
            return false;
    }
}