shithub: lpa

ref: 50104ba72987b26febfa4e398458d47db20e9bea
dir: /value.c/

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

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

/* Anything that can have a name in LPA: Arrays, functions, ... */
char *
printval(void *v)
{
	int tag;
	if(v){
		tag = getalloctag(v);
		switch(tag){
		case DataArray:
			return smprint("%s\n", printarray(v));
		default:
			return smprint("some value of type %d\n", tag);
		}
	}else
		return smprint("no value :(\n");
}

void *
parseval(char *buf, char **errp)
{
	Ast *ast;
	void *val = nil;

	TokenList *tokens = scan(buf, errp);
	if(tokens == nil)
		goto end;
	ast = parse(tokens, errp);
	if(ast == nil)
		goto end;

	/* Check that the ast is either a single function definition,
	 * or a single constant.
	 */
	if(!(ast->tag == AstProg && ast->childcount == 1)){
		*errp = "Expected single value or function definition";
		goto end;
	}

	ast = ast->children[0];
	switch(ast->tag){
	case AstConst:
		val = ast->val;
		break;
	case AstFunc:
		*errp = "Functions not supported yet";
		break;
	default:
		*errp = "Expected constant or function definition";
		goto end;
	}
end:
	return val;
}