2026-03-04 18:54:46 -06:00
|
|
|
#ifndef LEXER_H
|
|
|
|
|
#define LEXER_H
|
|
|
|
|
|
2026-04-23 15:37:16 -06:00
|
|
|
#include "arraylist.h"
|
2026-03-04 18:54:46 -06:00
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdbool.h>
|
2026-03-09 09:23:06 -06:00
|
|
|
#include <stdint.h>
|
2026-03-04 18:54:46 -06:00
|
|
|
|
|
|
|
|
// For identifing
|
|
|
|
|
typedef enum {
|
2026-03-09 09:23:06 -06:00
|
|
|
NODE_INTEGER,
|
2026-03-05 10:20:44 -06:00
|
|
|
NODE_BINARY_OP,
|
2026-05-12 18:15:36 -06:00
|
|
|
NODE_UNARY_OP,
|
2026-03-04 18:54:46 -06:00
|
|
|
} ASTNodeType;
|
|
|
|
|
|
|
|
|
|
// For classify operators
|
|
|
|
|
typedef enum {
|
|
|
|
|
OP_ADD,
|
|
|
|
|
OP_SUB,
|
|
|
|
|
OP_MUL,
|
2026-05-12 18:15:36 -06:00
|
|
|
OP_DIV,
|
|
|
|
|
OP_POW,
|
2026-03-04 18:54:46 -06:00
|
|
|
} Operator;
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
LEXER_OK = 0,
|
2026-03-09 11:58:55 -06:00
|
|
|
LEXER_INT_OVERFLOW,
|
2026-03-04 18:54:46 -06:00
|
|
|
LEXER_FAILED_NUMBER_CONVERSION,
|
|
|
|
|
LEXER_NOT_RECOGNIZED_SYMBOL,
|
|
|
|
|
LEXER_EMPTY_INPUT,
|
2026-03-09 09:06:06 -06:00
|
|
|
LEXER_BUF_OVERFLOW,
|
2026-03-04 18:54:46 -06:00
|
|
|
} LexerErr;
|
|
|
|
|
|
|
|
|
|
// Can be thought as tokens, they will be used by the parser.
|
|
|
|
|
typedef struct ASTNode {
|
|
|
|
|
ASTNodeType type;
|
|
|
|
|
union {
|
2026-03-09 09:23:06 -06:00
|
|
|
int64_t integer;
|
2026-03-05 10:20:44 -06:00
|
|
|
struct {
|
|
|
|
|
struct ASTNode *left;
|
|
|
|
|
struct ASTNode *right;
|
|
|
|
|
Operator op;
|
|
|
|
|
} binary;
|
2026-05-12 18:15:36 -06:00
|
|
|
struct {
|
|
|
|
|
struct ASTNode *val;
|
|
|
|
|
Operator op;
|
|
|
|
|
} unary;
|
2026-03-05 10:20:44 -06:00
|
|
|
} data;
|
2026-03-04 18:54:46 -06:00
|
|
|
} ASTNode;
|
|
|
|
|
|
2026-04-24 07:02:00 -06:00
|
|
|
typedef struct {
|
|
|
|
|
bool is_valid;
|
|
|
|
|
union {
|
|
|
|
|
LexerErr err;
|
|
|
|
|
ArrayList *arr;
|
|
|
|
|
};
|
|
|
|
|
} TokenizeResult;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
bool is_valid;
|
|
|
|
|
union {
|
|
|
|
|
LexerErr err;
|
|
|
|
|
ASTNode node;
|
|
|
|
|
};
|
|
|
|
|
} ASTNodeResult;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
bool is_valid;
|
|
|
|
|
union {
|
|
|
|
|
LexerErr err;
|
|
|
|
|
int64_t number;
|
|
|
|
|
};
|
2026-04-30 21:34:27 -06:00
|
|
|
} LexerI64Result;
|
2026-03-04 18:54:46 -06:00
|
|
|
|
|
|
|
|
// Lexer funtions as well as few functionality
|
2026-04-24 07:17:35 -06:00
|
|
|
TokenizeResult tokenize(const char* input);
|
|
|
|
|
ASTNodeResult tokenize_number(const char* input, size_t *offset);
|
2026-04-30 21:34:27 -06:00
|
|
|
LexerI64Result string_to_integer(const char buf[]);
|
2026-03-10 07:08:12 -06:00
|
|
|
bool isoperator(int c);
|
2026-03-13 07:58:38 -06:00
|
|
|
Operator char_to_operator(int c);
|
2026-03-25 06:59:52 -06:00
|
|
|
char operator_to_char(Operator op);
|
2026-03-04 18:54:46 -06:00
|
|
|
|
|
|
|
|
#endif // !LEXER_H
|