Finish ps2
parent
a70bb5d50d
commit
e9634ef6b7
Binary file not shown.
|
@ -13,6 +13,7 @@
|
|||
%left '*' '/'
|
||||
%nonassoc UMINUS
|
||||
%right '~'
|
||||
%expect 1
|
||||
|
||||
%nonassoc IF THEN
|
||||
%nonassoc ELSE
|
||||
|
|
|
@ -5,6 +5,50 @@ static void node_finalize ( node_t *discard );
|
|||
static void destroy_subtree ( node_t *discard );
|
||||
|
||||
|
||||
typedef struct stem_t *stem;
|
||||
struct stem_t { const char *str; stem next; };
|
||||
static void
|
||||
tree_print(node_t* root, stem head)
|
||||
{
|
||||
static const char *sdown = " │", *slast = " └", *snone = " ";
|
||||
struct stem_t col = {0, 0}, *tail;
|
||||
|
||||
for (tail = head; tail; tail = tail->next) {
|
||||
if (!tail->next) {
|
||||
if (!strcmp(sdown, tail->str))
|
||||
printf(" ├");
|
||||
else
|
||||
printf("%s", tail->str);
|
||||
break;
|
||||
}
|
||||
printf("%s", tail->str);
|
||||
}
|
||||
|
||||
printf("──%s", node_string[root->type] );
|
||||
if ( root->type == IDENTIFIER_DATA ||
|
||||
root->type == STRING_DATA ||
|
||||
root->type == EXPRESSION )
|
||||
printf("(%s)", (char *) root->data);
|
||||
else if (root->type == NUMBER_DATA)
|
||||
printf ( "(%ld)", *((int64_t *)root->data) );
|
||||
putchar ( '\n' );
|
||||
|
||||
if (!root->n_children) return;
|
||||
|
||||
if (tail && tail->str == slast)
|
||||
tail->str = snone;
|
||||
|
||||
if (!tail) tail = head = &col;
|
||||
else tail->next = &col;
|
||||
|
||||
for ( int64_t i=0; i < root->n_children; i++ ) {
|
||||
col.str = root->n_children - i - 1 ? sdown : slast;
|
||||
tree_print(root->children[i], head);
|
||||
}
|
||||
tail->next = 0;
|
||||
}
|
||||
|
||||
|
||||
/* External interface */
|
||||
void
|
||||
destroy_syntax_tree ( void )
|
||||
|
@ -16,7 +60,8 @@ destroy_syntax_tree ( void )
|
|||
void
|
||||
print_syntax_tree ( void )
|
||||
{
|
||||
node_print ( root, 0 );
|
||||
//node_print ( root, 0 );
|
||||
tree_print(root, 0);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue