numbas / Numbas

A completely browser-based e-assessment/e-learning system, with an emphasis on mathematics
http://www.numbas.org.uk
Apache License 2.0
197 stars 118 forks source link

Bug with bracketing in an `expression` call where the string is not just one literal #1013

Closed christianp closed 10 months ago

christianp commented 1 year ago

Minimal example: expression("{1}+" + "2"). I get an error "not enough arguments for operation +". My guess is that at some point it's trying to compile the first string literal as an entire expression.

Jonnie-Dev commented 11 months ago

what are you trying to achieve with this

christianp commented 10 months ago

The problem is that string literals inside expression() are each separately compiled before substitution is performed on the syntax tree, and then serialized back to a string.

The reason we can't just concatenate all the strings before subbing is something like expression("(a + " + n + " + c)"), where n could be any complicated expression that evaluates to something which can convert to string.

I tried using the forgiving parser to allow a missing right-hand argument for the case at the top of this issue, but I don't think that satisfies enough of the cases to justify introducing more weird behaviour.

This might just have to be something that can't work. We should push people to construct expressions by using substitute.