GithubHelp home page GithubHelp logo

differentmatt / filbert Goto Github PK

View Code? Open in Web Editor NEW

This project forked from acornjs/acorn

131.0 131.0 27.0 1.66 MB

JavaScript parser of Python

License: Other

JavaScript 82.49% CSS 0.38% HTML 17.13%

filbert's People

Contributors

abhidya avatar abraidwood avatar aparajita avatar arian avatar basicer avatar benekastah avatar bhargavvader avatar dideler avatar differentmatt avatar keeyipchan avatar kratorado avatar kyay10 avatar lehni avatar marijnh avatar mishoo avatar mrcarlberg avatar nwinter avatar oxyc avatar p01 avatar prust avatar wjx avatar xavion3 avatar zsjforcn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

filbert's Issues

'in' does not work on dictionaries

Incorrectly calls indexOf on the dictionary, as if it's a sequence.

a = {'p': 5}
'p' in a
var a = new __pythonRuntime.objects.dict([
        'p',
        5
    ]);
a.indexOf('p') >= 0;

Python language problem with advanced indexes for strings

It appears that the python parser has issues with indexes for strings (and I would assume arrays as well). Using negative numbers and using slices doesn't seem to work.

Negative indexes:

message = 'Company Halt!'
if message[-1] != "!":
     order()

The order function should not run, but it does. When I change it to:

message = 'Company Halt!'
if message[len(message)-1] != "!":
     order()

The order function is not run.

Slices:
message[:2]
message[4:9]
message[2:9:3]

None of these seem to work. (Though I only tested the first 2 methods).

So apparently python's extra ways of dealing with string indexing doesn't work.

Truth value testing

Wasn't sure the right way to title this one. I have a simple function that should only run if the list M is non-empty. I tried to implement it this way:

if M:
    a = 10 / len(M)

I get an error as soon as M=[]

This makes the error go away:

if len(M)>0:
    a = 10 / len(M)

true and false shouldn't be valid Python

In filbert, True and true in Python both get evaluated to true; in JS.
Similarly, False and false evaluate to false; in JS.

Only True and False should work. Python would consider true and false variables, and if they weren't defined beforehand, calling them would result in a NameError. E.g.

NameError: name 'true' is not defined

Remembering that Python booleans are titleized is another learning block, so this bug may give new programmers a less frustrating experience (avoids moments like "why isn't my if statement working?") but it also teaches them incorrect Python. Just something to consider.

Non-empty list constructor should not return empty list

list(1, 2, 3) should give a TypeError.

