Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #22037

Re: Source of the Python Translator to Javascript

Message-ID <1975627.sOtdtiOqRW@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-11-18 13:03 +0100
Subject Re: Source of the Python Translator to Javascript
Newsgroups comp.lang.javascript
References <0a90df3b-fa16-47de-ba68-9ba97ed9d4b7@googlegroups.com>

Show all headers | View raw


Salvatore DI DIO wrote:

> You can find the source of the translator here :
> 
> https://github.com/charleslaw/rapydscript_online
> 
> You can create the executable version, using Node-Webkit.
> 
> Here is an exeample of generated code :
> 
> Python:
> --------
> 
> class User:
>     def __init__(self, lang="Javascript"):
>         self.language = lang
> 
> 
> def add(x=0,y=0):
>     return x + y
> 
> 
> 
> def factorial(n):
>     if n == 0:
>         return 1
>     return n * factorial(n-1)
> 
> 
> 
> Translated Javascript:
             ^^^^^^^^^^
> ---------------------
> 
> User = function(lang){
>     var self = this;
>     if (typeof lang === "undefined") lang = "Javascript";
>     self.language = lang;
> };
> 
> function add(x, y) {
>     if (typeof x === "undefined") x = 0;
>     if (typeof y === "undefined") y = 0;
>     return x + y;
> }
> 
> function factorial(n) {
>     if (n == 0) {
>         return 1;
>     }
>     return n * factorial(n - 1);
> }

I figured as much.

Your so-called “translation” is _not_ equivalent to the Python code above 
because, from top to bottom:

- It has to be “var User = …”, else the result of this operation can be
  one of

    * overwriting a previously declared variable, parameter or previously
      assigned property value, whichever comes first in the scope chain;

    * exception or no-op if the property value cannot be overwritten
      (the former happens when this code runs in MSHTML and there is an
      element with name or ID “User” in the document);

    * ReferenceError in strict mode if there is no such variable or
      property in the scope chain

  in a conforming implementation of ECMAScript (from here: ES).

  Also note that in ES, variables can be assigned to before their
  declaration.   By contrast, variables are implicitly declared by
  and only after assignment in Python.

- One can pass the “undefined” value as argument for a formal parameter x
  (so that “typeof x === "undefined"” is true), which is _not_ equivalent
  to not passing an argument for the formal parameter at all.  You have to
  read “arguments.length” to be sure.

- Python will attempt conversion to a numeric type with “+” (as defined
  by the type of its arguments), while in ES string concatenation will be
  performed if one operand is of type String or can be converted into
  String.  You would have to force conversion to Number and then would
  have to deal with the fact that the Number type is an implementation of
  IEEE-754 doubles – there are no integer types as there are in Python.

  Indeed, if one of the operands refers to an object that has or inherits
  an __add__() method, Python will call that method instead of performing
  addition.  This is why in Python ["foo"] + ["bar"] is ['foo', 'bar']
  of type “list”, while in ES it is "foobar" of type “String”.  Different
  to Python, there is no operator overloading in ES (yet), but implicit
  type conversion instead (the only triggered methods that can be overridden
  are toString() and valueOf() so far).

- Different to Python, in ES implicit type conversion will be performed if
  you use “==”; insofar the “===” with “typeof” is too much (because both
  operands are of type String already) and the “== 0” is too little (because
  “n” may not be of type Number).

Also, the “self” local variable is superfluous in ECMAScript; presumably you 
declared it because it made “translation” easier.  The declaration will 
conflict, however, with any “self” formal parameter and references within 
closures.

Probably I have forgotten something else.

> Please don't make assumptions before testing :

Please do not attempt to write translators before you have a fair knowledge 
about what you are translating into (and from).

See also: <http://www.crockford.com/javascript/javascript.html>

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Source of the Python Translator to Javascript Salvatore DI DIO <salvatore.didio@gmail.com> - 2013-11-18 03:14 -0800
  Re: Source of the Python Translator to Javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-11-18 13:03 +0100
  Re: Source of the Python Translator to Javascript Salvatore DI DIO <salvatore.didio@gmail.com> - 2013-11-18 05:07 -0800
    Re: Source of the Python Translator to Javascript Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-11-18 14:49 +0100
      Re: Source of the Python Translator to Javascript Salvatore DI DIO <salvatore.didio@gmail.com> - 2013-11-18 07:10 -0800

csiph-web