Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
  • 2 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
View
2  lib5/Perlito5/Javascript/CORE.pm
@@ -10,7 +10,7 @@ package main;
use v5;
package Perlito5::Javascript::CORE;
sub Perlito5::Javascript::CORE::emit_javascript {
- return ('//' . chr(10) . '//' . chr(10) . '// lib/Perlito5/Javascript/CORE.js' . chr(10) . '//' . chr(10) . '// CORE functions for "Perlito" Perl5-in-Javascript' . chr(10) . '//' . chr(10) . '// AUTHORS' . chr(10) . '//' . chr(10) . '// Flavio Soibelmann Glock fglock@gmail.com' . chr(10) . '//' . chr(10) . '// COPYRIGHT' . chr(10) . '//' . chr(10) . '// Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.' . chr(10) . '//' . chr(10) . '// This program is free software; you can redistribute it and/or modify it' . chr(10) . '// under the same terms as Perl itself.' . chr(10) . '//' . chr(10) . '// See http://www.perl.com/perl/misc/Artistic.html' . chr(10) . chr(10) . 'var CORE = NAMESPACE.CORE;' . chr(10) . chr(10) . 'var _print_buf = "";' . chr(10) . 'CORE.print = function(List__) {' . chr(10) . ' var i;' . chr(10) . ' for (i = 0; i < List__.length; i++) {' . chr(10) . ' var s = string(List__[i]);' . chr(10) . ' if (s.substr(s.length - 2, 2) == "' . chr(92) . 'n") {' . chr(10) . ' print(_print_buf + s.substr(0, s.length - 2));' . chr(10) . ' _print_buf = "";' . chr(10) . ' }' . chr(10) . ' else if (s.substr(s.length - 1, 1) == "' . chr(92) . 'n") {' . chr(10) . ' print(_print_buf + s.substr(0, s.length - 1));' . chr(10) . ' _print_buf = "";' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' _print_buf = _print_buf + s;' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' return true;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.say = function(List__) {' . chr(10) . ' CORE.print(List__);' . chr(10) . ' return CORE.print(["' . chr(92) . 'n"]);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.die = function(List__) {' . chr(10) . ' var i;' . chr(10) . ' var s = "";' . chr(10) . ' for (i = 0; i < List__.length; i++) {' . chr(10) . ' s = s + string(List__[i]);' . chr(10) . ' }' . chr(10) . ' throw(new Error("Died: " + s));' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.warn = function(List__) {' . chr(10) . ' var i;' . chr(10) . ' var s = "";' . chr(10) . ' for (i = 0; i < List__.length; i++) {' . chr(10) . ' s = s + string(List__[i]);' . chr(10) . ' }' . chr(10) . ' CORE.print(["Warning: " + s + "' . chr(92) . 'n"]);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.bless = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var pkg_name = List__[1];' . chr(10) . ' if (typeof pkg_name === "object") {' . chr(10) . ' // bless {}, Class' . chr(10) . ' o._class_ = pkg_name;' . chr(10) . ' return o;' . chr(10) . ' }' . chr(10) . ' if (!NAMESPACE.hasOwnProperty(pkg_name)) {' . chr(10) . ' make_package(pkg_name);' . chr(10) . ' }' . chr(10) . ' o._class_ = NAMESPACE[pkg_name];' . chr(10) . ' return o;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.chr = function(List__) {' . chr(10) . ' return String.fromCharCode(num(List__[0]));' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.ord = function(List__) {' . chr(10) . ' return string(List__[0]).charCodeAt(0);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.substr = function(List__) {' . chr(10) . ' var expr = List__[0];' . chr(10) . ' var offset = List__[1];' . chr(10) . ' var length = List__[2];' . chr(10) . ' var replacement = List__[3];' . chr(10) . ' return string(expr).substr(offset, length);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.scalar = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o == null) {' . chr(10) . ' return 1;' . chr(10) . ' };' . chr(10) . ' if (typeof o.scalar === "function") {' . chr(10) . ' return o.scalar();' . chr(10) . ' }' . chr(10) . ' if (typeof o === "object" && (o instanceof Array)) {' . chr(10) . ' return o.length;' . chr(10) . ' }' . chr(10) . ' switch (typeof o) {' . chr(10) . ' case "string": return 1;' . chr(10) . ' case "function": return 1;' . chr(10) . ' case "number": return 1;' . chr(10) . ' case "boolean": return 1;' . chr(10) . ' }' . chr(10) . ' var l = 0;' . chr(10) . ' for (var i in o) {' . chr(10) . ' l++;' . chr(10) . ' }' . chr(10) . ' return l;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.values = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o == null) {' . chr(10) . ' return [];' . chr(10) . ' };' . chr(10) . ' if (typeof o.values === "function") {' . chr(10) . ' return o.values();' . chr(10) . ' }' . chr(10) . ' var out = [];' . chr(10) . ' for (var i in o) {' . chr(10) . ' out.push(o[i]);' . chr(10) . ' }' . chr(10) . ' return out;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.keys = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o == null) {' . chr(10) . ' return [];' . chr(10) . ' }' . chr(10) . ' if (typeof o.keys === "function") {' . chr(10) . ' return o.keys();' . chr(10) . ' }' . chr(10) . ' var out = [];' . chr(10) . ' for (var i in o) {' . chr(10) . ' out.push(i);' . chr(10) . ' }' . chr(10) . ' return out;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.pop = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o.length == null) {' . chr(10) . ' return null;' . chr(10) . ' }' . chr(10) . ' return o.pop();' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.shift = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o.length == null) {' . chr(10) . ' return null;' . chr(10) . ' }' . chr(10) . ' return o.shift();' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.push = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var v = List__[1];' . chr(10) . ' return o.push(v);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.unshift = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var v = List__[1];' . chr(10) . ' return o.unshift(v);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.join = function(List__) {' . chr(10) . ' var s = List__[0];' . chr(10) . ' var o = List__[1];' . chr(10) . ' return o.join(s);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.index = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var s = List__[1];' . chr(10) . ' try {' . chr(10) . ' return o.indexOf(s);' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' return -1;' . chr(10) . ' }' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.length = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (typeof o.string === "function") {' . chr(10) . ' return o.string().length;' . chr(10) . ' }' . chr(10) . ' return o.length;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.ref = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o == null) {' . chr(10) . ' return "";' . chr(10) . ' }' . chr(10) . ' if (o._class_ && typeof o._class_._ref_ === "string") {' . chr(10) . ' // blessed reference' . chr(10) . ' return o._class_._ref_;' . chr(10) . ' }' . chr(10) . ' if (typeof o._ref_ === "string") {' . chr(10) . ' // un-blessed reference' . chr(10) . ' return o._ref_;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "function") {' . chr(10) . ' return "CODE";' . chr(10) . ' }' . chr(10) . ' return "";' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.split = function(List__) {' . chr(10) . ' var pattern = List__[0];' . chr(10) . ' var s = List__[1];' . chr(10) . ' var limit = List__[2];' . chr(10) . ' if (typeof pattern === "string") {' . chr(10) . ' if (pattern == " ") {' . chr(10) . ' var res = [];' . chr(10) . ' for (var i_ = 0, a_ = s.split(/(?: |' . chr(92) . 'n)+/); i_ < a_.length ; i_++) {' . chr(10) . ' if (a_[i_] != "") {' . chr(10) . ' res.push(a_[i_])' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' return res;' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' CORE.die(["not implemented"]);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.prototype = function(List__, data) {' . chr(10) . ' var name = List__[0];' . chr(10) . ' // TODO - fully qualify "name" using information from "data"' . chr(10) . ' // TODO - lookup in CORE::GLOBAL' . chr(10) . ' NAMESPACE["Perlito5"].v_PROTO._hash_[name] || NAMESPACE["Perlito5"].v_CORE_PROTO._hash_[name]' . chr(10) . '};' . chr(10) . chr(10))
+ return ('//' . chr(10) . '//' . chr(10) . '// lib/Perlito5/Javascript/CORE.js' . chr(10) . '//' . chr(10) . '// CORE functions for "Perlito" Perl5-in-Javascript' . chr(10) . '//' . chr(10) . '// AUTHORS' . chr(10) . '//' . chr(10) . '// Flavio Soibelmann Glock fglock@gmail.com' . chr(10) . '//' . chr(10) . '// COPYRIGHT' . chr(10) . '//' . chr(10) . '// Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.' . chr(10) . '//' . chr(10) . '// This program is free software; you can redistribute it and/or modify it' . chr(10) . '// under the same terms as Perl itself.' . chr(10) . '//' . chr(10) . '// See http://www.perl.com/perl/misc/Artistic.html' . chr(10) . chr(10) . 'var CORE = NAMESPACE.CORE;' . chr(10) . chr(10) . 'var isNode = typeof require != "undefined";' . chr(10) . 'if (isNode) {' . chr(10) . ' CORE.print = function(List__) {' . chr(10) . ' var i;' . chr(10) . ' for (i = 0; i < List__.length; i++) {' . chr(10) . ' var s = string(List__[i]);' . chr(10) . ' process.stdout.write(s);' . chr(10) . ' }' . chr(10) . ' return true;' . chr(10) . ' }' . chr(10) . '} else {' . chr(10) . ' var _print_buf = "";' . chr(10) . ' CORE.print = function(List__) {' . chr(10) . ' var i;' . chr(10) . ' for (i = 0; i < List__.length; i++) {' . chr(10) . ' var s = string(List__[i]);' . chr(10) . ' if (s.substr(s.length - 2, 2) == "' . chr(92) . 'n") {' . chr(10) . ' print(_print_buf + s.substr(0, s.length - 2));' . chr(10) . ' _print_buf = "";' . chr(10) . ' }' . chr(10) . ' else if (s.substr(s.length - 1, 1) == "' . chr(92) . 'n") {' . chr(10) . ' print(_print_buf + s.substr(0, s.length - 1));' . chr(10) . ' _print_buf = "";' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' _print_buf = _print_buf + s;' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' return true;' . chr(10) . ' };' . chr(10) . '}' . chr(10) . chr(10) . 'CORE.say = function(List__) {' . chr(10) . ' CORE.print(List__);' . chr(10) . ' return CORE.print(["' . chr(92) . 'n"]);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.die = function(List__) {' . chr(10) . ' var i;' . chr(10) . ' var s = "";' . chr(10) . ' for (i = 0; i < List__.length; i++) {' . chr(10) . ' s = s + string(List__[i]);' . chr(10) . ' }' . chr(10) . ' throw(new Error("Died: " + s));' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.warn = function(List__) {' . chr(10) . ' var i;' . chr(10) . ' var s = "";' . chr(10) . ' for (i = 0; i < List__.length; i++) {' . chr(10) . ' s = s + string(List__[i]);' . chr(10) . ' }' . chr(10) . ' CORE.print(["Warning: " + s + "' . chr(92) . 'n"]);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.bless = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var pkg_name = List__[1];' . chr(10) . ' if (typeof pkg_name === "object") {' . chr(10) . ' // bless {}, Class' . chr(10) . ' o._class_ = pkg_name;' . chr(10) . ' return o;' . chr(10) . ' }' . chr(10) . ' if (!NAMESPACE.hasOwnProperty(pkg_name)) {' . chr(10) . ' make_package(pkg_name);' . chr(10) . ' }' . chr(10) . ' o._class_ = NAMESPACE[pkg_name];' . chr(10) . ' return o;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.chr = function(List__) {' . chr(10) . ' return String.fromCharCode(num(List__[0]));' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.ord = function(List__) {' . chr(10) . ' return string(List__[0]).charCodeAt(0);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.substr = function(List__) {' . chr(10) . ' var expr = List__[0];' . chr(10) . ' var offset = List__[1];' . chr(10) . ' var length = List__[2];' . chr(10) . ' var replacement = List__[3];' . chr(10) . ' return string(expr).substr(offset, length);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.scalar = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o == null) {' . chr(10) . ' return 1;' . chr(10) . ' };' . chr(10) . ' if (typeof o.scalar === "function") {' . chr(10) . ' return o.scalar();' . chr(10) . ' }' . chr(10) . ' if (typeof o === "object" && (o instanceof Array)) {' . chr(10) . ' return o.length;' . chr(10) . ' }' . chr(10) . ' switch (typeof o) {' . chr(10) . ' case "string": return 1;' . chr(10) . ' case "function": return 1;' . chr(10) . ' case "number": return 1;' . chr(10) . ' case "boolean": return 1;' . chr(10) . ' }' . chr(10) . ' var l = 0;' . chr(10) . ' for (var i in o) {' . chr(10) . ' l++;' . chr(10) . ' }' . chr(10) . ' return l;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.values = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o == null) {' . chr(10) . ' return [];' . chr(10) . ' };' . chr(10) . ' if (typeof o.values === "function") {' . chr(10) . ' return o.values();' . chr(10) . ' }' . chr(10) . ' var out = [];' . chr(10) . ' for (var i in o) {' . chr(10) . ' out.push(o[i]);' . chr(10) . ' }' . chr(10) . ' return out;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.keys = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o == null) {' . chr(10) . ' return [];' . chr(10) . ' }' . chr(10) . ' if (typeof o.keys === "function") {' . chr(10) . ' return o.keys();' . chr(10) . ' }' . chr(10) . ' var out = [];' . chr(10) . ' for (var i in o) {' . chr(10) . ' out.push(i);' . chr(10) . ' }' . chr(10) . ' return out;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.pop = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o.length == null) {' . chr(10) . ' return null;' . chr(10) . ' }' . chr(10) . ' return o.pop();' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.shift = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o.length == null) {' . chr(10) . ' return null;' . chr(10) . ' }' . chr(10) . ' return o.shift();' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.push = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var v = List__[1];' . chr(10) . ' return o.push(v);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.unshift = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var v = List__[1];' . chr(10) . ' return o.unshift(v);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.join = function(List__) {' . chr(10) . ' var s = List__[0];' . chr(10) . ' var o = List__[1];' . chr(10) . ' return o.join(s);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.index = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var s = List__[1];' . chr(10) . ' try {' . chr(10) . ' return o.indexOf(s);' . chr(10) . ' }' . chr(10) . ' catch(err) {' . chr(10) . ' return -1;' . chr(10) . ' }' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.length = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (typeof o.string === "function") {' . chr(10) . ' return o.string().length;' . chr(10) . ' }' . chr(10) . ' return o.length;' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.ref = function(List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' if (o == null) {' . chr(10) . ' return "";' . chr(10) . ' }' . chr(10) . ' if (o._class_ && typeof o._class_._ref_ === "string") {' . chr(10) . ' // blessed reference' . chr(10) . ' return o._class_._ref_;' . chr(10) . ' }' . chr(10) . ' if (typeof o._ref_ === "string") {' . chr(10) . ' // un-blessed reference' . chr(10) . ' return o._ref_;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "function") {' . chr(10) . ' return "CODE";' . chr(10) . ' }' . chr(10) . ' return "";' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.split = function(List__) {' . chr(10) . ' var pattern = List__[0];' . chr(10) . ' var s = List__[1];' . chr(10) . ' var limit = List__[2];' . chr(10) . ' if (typeof pattern === "string") {' . chr(10) . ' if (pattern == " ") {' . chr(10) . ' var res = [];' . chr(10) . ' for (var i_ = 0, a_ = s.split(/(?: |' . chr(92) . 'n)+/); i_ < a_.length ; i_++) {' . chr(10) . ' if (a_[i_] != "") {' . chr(10) . ' res.push(a_[i_])' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' return res;' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' CORE.die(["not implemented"]);' . chr(10) . '};' . chr(10) . chr(10) . 'CORE.prototype = function(List__, data) {' . chr(10) . ' var name = List__[0];' . chr(10) . ' // TODO - fully qualify "name" using information from "data"' . chr(10) . ' // TODO - lookup in CORE::GLOBAL' . chr(10) . ' NAMESPACE["Perlito5"].v_PROTO._hash_[name] || NAMESPACE["Perlito5"].v_CORE_PROTO._hash_[name]' . chr(10) . '};' . chr(10) . chr(10))
};
1;
View
2  lib5/Perlito5/Javascript/Runtime.pm
@@ -10,7 +10,7 @@ package main;
use v5;
package Perlito5::Javascript::Runtime;
sub Perlito5::Javascript::Runtime::emit_javascript {
- return ('//' . chr(10) . '// lib/Perlito5/Javascript/Runtime.js' . chr(10) . '//' . chr(10) . '// Runtime for "Perlito" Perl5-in-Javascript' . chr(10) . '//' . chr(10) . '// AUTHORS' . chr(10) . '//' . chr(10) . '// Flavio Soibelmann Glock fglock@gmail.com' . chr(10) . '//' . chr(10) . '// COPYRIGHT' . chr(10) . '//' . chr(10) . '// Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.' . chr(10) . '//' . chr(10) . '// This program is free software; you can redistribute it and/or modify it' . chr(10) . '// under the same terms as Perl itself.' . chr(10) . '//' . chr(10) . '// See http://www.perl.com/perl/misc/Artistic.html' . chr(10) . chr(10) . 'if (typeof NAMESPACE !== "object") {' . chr(10) . ' NAMESPACE = {};' . chr(10) . ' LOCAL = [];' . chr(10) . chr(10) . ' var universal = function () {};' . chr(10) . ' NAMESPACE.UNIVERSAL = new universal();' . chr(10) . ' NAMESPACE.UNIVERSAL._ref_ = "UNIVERSAL";' . chr(10) . ' NAMESPACE.UNIVERSAL.isa = function (List__) {' . chr(10) . ' // TODO - use @ISA' . chr(10) . ' return List__[0]._class_._ref_ == List__[1]' . chr(10) . ' };' . chr(10) . ' NAMESPACE.UNIVERSAL.can = function (List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var s = List__[1];' . chr(10) . ' if ( s.indexOf("::") == -1 ) {' . chr(10) . ' // TODO - use _method_lookup_' . chr(10) . ' return o._class_[s]' . chr(10) . ' }' . chr(10) . ' var c = s.split("::");' . chr(10) . ' s = c.pop(); ' . chr(10) . ' // TODO - use _method_lookup_' . chr(10) . ' return _method_lookup_(s, c.join("::"), {});' . chr(10) . ' };' . chr(10) . ' NAMESPACE.UNIVERSAL.DOES = NAMESPACE.UNIVERSAL.can;' . chr(10) . chr(10) . ' var core = function () {};' . chr(10) . ' NAMESPACE.CORE = new core();' . chr(10) . ' NAMESPACE.CORE._ref_ = "CORE";' . chr(10) . '}' . chr(10) . chr(10) . 'function make_package(pkg_name) {' . chr(10) . ' if (!NAMESPACE.hasOwnProperty(pkg_name)) {' . chr(10) . ' var tmp = function () {};' . chr(10) . ' tmp.prototype = NAMESPACE.CORE;' . chr(10) . ' NAMESPACE[pkg_name] = new tmp();' . chr(10) . ' NAMESPACE[pkg_name]._ref_ = pkg_name;' . chr(10) . ' NAMESPACE[pkg_name]._class_ = NAMESPACE[pkg_name]; // XXX memory leak' . chr(10) . chr(10) . ' // TODO - add the other package global variables' . chr(10) . ' NAMESPACE[pkg_name]["List_ISA"] = [];' . chr(10) . ' }' . chr(10) . '}' . chr(10) . chr(10) . 'function _method_lookup_(method, class_name, seen) {' . chr(10) . ' // default mro' . chr(10) . ' c = NAMESPACE[class_name];' . chr(10) . ' if ( c.hasOwnProperty(method) ) {' . chr(10) . ' return c[method]' . chr(10) . ' }' . chr(10) . ' var isa = c.List_ISA;' . chr(10) . ' for (var i = 0; i < isa.length; i++) {' . chr(10) . ' if (!seen[isa[i]]) {' . chr(10) . ' var m = _method_lookup_(method, isa[i]);' . chr(10) . ' if (m) {' . chr(10) . ' return m ' . chr(10) . ' }' . chr(10) . ' seen[isa[i]]++;' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' // TODO - AUTOLOAD' . chr(10) . '}' . chr(10) . chr(10) . 'function _call_(invocant, method, list) {' . chr(10) . ' list.unshift(invocant);' . chr(10) . ' if ( invocant._class_.hasOwnProperty(method) ) {' . chr(10) . ' return invocant._class_[method](list) ' . chr(10) . ' }' . chr(10) . ' var m = _method_lookup_(method, invocant._class_._ref_, {});' . chr(10) . ' if (m) {' . chr(10) . ' return m(list)' . chr(10) . ' }' . chr(10) . ' if ( NAMESPACE.UNIVERSAL.hasOwnProperty(method) ) {' . chr(10) . ' return NAMESPACE.UNIVERSAL[method](list) ' . chr(10) . ' }' . chr(10) . ' // TODO - cache the methods that were already looked up' . chr(10) . ' NAMESPACE.CORE.die(["method not found: ", method, " in class ", invocant._ref_]);' . chr(10) . '}' . chr(10) . chr(10) . 'make_package("main");' . chr(10) . 'make_package("Perlito5");' . chr(10) . 'make_package("Perlito5::IO");' . chr(10) . 'make_package("Perlito5::Runtime");' . chr(10) . 'make_package("Perlito5::Grammar");' . chr(10) . chr(10) . 'function make_sub(pkg_name, sub_name, func) {' . chr(10) . ' NAMESPACE[pkg_name][sub_name] = func;' . chr(10) . '}' . chr(10) . chr(10) . 'function set_local(namespace, name, sigil) {' . chr(10) . ' var v = name;' . chr(10) . ' if (sigil == "$") {' . chr(10) . ' v = "v_"+name;' . chr(10) . ' }' . chr(10) . ' LOCAL.push([namespace, v, namespace[v]]);' . chr(10) . '}' . chr(10) . chr(10) . 'function cleanup_local(idx, value) {' . chr(10) . ' while (LOCAL.length > idx) {' . chr(10) . ' l = LOCAL.pop();' . chr(10) . ' l[0][l[1]] = l[2];' . chr(10) . ' }' . chr(10) . ' return value;' . chr(10) . '}' . chr(10) . chr(10) . 'if (typeof arguments === "object") {' . chr(10) . ' List_ARGV = arguments;' . chr(10) . '}' . chr(10) . chr(10) . 'function HashRef(o) {' . chr(10) . ' this._hash_ = o;' . chr(10) . ' this._ref_ = "HASH";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . 'function ArrayRef(o) {' . chr(10) . ' this._array_ = o;' . chr(10) . ' this._ref_ = "ARRAY";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . 'function ScalarRef(o) {' . chr(10) . ' this._scalar_ = o;' . chr(10) . ' this._ref_ = "SCALAR";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . 'make_sub("Perlito5::IO", "slurp", function(List__) {' . chr(10) . ' var filename = List__[0];' . chr(10) . ' if (typeof readFile == "function") {' . chr(10) . ' return readFile(filename);' . chr(10) . ' }' . chr(10) . ' if (typeof read == "function") {' . chr(10) . ' // v8' . chr(10) . ' return read(filename);' . chr(10) . ' }' . chr(10) . ' NAMESPACE.CORE.die(["Perlito5::IO::slurp() not implemented"]);' . chr(10) . '});' . chr(10) . chr(10) . 'interpolate_array = function() {' . chr(10) . ' var res = [];' . chr(10) . ' for (i = 0; i < arguments.length; i++) {' . chr(10) . ' var o = arguments[i];' . chr(10) . ' if ( o == null' . chr(10) . ' || o._class_ // perl5 blessed reference' . chr(10) . ' || o._ref_ // perl5 un-blessed reference' . chr(10) . ' )' . chr(10) . ' {' . chr(10) . ' res.push(o);' . chr(10) . ' }' . chr(10) . ' else if (o instanceof Array) {' . chr(10) . ' // perl5 array' . chr(10) . ' for (j = 0; j < o.length; j++) {' . chr(10) . ' res.push(o[j]);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' else if (typeof o === "object") {' . chr(10) . ' // perl5 hash' . chr(10) . ' for(var j in o) {' . chr(10) . ' if (o.hasOwnProperty(j)) {' . chr(10) . ' res.push(j);' . chr(10) . ' res.push(o[j]);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' // non-ref' . chr(10) . ' res.push(o);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' return res;' . chr(10) . '};' . chr(10) . chr(10) . 'array_to_hash = function(a) {' . chr(10) . ' var res = {};' . chr(10) . ' for (i = 0; i < a.length; i+=2) {' . chr(10) . ' res[string(a[i])] = a[i+1];' . chr(10) . ' }' . chr(10) . ' return res;' . chr(10) . '};' . chr(10) . chr(10) . 'string = function(o) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return "";' . chr(10) . ' }' . chr(10) . ' if (typeof o === "object" && (o instanceof Array)) {' . chr(10) . ' var out = [];' . chr(10) . ' for (var i = 0; i < o.length; i++) {' . chr(10) . ' out.push(string(o[i]));' . chr(10) . ' }' . chr(10) . ' return out.join(" ");' . chr(10) . ' }' . chr(10) . ' if (typeof o.string === "function") {' . chr(10) . ' return o.string();' . chr(10) . ' }' . chr(10) . ' if (typeof o !== "string") {' . chr(10) . ' return "" + o;' . chr(10) . ' }' . chr(10) . ' return o;' . chr(10) . '};' . chr(10) . chr(10) . 'num = function(o) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return 0;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "object" && (o instanceof Array)) {' . chr(10) . ' return o.length;' . chr(10) . ' }' . chr(10) . ' if (typeof o.num === "function") {' . chr(10) . ' return o.num();' . chr(10) . ' }' . chr(10) . ' if (typeof o !== "number") {' . chr(10) . ' return parseFloat(string(o));' . chr(10) . ' }' . chr(10) . ' return o;' . chr(10) . '};' . chr(10) . chr(10) . 'add = function(o1, o2) {' . chr(10) . ' return num(o1) + num(o2)' . chr(10) . '};' . chr(10) . chr(10) . 'bool = function(o) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return o;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "boolean") {' . chr(10) . ' return o;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "number") {' . chr(10) . ' return o;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "string") {' . chr(10) . ' return o != "" && o != "0";' . chr(10) . ' }' . chr(10) . ' if (typeof o.bool === "function") {' . chr(10) . ' return o.bool();' . chr(10) . ' }' . chr(10) . ' if (typeof o.length === "number") {' . chr(10) . ' return o.length;' . chr(10) . ' }' . chr(10) . ' for (var i in o) {' . chr(10) . ' return true;' . chr(10) . ' }' . chr(10) . ' return false;' . chr(10) . '};' . chr(10) . chr(10) . 'and = function(a, fb) {' . chr(10) . ' if (bool(a)) {' . chr(10) . ' return fb();' . chr(10) . ' }' . chr(10) . ' return a;' . chr(10) . '};' . chr(10) . chr(10) . 'or = function(a, fb) {' . chr(10) . ' if (bool(a)) {' . chr(10) . ' return a;' . chr(10) . ' }' . chr(10) . ' return fb();' . chr(10) . '};' . chr(10) . chr(10) . 'defined_or = function(a, fb) {' . chr(10) . ' if (a == null) {' . chr(10) . ' return fb();' . chr(10) . ' }' . chr(10) . ' return a;' . chr(10) . '};' . chr(10) . chr(10) . 'str_replicate = function(o, n) {' . chr(10) . ' n = num(n);' . chr(10) . ' return n ? Array(n + 1).join(o) : "";' . chr(10) . '};' . chr(10) . chr(10) . 'make_sub("Perlito5::Grammar", "digit", function(List__) {' . chr(10) . ' var v_grammar = List__[0];' . chr(10) . ' var v_str = List__[1];' . chr(10) . ' var v_pos = List__[2];' . chr(10) . ' return NAMESPACE.CORE.bless([' . chr(10) . ' new HashRef({' . chr(10) . ' str: v_str,' . chr(10) . ' from: v_pos,' . chr(10) . ' to: v_pos + 1,' . chr(10) . ' bool: v_str.substr(v_pos, 1).match(/' . chr(92) . 'd/) != null,' . chr(10) . ' }),' . chr(10) . ' NAMESPACE["Perlito5::Match"]' . chr(10) . ' ]);' . chr(10) . '});' . chr(10) . chr(10) . 'make_sub("Perlito5::Grammar", "space", function(List__) {' . chr(10) . ' var v_grammar = List__[0];' . chr(10) . ' var v_str = List__[1];' . chr(10) . ' var v_pos = List__[2];' . chr(10) . ' return NAMESPACE.CORE.bless([' . chr(10) . ' new HashRef({' . chr(10) . ' str: v_str,' . chr(10) . ' from: v_pos,' . chr(10) . ' to: v_pos + 1,' . chr(10) . ' bool: v_str.substr(v_pos, 1).match(/' . chr(92) . 's/) != null,' . chr(10) . ' }),' . chr(10) . ' NAMESPACE["Perlito5::Match"]' . chr(10) . ' ]);' . chr(10) . '});' . chr(10) . chr(10) . 'function perl5_to_js( source, namespace, var_env_js ) {' . chr(10) . ' // say( "source: [" + source + "]" );' . chr(10) . chr(10) . ' var var_env_js_old = NAMESPACE["Perlito5"].v_VAR;' . chr(10) . ' NAMESPACE["Perlito5"].v_VAR = var_env_js;' . chr(10) . chr(10) . ' var namespace_old = NAMESPACE["Perlito5"].v_PKG_NAME;' . chr(10) . ' NAMESPACE["Perlito5"].v_PKG_NAME = namespace;' . chr(10) . chr(10) . ' match = NAMESPACE["Perlito5::Grammar"].exp_stmts([NAMESPACE["Perlito5::Grammar"], source, 0]);' . chr(10) . chr(10) . ' ast = NAMESPACE.CORE.bless([' . chr(10) . ' new HashRef({' . chr(10) . ' block: NAMESPACE.CORE.bless([' . chr(10) . ' new HashRef({' . chr(10) . ' stmts: match._class_.flat([match]),' . chr(10) . ' }),' . chr(10) . ' NAMESPACE["Perlito5::AST::Lit::Block"]' . chr(10) . ' ]),' . chr(10) . ' }),' . chr(10) . ' NAMESPACE["Perlito5::AST::Do"]' . chr(10) . ' ]);' . chr(10) . chr(10) . ' // CORE.say( "ast: [" + perl(ast) + "]" );' . chr(10) . ' js_code = ast._class_.emit_javascript([ast]);' . chr(10) . ' // CORE.say( "js-source: [" + js_code + "]" );' . chr(10) . chr(10) . ' NAMESPACE["Perlito5"].v_PKG_NAME = namespace_old;' . chr(10) . ' NAMESPACE["Perlito5"].v_VAR = var_env_js_old;' . chr(10) . ' return js_code;' . chr(10) . '}' . chr(10))
+ return ('//' . chr(10) . '// lib/Perlito5/Javascript/Runtime.js' . chr(10) . '//' . chr(10) . '// Runtime for "Perlito" Perl5-in-Javascript' . chr(10) . '//' . chr(10) . '// AUTHORS' . chr(10) . '//' . chr(10) . '// Flavio Soibelmann Glock fglock@gmail.com' . chr(10) . '//' . chr(10) . '// COPYRIGHT' . chr(10) . '//' . chr(10) . '// Copyright 2009, 2010, 2011, 2012 by Flavio Soibelmann Glock and others.' . chr(10) . '//' . chr(10) . '// This program is free software; you can redistribute it and/or modify it' . chr(10) . '// under the same terms as Perl itself.' . chr(10) . '//' . chr(10) . '// See http://www.perl.com/perl/misc/Artistic.html' . chr(10) . chr(10) . 'var isNode = true;' . chr(10) . chr(10) . 'if (typeof NAMESPACE !== "object") {' . chr(10) . ' NAMESPACE = {};' . chr(10) . ' LOCAL = [];' . chr(10) . chr(10) . ' var universal = function () {};' . chr(10) . ' NAMESPACE.UNIVERSAL = new universal();' . chr(10) . ' NAMESPACE.UNIVERSAL._ref_ = "UNIVERSAL";' . chr(10) . ' NAMESPACE.UNIVERSAL.isa = function (List__) {' . chr(10) . ' // TODO - use @ISA' . chr(10) . ' return List__[0]._class_._ref_ == List__[1]' . chr(10) . ' };' . chr(10) . ' NAMESPACE.UNIVERSAL.can = function (List__) {' . chr(10) . ' var o = List__[0];' . chr(10) . ' var s = List__[1];' . chr(10) . ' if ( s.indexOf("::") == -1 ) {' . chr(10) . ' // TODO - use _method_lookup_' . chr(10) . ' return o._class_[s]' . chr(10) . ' }' . chr(10) . ' var c = s.split("::");' . chr(10) . ' s = c.pop(); ' . chr(10) . ' // TODO - use _method_lookup_' . chr(10) . ' return _method_lookup_(s, c.join("::"), {});' . chr(10) . ' };' . chr(10) . ' NAMESPACE.UNIVERSAL.DOES = NAMESPACE.UNIVERSAL.can;' . chr(10) . chr(10) . ' var core = function () {};' . chr(10) . ' NAMESPACE.CORE = new core();' . chr(10) . ' NAMESPACE.CORE._ref_ = "CORE";' . chr(10) . '}' . chr(10) . chr(10) . 'function make_package(pkg_name) {' . chr(10) . ' if (!NAMESPACE.hasOwnProperty(pkg_name)) {' . chr(10) . ' var tmp = function () {};' . chr(10) . ' tmp.prototype = NAMESPACE.CORE;' . chr(10) . ' NAMESPACE[pkg_name] = new tmp();' . chr(10) . ' NAMESPACE[pkg_name]._ref_ = pkg_name;' . chr(10) . ' NAMESPACE[pkg_name]._class_ = NAMESPACE[pkg_name]; // XXX memory leak' . chr(10) . chr(10) . ' // TODO - add the other package global variables' . chr(10) . ' NAMESPACE[pkg_name]["List_ISA"] = [];' . chr(10) . ' }' . chr(10) . '}' . chr(10) . chr(10) . 'function _method_lookup_(method, class_name, seen) {' . chr(10) . ' // default mro' . chr(10) . ' c = NAMESPACE[class_name];' . chr(10) . ' if ( c.hasOwnProperty(method) ) {' . chr(10) . ' return c[method]' . chr(10) . ' }' . chr(10) . ' var isa = c.List_ISA;' . chr(10) . ' for (var i = 0; i < isa.length; i++) {' . chr(10) . ' if (!seen[isa[i]]) {' . chr(10) . ' var m = _method_lookup_(method, isa[i]);' . chr(10) . ' if (m) {' . chr(10) . ' return m ' . chr(10) . ' }' . chr(10) . ' seen[isa[i]]++;' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' // TODO - AUTOLOAD' . chr(10) . '}' . chr(10) . chr(10) . 'function _call_(invocant, method, list) {' . chr(10) . ' list.unshift(invocant);' . chr(10) . ' if ( invocant._class_.hasOwnProperty(method) ) {' . chr(10) . ' return invocant._class_[method](list) ' . chr(10) . ' }' . chr(10) . ' var m = _method_lookup_(method, invocant._class_._ref_, {});' . chr(10) . ' if (m) {' . chr(10) . ' return m(list)' . chr(10) . ' }' . chr(10) . ' if ( NAMESPACE.UNIVERSAL.hasOwnProperty(method) ) {' . chr(10) . ' return NAMESPACE.UNIVERSAL[method](list) ' . chr(10) . ' }' . chr(10) . ' // TODO - cache the methods that were already looked up' . chr(10) . ' NAMESPACE.CORE.die(["method not found: ", method, " in class ", invocant._ref_]);' . chr(10) . '}' . chr(10) . chr(10) . 'make_package("main");' . chr(10) . 'make_package("Perlito5");' . chr(10) . 'make_package("Perlito5::IO");' . chr(10) . 'make_package("Perlito5::Runtime");' . chr(10) . 'make_package("Perlito5::Grammar");' . chr(10) . chr(10) . 'function make_sub(pkg_name, sub_name, func) {' . chr(10) . ' NAMESPACE[pkg_name][sub_name] = func;' . chr(10) . '}' . chr(10) . chr(10) . 'function set_local(namespace, name, sigil) {' . chr(10) . ' var v = name;' . chr(10) . ' if (sigil == "$") {' . chr(10) . ' v = "v_"+name;' . chr(10) . ' }' . chr(10) . ' LOCAL.push([namespace, v, namespace[v]]);' . chr(10) . '}' . chr(10) . chr(10) . 'function cleanup_local(idx, value) {' . chr(10) . ' while (LOCAL.length > idx) {' . chr(10) . ' l = LOCAL.pop();' . chr(10) . ' l[0][l[1]] = l[2];' . chr(10) . ' }' . chr(10) . ' return value;' . chr(10) . '}' . chr(10) . chr(10) . 'if (typeof isNode != "undefined") {' . chr(10) . ' List_ARGV = process.argv.splice(2);' . chr(10) . '} else if (typeof arguments === "object") {' . chr(10) . ' List_ARGV = arguments;' . chr(10) . '}' . chr(10) . chr(10) . 'function HashRef(o) {' . chr(10) . ' this._hash_ = o;' . chr(10) . ' this._ref_ = "HASH";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . 'function ArrayRef(o) {' . chr(10) . ' this._array_ = o;' . chr(10) . ' this._ref_ = "ARRAY";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . 'function ScalarRef(o) {' . chr(10) . ' this._scalar_ = o;' . chr(10) . ' this._ref_ = "SCALAR";' . chr(10) . ' this.bool = function() { return 1 };' . chr(10) . '}' . chr(10) . chr(10) . 'if (isNode) {' . chr(10) . ' var fs = require("fs");' . chr(10) . ' make_sub("Perlito5::IO", "slurp", function(List__) {' . chr(10) . ' return fs.readFileSync(List__[0],"utf8");' . chr(10) . ' });' . chr(10) . '} else {' . chr(10) . ' make_sub("Perlito5::IO", "slurp", function(List__) {' . chr(10) . ' var filename = List__[0];' . chr(10) . ' if (typeof readFile == "function") {' . chr(10) . ' return readFile(filename);' . chr(10) . ' }' . chr(10) . ' if (typeof read == "function") {' . chr(10) . ' // v8' . chr(10) . ' return read(filename);' . chr(10) . ' }' . chr(10) . ' NAMESPACE.CORE.die(["Perlito5::IO::slurp() not implemented"]);' . chr(10) . ' });' . chr(10) . '}' . chr(10) . chr(10) . 'interpolate_array = function() {' . chr(10) . ' var res = [];' . chr(10) . ' for (i = 0; i < arguments.length; i++) {' . chr(10) . ' var o = arguments[i];' . chr(10) . ' if ( o == null' . chr(10) . ' || o._class_ // perl5 blessed reference' . chr(10) . ' || o._ref_ // perl5 un-blessed reference' . chr(10) . ' )' . chr(10) . ' {' . chr(10) . ' res.push(o);' . chr(10) . ' }' . chr(10) . ' else if (o instanceof Array) {' . chr(10) . ' // perl5 array' . chr(10) . ' for (j = 0; j < o.length; j++) {' . chr(10) . ' res.push(o[j]);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' else if (typeof o === "object") {' . chr(10) . ' // perl5 hash' . chr(10) . ' for(var j in o) {' . chr(10) . ' if (o.hasOwnProperty(j)) {' . chr(10) . ' res.push(j);' . chr(10) . ' res.push(o[j]);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' else {' . chr(10) . ' // non-ref' . chr(10) . ' res.push(o);' . chr(10) . ' }' . chr(10) . ' }' . chr(10) . ' return res;' . chr(10) . '};' . chr(10) . chr(10) . 'array_to_hash = function(a) {' . chr(10) . ' var res = {};' . chr(10) . ' for (i = 0; i < a.length; i+=2) {' . chr(10) . ' res[string(a[i])] = a[i+1];' . chr(10) . ' }' . chr(10) . ' return res;' . chr(10) . '};' . chr(10) . chr(10) . 'string = function(o) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return "";' . chr(10) . ' }' . chr(10) . ' if (typeof o === "object" && (o instanceof Array)) {' . chr(10) . ' var out = [];' . chr(10) . ' for (var i = 0; i < o.length; i++) {' . chr(10) . ' out.push(string(o[i]));' . chr(10) . ' }' . chr(10) . ' return out.join(" ");' . chr(10) . ' }' . chr(10) . ' if (typeof o.string === "function") {' . chr(10) . ' return o.string();' . chr(10) . ' }' . chr(10) . ' if (typeof o !== "string") {' . chr(10) . ' return "" + o;' . chr(10) . ' }' . chr(10) . ' return o;' . chr(10) . '};' . chr(10) . chr(10) . 'num = function(o) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return 0;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "object" && (o instanceof Array)) {' . chr(10) . ' return o.length;' . chr(10) . ' }' . chr(10) . ' if (typeof o.num === "function") {' . chr(10) . ' return o.num();' . chr(10) . ' }' . chr(10) . ' if (typeof o !== "number") {' . chr(10) . ' return parseFloat(string(o));' . chr(10) . ' }' . chr(10) . ' return o;' . chr(10) . '};' . chr(10) . chr(10) . 'add = function(o1, o2) {' . chr(10) . ' return num(o1) + num(o2)' . chr(10) . '};' . chr(10) . chr(10) . 'bool = function(o) {' . chr(10) . ' if (o == null) {' . chr(10) . ' return o;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "boolean") {' . chr(10) . ' return o;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "number") {' . chr(10) . ' return o;' . chr(10) . ' }' . chr(10) . ' if (typeof o === "string") {' . chr(10) . ' return o != "" && o != "0";' . chr(10) . ' }' . chr(10) . ' if (typeof o.bool === "function") {' . chr(10) . ' return o.bool();' . chr(10) . ' }' . chr(10) . ' if (typeof o.length === "number") {' . chr(10) . ' return o.length;' . chr(10) . ' }' . chr(10) . ' for (var i in o) {' . chr(10) . ' return true;' . chr(10) . ' }' . chr(10) . ' return false;' . chr(10) . '};' . chr(10) . chr(10) . 'and = function(a, fb) {' . chr(10) . ' if (bool(a)) {' . chr(10) . ' return fb();' . chr(10) . ' }' . chr(10) . ' return a;' . chr(10) . '};' . chr(10) . chr(10) . 'or = function(a, fb) {' . chr(10) . ' if (bool(a)) {' . chr(10) . ' return a;' . chr(10) . ' }' . chr(10) . ' return fb();' . chr(10) . '};' . chr(10) . chr(10) . 'defined_or = function(a, fb) {' . chr(10) . ' if (a == null) {' . chr(10) . ' return fb();' . chr(10) . ' }' . chr(10) . ' return a;' . chr(10) . '};' . chr(10) . chr(10) . 'str_replicate = function(o, n) {' . chr(10) . ' n = num(n);' . chr(10) . ' return n ? Array(n + 1).join(o) : "";' . chr(10) . '};' . chr(10) . chr(10) . 'make_sub("Perlito5::Grammar", "digit", function(List__) {' . chr(10) . ' var v_grammar = List__[0];' . chr(10) . ' var v_str = List__[1];' . chr(10) . ' var v_pos = List__[2];' . chr(10) . ' return NAMESPACE.CORE.bless([' . chr(10) . ' new HashRef({' . chr(10) . ' str: v_str,' . chr(10) . ' from: v_pos,' . chr(10) . ' to: v_pos + 1,' . chr(10) . ' bool: v_str.substr(v_pos, 1).match(/' . chr(92) . 'd/) != null,' . chr(10) . ' }),' . chr(10) . ' NAMESPACE["Perlito5::Match"]' . chr(10) . ' ]);' . chr(10) . '});' . chr(10) . chr(10) . 'make_sub("Perlito5::Grammar", "space", function(List__) {' . chr(10) . ' var v_grammar = List__[0];' . chr(10) . ' var v_str = List__[1];' . chr(10) . ' var v_pos = List__[2];' . chr(10) . ' return NAMESPACE.CORE.bless([' . chr(10) . ' new HashRef({' . chr(10) . ' str: v_str,' . chr(10) . ' from: v_pos,' . chr(10) . ' to: v_pos + 1,' . chr(10) . ' bool: v_str.substr(v_pos, 1).match(/' . chr(92) . 's/) != null,' . chr(10) . ' }),' . chr(10) . ' NAMESPACE["Perlito5::Match"]' . chr(10) . ' ]);' . chr(10) . '});' . chr(10) . chr(10) . 'function perl5_to_js( source, namespace, var_env_js ) {' . chr(10) . ' // say( "source: [" + source + "]" );' . chr(10) . chr(10) . ' var var_env_js_old = NAMESPACE["Perlito5"].v_VAR;' . chr(10) . ' NAMESPACE["Perlito5"].v_VAR = var_env_js;' . chr(10) . chr(10) . ' var namespace_old = NAMESPACE["Perlito5"].v_PKG_NAME;' . chr(10) . ' NAMESPACE["Perlito5"].v_PKG_NAME = namespace;' . chr(10) . chr(10) . ' match = NAMESPACE["Perlito5::Grammar"].exp_stmts([NAMESPACE["Perlito5::Grammar"], source, 0]);' . chr(10) . chr(10) . ' ast = NAMESPACE.CORE.bless([' . chr(10) . ' new HashRef({' . chr(10) . ' block: NAMESPACE.CORE.bless([' . chr(10) . ' new HashRef({' . chr(10) . ' stmts: match._class_.flat([match]),' . chr(10) . ' }),' . chr(10) . ' NAMESPACE["Perlito5::AST::Lit::Block"]' . chr(10) . ' ]),' . chr(10) . ' }),' . chr(10) . ' NAMESPACE["Perlito5::AST::Do"]' . chr(10) . ' ]);' . chr(10) . chr(10) . ' // CORE.say( "ast: [" + perl(ast) + "]" );' . chr(10) . ' js_code = ast._class_.emit_javascript([ast]);' . chr(10) . ' // CORE.say( "js-source: [" + js_code + "]" );' . chr(10) . chr(10) . ' NAMESPACE["Perlito5"].v_PKG_NAME = namespace_old;' . chr(10) . ' NAMESPACE["Perlito5"].v_VAR = var_env_js_old;' . chr(10) . ' return js_code;' . chr(10) . '}' . chr(10))
};
1;
View
46 src5/lib/Perlito5/Javascript/CORE.pm
@@ -25,25 +25,37 @@ sub emit_javascript {
var CORE = NAMESPACE.CORE;
-var _print_buf = "";
-CORE.print = function(List__) {
- var i;
- for (i = 0; i < List__.length; i++) {
- var s = string(List__[i]);
- if (s.substr(s.length - 2, 2) == "\n") {
- print(_print_buf + s.substr(0, s.length - 2));
- _print_buf = "";
- }
- else if (s.substr(s.length - 1, 1) == "\n") {
- print(_print_buf + s.substr(0, s.length - 1));
- _print_buf = "";
- }
- else {
- _print_buf = _print_buf + s;
+var isNode = typeof require != "undefined";
+if (isNode) {
+ CORE.print = function(List__) {
+ var i;
+ for (i = 0; i < List__.length; i++) {
+ var s = string(List__[i]);
+ process.stdout.write(s);
}
+ return true;
}
- return true;
-};
+} else {
+ var _print_buf = "";
+ CORE.print = function(List__) {
+ var i;
+ for (i = 0; i < List__.length; i++) {
+ var s = string(List__[i]);
+ if (s.substr(s.length - 2, 2) == "\n") {
+ print(_print_buf + s.substr(0, s.length - 2));
+ _print_buf = "";
+ }
+ else if (s.substr(s.length - 1, 1) == "\n") {
+ print(_print_buf + s.substr(0, s.length - 1));
+ _print_buf = "";
+ }
+ else {
+ _print_buf = _print_buf + s;
+ }
+ }
+ return true;
+ };
+}
CORE.say = function(List__) {
CORE.print(List__);
View
35 src5/lib/Perlito5/Javascript/Runtime.pm
@@ -22,6 +22,8 @@ sub emit_javascript {
//
// See http://www.perl.com/perl/misc/Artistic.html
+var isNode = true;
+
if (typeof NAMESPACE !== "object") {
NAMESPACE = {};
LOCAL = [];
@@ -126,7 +128,9 @@ function cleanup_local(idx, value) {
return value;
}
-if (typeof arguments === "object") {
+if (typeof isNode != "undefined") {
+ List_ARGV = process.argv.splice(2);
+} else if (typeof arguments === "object") {
List_ARGV = arguments;
}
@@ -148,17 +152,24 @@ function ScalarRef(o) {
this.bool = function() { return 1 };
}
-make_sub("Perlito5::IO", "slurp", function(List__) {
- var filename = List__[0];
- if (typeof readFile == "function") {
- return readFile(filename);
- }
- if (typeof read == "function") {
- // v8
- return read(filename);
- }
- NAMESPACE.CORE.die(["Perlito5::IO::slurp() not implemented"]);
-});
+if (isNode) {
+ var fs = require("fs");
+ make_sub("Perlito5::IO", "slurp", function(List__) {
+ return fs.readFileSync(List__[0],"utf8");
+ });
+} else {
+ make_sub("Perlito5::IO", "slurp", function(List__) {
+ var filename = List__[0];
+ if (typeof readFile == "function") {
+ return readFile(filename);
+ }
+ if (typeof read == "function") {
+ // v8
+ return read(filename);
+ }
+ NAMESPACE.CORE.die(["Perlito5::IO::slurp() not implemented"]);
+ });
+}
interpolate_array = function() {
var res = [];
View
2  util-js/make-perlito5-js.sh
@@ -1,4 +1,4 @@
-
+#!/bin/sh
rm tmp.js
perl -Ilib5 perlito5.pl --verbose -Cjs src5/util/perlito5-browser.pl > tmp.js
rm perlito5-old.js
View
2  util-js/make-perlito6-js.sh
@@ -1,4 +1,4 @@
-
+#!/bin/sh
rm tmp.js
perl -Ilib5 perlito6.pl --verbose -Cjs src6/util/perlito6-browser.pl > tmp.js
rm perlito-old.js
View
1  util-perl5/bootstrap-perlito5-perl5.sh
@@ -1,3 +1,4 @@
+#!/bin/sh
set -x
export PERL5LIB=./lib5
View
1  util-perl5/bootstrap-perlito6-perl5.sh
@@ -1,3 +1,4 @@
+#!/bin/sh
set -x
export PERL5LIB=./lib5
View
1  util-perl6/setup-perlito5-perl6.sh
@@ -1,3 +1,4 @@
+#!/bin/sh
set -x
export PERL6LIB=./lib6

No commit comments for this range

Something went wrong with that request. Please try again.