init basic
This commit is contained in:
67
ccompiler/frontend/parser/parser.c
Normal file
67
ccompiler/frontend/parser/parser.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "parser.h"
|
||||
#include "type.h"
|
||||
#include "ast/ast.h"
|
||||
|
||||
int poptok(struct Parser* parser) {
|
||||
if (parser->size == 0) {
|
||||
return -1;
|
||||
}
|
||||
int idx = parser->cur_idx;
|
||||
parser->cur_idx = (idx + 1) % PARSER_MAX_TOKEN_QUEUE;
|
||||
parser->size--;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void flushpeektok(struct Parser* parser) {
|
||||
parser->peek_idx = parser->cur_idx;
|
||||
}
|
||||
|
||||
struct Token* peektok(struct Parser* parser) {
|
||||
int idx = parser->peek_idx;
|
||||
idx = (idx + 1) % PARSER_MAX_TOKEN_QUEUE;
|
||||
if (parser->size >= PARSER_MAX_TOKEN_QUEUE) {
|
||||
warn("peek maybe too deep");
|
||||
}
|
||||
if (parser->peek_idx == parser->end_idx) {
|
||||
if (parser->size == PARSER_MAX_TOKEN_QUEUE) {
|
||||
// FIXME
|
||||
error("buffer overflow");
|
||||
}
|
||||
get_valid_token(parser->lexer, &(parser->TokenBuffer[idx]));
|
||||
parser->size++;
|
||||
parser->end_idx = idx;
|
||||
}
|
||||
|
||||
parser->peek_idx = idx;
|
||||
return &(parser->TokenBuffer[idx]);
|
||||
}
|
||||
|
||||
enum TokenType peektoktype(struct Parser* parser) {
|
||||
return peektok(parser)->type;
|
||||
}
|
||||
|
||||
void expecttok(struct Parser* parser, enum TokenType type) {
|
||||
struct Token* tok = peektok(parser);
|
||||
if (tok->type != type) {
|
||||
error("expected tok: %s, got %s", get_token_name(type), get_token_name(tok->type));
|
||||
} else {
|
||||
poptok(parser);
|
||||
}
|
||||
}
|
||||
|
||||
void init_parser(struct Parser* parser, struct Lexer* lexer, struct SymbolTable* symtab) {
|
||||
parser->cur_node = NULL;
|
||||
parser->root = NULL;
|
||||
|
||||
parser->cur_idx = 0;
|
||||
parser->peek_idx = 0;
|
||||
parser->end_idx = 0;
|
||||
parser->size = 0;
|
||||
parser->lexer = lexer;
|
||||
parser->symtab = symtab;
|
||||
// TODO
|
||||
}
|
||||
|
||||
void run_parser(struct Parser* parser) {
|
||||
parse_prog(parser);
|
||||
}
|
||||
Reference in New Issue
Block a user