Permalink
Browse files

Go backend - updated to new syntax; thanks to skelterjohn at #go-nuts

  • Loading branch information...
1 parent b6e4d56 commit 369439f4cf62c772152e016028edb4da672b20dd @fglock committed Jan 14, 2010
Showing with 97 additions and 49 deletions.
  1. +0 −2 README
  2. +24 −23 lib/MiniPerl6/Go/Emitter.pm
  3. +3 −7 lib/MiniPerl6/Go/Runtime.go
  4. +27 −3 lib5/MiniPerl6/Go/Emitter.pm
  5. +43 −14 liblisp/MiniPerl6/Go/Emitter.lisp
View
2 README
@@ -6,8 +6,6 @@ Go backend
perl mp6.pl -Bgo t/04-op.t
- The Go release from about 11 Nov 2009 is required. Newer versions may not work.
-
See also: Go homepage at http://golang.org
Javascript backend - browser
@@ -470,18 +470,18 @@ class Lit::Object {
for @$fields -> $field {
$str := $str
- ~ 'if m.v_' ~ ($field[0]).buf ~ ' == nil {'
- ~ 'var p Any; '
- ~ 'm.v_' ~ ($field[0]).buf ~ ' = &p; '
- ~ '}'
+ ~ 'if m.v_' ~ ($field[0]).buf ~ ' == nil {' ~ "\n"
+ ~ 'var p Any; ' ~ "\n"
+ ~ 'm.v_' ~ ($field[0]).buf ~ ' = &p; ' ~ "\n"
+ ~ '}' ~ "\n"
- ~ '*m.v_' ~ ($field[0]).buf ~ ' = *' ~ ($field[1]).emit_go ~ '; ';
+ ~ '*m.v_' ~ ($field[0]).buf ~ ' = *' ~ ($field[1]).emit_go ~ '; ' ~ "\n";
};
- 'func() *Any { '
- ~ 'var m = new(' ~ Main::to_go_namespace($.class) ~ '); '
- ~ $str
- ~ 'var m1 Any = m; '
- ~ 'return &m1; '
+ 'func() *Any { ' ~ "\n"
+ ~ ' var m = new(' ~ Main::to_go_namespace($.class) ~ '); ' ~ "\n"
+ ~ ' ' ~ $str ~ "\n"
+ ~ ' var m1 Any = m; ' ~ "\n"
+ ~ ' return &m1; ' ~ "\n"
~ '}()';
}
}
@@ -664,12 +664,13 @@ class Call {
if ($.hyper) {
return
- 'func (a_ *Any) *Any { '
- ~ 'var out = a_array(); '
- ~ 'var i = (*(*a_).(array_er).f_array(Capture{})).(*Array); '
- ~ 'for pos := 0; pos <= i.n; pos++ { '
- ~ '(*out).(push_er).f_push( Capture{p: []*Any{ (*i.v[pos]).(' ~ $meth ~ '_er).f_' ~ $meth ~ '(Capture{ p : []*Any{} }) }} ) } '
- ~ 'return out; '
+ 'func (a_ *Any) *Any { ' ~ "\n"
+ ~ ' var out = a_array(); ' ~ "\n"
+ ~ ' var i = (*(*a_).(array_er).f_array(Capture{})).(*Array); ' ~ "\n"
+ ~ ' for pos := 0; pos <= i.n; pos++ { ' ~ "\n"
+ ~ ' (*out).(push_er).f_push( Capture{p: []*Any{ (*i.v[pos]).(' ~ $meth ~ '_er).f_' ~ $meth ~ '(Capture{ p : []*Any{} }) }} )' ~ "\n"
+ ~ ' } ' ~ "\n"
+ ~ ' return out; ' ~ "\n"
~ '}(' ~ $invocant ~ ')'
}
return
@@ -914,13 +915,13 @@ class For {
has @.body;
has @.topic;
method emit_go {
- 'func (a_ *Any) { '
- ~ 'var i = (*(*a_).(array_er).f_array(Capture{})).(*Array); '
- ~ 'for pos := 0; pos <= i.n; pos++ { '
- ~ 'func (' ~ $.topic.emit_go ~ ' *Any) { '
- ~ ::MiniPerl6::Go::LexicalBlock( block => @.body, needs_return => 0 ).emit_go
- ~ ' }(i.v[pos]) '
- ~ '} '
+ 'func (a_ *Any) { ' ~ "\n"
+ ~ ' var i = (*(*a_).(array_er).f_array(Capture{})).(*Array); ' ~ "\n"
+ ~ ' for pos := 0; pos <= i.n; pos++ { ' ~ "\n"
+ ~ ' func (' ~ $.topic.emit_go ~ ' *Any) { ' ~ "\n"
+ ~ ' ' ~ ::MiniPerl6::Go::LexicalBlock( block => @.body, needs_return => 0 ).emit_go ~ "\n"
+ ~ ' }(i.v[pos]) ' ~ "\n"
+ ~ ' } ' ~ "\n"
~ '}(' ~ $.cond.emit_go ~ ')'
}
}
@@ -30,7 +30,7 @@ import (
"runtime";
"unicode";
// "reflect";
- "io";
+ "io/ioutil";
"utf8";
)
@@ -224,11 +224,7 @@ func (i Str) f_str_equal(v Capture) *Any {
}
func (i Str) f_perl(Capture) *Any {
var s Any = i;
- return toStr(
- "'"
- + tostr( Namespace_Main.f_perl_escape_string(Capture{ p : []*Any{ &s } }) )
- + "'"
- )
+ return toStr( "'" + tostr( Namespace_Main.f_perl_escape_string(Capture{ p : []*Any{ &s } }) ) + "'" )
}
func (i Str) f_isa (v Capture) *Any { return toBool( "Str" == tostr( v.p[0] ) ) }
func (i Str) f_chars (v Capture) *Any {
@@ -737,7 +733,7 @@ func Init_Prelude() {
};
Namespace_IO.f_slurp = func (cap Capture) *Any {
var filename = tostr(cap.p[0]);
- s, error := io.ReadFile(filename);
+ s, error := ioutil.ReadFile(filename);
if error != nil {
return u_undef();
}
@@ -170,7 +170,17 @@ package Lit::Object;
sub new { shift; bless { @_ }, "Lit::Object" }
sub class { @_ == 1 ? ( $_[0]->{class} ) : ( $_[0]->{class} = $_[1] ) };
sub fields { @_ == 1 ? ( $_[0]->{fields} ) : ( $_[0]->{fields} = $_[1] ) };
-sub emit_go { my $self = shift; my $List__ = \@_; do { [] }; (my $fields = $self->{fields}); (my $str = ''); do { for my $field ( @{$fields} ) { ($str = $str . 'if m.v_' . $field->[0]->buf() . ' == nil {' . 'var p Any; ' . 'm.v_' . $field->[0]->buf() . ' = &p; ' . '}' . '*m.v_' . $field->[0]->buf() . ' = *' . $field->[1]->emit_go() . '; ') } }; 'func() *Any { ' . 'var m = new(' . Main::to_go_namespace($self->{class}) . '); ' . $str . 'var m1 Any = m; ' . 'return &m1; ' . '}()' }
+sub emit_go { my $self = shift; my $List__ = \@_; do { [] }; (my $fields = $self->{fields}); (my $str = ''); do { for my $field ( @{$fields} ) { ($str = $str . 'if m.v_' . $field->[0]->buf() . ' == nil {' . '
+' . 'var p Any; ' . '
+' . 'm.v_' . $field->[0]->buf() . ' = &p; ' . '
+' . '}' . '
+' . '*m.v_' . $field->[0]->buf() . ' = *' . $field->[1]->emit_go() . '; ' . '
+') } }; 'func() *Any { ' . '
+' . ' var m = new(' . Main::to_go_namespace($self->{class}) . '); ' . '
+' . ' ' . $str . '
+' . ' var m1 Any = m; ' . '
+' . ' return &m1; ' . '
+' . '}()' }
}
{
@@ -222,7 +232,14 @@ sub invocant { @_ == 1 ? ( $_[0]->{invocant} ) : ( $_[0]->{invocant} = $_[1] ) }
sub hyper { @_ == 1 ? ( $_[0]->{hyper} ) : ( $_[0]->{hyper} = $_[1] ) };
sub method { @_ == 1 ? ( $_[0]->{method} ) : ( $_[0]->{method} = $_[1] ) };
sub arguments { @_ == 1 ? ( $_[0]->{arguments} ) : ( $_[0]->{arguments} = $_[1] ) };
-sub emit_go { my $self = shift; my $List__ = \@_; do { [] }; (my $invocant = $self->{invocant}->emit_go()); do { if (Main::isa($self->{invocant}, 'Proto')) { do { if (($self->{invocant}->name() eq 'self')) { ($invocant = 'v_self') } else { ($invocant = 'Proto_' . $invocant) } } } else { } }; (my $meth = $self->{method}); do { if (($meth eq 'postcircumfix:<( )>')) { do { if ($self->{hyper}) { ($meth = '') } else { return($invocant . '( Capture{ p : []*Any{ ' . Main::join([ map { $_->emit_go() } @{ $self->{arguments} } ], ', ') . ' } } )') } } } else { } }; do { if ($self->{hyper}) { return('func (a_ *Any) *Any { ' . 'var out = a_array(); ' . 'var i = (*(*a_).(array_er).f_array(Capture{})).(*Array); ' . 'for pos := 0; pos <= i.n; pos++ { ' . '(*out).(push_er).f_push( Capture{p: []*Any{ (*i.v[pos]).(' . $meth . '_er).f_' . $meth . '(Capture{ p : []*Any{} }) }} ) } ' . 'return out; ' . '}(' . $invocant . ')') } else { } }; return('(*' . $invocant . ').(' . $meth . '_er).f_' . $meth . '( Capture{ p : []*Any{ ' . Main::join([ map { $_->emit_go() } @{ $self->{arguments} } ], ', ') . ' } } )') };
+sub emit_go { my $self = shift; my $List__ = \@_; do { [] }; (my $invocant = $self->{invocant}->emit_go()); do { if (Main::isa($self->{invocant}, 'Proto')) { do { if (($self->{invocant}->name() eq 'self')) { ($invocant = 'v_self') } else { ($invocant = 'Proto_' . $invocant) } } } else { } }; (my $meth = $self->{method}); do { if (($meth eq 'postcircumfix:<( )>')) { do { if ($self->{hyper}) { ($meth = '') } else { return($invocant . '( Capture{ p : []*Any{ ' . Main::join([ map { $_->emit_go() } @{ $self->{arguments} } ], ', ') . ' } } )') } } } else { } }; do { if ($self->{hyper}) { return('func (a_ *Any) *Any { ' . '
+' . ' var out = a_array(); ' . '
+' . ' var i = (*(*a_).(array_er).f_array(Capture{})).(*Array); ' . '
+' . ' for pos := 0; pos <= i.n; pos++ { ' . '
+' . ' (*out).(push_er).f_push( Capture{p: []*Any{ (*i.v[pos]).(' . $meth . '_er).f_' . $meth . '(Capture{ p : []*Any{} }) }} )' . '
+' . ' } ' . '
+' . ' return out; ' . '
+' . '}(' . $invocant . ')') } else { } }; return('(*' . $invocant . ').(' . $meth . '_er).f_' . $meth . '( Capture{ p : []*Any{ ' . Main::join([ map { $_->emit_go() } @{ $self->{arguments} } ], ', ') . ' } } )') };
sub emit_go_call { my $List__ = \@_; my $invocant; my $meth_name; do { ($invocant = $List__->[0]); ($meth_name = $List__->[1]); [$invocant, $meth_name] }; (my $invocant1 = $invocant->emit_go()); do { if (Main::isa($invocant, 'Proto')) { ($invocant1 = 'Proto_' . $invocant1) } else { } }; (my $meth = $meth_name); return('(*' . $invocant1 . ').(' . $meth . '_er).f_' . $meth . '(Capture{})') }
}
@@ -258,7 +275,14 @@ sub new { shift; bless { @_ }, "For" }
sub cond { @_ == 1 ? ( $_[0]->{cond} ) : ( $_[0]->{cond} = $_[1] ) };
sub body { @_ == 1 ? ( $_[0]->{body} ) : ( $_[0]->{body} = $_[1] ) };
sub topic { @_ == 1 ? ( $_[0]->{topic} ) : ( $_[0]->{topic} = $_[1] ) };
-sub emit_go { my $self = shift; my $List__ = \@_; do { [] }; 'func (a_ *Any) { ' . 'var i = (*(*a_).(array_er).f_array(Capture{})).(*Array); ' . 'for pos := 0; pos <= i.n; pos++ { ' . 'func (' . $self->{topic}->emit_go() . ' *Any) { ' . MiniPerl6::Go::LexicalBlock->new( 'block' => $self->{body},'needs_return' => 0, )->emit_go() . ' }(i.v[pos]) ' . '} ' . '}(' . $self->{cond}->emit_go() . ')' }
+sub emit_go { my $self = shift; my $List__ = \@_; do { [] }; 'func (a_ *Any) { ' . '
+' . ' var i = (*(*a_).(array_er).f_array(Capture{})).(*Array); ' . '
+' . ' for pos := 0; pos <= i.n; pos++ { ' . '
+' . ' func (' . $self->{topic}->emit_go() . ' *Any) { ' . '
+' . ' ' . MiniPerl6::Go::LexicalBlock->new( 'block' => $self->{body},'needs_return' => 0, )->emit_go() . '
+' . ' }(i.v[pos]) ' . '
+' . ' } ' . '
+' . '}(' . $self->{cond}->emit_go() . ')' }
}
{
Oops, something went wrong.

0 comments on commit 369439f

Please sign in to comment.