>>> list(1, 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list() takes at most 1 argument (3 given)
>>> list((1, 2, 3))
[1, 2, 3]

In filbert, list(1, 2, 3) evaluates to an empty list.
Note that in filbert list() correctly evaluates to an empty list and list((1, 2,3)) to a non-empty list.

Treat literal notation for dicts, lists, tuples as literal expression?

"" evaluates to a literal expression

{
      "type": "ExpressionStatement",
      "expression": {
        "type": "Literal",
        "value": "",
        "raw": "\"\""
      }
    },

[] evaluates to a new expression

{
      "type": "ExpressionStatement",
      "expression": {
        "type": "NewExpression",
        "arguments": [],
        "callee": {
          "type": "MemberExpression",
          "object": {
            "type": "MemberExpression",
            "object": {
              "type": "Identifier",
              "name": "__pythonRuntime"
            },
            "property": {
              "type": "Identifier",
              "name": "objects"
            },
            "computed": false
          },
          "property": {
            "type": "Identifier",
            "name": "list"
          },
          "computed": false
        }
      }
    },

Would it be useful to treat literal notations like [], {}, () as literal expressions? Unfortunately I'm not familiar enough with filbert to think about it properly, so I'm just suggesting it.

When the literal notation isn't empty, could it make things difficult since brackets and parentheses also have other uses? E.g. [1, 2, 3] vs [x for x in xrange(5) if x % 2 == 0] or (1, 2, 3) vs (2 + 3) * 4.

Empty list should be false in conditional

The "Pythonic" way to check for an empty list is if not my_list and for a non-empty list is if my_list.

if not []: # List is empty, should evaluate to true (does not work)
if not [1, 2]: # List is not empty, should evaluate to false (works)

if []: # List is empty, should evaluate to false (does not work)
if [1, 2]: # List is not empty, should evaluate to true (works)

l = []
if l :# List is empty, should evaluate to false (does not work)
if not l: # List is empty, should evaluate to true (does not work)

l.append(1)
if l: # List is not empty, should evaluate to true (works)
if not l: # List is not empty, should evaluate to false (works)

A temporary workaround for checking for empty lists is to check the length of the list:

if len([]) != 0:

Note that bool() evaluates lists correctly in filbert; the issue seems to just be with conditionals.

Get imports working.

So here for discussion about imports, my current idea is to have a modules container where we store them and move them into scope as they get imported. I'll put up an example snippet when I get home.

Empty tuple should be false in conditional

The "Pythonic" way to check for a non-empty tuple is if my_tuple.

t = ()
if t: # Should be false (does not work)

if (): # Should be false (does not work, raises error - see issue #48)

if (1, 2): # Should be true (works, but see comment in issue #48)

Note that bool() evaluates tuples correctly in filbert; the issue seems to just be with conditionals.

Compound Boolean Expressions don't work properly

This is from the Filbert Interactive Testing page:

a = 5
b = 5

if a == b > 4:
    print('Compound Works')
else:
    print('Compound Fails')

if a==b and b>4:
    print('And Works')
else:
    print('And Fails')

Output:

Compound Fails
And Works

Both should work.

No support for sort keys?

a=["a","aa"]
a.sort(key=len)
print(a)

This returns an error that key is not a defined keyword argument:
SyntaxError: Unexpected token
TypeError: Cannot read property 'type' of undefined

triple quoted comment blocks

My favorite way to comment out a block of code is to put triple quotes around it, this doesn't seem to work with the parser:

'''
This 
is
all
comments.
'''

Empty dict should be false in conditional

if {}: # Should evaluate to false (does not work)
if {1: 2}: # Should evaluate to true (works)

Note that bool() evaluates dict correctly in filbert; the issue seems to just be with conditionals.

For statement init nested tuples

We don't support this currently:

for ((i,j),k) in [[[1,2],5], [[3,4],6]]: print(i, j, k)
(1, 2, 5)
(3, 4, 6)
for ((i,i),i) in [[[1,2],5], [[3,4],6]]: print(i)
5
6

Multiline arrays fail to parse

friends = [
    'Joan',
    'Ronan',
    'Nikita',
    'Augustus'
]

leads to a Filbert parser TypeError in the demo page and dies on CodeCombat, too. Putting everything on one line works:

friends = ['Joan', 'Ronan', 'Nikita', 'Augustus']

Creating functions with optional parameters

I seem to get an error if I try to create a function with a default value assigned to a parameter:

class A:
   def __init__(self):
        self.b = 'hi'

   def main(self):
       def r(a,b=7):
           return a*7
       self.r = r

a=A()
a.main()
print(a.r)
print(a.r(8))

This code run just fine (in the filbert tester) until I added the parameter b=7.

Line detection is broken

h="Hello" w='World' print(h) print(w)

That code runs. It should not do that, clearly something in whatever separates lines is seriously broken.

Include filbert_loose.js testing in 'grunt test'

Running grunt test should test filbert.js and filbert_loose.js.

Currently it only runs tests against filbert.js. To test filbert_loose.js, you have to
hack test/spec/util.js to target filbert_loose.js instead.

Single line while can't handle multiple statements

This is probably not limited to while loops.

Input:

total = 0
while True: total += 1; break;
print(total)

Output:

var total = 0;
while (true) {
    total = __pythonRuntime.ops.add(total, 1);
}
break;
;
__pythonRuntime.functions.print(total);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.