From: Dale Weiler Date: Thu, 7 Mar 2013 23:05:40 +0000 (+0000) Subject: Actually generate an ast_call for __builtin_pow for the ** operator, otherwise the... X-Git-Tag: before-library~100 X-Git-Url: https://git.xonotic.org/?p=xonotic%2Fgmqcc.git;a=commitdiff_plain;h=12340a6bd077a5ff3454fe48913cfadb661f52cc Actually generate an ast_call for __builtin_pow for the ** operator, otherwise the operator yeilds a ast_function, making "a ** b" not work, but since it's a function, allows **(a, b). Also added tests for exponentiation operator. --- diff --git a/parser.c b/parser.c index b97d92c..6ab55e3 100644 --- a/parser.c +++ b/parser.c @@ -1086,7 +1086,10 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy) if (CanConstFold(exprs[0], exprs[1])) { out = (ast_expression*)parser_const_float(parser, powf(ConstF(0), ConstF(1))); } else { - out = parser_builtin_pow(parser); + ast_call *gencall = ast_call_new(parser_ctx(parser), parser_builtin_pow(parser)); + vec_push(gencall->params, exprs[0]); + vec_push(gencall->params, exprs[1]); + out = (ast_expression*)gencall; } break; @@ -1435,7 +1438,8 @@ static bool parser_sy_apply_operator(parser_t *parser, shunt *sy) if(CanConstFold1(exprs[0])) out = (ast_expression*)parser_const_float(parser, ~(qcint)ConstF(0)); else - out = (ast_expression*)ast_binary_new(ctx, INSTR_SUB_F, (ast_expression*)parser_const_float_neg1(parser), exprs[0]); + out = (ast_expression*) + ast_binary_new(ctx, INSTR_SUB_F, (ast_expression*)parser_const_float_neg1(parser), exprs[0]); break; } #undef NotSameType diff --git a/tests/exponentiation.qc b/tests/exponentiation.qc new file mode 100644 index 0000000..f3ab7d3 --- /dev/null +++ b/tests/exponentiation.qc @@ -0,0 +1,11 @@ +float pow(float x, float y) { + return __builtin_pow(x, y); +} + +void main() { + float hundy = pow(10, 2); // 10^2 == 100 + print(ftos(hundy), "\n"); // prints: 100 + + hundy -= 90; // 100-90 = 10 + print(ftos(hundy ** 2), "\n"); // prints: 100 +} diff --git a/tests/exponentiation.tmpl b/tests/exponentiation.tmpl new file mode 100644 index 0000000..47741ea --- /dev/null +++ b/tests/exponentiation.tmpl @@ -0,0 +1,8 @@ +# used to test the builtins +I: exponentiation.qc +D: test exponentiation operator and __builtin_pow +T: -execute +C: -std=gmqcc +E: $null +M: 100 +M: 100