tokin/tökin.c
2025-03-10 17:39:02 +02:00

41 lines
982 B
C

#include <err.h>
#include <stdio.h>
#include "tökin.h"
#include "y.tab.h"
static void indent(int nesting) {
for (int i = 0; i < nesting; i++)
putchar(' ');
}
static const char *operator_name(int operator) {
switch (operator) {
case GE: return ">=";
case LE: return "<=";
case NE: return "!=";
case EQ: return "==";
case APPLICATION: return "apply";
default:
errx(1, "unknown operator %d\n", operator);
}
}
void print_tree(struct node *p, int nesting) {
switch (p->type) {
case literal_node:
indent(nesting); printf("literal: %s\n", p->text); break;
case identifier_node:
indent(nesting); printf("identifier: %s\n", p->text); break;
case operation_node:
indent(nesting);
if (p->op.operator < 256)
printf("op: %c\n", p->op.operator);
else
printf("op: %s\n", operator_name(p->op.operator));
for (int i = 0; i < p->op.arity; i++)
print_tree(p->op.operands[i], nesting + 1);
break;
default:
errx(1, "unknown node type %d\n", p->type);
}
}