Fold length operations when argument is a constant string (#141)

Co-authored-by: Arseny Kapoulkine <arseny.kapoulkine@gmail.com>
This commit is contained in:
bmcq-0 2021-11-05 16:24:30 -04:00 committed by GitHub
parent a80fc93646
commit ad9b47b72f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -2900,6 +2900,11 @@ struct Compiler
break;
case AstExprUnary::Len:
if (arg.type == Constant::Type_String)
{
result.type = Constant::Type_Number;
result.valueNumber = double(arg.valueString.size);
}
break;
default:

View File

@ -1168,6 +1168,17 @@ RETURN R0 1
)");
}
TEST_CASE("ConstantFoldStringLen")
{
CHECK_EQ("\n" + compileFunction0("return #'string', #'', #'a', #('b')"), R"(
LOADN R0 6
LOADN R1 0
LOADN R2 1
LOADN R3 1
RETURN R0 4
)");
}
TEST_CASE("ConstantFoldCompare")
{
// ordered comparisons