]> de.git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - parser.c
Starting some typechecking. Function parameters must be part of ast_expression_common...
[xonotic/gmqcc.git] / parser.c
index 56f20f271957c73d056dd173a57bfc727d4d2039..0c87a03b8798c2984f2a35412ae6f406fbc42ee3 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -192,8 +192,10 @@ 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
@@ -209,6 +211,7 @@ static sy_elem syexp(lex_ctx ctx, ast_expression *v) {
     e.etype = 0;
     e.out   = v;
     e.value = NULL;
+    e.block = NULL;
     e.ctx   = ctx;
     e.paren = 0;
     return e;
@@ -218,6 +221,18 @@ static sy_elem syval(lex_ctx ctx, ast_value *v) {
     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;
@@ -228,16 +243,19 @@ static sy_elem syop(lex_ctx ctx, const oper_info *op) {
     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;
@@ -250,6 +268,7 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy)
     ast_expression *out = NULL;
     ast_expression *exprs[3];
     ast_value      *vars[3];
+    ast_block      *blocks[3];
     size_t i;
 
     if (!sy->ops_count) {
@@ -274,8 +293,14 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy)
 
     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;
     }
 
     switch (op->id)
@@ -284,6 +309,24 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy)
             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",
@@ -391,6 +434,115 @@ static bool parser_sy_pop(parser_t *parser, shunt *sy)
     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;
@@ -438,11 +590,16 @@ static ast_expression* parser_expression(parser_t *parser)
             }
             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");
@@ -453,13 +610,7 @@ static ast_expression* parser_expression(parser_t *parser)
         } else {
             if (parser->tok == '(') {
                 /* we expected an operator, this is the function-call operator */
-                if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), 'f'))) {
-                    parseerror(parser, "out of memory");
-                    goto onerr;
-                }
-            }
-            else if (parser->tok == ',') {
-                if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), ','))) {
+                if (!shunt_ops_add(&sy, syparen(parser_ctx(parser), 'f', sy.out_count-1))) {
                     parseerror(parser, "out of memory");
                     goto onerr;
                 }
@@ -467,22 +618,8 @@ static ast_expression* parser_expression(parser_t *parser)
             else if (parser->tok == ')') {
                 /* we do expect an operator next */
                 /* closing an opening paren */
-                if (!sy.ops_count) {
-                    parseerror(parser, "unmatched closing paren");
+                if (!parser_close_paren(parser, &sy, false))
                     goto onerr;
-                }
-                if (sy.ops[sy.ops_count-1].paren == 1) {
-                    parseerror(parser, "empty parenthesis expression");
-                    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");