From 9f663c0a1ab89854855fc376362a233d449b33c4 Mon Sep 17 00:00:00 2001 From: trizen <614513+trizen@users.noreply.github.com> Date: Tue, 19 Mar 2019 21:48:22 +0200 Subject: [PATCH] - Fixed the generated code for a constantant `const` named `v`. (which confused the Perl parser into parsing it as a version number) (fixes https://github.com/trizen/sidef/issues/95) Example: func foo() { const v = 42 return v } say foo() Special thanks to @catb0t for finding and reporting this issue. --- lib/Sidef/Deparse/Perl.pm | 2 +- lib/Sidef/Object/Object.pod | 16 ++++++++++++++++ scripts/Tests/const_tests.sf | 20 ++++++++++++++++++++ scripts/Tests/lazy_methods.sf | 1 + 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/Sidef/Deparse/Perl.pm b/lib/Sidef/Deparse/Perl.pm index a44c0680a..952907cad 100644 --- a/lib/Sidef/Deparse/Perl.pm +++ b/lib/Sidef/Deparse/Perl.pm @@ -763,7 +763,7 @@ HEADER $code = $value; } elsif ($ref eq 'Sidef::Variable::Const') { - my $name = $obj->{name} . $refaddr; + my $name = "const_$obj->{name}" . $refaddr; my $value = '(' . $name . ')'; if (not exists $obj->{inited}) { $obj->{inited} = 1; diff --git a/lib/Sidef/Object/Object.pod b/lib/Sidef/Object/Object.pod index 2fbe33e18..356bed8dc 100644 --- a/lib/Sidef/Object/Object.pod +++ b/lib/Sidef/Object/Object.pod @@ -138,6 +138,22 @@ Aliases: I, I =cut +=head2 is_object + +Object.is_object() -> I + +Return the + +=cut + +=head2 is_typename + +Object.is_typename() -> I + +Return the + +=cut + =head2 lazy Object.lazy() -> I diff --git a/scripts/Tests/const_tests.sf b/scripts/Tests/const_tests.sf index 9aa084ce1..a5e75ff57 100644 --- a/scripts/Tests/const_tests.sf +++ b/scripts/Tests/const_tests.sf @@ -47,4 +47,24 @@ do { assert_eq(foo(), 43) } +do { + const v = 42 + assert_eq(v, 42) + + func foo(n) { + const v = n + } + + assert_eq(foo(3), 3) + assert_eq(foo(4), 4) + + func bar(n) { + const v = n + return v + } + + assert_eq(bar(5), 5) + assert_eq(bar(6), 6) +} + say "** Test passed!" diff --git a/scripts/Tests/lazy_methods.sf b/scripts/Tests/lazy_methods.sf index e24c4c7f6..778871b40 100644 --- a/scripts/Tests/lazy_methods.sf +++ b/scripts/Tests/lazy_methods.sf @@ -79,6 +79,7 @@ assert_eq(Number.methods(4, 5){:add}(), 9) assert_eq(Number.method(:add)(4, 5), 9) assert_eq(Number.method(:add, 4)(5), 9) +assert_eq(Number.method(:add, 4, 5)(), 9) assert_eq(4.method(:add)(5), 9) assert_eq(4.method(:add, 5)(), 9)