Permalink
Browse files

Perlito5 - js2 - allow using a string as a reference

  • Loading branch information...
1 parent 574d226 commit cb3148268dd9278d9858fe9d87d85940b469ea82 @fglock committed Nov 27, 2012
View
@@ -184,6 +184,35 @@ function p5call_sub(namespace, name, list, p5want) {
p5pkg.CORE.die(["Undefined subroutine &" + namespace + "::" + name]);
}
+function p5scalar_deref(v) {
+ if (typeof v === "string") {
+ var pkg_name = v.split(/::/);
+ if (pkg_name.length > 1) {
+ var name = pkg_name.pop();
+ pkg_name = pkg_name.join("::");
+ return p5pkg[pkg_name]["v_"+name];
+ }
+ return p5pkg[p5pkg["Perlito5"].v_PKG_NAME]["v_"+v];
+ }
+ return v._scalar_;
+}
+
+function p5scalar_deref_set(v, n) {
+ if (typeof v === "string") {
+ var pkg_name = v.split(/::/);
+ if (pkg_name.length > 1) {
+ var name = pkg_name.pop();
+ pkg_name = pkg_name.join("::");
+ p5pkg[pkg_name]["v_"+name] = n;
+ return p5pkg[pkg_name]["v_"+name];
+ }
+ p5pkg[p5pkg["Perlito5"].v_PKG_NAME]["v_"+v] = n;
+ return p5pkg[p5pkg["Perlito5"].v_PKG_NAME]["v_"+v];
+ }
+ v._scalar_ = n;
+ return v._scalar_;
+}
+
p5make_package("main");
p5pkg["main"]["v_@"] = []; // $@
p5pkg["main"]["v_|"] = 0; // $|
@@ -3456,6 +3485,30 @@ var p5100 = p5pkg['main'];
}
}
});
+ p5make_sub("Perlito5::AST::Apply", "emit_javascript2_set", function (List__, p5want) {
+ try {
+ var v_self;
+ (v_self = (List__.shift()));
+ var v_arguments;
+ (v_arguments = (List__.shift()));
+ var v_level;
+ (v_level = (List__.shift()));
+ var v_code;
+ (v_code = ((v_self || (v_self = new p5HashRef({})))._hash_.p5hget('code')));
+ if ( (p5str(v_code) == 'prefix:<$>') ) {
+ throw(p5context([('p5scalar_deref_set(' + p5str(p5pkg["Perlito5::Javascript2"].emit_javascript2_autovivify(p5list_to_a((v_self || (v_self = new p5HashRef({})))._hash_.p5hget_array('arguments')._array_.p5aget(0), (p5num(v_level) + 1), 'scalar'), 0)) + ', ' + p5str(p5pkg["Perlito5::Javascript2"].to_scalar(p5list_to_a((new p5ArrayRef([v_arguments])), (p5num(v_level) + 1)), 0)) + ')')], p5want));
+ };
+ return (p5context([('(' + p5str(p5call(v_self, "emit_javascript2", [(p5num(v_level) + 1)], 0)) + ' = ' + p5str(p5call(v_arguments, "emit_javascript2", [(p5num(v_level) + 1)], 0)) + ')')], p5want));
+ }
+ catch(err) {
+ if ( err instanceof Error ) {
+ throw(err);
+ }
+ else {
+ return(err);
+ }
+ }
+ });
var Hash_emit_js = {};
(Hash_emit_js = p5a_to_h(['infix:<=~>', function (List__, p5want) {
var v_self;
@@ -3560,7 +3613,7 @@ var p5100 = p5pkg['main'];
(v_level = (List__.p5aget(1)));
var v_arg;
(v_arg = ((v_self || (v_self = new p5HashRef({})))._hash_.p5hget_array('arguments')._array_.p5aget(0)));
- return (p5context([(p5str(p5pkg["Perlito5::Javascript2"].emit_javascript2_autovivify([v_arg, v_level, 'scalar'], 0)) + '._scalar_')], p5want));
+ return (p5context([('p5scalar_deref(' + p5str(p5pkg["Perlito5::Javascript2"].emit_javascript2_autovivify([v_arg, v_level, 'scalar'], 0)) + ')')], p5want));
}, 'prefix:<@>', function (List__, p5want) {
var v_self;
(v_self = (List__.p5aget(0)));
@@ -3829,7 +3882,7 @@ var p5100 = p5pkg['main'];
}, p5list_to_a(p5call(v_parameters, "arguments", [], p5want)._array_)), ('return ' + p5str(v_tmp2)))], 0) + ' })()')], p5want));
})();
};
- if ( ((((p5bool(p5call(v_parameters, "isa", ['Perlito5::AST::Index'], 0)) || p5bool(p5call(v_parameters, "isa", ['Perlito5::AST::Lookup'], 0))) || p5bool(p5call(v_parameters, "isa", ['Perlito5::AST::Call'], 0))) || p5bool(p5call(v_parameters, "isa", ['Perlito5::AST::Var'], 0))) || p5bool(p5call(v_parameters, "isa", ['Perlito5::AST::Decl'], 0))) ) {
+ if ( (((((p5bool(p5call(v_parameters, "isa", ['Perlito5::AST::Index'], 0)) || p5bool(p5call(v_parameters, "isa", ['Perlito5::AST::Lookup'], 0))) || p5bool(p5call(v_parameters, "isa", ['Perlito5::AST::Call'], 0))) || p5bool(p5call(v_parameters, "isa", ['Perlito5::AST::Var'], 0))) || p5bool(p5call(v_parameters, "isa", ['Perlito5::AST::Decl'], 0))) || p5bool(p5call(v_parameters, "isa", ['Perlito5::AST::Apply'], 0))) ) {
throw(p5call(v_parameters, "emit_javascript2_set", [v_arguments, (p5num(v_level) + 1)], p5want));
};
return (p5context([('(' + p5str(p5call(v_parameters, "emit_javascript2", [v_level], 0)) + ' = ' + p5str(p5call(v_arguments, "emit_javascript2", [(p5num(v_level) + 1)], 0)) + ')')], p5want));
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1315,6 +1315,22 @@ package Perlito5::AST::Apply;
die "Error: regex emitter";
}
+ sub emit_javascript2_set {
+ my $self = shift;
+ my $arguments = shift;
+ my $level = shift;
+
+ my $code = $self->{code};
+
+ if ($code eq 'prefix:<$>') {
+ return 'p5scalar_deref_set('
+ . Perlito5::Javascript2::emit_javascript2_autovivify( $self->{arguments}->[0], $level+1, 'scalar' ) . ', '
+ . Perlito5::Javascript2::to_scalar([$arguments], $level+1)
+ . ')';
+ }
+
+ '(' . $self->emit_javascript2( $level+1 ) . ' = ' . $arguments->emit_javascript2( $level+1 ) . ')';
+ }
my %emit_js = (
'infix:<=~>' => sub {
@@ -1407,7 +1423,7 @@ package Perlito5::AST::Apply;
my $self = $_[0];
my $level = $_[1];
my $arg = $self->{arguments}->[0];
- Perlito5::Javascript2::emit_javascript2_autovivify( $arg, $level, 'scalar' ) . '._scalar_';
+ return 'p5scalar_deref(' . Perlito5::Javascript2::emit_javascript2_autovivify( $arg, $level, 'scalar' ) . ')';
},
'prefix:<@>' => sub {
my $self = $_[0];
@@ -1636,6 +1652,7 @@ package Perlito5::AST::Apply;
|| $parameters->isa( 'Perlito5::AST::Call')
|| $parameters->isa( 'Perlito5::AST::Var')
|| $parameters->isa( 'Perlito5::AST::Decl')
+ || $parameters->isa( 'Perlito5::AST::Apply')
)
{
return $parameters->emit_javascript2_set($arguments, $level+1);
@@ -190,6 +190,35 @@ function p5call_sub(namespace, name, list, p5want) {
p5pkg.CORE.die(["Undefined subroutine &" + namespace + "::" + name]);
}
+function p5scalar_deref(v) {
+ if (typeof v === "string") {
+ var pkg_name = v.split(/::/);
+ if (pkg_name.length > 1) {
+ var name = pkg_name.pop();
+ pkg_name = pkg_name.join("::");
+ return p5pkg[pkg_name]["v_"+name];
+ }
+ return p5pkg[p5pkg["Perlito5"].v_PKG_NAME]["v_"+v];
+ }
+ return v._scalar_;
+}
+
+function p5scalar_deref_set(v, n) {
+ if (typeof v === "string") {
+ var pkg_name = v.split(/::/);
+ if (pkg_name.length > 1) {
+ var name = pkg_name.pop();
+ pkg_name = pkg_name.join("::");
+ p5pkg[pkg_name]["v_"+name] = n;
+ return p5pkg[pkg_name]["v_"+name];
+ }
+ p5pkg[p5pkg["Perlito5"].v_PKG_NAME]["v_"+v] = n;
+ return p5pkg[p5pkg["Perlito5"].v_PKG_NAME]["v_"+v];
+ }
+ v._scalar_ = n;
+ return v._scalar_;
+}
+
p5make_package("main");
p5pkg["main"]["v_@"] = []; // $@
p5pkg["main"]["v_|"] = 0; // $|
@@ -206,6 +206,35 @@ function p5call_sub(namespace, name, list, p5want) {
p5pkg.CORE.die(["Undefined subroutine &" + namespace + "::" + name]);
}
+function p5scalar_deref(v) {
+ if (typeof v === "string") {
+ var pkg_name = v.split(/::/);
+ if (pkg_name.length > 1) {
+ var name = pkg_name.pop();
+ pkg_name = pkg_name.join("::");
+ return p5pkg[pkg_name]["v_"+name];
+ }
+ return p5pkg[p5pkg["Perlito5"].v_PKG_NAME]["v_"+v];
+ }
+ return v._scalar_;
+}
+
+function p5scalar_deref_set(v, n) {
+ if (typeof v === "string") {
+ var pkg_name = v.split(/::/);
+ if (pkg_name.length > 1) {
+ var name = pkg_name.pop();
+ pkg_name = pkg_name.join("::");
+ p5pkg[pkg_name]["v_"+name] = n;
+ return p5pkg[pkg_name]["v_"+name];
+ }
+ p5pkg[p5pkg["Perlito5"].v_PKG_NAME]["v_"+v] = n;
+ return p5pkg[p5pkg["Perlito5"].v_PKG_NAME]["v_"+v];
+ }
+ v._scalar_ = n;
+ return v._scalar_;
+}
+
p5make_package("main");
p5pkg["main"]["v_@"] = new p5Scalar(""); // $@
p5pkg["main"]["v_|"] = new p5Scalar(0); // $|

0 comments on commit cb31482

Please sign in to comment.