Skip to content

Commit

Permalink
curlies interpolation in string - escape curlies in the source
Browse files Browse the repository at this point in the history
  • Loading branch information
fglock committed Sep 1, 2010
1 parent 02da602 commit f20bb52
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 43 deletions.
2 changes: 1 addition & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ dev version - 2010-09-01
- new distribution "v6.pm" (Perlito in Perl 5)
This replaces Pugs::Compiler::Perl5 as the v6.pm compiler in CPAN.
- correct operator precedence
- interpolation of variables in strings
- interpolation of variables and code in strings
- 'unless'
- lists
- 'Mu'
Expand Down
14 changes: 5 additions & 9 deletions lib/Perlito/Go/Emitter.pm
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ class CompUnit {
$str = $str ~ $comp_unit.emit_go;
}
if !(%unit_seen{"Perlito::Grammar"}) {
$str = $str ~ "type Perlito__Grammar struct{}\n";
$str = $str ~ "type Perlito__Grammar struct\{}\n";
}
$str = $str ~ "// interfaces for all methods\n";
my %meth_seen = {
Expand Down Expand Up @@ -349,7 +349,7 @@ class CompUnit {
my $meth = $stmt.name;
$str = $str ~ "type "
~ $meth
~ "_er interface { f_"
~ "_er interface \{ f_"
~ $meth
~ " (Capture) *Any }\n";
%meth_seen{$meth} = 1;
Expand All @@ -358,7 +358,7 @@ class CompUnit {
my $meth = ($stmt.var).name;
$str = $str ~ "type "
~ $meth
~ "_er interface { f_"
~ "_er interface \{ f_"
~ $meth
~ " (Capture) *Any }\n";
%meth_seen{$meth} = 1;
Expand All @@ -367,7 +367,7 @@ class CompUnit {
}

$str = $str ~ "\n"
~ "func main () {\n"
~ "func main () \{\n"
~ " Init_Perlito__Match();\n";
for @($comp_units) -> $comp_unit {
$str = $str ~ " Init_" ~ Main::to_go_namespace( $comp_unit.name ) ~ "();\n"
Expand Down Expand Up @@ -986,10 +986,6 @@ class While {
}
}

class Leave {
method emit_go { die "TODO - Leave" }
}
class Decl {
has $.decl;
has $.type;
Expand Down Expand Up @@ -1032,7 +1028,7 @@ class Sig {
for @($.positional) -> $decl {
$str = $str
~ " var " ~ $decl.emit_go ~ " *Any;\n"
~ " if len(v.p) > " ~ $i ~ " {\n"
~ " if len(v.p) > " ~ $i ~ " \{\n"
~ " " ~ $decl.emit_go ~ " = v.p[" ~ $i ~ "];\n"
~ " }\n";
# avoid "x declared and not used"
Expand Down
8 changes: 4 additions & 4 deletions lib/Perlito/Python/Emitter.pm
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ class Perlito::Python::LexicalBlock {
my $label = "_anon_" ~ Perlito::Python::LexicalBlock::get_ident_python;
push @s, Python::tab($level) ~ 'def f_' ~ $label ~ '(v_self):';
push @s, Python::tab($level+1) ~ 'return v_self.v_' ~ ($decl.var).name;
push @s, Python::tab($level) ~ "self.__dict__.update({'f_" ~ ($decl.var).name ~ "':f_" ~ $label ~ "})";
push @s, Python::tab($level) ~ "self.__dict__.update(\{'f_" ~ ($decl.var).name ~ "':f_" ~ $label ~ "})";
}
if $decl.isa( 'Bind' ) && ($decl.parameters).isa( 'Decl' ) && ( ($decl.parameters).decl eq 'has' ) {
my $label = "_anon_" ~ Perlito::Python::LexicalBlock::get_ident_python;
push @s, Python::tab($level) ~ 'def f_' ~ $label ~ '(v_self):';
push @s, Python::tab($level+1) ~ 'return v_self.v_' ~ (($decl.parameters).var).name;
push @s, Python::tab($level) ~ "self.__dict__.update({'f_" ~ (($decl.parameters).var).name ~ "':f_" ~ $label ~ "})";
push @s, Python::tab($level) ~ "self.__dict__.update(\{'f_" ~ (($decl.parameters).var).name ~ "':f_" ~ $label ~ "})";
}
}

Expand Down Expand Up @@ -774,7 +774,7 @@ class Method {
push @s, $block.emit_python_indented($level + 2);
push @s, Python::tab($level+1) ~ "except mp6_Return, r:";
push @s, Python::tab($level+2) ~ "return r.value";
push @s, Python::tab($level) ~ "self.__dict__.update({'f_" ~ $.name ~ "':f_" ~ $label ~ "})";
push @s, Python::tab($level) ~ "self.__dict__.update(\{'f_" ~ $.name ~ "':f_" ~ $label ~ "})";
return @s.join("\n");
}
}
Expand Down Expand Up @@ -830,7 +830,7 @@ class Sub {
push @s, Python::tab($level) ~ $label2 ~ " = f_" ~ $.name;
push @s, Python::tab($level) ~ "def f_" ~ $label ~ "(" ~ $meth_args.join(", ") ~ "):";
push @s, Python::tab($level+1) ~ "return " ~ $label2 ~ "(" ~ $args.join(", ") ~ ")";
push @s, Python::tab($level) ~ "self.__dict__.update({'f_" ~ $.name ~ "':f_" ~ $label ~ "})";
push @s, Python::tab($level) ~ "self.__dict__.update(\{'f_" ~ $.name ~ "':f_" ~ $label ~ "})";
return @s.join("\n");
}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/Perlito/Ruby/Emitter.pm
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class Perlito::Ruby::LexicalBlock {
}
}
if $has_my_decl {
push @s, Ruby::tab($level) ~ "Proc.new{ |" ~ @my_decl.join(", ") ~ "|";
push @s, Ruby::tab($level) ~ "Proc.new\{ |" ~ @my_decl.join(", ") ~ "|";
}

my $last_statement;
Expand Down Expand Up @@ -367,7 +367,7 @@ class Lit::Array {
}
push @block, $temp_array;
my $body_block = Perlito::Ruby::LexicalBlock.new( block => @block );
return Ruby::tab($level) ~ "Proc.new { |list_a| " ~ "\n"
return Ruby::tab($level) ~ "Proc.new \{ |list_a| " ~ "\n"
~ $body_block.emit_ruby_indented( $level + 1 ) ~ "\n"
~ Ruby::tab($level) ~ "}.call([])";
}
Expand Down Expand Up @@ -407,7 +407,7 @@ class Lit::Object {
for @$fields -> $field {
push @str, "o.v_" ~ ($field[0]).buf ~ '=' ~ ($field[1]).emit_ruby ~ "; ";
}
Ruby::tab($level) ~ "Proc.new { |o| "
Ruby::tab($level) ~ "Proc.new \{ |o| "
~ @str.join(' ')
~ "o }.call(C_" ~ Main::to_go_namespace($.class) ~ ".new)";
}
Expand Down Expand Up @@ -519,7 +519,7 @@ class Call {
|| ($.method eq 'isa')
{
if ($.hyper) {
return $invocant ~ ".map {|x| x." ~ $.method ~ "(" ~ (@.arguments.>>emit_ruby).join(', ') ~ ")}";
return $invocant ~ ".map \{|x| x." ~ $.method ~ "(" ~ (@.arguments.>>emit_ruby).join(', ') ~ ")}";
}
else {
return "mp6_" ~ $.method ~ '(' ~ ([ $.invocant, @.arguments].>>emit_ruby).join(', ') ~ ')';
Expand All @@ -546,7 +546,7 @@ class Call {

my $call = 'f_' ~ $meth ~ '(' ~ (@.arguments.>>emit_ruby).join(', ') ~ ')';
if ($.hyper) {
Ruby::tab($level) ~ $invocant ~ ".map {|x| x." ~ $call ~ "}";
Ruby::tab($level) ~ $invocant ~ ".map \{|x| x." ~ $call ~ "}";
}
else {
Ruby::tab($level) ~ $invocant ~ '.' ~ $call;
Expand Down Expand Up @@ -781,7 +781,7 @@ class Method {
block => @.block,
needs_return => 1 );
my @s;
push @s, Ruby::tab($level) ~ 'send( :define_method, "f_' ~ $.name ~ '".to_sym, lambda{ |' ~ $default_args.join(", ") ~ '|';
push @s, Ruby::tab($level) ~ 'send( :define_method, "f_' ~ $.name ~ '".to_sym, lambda\{ |' ~ $default_args.join(", ") ~ '|';
push @s, Ruby::tab($level+1) ~ $invocant.emit_ruby_name ~ " = self";
push @s, $block.emit_ruby_indented($level + 1);
push @s, Ruby::tab($level) ~ "} )";
Expand Down Expand Up @@ -838,7 +838,7 @@ class Do {
method emit_ruby { $self.emit_ruby_indented(0) }
method emit_ruby_indented( $level ) {
my @s;
push @s, Ruby::tab($level) ~ "Proc.new{ || ";
push @s, Ruby::tab($level) ~ "Proc.new\{ || ";
push @s, (Perlito::Ruby::LexicalBlock.new( block => @.block, needs_return => 0 )).emit_ruby_indented($level+1);
push @s, Ruby::tab($level) ~ "}.call()";
return @s.join("\n");
Expand Down
17 changes: 5 additions & 12 deletions lib5/Perlito/Go/Emitter.pm
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ sub emit_go { my $self = $_[0]; (my $class_name = Main::to_go_namespace($self->
')) } }; ($str = $str . ' }' . '
'); ($str = $str . '}' . '
'); return($str) };
sub emit_go_program { my $comp_units = $_[0]; (my $str = ''); my $Hash_unit_seen; my $List_tmp_comp_unit; for my $comp_unit ( @{[@{(($comp_units) || []) || []}] || []} ) { (my $name = $comp_unit->name()); if (Main::bool($Hash_unit_seen->{$name})) { for my $stmt ( @{[@{(($comp_unit->body()) || []) || []}] || []} ) { push( @{($Hash_unit_seen->{$name})->body()}, $stmt ) } } else { ($Hash_unit_seen->{$name} = $comp_unit); push( @{$List_tmp_comp_unit}, $comp_unit ) } }; ($comp_units = $List_tmp_comp_unit); for my $comp_unit ( @{[@{(($comp_units) || []) || []}] || []} ) { for my $stmt ( @{[@{(($comp_unit->body()) || []) || []}] || []} ) { if (Main::bool(Main::isa($stmt, 'Method'))) { (($comp_unit->methods())->{$stmt->name()} = $stmt) } ; if (Main::bool((Main::isa($stmt, 'Decl') && (($stmt->decl() eq 'has'))))) { (($comp_unit->attributes())->{($stmt->var())->name()} = $stmt) } } }; for my $comp_unit ( @{[@{(($comp_units) || []) || []}] || []} ) { ($str = $str . $comp_unit->emit_go()) }; if (Main::bool((($Hash_unit_seen->{'Perlito::Grammar'}) ? 0 : 1))) { ($str = $str . 'type Perlito__Grammar struct{}' . '
sub emit_go_program { my $comp_units = $_[0]; (my $str = ''); my $Hash_unit_seen; my $List_tmp_comp_unit; for my $comp_unit ( @{[@{(($comp_units) || []) || []}] || []} ) { (my $name = $comp_unit->name()); if (Main::bool($Hash_unit_seen->{$name})) { for my $stmt ( @{[@{(($comp_unit->body()) || []) || []}] || []} ) { push( @{($Hash_unit_seen->{$name})->body()}, $stmt ) } } else { ($Hash_unit_seen->{$name} = $comp_unit); push( @{$List_tmp_comp_unit}, $comp_unit ) } }; ($comp_units = $List_tmp_comp_unit); for my $comp_unit ( @{[@{(($comp_units) || []) || []}] || []} ) { for my $stmt ( @{[@{(($comp_unit->body()) || []) || []}] || []} ) { if (Main::bool(Main::isa($stmt, 'Method'))) { (($comp_unit->methods())->{$stmt->name()} = $stmt) } ; if (Main::bool((Main::isa($stmt, 'Decl') && (($stmt->decl() eq 'has'))))) { (($comp_unit->attributes())->{($stmt->var())->name()} = $stmt) } } }; for my $comp_unit ( @{[@{(($comp_units) || []) || []}] || []} ) { ($str = $str . $comp_unit->emit_go()) }; if (Main::bool((($Hash_unit_seen->{'Perlito::Grammar'}) ? 0 : 1))) { ($str = $str . 'type Perlito__Grammar struct' . '{' . '}' . '
') } ; ($str = $str . '// interfaces for all methods' . '
'); (my $Hash_meth_seen = { ('join' => 1),('perl' => 1),('scalar' => 1),('isa' => 1),('values' => 1),('keys' => 1),('exists' => 1),('bind' => 1),('int' => 1),('num' => 1),('str' => 1),('Str' => 1),('bool' => 1),('Bool' => 1),('array' => 1),('hash' => 1),('push' => 1),('pop' => 1),('shift' => 1),('lookup' => 1),('index' => 1),('function' => 1), }); for my $comp_unit ( @{[@{(($comp_units) || []) || []}] || []} ) { for my $stmt ( @{[@{(($comp_unit->body()) || []) || []}] || []} ) { if (Main::bool((Main::isa($stmt, 'Method') && (($Hash_meth_seen->{$stmt->name()}) ? 0 : 1)))) { (my $meth = $stmt->name()); ($str = $str . 'type ' . $meth . '_er interface { f_' . $meth . ' (Capture) *Any }' . '
'); ($Hash_meth_seen->{$meth} = 1) } ; if (Main::bool(((Main::isa($stmt, 'Decl') && (($stmt->decl() eq 'has'))) && (($Hash_meth_seen->{($stmt->var())->name()}) ? 0 : 1)))) { (my $meth = ($stmt->var())->name()); ($str = $str . 'type ' . $meth . '_er interface { f_' . $meth . ' (Capture) *Any }' . '
'); (my $Hash_meth_seen = { ('join' => 1),('perl' => 1),('scalar' => 1),('isa' => 1),('values' => 1),('keys' => 1),('exists' => 1),('bind' => 1),('int' => 1),('num' => 1),('str' => 1),('Str' => 1),('bool' => 1),('Bool' => 1),('array' => 1),('hash' => 1),('push' => 1),('pop' => 1),('shift' => 1),('lookup' => 1),('index' => 1),('function' => 1), }); for my $comp_unit ( @{[@{(($comp_units) || []) || []}] || []} ) { for my $stmt ( @{[@{(($comp_unit->body()) || []) || []}] || []} ) { if (Main::bool((Main::isa($stmt, 'Method') && (($Hash_meth_seen->{$stmt->name()}) ? 0 : 1)))) { (my $meth = $stmt->name()); ($str = $str . 'type ' . $meth . '_er interface ' . '{' . ' f_' . $meth . ' (Capture) *Any }' . '
'); ($Hash_meth_seen->{$meth} = 1) } ; if (Main::bool(((Main::isa($stmt, 'Decl') && (($stmt->decl() eq 'has'))) && (($Hash_meth_seen->{($stmt->var())->name()}) ? 0 : 1)))) { (my $meth = ($stmt->var())->name()); ($str = $str . 'type ' . $meth . '_er interface ' . '{' . ' f_' . $meth . ' (Capture) *Any }' . '
'); ($Hash_meth_seen->{$meth} = 1) } } }; ($str = $str . '
' . 'func main () {' . '
' . 'func main () ' . '{' . '
' . ' Init_Perlito__Match();' . '
'); for my $comp_unit ( @{[@{(($comp_units) || []) || []}] || []} ) { ($str = $str . ' Init_' . Main::to_go_namespace($comp_unit->name()) . '();' . '
') }; ($str = $str . ' Init_Prelude();' . '
Expand Down Expand Up @@ -330,13 +330,6 @@ sub body { $_[0]->{body} };
sub emit_go { my $self = $_[0]; (my $cond = $self->{cond}); if (Main::bool((Main::isa($cond, 'Var') && ($cond->sigil() eq '@')))) { ($cond = Apply->new(('code' => 'prefix:<@>'), ('arguments' => [$cond]))) } ; return('for ' . ((Main::bool($self->{init}) ? $self->{init}->emit_go(("" . '; ')) : ';')) . 'tobool( ' . Call::emit_go_call($cond, 'Bool') . ' ); ' . ((Main::bool($self->{continue}) ? $self->{continue}->emit_go(("" . ' ')) : '')) . '{ ' . (Perlito::Go::LexicalBlock->new(('block' => $self->{body}), ('needs_return' => 0)))->emit_go(("" . ' }'))) }
}
;
{
package Leave;
sub new { shift; bless { @_ }, "Leave" }
sub emit_go { my $self = $_[0]; die('TODO - Leave') }
}
;
{
package Decl;
Expand All @@ -362,7 +355,7 @@ sub positional { $_[0]->{positional} };
sub named { $_[0]->{named} };
sub emit_go { my $self = $_[0]; ' print \'Signature - TODO\'; die \'Signature - TODO\'; ' };
sub emit_go_bind { my $self = $_[0]; (my $str = ''); (my $i = 0); for my $decl ( @{[@{(($self->{positional}) || []) || []}] || []} ) { ($str = $str . ' var ' . $decl->emit_go(("" . ' *Any;' . '
') . ' if len(v.p) > ' . $i . ' {' . '
') . ' if len(v.p) > ' . $i . ' ' . '{' . '
' . ' ' . $decl->emit_go(("" . ' = v.p[') . $i . '];' . '
' . ' }' . '
'))); ($str = $str . $decl->emit_go(("" . ' = ') . $decl->emit_go(("" . '; ')))); ($i = ($i + 1)) }; return($str) }
Expand Down
Loading

0 comments on commit f20bb52

Please sign in to comment.