Code

analyzer: Verify the type of arithmetic operands.
authorSebastian Harl <sh@tokkee.org>
Fri, 27 Feb 2015 18:22:35 +0000 (19:22 +0100)
committerSebastian Harl <sh@tokkee.org>
Fri, 27 Feb 2015 18:22:35 +0000 (19:22 +0100)
src/frontend/analyzer.c

index 607015c233ce15cd4df4129b32024b95e7c735e1..75aa94d1579da92f8f9e25b3ee03c74273d70b74 100644 (file)
@@ -68,6 +68,14 @@ cmp_error(sdb_strbuf_t *errbuf, int op, int left, int right)
                        SDB_TYPE_TO_STRING(right));
 } /* cmp_error */
 
+static void
+op_error(sdb_strbuf_t *errbuf, int op, int left, int right)
+{
+       sdb_strbuf_sprintf(errbuf, "Invalid operator %s for types %s and %s",
+                       SDB_DATA_OP_TO_STRING(op), SDB_TYPE_TO_STRING(left),
+                       SDB_TYPE_TO_STRING(right));
+} /* cmp_error */
+
 static int
 analyze_expr(int context, sdb_store_expr_t *e, sdb_strbuf_t *errbuf)
 {
@@ -104,6 +112,15 @@ analyze_expr(int context, sdb_store_expr_t *e, sdb_strbuf_t *errbuf)
                                return -1;
                        if (analyze_expr(context, e->right, errbuf))
                                return -1;
+
+                       if ((e->left->data_type > 0) && (e->right->data_type > 0)) {
+                               if (sdb_data_expr_type(e->type, e->left->data_type,
+                                                       e->right->data_type) < 0) {
+                                       op_error(errbuf, e->type, e->left->data_type,
+                                                       e->right->data_type);
+                                       return -1;
+                               }
+                       }
                        break;
        }
        return 0;