ast_value* parser_find_local(parser_t *parser, const char *name, size_t upto)
{
size_t i;
+ ast_value *fun;
for (i = parser->locals_count; i > upto;) {
--i;
if (!strcmp(parser->locals[i]->name, name))
return parser->locals[i];
}
+ fun = parser->function->vtype;
+ for (i = 0; i < fun->params_count; ++i) {
+ if (!strcmp(fun->params[i]->name, name))
+ return fun->params[i];
+ }
return NULL;
}
lex_ctx ctx = parser_ctx(parser);
int vtype = basetype;
int temptype;
+ size_t i;
MEM_VECTOR_INIT(¶ms, p);
ast_value *param;
bool dummy;
- if (!parser_next(parser)) {
- MEM_VECTOR_CLEAR(¶ms, p);
- return NULL;
- }
+ if (!parser_next(parser))
+ goto on_error;
if (parser->tok == ')')
break;
temptype = parser_token(parser)->constval.t;
- if (!parser_next(parser)) {
- MEM_VECTOR_CLEAR(¶ms, p);
- return NULL;
- }
+ if (!parser_next(parser))
+ goto on_error;
+
param = parser_parse_type(parser, temptype, &dummy);
(void)dummy;
- if (!param) {
- MEM_VECTOR_CLEAR(¶ms, p);
- return NULL;
+ if (!param)
+ goto on_error;
+
+ if (parser->tok == TOKEN_IDENT) {
+ /* named parameter */
+ if (!ast_value_set_name(param, parser_tokval(parser)))
+ goto on_error;
+ if (!parser_next(parser))
+ goto on_error;
}
if (!paramlist_t_p_add(¶ms, param)) {
- MEM_VECTOR_CLEAR(¶ms, p);
parseerror(parser, "Out of memory while parsing typename");
- return NULL;
+ goto on_error;
}
if (parser->tok == ',')
continue;
if (parser->tok == ')')
break;
- MEM_VECTOR_CLEAR(¶ms, p);
parseerror(parser, "Unexpected token");
- return NULL;
- }
- if (!parser_next(parser)) {
- MEM_VECTOR_CLEAR(¶ms, p);
- return NULL;
+ goto on_error;
}
+ if (!parser_next(parser))
+ goto on_error;
}
var = ast_value_new(ctx, "<unnamed>", vtype);
- if (!var) {
- MEM_VECTOR_CLEAR(¶ms, p);
- return NULL;
- }
+ if (!var)
+ goto on_error;
MEM_VECTOR_MOVE(¶ms, p, var, params);
return var;
+on_error:
+ for (i = 0; i < params.p_count; ++i)
+ ast_value_delete(params.p[i]);
+ MEM_VECTOR_CLEAR(¶ms, p);
+ return NULL;
}
typedef struct
{
size_t etype; /* 0 = expression, others are operators */
int paren;
+ size_t off;
ast_expression *out;
ast_value *value; /* need to know if we can assign */
+ ast_block *block; /* for commas and function calls */
lex_ctx ctx;
} sy_elem;
typedef struct
e.etype = 0;
e.out = v;
e.value = NULL;
+ e.block = NULL;
e.ctx = ctx;
e.paren = 0;
return e;
e.etype = 0;
e.out = (ast_expression*)v;
e.value = v;
+ e.block = NULL;
+ e.ctx = ctx;
+ e.paren = 0;
+ return e;
+}
+
+static sy_elem syblock(lex_ctx ctx, ast_block *v) {
+ sy_elem e;
+ e.etype = 0;
+ e.out = (ast_expression*)v;
+ e.value = NULL;
+ e.block = v;
e.ctx = ctx;
e.paren = 0;
return e;
e.etype = 1 + (op - operators);
e.out = NULL;
e.value = NULL;
+ e.block = NULL;
e.ctx = ctx;
e.paren = 0;
return e;
}
-static sy_elem syparen(lex_ctx ctx, int p) {
+static sy_elem syparen(lex_ctx ctx, int p, size_t off) {
sy_elem e;
e.etype = 0;
+ e.off = off;
e.out = NULL;
e.value = NULL;
+ e.block = NULL;
e.ctx = ctx;
e.paren = p;
return e;
ast_expression *out = NULL;
ast_expression *exprs[3];
ast_value *vars[3];
+ ast_block *blocks[3];
size_t i;
if (!sy->ops_count) {
sy->out_count -= op->operands;
for (i = 0; i < op->operands; ++i) {
- exprs[i] = sy->out[sy->out_count+i].out;
- vars[i] = sy->out[sy->out_count+i].value;
+ exprs[i] = sy->out[sy->out_count+i].out;
+ vars[i] = sy->out[sy->out_count+i].value;
+ blocks[i] = sy->out[sy->out_count+i].block;
+ }
+
+ if (blocks[0] && !blocks[0]->exprs_count && op->id != opid1(',')) {
+ parseerror(parser, "internal error: operator cannot be applied on empty blocks");
+ return false;
}
- printf("Applying operator %s\n", op->op);
switch (op->id)
{
default:
parseerror(parser, "internal error: unhandled operand");
return false;
+ case opid1(','):
+ if (blocks[0]) {
+ if (!ast_block_exprs_add(blocks[0], exprs[1]))
+ return false;
+ } else {
+ blocks[0] = ast_block_new(ctx);
+ if (!ast_block_exprs_add(blocks[0], exprs[0]) ||
+ !ast_block_exprs_add(blocks[0], exprs[1]))
+ {
+ return false;
+ }
+ }
+ if (!ast_block_set_type(blocks[0], exprs[1]))
+ return false;
+
+ sy->out[sy->out_count++] = syblock(ctx, blocks[0]);
+ return true;
+
case opid1('+'):
if (exprs[0]->expression.vtype != exprs[1]->expression.vtype) {
parseerror(parser, "Cannot add type %s and %s",
return true;
}
+static bool parser_close_call(parser_t *parser, shunt *sy)
+{
+ /* was a function call */
+ ast_expression *fun;
+ ast_call *call;
+
+ size_t fid;
+ size_t paramcount;
+
+ sy->ops_count--;
+ fid = sy->ops[sy->ops_count].off;
+
+ /* out[fid] is the function
+ * everything above is parameters...
+ * 0 params = nothing
+ * 1 params = ast_expression
+ * more = ast_block
+ */
+
+ if (sy->out_count < 1 || sy->out_count <= fid) {
+ parseerror(parser, "internal error: function call needs function and parameter list...");
+ return false;
+ }
+
+ fun = sy->out[fid].out;
+
+ call = ast_call_new(sy->ops[sy->ops_count].ctx, fun);
+ if (!call) {
+ parseerror(parser, "out of memory");
+ return false;
+ }
+
+ printf("fid = %i, out_count = %i\n", (int)fid, (int)sy->out_count);
+
+ if (fid+1 == sy->out_count) {
+ /* no arguments */
+ paramcount = 0;
+ } else if (fid+2 == sy->out_count) {
+ ast_block *params;
+ sy->out_count--;
+ params = sy->out[sy->out_count].block;
+ if (!params) {
+ /* 1 param */
+ paramcount = 1;
+ if (!ast_call_params_add(call, sy->out[sy->out_count].out)) {
+ ast_delete(sy->out[sy->out_count].out);
+ parseerror(parser, "out of memory");
+ return false;
+ }
+ } else {
+ paramcount = params->exprs_count;
+ MEM_VECTOR_MOVE(params, exprs, call, params);
+ ast_delete(params);
+ }
+ } else {
+ parseerror(parser, "invalid function call");
+ return false;
+ }
+
+ /* overwrite fid, the function, with a call */
+ sy->out[fid] = syexp(call->expression.node.context, (ast_expression*)call);
+
+ if (fun->expression.vtype != TYPE_FUNCTION) {
+ parseerror(parser, "not a function");
+ return false;
+ }
+
+ if (!fun->expression.next) {
+ parseerror(parser, "could not determine function parameters");
+ return false;
+ } else {
+ /*
+ ast_value *v = (ast_value*)(fun->expression.next);
+ if (v->params_count != paramcount) {
+ parseerror(parser, "expected %i parameters, got %i", (int)v->params_count, paramcount);
+ return false;
+ }
+ */
+ }
+
+ return true;
+}
+
+static bool parser_close_paren(parser_t *parser, shunt *sy, bool functions_only)
+{
+ if (!sy->ops_count) {
+ parseerror(parser, "unmatched closing paren");
+ return false;
+ }
+ if (sy->ops[sy->ops_count-1].paren == 1) {
+ parseerror(parser, "empty parenthesis expression");
+ return false;
+ }
+ while (sy->ops_count) {
+ if (sy->ops[sy->ops_count-1].paren == 'f') {
+ if (!parser_close_call(parser, sy))
+ return false;
+ break;
+ }
+ if (sy->ops[sy->ops_count-1].paren == 1) {
+ sy->ops_count--;
+ return !functions_only;
+ }
+ if (!parser_sy_pop(parser, sy))
+ return false;
+ }
+ return true;
+}
+
static ast_expression* parser_expression(parser_t *parser)
{
ast_expression *expr = NULL;
parseerror(parser, "out of memory");
goto onerr;
}
- printf("Added: %s\n", var->name);
}
else if (parser->tok == TOKEN_FLOATCONST) {
ast_value *val = parser_const_float(parser, (parser_token(parser)->constval.f));
parseerror(parser, "out of memory");
goto onerr;
}
- printf("Added: %i\n", parser_token(parser)->constval.i);
}
else if (parser->tok == '(') {
nextwant = false; /* not expecting an operator next */
- if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), 1))) {
+ if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), 1, 0))) {
parseerror(parser, "out of memory");
goto onerr;
}
}
+ else if (parser->tok == ')') {
+ /* allowed for function calls */
+ if (!parser_close_paren(parser, &sy, true))
+ goto onerr;
+ }
else {
/* TODO: prefix operators */
parseerror(parser, "expected statement");
wantop = nextwant;
parser->lex->flags.noops = !wantop;
} else {
- if (parser->tok == ')') {
- /* we do expect an operator next */
- /* closing an opening paren */
- printf("Applying closing paren\n");
- if (!sy.ops_count) {
- parseerror(parser, "unmatched closing paren");
+ if (parser->tok == '(') {
+ /* we expected an operator, this is the function-call operator */
+ if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), 'f', sy.out_count-1))) {
+ parseerror(parser, "out of memory");
goto onerr;
}
- if (sy.ops[sy.ops_count-1].paren == 1) {
- parseerror(parser, "empty parenthesis expression");
+ }
+ else if (parser->tok == ')') {
+ /* we do expect an operator next */
+ /* closing an opening paren */
+ if (!parser_close_paren(parser, &sy, false))
goto onerr;
- }
- while (sy.ops_count) {
- if (sy.ops[sy.ops_count-1].paren == 1) {
- sy.ops_count--;
- break;
- }
- if (!parser_sy_pop(parser, &sy))
- goto onerr;
- }
}
else if (parser->tok != TOKEN_OPERATOR) {
parseerror(parser, "expected operator or end of statement");
if (!shunt_ops_add(&sy, syop(parser_ctx(parser), op)))
goto onerr;
- printf("Added op %s\n", op->op);
}
wantop = false;
parser->lex->flags.noops = true;
goto onerr;
}
if (parser->tok == ';') {
- printf("End of statement\n");
break;
}
}
return false;
return true;
}
+ else if (parser->tok == TOKEN_KEYWORD)
+ {
+ if (!strcmp(parser_tokval(parser), "return"))
+ {
+ ast_expression *exp;
+ ast_return *ret;
+
+ if (!parser_next(parser)) {
+ parseerror(parser, "expected return expression");
+ return false;
+ }
+
+ exp = parser_expression(parser);
+ if (!exp)
+ return false;
+ ret = ast_return_new(exp->expression.node.context, exp);
+ if (!ret) {
+ ast_delete(exp);
+ return false;
+ }
+ if (!ast_block_exprs_add(block, (ast_expression*)ret)) {
+ ast_delete(ret);
+ return false;
+ }
+ return true;
+ }
+ parseerror(parser, "Unexpected keyword");
+ return false;
+ }
else if (parser->tok == '{')
{
/* a block */
- parseerror(parser, "TODO: inner blocks");
+ parseerror(parser, "TODO: inner blocks: %s", parser_tokval(parser));
return false;
}
else
ast_expression *exp = parser_expression(parser);
if (!exp)
return false;
- if (!ast_block_exprs_add(block, exp))
+ if (!ast_block_exprs_add(block, exp)) {
+ ast_delete(exp);
return false;
+ }
return true;
}
}