80 lines
1.5 KiB
C
80 lines
1.5 KiB
C
#ifndef LEXER_H
|
|
#define LEXER_H
|
|
|
|
#include "arraylist.h"
|
|
#include <stddef.h>
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
// For identifing
|
|
typedef enum {
|
|
NODE_INTEGER,
|
|
NODE_BINARY_OP,
|
|
} ASTNodeType;
|
|
|
|
// For classify operators
|
|
typedef enum {
|
|
OP_ADD,
|
|
OP_SUB,
|
|
OP_MUL,
|
|
OP_DIV
|
|
} Operator;
|
|
|
|
typedef enum {
|
|
LEXER_OK = 0,
|
|
LEXER_INT_OVERFLOW,
|
|
LEXER_FAILED_NUMBER_CONVERSION,
|
|
LEXER_NOT_RECOGNIZED_SYMBOL,
|
|
LEXER_EMPTY_INPUT,
|
|
LEXER_NULL_ARG,
|
|
LEXER_WRONG_SYNTAX,
|
|
LEXER_BUF_OVERFLOW,
|
|
} LexerErr;
|
|
|
|
// Can be thought as tokens, they will be used by the parser.
|
|
typedef struct ASTNode {
|
|
ASTNodeType type;
|
|
union {
|
|
int64_t integer;
|
|
struct {
|
|
struct ASTNode *left;
|
|
struct ASTNode *right;
|
|
Operator op;
|
|
} binary;
|
|
} data;
|
|
} ASTNode;
|
|
|
|
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;
|
|
};
|
|
} I64Result;
|
|
|
|
// Lexer funtions as well as few functionality
|
|
TokenizeResult tokenize(const char* input);
|
|
ASTNodeResult tokenize_number(const char* input, size_t *offset);
|
|
I64Result string_to_integer(const char buf[]);
|
|
bool isoperator(int c);
|
|
Operator char_to_operator(int c);
|
|
char operator_to_char(Operator op);
|
|
|
|
#endif // !LEXER_H
|