summaryrefslogtreecommitdiff
path: root/src/lexer.h
blob: d03e621a74017da82a57b1c7374af114e53ad1da (plain)
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
/*
 * Copyright (C) 2025  Johnny Richard <johnny@johnnyrichard.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#ifndef LEXER_H
#define LEXER_H
#include "string_view.h"

typedef enum token_kind
{
    TOKEN_KW_PUSH,
    TOKEN_KW_DUP,
    TOKEN_KW_COPY,
    TOKEN_KW_SWAP,
    TOKEN_KW_DROP,
    TOKEN_KW_SLIDE,
    TOKEN_KW_ADD,
    TOKEN_KW_SUB,
    TOKEN_KW_MUL,
    TOKEN_KW_DIV,
    TOKEN_KW_MOD,
    TOKEN_KW_STORE,
    TOKEN_KW_LOAD,
    TOKEN_KW_CALL,
    TOKEN_KW_RET,
    TOKEN_KW_JMP,
    TOKEN_KW_JMPZ,
    TOKEN_KW_JMPN,
    TOKEN_KW_PRINTI,
    TOKEN_KW_PRINTC,
    TOKEN_KW_READI,
    TOKEN_KW_READC,
    TOKEN_KW_END,
    TOKEN_EOS,
    TOKEN_NUMBER,
    TOKEN_IDENT,
    TOKEN_COLON,
    TOKEN_UNKOWN,
    TOKEN_EOF
} token_kind_t;

typedef struct lex_loc
{
    size_t offset;
    size_t lineoffset;
    size_t lineno;
} lex_loc_t;

typedef struct token
{
    token_kind_t kind;
    string_view_t value;
    lex_loc_t loc;
} token_t;

typedef struct lexer
{
    char* file_name;
    string_view_t source;
    lex_loc_t loc;
} lexer_t;

void
lexer_init(lexer_t* lexer, char* file_name);

bool
lexer_is_eof(lexer_t* lexer);

char
lexer_current_char(lexer_t* lexer);

char
lexer_next_char(lexer_t* lexer);

void
lexer_next_token(lexer_t* lexer, token_t* token);

char*
token_to_cstr(token_kind_t kind);

#endif /* LEXER_H */