shithub: lpa

ref: 50104ba72987b26febfa4e398458d47db20e9bea
dir: /util.c/

View raw version
#include <u.h>
#include <libc.h>
#include <thread.h>

#include "dat.h"
#include "fns.h"

Enumeration *
allocenum(uvlong count)
{
	Enumeration *e = alloc(DataEnumeration);
	setroot(e, 1);
	e->count = count;
	e->items = allocextra(e, sizeof(void *) * count);
	return e;
}

void
trim(char *str)
{
	for(int i = strlen(str)-1; i > 0; i--){
		if(str[i] != '\n')
			break;
		else
			str[i] = 0;
	}
}

static void
indent(int depth)
{
	for(int i = 0; i < depth; i++)
		print("  ");
}

static void
printchild(char *desc, Ast *ast, int depth)
{
	indent(depth);
	print("%s:\n", desc);
	indent(depth+1);
	debugast(ast, depth+1);
}

void
debugast(Ast *ast, int depth)
{
	if(ast == nil){
		print("<nil>\n");
		return;
	}

	int printchildren = 0;

	switch(ast->tag){
	case AstProg:
		print("prog\n");
		printchildren = 1;
		break;
	case AstFunc:
		print("func\n");
		depth++;
		printchild("name", ast->funcname, depth);
		printchild("result", ast->funcresult, depth);
		printchild("left arg", ast->funcleftarg, depth);
		printchild("right arg", ast->funcrightarg, depth);
		printchild("local vars", ast->funclocals, depth);
		indent(depth); print("body\n");
		printchildren = 1;
		break;
	case AstName:
		print("\"%s\"\n", ast->name);
		break;
	case AstLocals:
		print("locals\n");
		printchildren = 1;
		break;
	case AstAssign:
		print("assign\n");
		printchild("name", ast->left, depth+1);
		printchild("value", ast->right, depth+1);
		break;
	case AstMonadic:
		print("monadic call\n");
		printchild("func", ast->func, depth+1);
		printchild("right", ast->right, depth+1);
		break;
	case AstDyadic:
		print("dyadic call\n");
		printchild("func", ast->func, depth+1);
		printchild("left", ast->left, depth+1);
		printchild("right", ast->right, depth+1);
		break;
	case AstConst:
		print("const:\n");
		indent(depth+1);
		print("%s\n", printarray(ast->val));
		break;
	case AstPrim:
		print("prim: %d\n", ast->prim);
		break;
	case AstStrand:
		print("strand\n");
		printchildren = 1;
		break;
	default:
		print("<ast node %d>\n", ast->tag);
		break;
	}

	if(printchildren){
		for(uvlong i = 0; i < ast->childcount; i++){
			indent(depth+1);
			debugast(ast->children[i], depth+1);
		}
	}
}