Files
rifle/core/parser.c
2026-02-15 16:27:56 -05:00

357 lines
6.5 KiB
C

#include <stdint.h>
#include <stddef.h>
#include "rifle/parser.h"
#include "rifle/token.h"
#include "rifle/trace.h"
#include "rifle/lexer.h"
#include "rifle/state.h"
#include "rifle/symbol.h"
/* Symbolic token */
#define symtok(tok) \
"[" tok "]"
/* Quoted token */
#define qtok(tok) \
"'" tok "'"
/* Convert token to string */
#define tokstr1(tt) \
toktab[(tt)]
/* Convert token to string */
#define tokstr(tok) \
tokstr1((tok)->type)
/* Unexpected end of file */
#define ueof(state) \
trace_error( \
(state), \
"unexpected end of file\n" \
);
/* Unexpected token */
#define utok(state, expt, got) \
trace_error( \
(state), \
"expected %s, got %s instead", \
(expt), \
(got) \
)
/*
* Table used to convert token constants into human
* readable strings
*/
static const char *toktab[] = {
[TT_NONE] = symtok("none"),
[TT_IDENT] = symtok("ident"),
[TT_PLUS] = qtok("+"),
[TT_MINUS] = qtok("-"),
[TT_STAR] = qtok("*"),
[TT_SLASH] = qtok("/"),
[TT_COLON] = qtok(":"),
[TT_LPAREN] = qtok("("),
[TT_RPAREN] = qtok(")"),
[TT_F] = qtok(".f"),
[TT_EXTERN] = qtok(".extern"),
[TT_DEFINE] = qtok("#define"),
[TT_IFDEF] = qtok("#ifdef"),
[TT_IFNDEF] = qtok("#ifndef"),
[TT_ENDIF] = qtok("#endif")
};
/*
* Parse-side token scan function
*
* @state: Compiler state
* @tok: Last token
*
* Returns zero on success
*/
static int
parse_scan(struct rifle_state *state, struct token *tok)
{
struct token *popped;
if (state == NULL || tok == NULL) {
return -1;
}
switch (state->pass_num) {
case 0:
if (lexer_scan(state, tok) < 0) {
return -1;
}
break;
case 1:
if ((popped = tokbuf_pop(&state->tokbuf)) == NULL) {
return -1;
}
*tok = *popped;
break;
}
return 0;
}
/*
* Assert that the next token is of a specific type
*
* @state: Compiler state
* @what: Token to assert that is next
* @tok: Last token
*
* Returns zero on success
*/
static int
parse_expect(struct rifle_state *state, tt_t what, struct token *tok)
{
if (state == NULL || tok == NULL) {
return -1;
}
if (parse_scan(state, tok) < 0) {
ueof(state);
return -1;
}
if (tok->type != what) {
utok(state, tokstr1(what), tokstr(tok));
return -1;
}
return 0;
}
static int
parse_define(struct rifle_state *state, struct token *tok)
{
int error;
if (state == NULL || tok == NULL) {
return -1;
}
if (tok->type != TT_DEFINE) {
return -1;
}
/* EXPECT <IDENT> */
if (parse_expect(state, TT_IDENT, tok) < 0) {
return -1;
}
error = symbol_new(
&state->symtab,
tok->s,
SYMBOL_MACRO,
NULL
);
if (error < 0) {
trace_error(state, "failed to create symbol '%s'\n", tok->s);
return -1;
}
return 0;
}
/*
* Skip to an '#endif' token
*
* @state: Compiler state
* @tok: Last token
*
* Returns zero on success
*/
static int
parse_skip_to_endif(struct rifle_state *state, struct token *tok)
{
if (state == NULL || tok == NULL) {
return -1;
}
while (parse_scan(state, tok) == 0) {
if (tok->type == TT_ENDIF) {
break;
}
}
if (tok->type == TT_ENDIF) {
--state->ifx_depth;
}
return 0;
}
/*
* Parse an '#ifdef' preprocessing directive
*
* @state: Compiler state
* @tok: Last token
*
* Returns zero on success
*/
static int
parse_ifdef(struct rifle_state *state, struct token *tok)
{
struct symbol *symbol;
if (state == NULL || tok == NULL) {
return -1;
}
if (tok->type != TT_IFDEF) {
return -1;
}
/* EXPECT <IDENT> */
if (parse_expect(state, TT_IDENT, tok) < 0) {
return -1;
}
symbol = symbol_from_name(
&state->symtab,
tok->s
);
if (symbol == NULL) {
parse_skip_to_endif(state, tok);
}
return 0;
}
/*
* Handle an '#ifndef' directive
*
* @state: Compiler state
* @tok: Last token
*
* Returns zero on success
*/
static int
parse_ifndef(struct rifle_state *state, struct token *tok)
{
struct symbol *symbol;
if (state == NULL || tok == NULL) {
return -1;
}
if (tok->type != TT_IFNDEF) {
return -1;
}
if (parse_expect(state, TT_IDENT, tok) < 0) {
return -1;
}
symbol = symbol_from_name(
&state->symtab,
tok->s
);
if (symbol != NULL) {
parse_skip_to_endif(state, tok);
}
return 0;
}
/*
* Handle the preprocessing stage
*
* @state: Compiler state
*/
static int
parse_preprocess(struct rifle_state *state)
{
struct token tok;
while (parse_scan(state, &tok) == 0) {
switch (tok.type) {
case TT_DEFINE:
if (parse_define(state, &tok) < 0) {
return -1;
}
break;
case TT_IFDEF:
++state->ifx_depth;
if (parse_ifdef(state, &tok) < 0) {
return -1;
}
break;
case TT_IFNDEF:
++state->ifx_depth;
if (parse_ifndef(state, &tok) < 0) {
return -1;
}
break;
case TT_ENDIF:
if (state->ifx_depth == 0) {
trace_error(state, "Extraneous '#endif' directive\n");
return -1;
}
--state->ifx_depth;
break;
default:
tokbuf_push(&state->tokbuf, &tok);
break;
}
}
return 0;
}
static int
parse_begin(struct rifle_state *state)
{
struct token tok;
while (parse_scan(state, &tok) == 0) {
printf("got token %s\n", tokstr(&tok));
}
return 0;
}
int
parser_parse(struct rifle_state *state)
{
if (state == NULL) {
return 0;
}
switch (state->pass_num) {
case 0:
/* Pre-processor */
if (parse_preprocess(state) < 0) {
return -1;
}
if (state->ifx_depth > 0) {
trace_error(state, "Unterminated '#if' directive\n");
return -1;
}
break;
case 1:
/* Parse loop */
if (parse_begin(state) < 0) {
return -1;
}
break;
}
++state->pass_num;
return 0;
}