Beispiel #1
0
static void
ast_return_do_print(AstNode *self, FILE *out, int indention)
{
    assert(AST_IS_RETURN(self));
    assert(out);

    fprintf(out, "  return ");
    AstReturn *ret = (AstReturn *)self;
    if (ret->return_value != NULL)
    {
        ast_node_print(AST_NODE(ret->return_value), out, indention);
    }
    fprintf(out, "\n");
}
Beispiel #2
0
// pretty-printer
void
ast_node_print(FILE *file, ast_node_t *node, int recursive)
{
	if (node == NULL) {
		return;
	}
	const int node_ty = NODE_TY(node);

	if (node_ty == AST_VALUE_ID) {
		print_id_string(file, AV_ID(node), false);
	} else if (node_ty <= AST_VALUE_MAX) {
		print_value_node(file, (ast_value_node_t *) node);
	} else {
		switch (node_ty) {

		case AST_NODE_METHODAPP:
		case AST_NODE_MEMBER:
			ast_node_print(file, node->children[0], recursive);
			fprintf(file, ".");
			for (int i = 1; i < node->children_nr; ++i) {
				ast_node_print(file,node->children[i], recursive);
			}
			break;

		case AST_NODE_BREAK:
			fprintf(file, "break");
			break;

		case AST_NODE_CONTINUE:
			fprintf(file, "continue");
			break;

		case AST_NODE_ISPRIMTY: {
			int type = node->type & TYPE_FLAGS;
			ast_node_print(file, node->children[0], recursive);
			fprintf(file, " is ");
			print_primty(file, type);
			break;
		}

		case AST_NODE_SKIP:
			fprintf(file, ";");
			break;

		case AST_NODE_ACTUALS:
			//e fall through
		case AST_NODE_FORMALS: {
			fprintf(file, "(");
			for (int i = 0; i < node->children_nr; ++i) {
				if (i > 0) {
					fprintf(file, ", ");
				}
				ast_node_print(file, node->children[i], recursive);
			}
			fprintf(file, ")");
			break;
		}

		case AST_NODE_ARRAYSUB:
			ast_node_print(file, node->children[0], recursive);
			fprintf(file, "[");
			ast_node_print(file, node->children[1], recursive);
			fprintf(file, "]");
			break;

		case AST_NODE_VARDECL:
			print_primty(file, node->type & TYPE_FLAGS);
			fprintf(file, " ");
			ast_node_print(file, node->children[0], recursive);
			if (node->children_nr > 1 && node->children[1]) {
				fprintf(file, " = ");
				ast_node_print(file, node->children[1], recursive);
			}
			break;

		case AST_NODE_NULL:
			fprintf(file, "NULL");
			break;

		case AST_NODE_RETURN:
			fprintf(file, "return");
			if (node->children_nr > 0) {
				fprintf(file, " ");
				ast_node_print(file, node->children[0], recursive);
			}
			break;

		case AST_NODE_ASSIGN:
			ast_node_print(file, node->children[0], recursive);
			fprintf(file, " := ");
			ast_node_print(file, node->children[1], recursive);
			break;

		case AST_NODE_NEWINSTANCE:
		case AST_NODE_FUNAPP:
			ast_node_print(file, node->children[0], recursive);
			ast_node_print(file, node->children[1], recursive);
			break;

		case AST_NODE_FUNDEF:
			//e fall through
		case AST_NODE_CLASSDEF:
			print_primty(file, node->type & TYPE_FLAGS);
			fprintf(file, " ");
			for (int i = 0; i < node->children_nr; ++i) {
				ast_node_print(file, node->children[i], recursive);
			}
			break;

		case AST_NODE_ISINSTANCE:
			ast_node_print(file, node->children[0], recursive);
			fprintf(file, " is ");
			ast_node_print(file, node->children[1], recursive);
			break;

		case AST_NODE_ARRAYLIST:
			for (int i = 0; i < node->children_nr; ++i) {
				if (i > 0) {
					fprintf(file, ", ");
				}
				ast_node_print(file, node->children[i], recursive);
			}
			break;

		case AST_NODE_ARRAYVAL:
			fprintf(file, "[");
			ast_node_print(file, node->children[0], recursive);
			if (node->children[0] && node->children[0]->children_nr) {
				fprintf(file, ",");
			}
			if (node->children_nr > 1) {
				fprintf(file, "/");
				ast_node_print(file, node->children[1], recursive);
			}
			fprintf(file, "]");
			break;

		case AST_NODE_IF:
			fprintf(file, "if ");
			ast_node_print(file, node->children[0], recursive);
			if (node->children[1]) {
				ast_node_print(file, node->children[1], recursive);
			}
			if (node->children[2]) {
				fprintf(file, " else ");
				ast_node_print(file, node->children[2], recursive);
			}
			break;

		case AST_NODE_WHILE:
			fprintf(file, "while (");
			ast_node_print(file, node->children[0], recursive);
			fprintf(file, ") ");
			ast_node_print(file, node->children[1], recursive);
			break;

		case AST_NODE_BLOCK:
			fprintf(file, "{ ");
			for (int i = 0; i < node->children_nr; ++i) {
				ast_node_print(file, node->children[i], recursive);
				fprintf(file, "; ");
			}
			fprintf(file, "}");
		default:
			ast_node_dump(file, node, 0);
		}
	}
}