differentmatt / filbert Goto Github PK
View Code? Open in Web Editor NEWThis project forked from acornjs/acorn
JavaScript parser of Python
License: Other
This project forked from acornjs/acorn
JavaScript parser of Python
License: Other
How hard will it be to get Python string formatting / interpolation, where we can do things like:
print("My name is %s." % name)
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)
'dict' is currently treated internally as a keyword. It should be a built-in function.
Line 329 in 497a598
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.
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
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']
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.
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.
Default arguments
Keyword arguments
Arbitrary argument lists
Unpacking argument lists
https://docs.python.org/3.4/tutorial/controlflow.html#more-on-defining-functions
This may need to be broken up into separate pieces. Please feel free to take on a subset of this work instead of the whole enchilada.
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.
'''
if False: print('false')
else print('true')
if (false) {
__pythonRuntime.functions.print('false');
}
__pythonRuntime.functions.print('true');
Specifically, the membership and concatenation operations described here:
https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange
Slices, len, min, max, index, and count are already supported.
a = []
a += [12]
a += [34]
# a should be [12, 34]
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);
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.
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.
filbert_loose.parse_dammit runs out of memory on invalid source code 'def'. Likely an infinite loop.
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.
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.
See #8 for background.
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;
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.
It should be expecting a Python-style single argument function which returns the value to use for comparison.
From https://wiki.python.org/moin/HowTo/Sorting:
The value of the key parameter should be a function that takes a single argument and returns a key to use for sorting purposes. This technique is fast because the key function is called exactly once for each input record.
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.
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
https://wiki.python.org/moin/HowTo/Sorting
Needed to fix #25
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.
Functions like complex(), help(), dir(), divmod(), hash(), map() are unavailable. Haven't tested all of them though.
https://docs.python.org/3.4/reference/expressions.html#not-in
List, tuple, and dictionary equality would be great first steps.
# These are True in Python, false in JavaScript
[1, 2] == [1, 2]
{'a': 10, 'b':'hi'} == {'b':'hi', 'a':10}
def get_highest_value_coin(coins):
return max(coins, key = lambda c: c.bountyGold)
""
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
.
See #8 for background.
Negative indexes should return items offset from the end of the sequence.
The functionality was changed in Python3.
https://docs.python.org/3/library/stdtypes.html#list.sort
https://docs.python.org/3/library/functools.html#functools.cmp_to_key
def foo(x):
return -x
sorted([100, 1, 0, 22, 33, 5, 55], key=foo)
should sort the array in descending order but the output is different
Expected output: [ 100, 55, 33, 22, 5, 1, 0 ]
Actual output: [ 100, 1, 0, 22, 33, 5, 55 ]
http://legacy.python.org/dev/peps/pep-3132/
In [8]: a, *b, c = range(10)
In [9]: a
Out[9]: 0
In [10]: b
Out[10]: [1, 2, 3, 4, 5, 6, 7, 8]
In [11]: c
Out[11]: 9
dictTest1 = {'A': {'A1': 1}, 'B': {'B1': 2}} # This works
dictTest2 = {'A': {'A1': 1},
'B': {'B1': 2}} # This throws SyntaxError: Unexpected token
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.