Parser tests: Add helpers for creating consts

This commit is contained in:
Laria 2021-12-18 16:11:23 +01:00
parent 1ba891282e
commit 632687b47d

View file

@ -211,6 +211,51 @@ new_dot(struct parser_test *pt, int line, int col, const char *rhs, struct apfl_
return new_helper(pt, sizeof(struct apfl_expr), &expr);
}
static struct apfl_expr_const
num_const(apfl_number n)
{
return (struct apfl_expr_const) {
.type = APFL_EXPR_CONST_NUMBER,
.number = n,
};
}
static struct apfl_expr_const
string_const(struct parser_test *pt, const char *s)
{
return (struct apfl_expr_const) {
.type = APFL_EXPR_CONST_STRING,
.string = new_string(pt, s),
};
}
static struct apfl_expr_const
bool_const(bool b)
{
return (struct apfl_expr_const) {
.type = APFL_EXPR_CONST_BOOLEAN,
.boolean = b,
};
}
static struct apfl_expr
const_expr(int line, int col, struct apfl_expr_const constant)
{
return (struct apfl_expr) {
.type = APFL_EXPR_CONSTANT,
.position = POS(line, col),
.constant = constant,
};
}
static struct apfl_expr *
new_const_expr(struct parser_test *pt, int line, int col, struct apfl_expr_const constant)
{
struct apfl_expr expr = const_expr(line, col, constant);
return new_helper(pt, sizeof(struct apfl_expr), &expr);
}
TEST(empty, t) {
struct parser_test *pt = new_parser_test(t, "");
expect_eof(pt);
@ -227,12 +272,7 @@ TEST(hello_world, t) {
.arguments = LIST_BEGIN(pt, list)
LIST_ADD (struct apfl_expr_list_item) {
.expand = false,
.expr = BEGIN_NEW(pt, struct apfl_expr) {
.type = APFL_EXPR_CONSTANT,
.position = POS(1, 7),
.constant.type = APFL_EXPR_CONST_STRING,
.constant.string = new_string(pt, "Hello World!"),
} END_NEW,
.expr = new_const_expr(pt, 1, 7, string_const(pt, "Hello World!")),
},
LIST_END,
},
@ -386,20 +426,10 @@ TEST(complex_dot_at_access, t) {
.rhs = new_var(pt, 1, 3, "b"),
}
} END_NEW),
.rhs = BEGIN_NEW(pt, struct apfl_expr) {
.type = APFL_EXPR_CONSTANT,
.position = POS(1, 7),
.constant.type = APFL_EXPR_CONST_NUMBER,
.constant.number = 1,
} END_NEW,
.rhs = new_const_expr(pt, 1, 7, num_const(1)),
},
} END_NEW,
.rhs = BEGIN_NEW(pt, struct apfl_expr) {
.type = APFL_EXPR_CONSTANT,
.position = POS(1, 9),
.constant.type = APFL_EXPR_CONST_STRING,
.constant.string = new_string(pt, "d"),
} END_NEW,
.rhs = new_const_expr(pt, 1, 9, string_const(pt, "d")),
},
} END_NEW,
.rhs = BEGIN_NEW(pt, struct apfl_expr) {
@ -465,21 +495,11 @@ TEST(factorial, t) {
.params = LIST_BEGIN(pt, params)
LIST_ADD (struct apfl_expr_param) {
.type = APFL_EXPR_PARAM_CONSTANT,
.constant = (struct apfl_expr_const) {
.type = APFL_EXPR_CONST_NUMBER,
.number = 0,
},
.constant = num_const(0),
},
LIST_END,
.body = LIST_BEGIN(pt, body)
LIST_ADD (struct apfl_expr) {
.type = APFL_EXPR_CONSTANT,
.position = POS(2, 10),
.constant = (struct apfl_expr_const) {
.type = APFL_EXPR_CONST_NUMBER,
.number = 1,
},
},
LIST_ADD const_expr(2, 10, num_const(1)),
LIST_END
},
LIST_ADD (struct apfl_expr_subfunc) {
@ -510,14 +530,7 @@ TEST(factorial, t) {
.callee = new_var(pt, 3, 26, "-"),
.arguments = LIST_BEGIN(pt, list)
LIST_ADD list_item(false, new_var(pt, 3, 28, "n")),
LIST_ADD list_item(false, BEGIN_NEW(pt, struct apfl_expr) {
.type = APFL_EXPR_CONSTANT,
.position = POS(3, 30),
.constant = (struct apfl_expr_const) {
.type = APFL_EXPR_CONST_NUMBER,
.number = 1,
},
} END_NEW),
LIST_ADD list_item(false, new_const_expr(pt, 3, 30,num_const(1))),
LIST_END,
},
} END_NEW),
@ -541,14 +554,7 @@ TEST(factorial, t) {
.call = (struct apfl_expr_call) {
.callee = new_var(pt, 5, 1, "factorial"),
.arguments = LIST_BEGIN(pt, list)
LIST_ADD list_item(false, BEGIN_NEW(pt, struct apfl_expr) {
.type = APFL_EXPR_CONSTANT,
.position = POS(5, 11),
.constant = (struct apfl_expr_const) {
.type = APFL_EXPR_CONST_NUMBER,
.number = 10,
},
} END_NEW),
LIST_ADD list_item(false, new_const_expr(pt, 5, 11, num_const(10))),
LIST_END,
},
});