So just added node back but now clearly separated by tokens and nodes of the AST as it should be, now real rework the mess that is the parser
76 lines
1.3 KiB
C
76 lines
1.3 KiB
C
#ifndef PARSER_H
|
|
#define PARSER_H
|
|
|
|
#include "lexer.h"
|
|
#include "arena.h"
|
|
#include "arraylist.h"
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
typedef enum {
|
|
NODE_INT,
|
|
NODE_BINARY_OP,
|
|
NODE_UNARY_OP,
|
|
} NodeType;
|
|
|
|
typedef struct {
|
|
NodeType type;
|
|
union {
|
|
int64_t num;
|
|
struct {
|
|
Operator op;
|
|
struct Node *left;
|
|
struct Node *right;
|
|
}binary;
|
|
struct {
|
|
Operator op;
|
|
struct Node *to;
|
|
}unary;
|
|
};
|
|
} Node;
|
|
|
|
typedef enum {
|
|
PARSER_OK = 0,
|
|
PARSER_UNEXPECTED_TOKEN,
|
|
PARSER_MISSING_OPERAND,
|
|
PARSER_UNMATCHED_PAREN,
|
|
PARSER_OUT_OF_MEMORY,
|
|
} ParserErr;
|
|
|
|
typedef struct {
|
|
bool is_valid;
|
|
union {
|
|
ParserErr err;
|
|
struct {
|
|
Arena arena;
|
|
Node *tree;
|
|
};
|
|
};
|
|
} ParserResult;
|
|
|
|
typedef struct {
|
|
bool is_valid;
|
|
union {
|
|
ParserErr err;
|
|
Node *node;
|
|
};
|
|
} NodeResult;
|
|
|
|
typedef struct {
|
|
bool is_valid;
|
|
union {
|
|
ParserErr err;
|
|
uint8_t num;
|
|
};
|
|
} ParserU8Result;
|
|
|
|
ParserU8Result prefix_rbp(Node node);
|
|
ParserU8Result postfix_lbp(Node node);
|
|
ParserU8Result infix_lbp(Node node);
|
|
ParserU8Result infix_rbp(Node node);
|
|
|
|
ParserResult parse(TokenizeResult tokens);
|
|
NodeResult parse_expr(ArraySlice *slice, Arena *arena, uint8_t min_bp);
|
|
|
|
#endif // !PARSER_H
|