Files
Calculator/include/parser.h
laentropia 542a94ef81 refactor: All of parser.c
DAMN, it wasn't that difficult, just bothers me a bit the part that
checks if both lbp and rbp of the infix are valid, like i do validation
twice but is fine i guess, maybe using an else?, i'll see if i change
it, for now i need to change the evaluator
2026-05-13 11:09:22 -06:00

80 lines
1.4 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,
NODE_PARENTHESIS,
} NodeType;
typedef struct Node {
NodeType type;
union {
int64_t num;
struct {
Operator op;
struct Node *left;
struct Node *right;
}binary;
struct {
Operator op;
struct Node *to;
}unary;
Operator par;
};
} 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;
Node token_to_node(Token token);
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