pass-lang/src/lex.h

86 lines
1.8 KiB
C

#ifndef LEX_H
#define LEX_H
#include <stdbool.h>
#include <stdint.h>
enum token_type {
TOK_EOF, // end of file
TOK_NAME, // foo, bar_quux123, loop
TOK_LABEL, // 'my_loop
TOK_INTEGER, // -123, 0xDEADBEEF
TOK_STRING, // "..."
TOK_OPERATOR,
TOK_OPEN_BLOCK, // `{` or `:` at the end of a line
TOK_CLOSE_BLOCK, // `}` or inferred from indentation
TOK_OPEN_GROUP, // `(`
TOK_CLOSE_GROUP, // `)`
TOK_TERMINATOR, // `;` or inferred from indentation, used to separate statements in blocks
TOK_SEPARATOR, // `,`, used to separate variables in initializers
TOK_EQUALS, // `=`, used for assignments or as an equality operator
TOK_IF, // if
TOK_ELSE, // else
TOK_MATCH, // match
TOK_CASE, // case
TOK_LOOP, // loop
TOK_FN, // fn
TOK_NEXT, // next
TOK_EXIT, // exit
TOK_RECURSE, // recurse
TOK_RETURN, // return
};
enum operator_ {
OP_EQ, // =
OP_ADD, // +
OP_SUB, // -
OP_MUL, // *
OP_DIV, // /
OP_MOD, // %
OP_INV, // ~
OP_AND, // &
OP_OR, // |
OP_XOR, // ^
OP_SHL, // <<
OP_SAR, // >>
OP_SHR, // >>>
OP_NOT, // !
OP_GT, // >
OP_LT, // <
OP_GTE, // >=
OP_LTE, // <=
OP_NE, // !=
OP_TYPE, // :
OP_FUN, // ->
OP_JUXT, // space! but this is not emitted by the lexer.
};
union token_data {
char* name;
char* label;
char* string;
int64_t int_;
enum operator_ op;
};
struct token {
enum token_type type;
union token_data data;
};
_Bool is_unary(enum operator_ op);
_Bool is_binary(enum operator_ op);
_Bool is_lit(struct token tok);
struct token next(void);
struct token peek(void);
void print_token(struct token tok);
#endif