Skip to content

Commit 8e3b67c

Browse files
committed
Add option to treat [] as parentheses
Also fix unit tests when not compiling as C++20. Blake-Madden#13
1 parent 352b48a commit 8e3b67c

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

docs/manual/compile-time-options.qmd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ This flag will also disable all bitwise functions and operators.
1616
By default, the operators `&`, `|`, and `^` represent logical AND, logical OR, and exponentiation. If `TE_BITWISE_OPERATORS` is defined,
1717
then they will represent bitwise AND, OR, and XOR.
1818

19+
## `TE_BRACKETS_AS_PARENS` {-}
20+
21+
Define this flag to treat `[]` pairs the same as `()`.
22+
This can be useful if using the parser for a debugger's watch window.
23+
For example, an expression such as `[rsp+1000]` would be evaluated as `1000` being added with the variable `rsp`.
24+
In this context, `rsp` could represent a memory address in the debugger.
25+
1926
## `TE_NO_BOOKKEEPING` {-}
2027

2128
By default, the parser will keep track of all functions and variables used in the last expression it evaluated.

docs/manual/operators.qmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ For operators, the order of precedence is:
3838

3939
| Operator | Description |
4040
| :-- | :-- |
41-
| ( ) | Instructions in parentheses are executed first. |
41+
| ( ) | Instructions in parentheses are executed first. (If `TE_BRACKETS_AS_PARENS` is defined, then `[]` are treated the same way.) |
4242
| \+ and - | Positive or negative sign for a value. |
4343
| ^ | Exponentiation. |
4444
| \*, /, and % | Multiplication, division, and modulus. |

tests/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ else()
6363
message(STATUS "Extended bitwise operators: disabled")
6464
endif(TE_BITWISE_OPERATORS)
6565

66+
if(TE_BRACKETS_AS_PARENS)
67+
message(STATUS "[] being treats as (): enabled")
68+
add_definitions(-DTE_BRACKETS_AS_PARENS)
69+
else()
70+
message(STATUS "[] being treats as (): disabled")
71+
endif(TE_BRACKETS_AS_PARENS)
72+
6673
# place Catch2 at the same folder level as this repo if it isn't installed
6774
# (you will need to do this on Windows or macOS or if version 3 of Catch2 isn't installed)
6875
get_filename_component(_fullpath "${_dir}" REALPATH)

tests/tetests.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,20 @@ TEST_CASE("Zeros", "[zeros]")
758758
CHECK(std::isnan(tep.evaluate("(1%0)%1")));
759759
}
760760

761+
762+
TEST_CASE("Braces", "[braces]")
763+
{
764+
te_type x{ 100 };
765+
766+
te_parser tep;
767+
tep.set_variables_and_functions({ {"x", &x} });
768+
#ifdef TE_BRACKETS_AS_PARENS
769+
CHECK(tep.evaluate("[x+86]") == 186);
770+
#else
771+
CHECK(std::isnan(tep.evaluate("[x+86]")));
772+
#endif
773+
}
774+
761775
TEST_CASE("Functions", "[functions]")
762776
{
763777
te_type x{ 0 }, y{ 0 };
@@ -2307,7 +2321,9 @@ TEST_CASE("Rotate operators", "[rotate]")
23072321
CHECK(tep.evaluate("BITRROTATE64(4294967295, -1)") == std::rotr(i, -1));
23082322
}
23092323
}
2324+
#endif
23102325

2326+
#ifndef TE_FLOAT
23112327
TEST_CASE("Shift operators", "[shift]")
23122328
{
23132329
te_parser tep;
@@ -2403,7 +2419,7 @@ TEST_CASE("Shift operators", "[shift]")
24032419
CHECK(tep.get_last_error_message().empty());
24042420
}
24052421
}
2406-
#else
2422+
#elif defined(TE_FLOAT)
24072423
// just make sure bitwise operators cause an error as they aren't supported for float
24082424
TEST_CASE("Bitwise operators null", "[shift,rotate]")
24092425
{

tinyexpr.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,11 +1195,19 @@ void te_parser::next_token(te_parser::state* theState)
11951195
theState->m_type = te_parser::state::token_type::TOK_INFIX;
11961196
theState->m_value = te_builtins::te_modulus;
11971197
}
1198+
#ifdef TE_BRACKETS_AS_PARENS
1199+
else if (tok == '(' || tok == '[')
1200+
#else
11981201
else if (tok == '(')
1202+
#endif
11991203
{
12001204
theState->m_type = te_parser::state::token_type::TOK_OPEN;
12011205
}
1206+
#ifdef TE_BRACKETS_AS_PARENS
1207+
else if (tok == ')' || tok == ']')
1208+
#else
12021209
else if (tok == ')')
1210+
#endif
12031211
{
12041212
theState->m_type = te_parser::state::token_type::TOK_CLOSE;
12051213
}

0 commit comments

Comments
 (0)