Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Perlito5 - js - tie() emitter
  • Loading branch information
fglock committed Oct 4, 2012
1 parent bc4bbb3 commit 9a2d51f
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 72 deletions.
89 changes: 54 additions & 35 deletions html/perlito5.js
Expand Up @@ -326,6 +326,31 @@ Object.defineProperty( Array.prototype, "p5aget_hash", {
}
});

p5tie_array = function(v, List__) {
var pkg_name = p5str(List__.shift());

var res = p5call(pkg_name, 'TIEARRAY', List__, null);

// TODO

// A class implementing an ordinary array should have the following methods:
// TIEARRAY pkg_name, LIST
// FETCH this, key
// STORE this, key, value
// FETCHSIZE this
// STORESIZE this, count
// CLEAR this
// PUSH this, LIST
// POP this
// SHIFT this
// UNSHIFT this, LIST
// SPLICE this, offset, length, LIST
// EXTEND this, count
// DESTROY this
// UNTIE this

return res;
};

//-------- Hash

Expand Down Expand Up @@ -880,41 +905,6 @@ CORE.bless = function(List__) {
return o;
};

CORE.tie = function(List__) {
var v = List__[0];
var pkg_name = List__[1];
var args = List__[2];

// array, scalar, hash, ... ??? -- could use some help from the emitter here
if (v instanceof Array) {

var res = p5call(pkg_name, 'TIEARRAY', args, null);

// TODO

// A class implementing an ordinary array should have the following methods:
// TIEARRAY pkg_name, LIST
// FETCH this, key
// STORE this, key, value
// FETCHSIZE this
// STORESIZE this, count
// CLEAR this
// PUSH this, LIST
// POP this
// SHIFT this
// UNSHIFT this, LIST
// SPLICE this, offset, length, LIST
// EXTEND this, count
// DESTROY this
// UNTIE this

return res;
}

CORE.die("don't know how to tie() this");

};

CORE.chr = function(List__) {
var v = p5num(List__[0]);
return String.fromCharCode(v >= 0 ? v : 65533);
Expand Down Expand Up @@ -3716,6 +3706,35 @@ var p5100 = p5pkg['main'];
return(err);
}
}
}, 'tie', function (List__, p5want) {
var v_self = null;
(v_self = (p5pkg["Perlito5::AST::Apply"].shift([List__])));
var v_level = null;
(v_level = (p5pkg["Perlito5::AST::Apply"].shift([List__])));
var v_wantarray = null;
(v_wantarray = (p5pkg["Perlito5::AST::Apply"].shift([List__])));
var List_arguments = [];
(List_arguments = p5list_to_a((v_self || (v_self = new p5HashRef({})))._hash_.p5hget_array('arguments')._array_));
var v_v = null;
(v_v = (p5pkg["Perlito5::AST::Apply"].shift([List_arguments])));
var v_meth = null;
if ( (p5bool(p5call(v_v, "isa", ['Perlito5::AST::Var'], 0)) && (p5str(p5call(v_v, "sigil", [], 0)) == '%')) ) {
(v_meth = ('hash'));
}
else {
if ( (p5bool(p5call(v_v, "isa", ['Perlito5::AST::Var'], 0)) && (p5str(p5call(v_v, "sigil", [], 0)) == '@')) ) {
(v_meth = ('array'));
}
else {
if ( (p5bool(p5call(v_v, "isa", ['Perlito5::AST::Var'], 0)) && (p5str(p5call(v_v, "sigil", [], 0)) == '$')) ) {
(v_meth = ('scalar'));
}
else {
p5pkg["Perlito5::AST::Apply"].die([p5list_to_a('tie ' + String.fromCharCode(39), p5pkg["Perlito5::AST::Apply"].ref([v_v], 1), String.fromCharCode(39) + ' not implemented')], null);
};
};
};
return (p5context([('p5tie_' + p5str(v_meth) + '(' + p5str(p5call(v_v, "emit_javascript2", [v_level], 0)) + ', ' + p5str(p5pkg["Perlito5::Javascript2"].to_list([(new p5ArrayRef(List_arguments))], 0)) + ')')], p5want));
}, 'map', function (List__, p5want) {
var v_self = null;
(v_self = (p5pkg["Perlito5::AST::Apply"].shift([List__])));
Expand Down
28 changes: 26 additions & 2 deletions perlito5.pl

Large diffs are not rendered by default.

35 changes: 0 additions & 35 deletions src5/lib/Perlito5/Javascript2/CORE.pm
Expand Up @@ -92,41 +92,6 @@ CORE.bless = function(List__) {
return o;
};
CORE.tie = function(List__) {
var v = List__[0];
var pkg_name = List__[1];
var args = List__[2];
// array, scalar, hash, ... ??? -- could use some help from the emitter here
if (v instanceof Array) {
var res = p5call(pkg_name, 'TIEARRAY', args, null);
// TODO
// A class implementing an ordinary array should have the following methods:
// TIEARRAY pkg_name, LIST
// FETCH this, key
// STORE this, key, value
// FETCHSIZE this
// STORESIZE this, count
// CLEAR this
// PUSH this, LIST
// POP this
// SHIFT this
// UNSHIFT this, LIST
// SPLICE this, offset, length, LIST
// EXTEND this, count
// DESTROY this
// UNTIE this
return res;
}
CORE.die("don't know how to tie() this");
};
CORE.chr = function(List__) {
var v = p5num(List__[0]);
return String.fromCharCode(v >= 0 ? v : 65533);
Expand Down
24 changes: 24 additions & 0 deletions src5/lib/Perlito5/Javascript2/Emitter.pm
Expand Up @@ -1788,6 +1788,30 @@ package Perlito5::AST::Apply;
return Perlito5::Javascript2::pkg() . '.shift([List__])'
},

'tie' => sub {
my $self = shift;
my $level = shift;
my $wantarray = shift;

my @arguments = @{$self->{arguments}};
my $v = shift @arguments; # TODO - this argument can also be a 'Decl' instead of 'Var'

my $meth;
if ( $v->isa('Perlito5::AST::Var') && $v->sigil eq '%' ) {
$meth = 'hash';
}
elsif ( $v->isa('Perlito5::AST::Var') && $v->sigil eq '@' ) {
$meth = 'array';
}
elsif ( $v->isa('Perlito5::AST::Var') && $v->sigil eq '$' ) {
$meth = 'scalar';
}
else {
die "tie '", ref($v), "' not implemented";
}
return 'p5tie_' . $meth . '(' . $v->emit_javascript2( $level ) . ', ' . Perlito5::Javascript2::to_list(\@arguments) . ')';
},

'map' => sub {
my $self = shift;
my $level = shift;
Expand Down
25 changes: 25 additions & 0 deletions src5/lib/Perlito5/Javascript2/Runtime.pm
Expand Up @@ -332,6 +332,31 @@ Object.defineProperty( Array.prototype, "p5aget_hash", {
}
});
p5tie_array = function(v, List__) {
var pkg_name = p5str(List__.shift());
var res = p5call(pkg_name, 'TIEARRAY', List__, null);
// TODO
// A class implementing an ordinary array should have the following methods:
// TIEARRAY pkg_name, LIST
// FETCH this, key
// STORE this, key, value
// FETCHSIZE this
// STORESIZE this, count
// CLEAR this
// PUSH this, LIST
// POP this
// SHIFT this
// UNSHIFT this, LIST
// SPLICE this, offset, length, LIST
// EXTEND this, count
// DESTROY this
// UNTIE this
return res;
};
//-------- Hash
Expand Down

0 comments on commit 9a2d51f

Please sign in to comment.