refactor: changed parser.h, added Node
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
This commit is contained in:
@@ -4,11 +4,30 @@
|
|||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
#include "arena.h"
|
#include "arena.h"
|
||||||
#include "arraylist.h"
|
#include "arraylist.h"
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
NODE_INT,
|
||||||
|
NODE_BINARY_OP,
|
||||||
|
NODE_UNARY_OP,
|
||||||
|
} NodeType;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
ASTNode *head;
|
NodeType type;
|
||||||
} AST;
|
union {
|
||||||
|
int64_t num;
|
||||||
|
struct {
|
||||||
|
Operator op;
|
||||||
|
struct Node *left;
|
||||||
|
struct Node *right;
|
||||||
|
}binary;
|
||||||
|
struct {
|
||||||
|
Operator op;
|
||||||
|
struct Node *to;
|
||||||
|
}unary;
|
||||||
|
};
|
||||||
|
} Node;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
PARSER_OK = 0,
|
PARSER_OK = 0,
|
||||||
@@ -24,20 +43,33 @@ typedef struct {
|
|||||||
ParserErr err;
|
ParserErr err;
|
||||||
struct {
|
struct {
|
||||||
Arena arena;
|
Arena arena;
|
||||||
ASTNode *tree;
|
Node *tree;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
} ParseResult;
|
} ParserResult;
|
||||||
|
|
||||||
ASTNode *nud(ArraySlice *slice);
|
typedef struct {
|
||||||
ASTNode *led(ArraySlice *slice, size_t right_precedence);
|
bool is_valid;
|
||||||
|
union {
|
||||||
|
ParserErr err;
|
||||||
|
Node *node;
|
||||||
|
};
|
||||||
|
} NodeResult;
|
||||||
|
|
||||||
uint8_t prefix_rbp(ASTNode node);
|
typedef struct {
|
||||||
uint8_t postfix_lbp(ASTNode node);
|
bool is_valid;
|
||||||
uint8_t infix_lbp(ASTNode node);
|
union {
|
||||||
uint8_t infix_rbp(ASTNode node);
|
ParserErr err;
|
||||||
|
uint8_t num;
|
||||||
|
};
|
||||||
|
} ParserU8Result;
|
||||||
|
|
||||||
ParseResult parse(TokenizeResult tokens);
|
ParserU8Result prefix_rbp(Node node);
|
||||||
ASTNode *parse_expr(ArraySlice *slice, Arena *arena, uint8_t min_bp);
|
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
|
#endif // !PARSER_H
|
||||||
|
|||||||
Reference in New Issue
Block a user