bshell: add functions for printing lex tokens and ast nodes

This commit is contained in:
2026-05-09 18:59:42 +01:00
parent 090f6a0002
commit b12f59ed2c
2 changed files with 147 additions and 0 deletions
+135
View File
@@ -0,0 +1,135 @@
#include "debug.h"
#include "ast/ast.h"
#include "parse/token.h"
#include <fx/string.h>
#include <fx/term/print.h>
#include <stdio.h>
extern void print_lex_token(struct lex_token *tok)
{
printf("[%lu:%lu - %lu:%lu] ",
tok->tok_start.c_row,
tok->tok_start.c_col,
tok->tok_end.c_row,
tok->tok_end.c_col);
switch (tok->tok_type) {
case TOK_KEYWORD:
fx_puts("[magenta]");
break;
case TOK_SYMBOL:
fx_puts("[blue]");
break;
case TOK_INT:
case TOK_DOUBLE:
fx_puts("[yellow]");
break;
case TOK_FLAG:
fx_puts("[red]");
break;
case TOK_WORD:
case TOK_VAR:
case TOK_VAR_SPLAT:
fx_puts("[cyan]");
break;
case TOK_STRING:
fx_puts("[green]");
break;
case TOK_STR_START:
fx_puts("[green]");
break;
case TOK_STR_END:
fx_puts("[green]");
break;
case TOK_LINEFEED:
fx_puts("[dark_grey]");
break;
default:
break;
}
fx_puts(token_type_to_string(tok->tok_type));
switch (tok->tok_type) {
case TOK_WORD:
case TOK_FLAG:
case TOK_STRING:
case TOK_VAR:
case TOK_VAR_SPLAT:
printf("(%s)", tok->tok_str);
break;
case TOK_SYMBOL:
printf("(%s)", token_symbol_to_string(tok->tok_symbol));
break;
case TOK_KEYWORD:
printf("(%s)", token_keyword_to_string(tok->tok_keyword));
break;
case TOK_INT:
printf("(%lld)", tok->tok_int);
break;
case TOK_DOUBLE:
printf("(%lf)", tok->tok_double);
break;
default:
break;
}
fx_puts("[reset]\n");
}
void print_ast_node(struct ast_node *node)
{
struct ast_iterator it = {0};
ast_node_iterate(node, &it);
while (1) {
node = ast_iterator_peek(&it);
if (!node) {
break;
}
for (unsigned long i = 0; i < node->n_it.e_depth; i++) {
fx_puts(" ");
}
switch (node->n_type) {
case AST_REDIRECTION:
case AST_PIPELINE:
fx_puts("[blue]");
break;
case AST_CMDCALL:
fx_puts("[red]");
break;
case AST_INT:
case AST_DOUBLE:
fx_puts("[yellow]");
break;
case AST_WORD:
fx_puts("[cyan]");
break;
case AST_STRING:
case AST_FSTRING:
fx_puts("[green]");
break;
default:
break;
}
fx_printf("%s", ast_node_type_to_string(node->n_type));
char s[128] = {0};
fx_bstr str;
fx_bstr_begin(&str, s, sizeof s);
ast_node_to_string(node, &str);
if (fx_bstr_get_size(&str)) {
fx_printf("(%s)", fx_bstr_end(&str));
}
fx_printf("[reset]\n");
ast_iterator_dequeue(&it);
}
}
+12
View File
@@ -0,0 +1,12 @@
#ifndef DEBUG_H_
#define DEBUG_H_
#include <stdbool.h>
struct ast_node;
struct lex_token;
extern void print_lex_token(struct lex_token *tok);
extern void print_ast_node(struct ast_node *node);
#endif