Skip to content

Commit

Permalink
- Fixed the generated code for a constantant const named v. (whic…
Browse files Browse the repository at this point in the history
…h confused the Perl parser into parsing it as a version number) (fixes #95)

Example:

	func foo() {
	    const v = 42
	    return v
	}
	say foo()

Special thanks to @catb0t for finding and reporting this issue.
  • Loading branch information
trizen committed Mar 19, 2019
1 parent 5b08093 commit 9f663c0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Sidef/Deparse/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions lib/Sidef/Object/Object.pod
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ Aliases: I<is_an>, I<kind_of>

=cut

=head2 is_object

Object.is_object() -> I<Obj>

Return the

=cut

=head2 is_typename

Object.is_typename() -> I<Obj>

Return the

=cut

=head2 lazy

Object.lazy() -> I<Obj>
Expand Down
20 changes: 20 additions & 0 deletions scripts/Tests/const_tests.sf
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
1 change: 1 addition & 0 deletions scripts/Tests/lazy_methods.sf
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

3 comments on commit 9f663c0

@catb0t
Copy link
Contributor

@catb0t catb0t commented on 9f663c0 Mar 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

o.o what an incredible bug

@trizen
Copy link
Owner Author

@trizen trizen commented on 9f663c0 Mar 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Strangely enough, this works correctly:

$ perl -E 'sub v94055783397600 { 42 } say (v94055783397600)'

while this seems to confuse the Perl parser:

$ perl -E 'my sub v94055783397600 { 42 } say (v94055783397600)'

@catb0t
Copy link
Contributor

@catb0t catb0t commented on 9f663c0 Mar 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that behaviour documented somewhere? I don't see why it would be intentional...

Please sign in to comment.