#include <stdio.h>
#include <lyn/ast.h>

struct ast *reverse_ast_list(struct ast *root)
{
	struct ast *new_root = NULL;
	while (root) {
		struct ast *next = root->next;
		root->next = new_root;
		new_root = root;
		root = next;
	}

	return new_root;
}

#define dump(depth, fmt, ...)                   \
	do {                                    \
		printf("//%*s", 2 * depth, ""); \
		printf(fmt,##__VA_ARGS__);      \
	} while (0)

void ast_dump(int depth, struct ast *ast)
{
	switch (ast->kind) {
	case LYN_ID: dump(depth, "%s\n", ast->s); return;
	case LYN_STR: dump(depth, "\"%s\"\n", ast->s); return;
	case LYN_INT: dump(depth, "%lld\n", ast->i); return;
	case LYN_FLOAT: dump(depth, "%f\n", ast->d); return;
	case LYN_CMD:
			dump(depth, "CMD\n");
			ast_dump_list(depth + 1, ast->args);
			return;

	case LYN_LIST:
			dump(depth, "LIST\n");
			ast_dump_list(depth + 1, ast->args);
			return;

	case LYN_APPLY:
			dump(depth, "APPLY\n");
			ast_dump_list(depth + 1, ast->args);
			return;
	}
}

void ast_dump_list(int depth, struct ast *ast)
{
	while (ast) {
		ast_dump(depth, ast);
		ast = ast->next;
	}
}