[jsinterp] Adding ast to test_function_expression
This commit is contained in:
parent
f7993a196a
commit
2533dc421b
@ -880,22 +880,56 @@ class TestJSInterpreterParser(unittest.TestCase):
|
||||
ast = []
|
||||
self.assertEqual(list(jsi.statements()), ast)
|
||||
|
||||
@unittest.skip('Test not yet implemented: missing ast')
|
||||
def test_function_expression(self):
|
||||
# ASAP function expression test
|
||||
jsi = JSInterpreter('''
|
||||
function f() {
|
||||
var add = (function () {
|
||||
var counter = 0;
|
||||
return function () {return counter += 1;}
|
||||
return function () {return counter += 1;};
|
||||
})();
|
||||
add();
|
||||
add();
|
||||
return add();
|
||||
}
|
||||
''')
|
||||
ast = []
|
||||
self.assertEqual(list(jsi.statements()), ast)
|
||||
ast = [
|
||||
(Token.FUNC, 'f', [],
|
||||
(Token.BLOCK, [
|
||||
(Token.VAR, zip(['add'], [(Token.ASSIGN, None, (Token.OPEXPR, [
|
||||
(Token.MEMBER, (Token.EXPR, [(Token.ASSIGN, None, (Token.OPEXPR, [
|
||||
(Token.MEMBER, (Token.FUNC, None, [], (Token.BLOCK, [
|
||||
(Token.VAR, zip(
|
||||
['counter'],
|
||||
[(Token.ASSIGN, None, (Token.OPEXPR, [
|
||||
(Token.MEMBER, (Token.INT, 0), None, None)
|
||||
]), None)]
|
||||
)),
|
||||
(Token.RETURN, (Token.EXPR, [(Token.ASSIGN, None, (Token.OPEXPR, [
|
||||
(Token.MEMBER, (Token.FUNC, None, [], (Token.BLOCK, [
|
||||
(Token.RETURN, (Token.EXPR, [
|
||||
(Token.ASSIGN, _ASSIGN_OPERATORS['+='][1], (Token.OPEXPR, [
|
||||
(Token.MEMBER, (Token.ID, 'counter'), None, None)
|
||||
]), (Token.ASSIGN, None, (Token.OPEXPR, [
|
||||
(Token.MEMBER, (Token.INT, 1), None, None)
|
||||
]), None))
|
||||
]))
|
||||
])), None, None)
|
||||
]), None)]))
|
||||
])), None, None),
|
||||
]), None)]), None, (Token.CALL, [], None))
|
||||
]), None)])),
|
||||
(Token.EXPR, [(Token.ASSIGN, None, (Token.OPEXPR, [
|
||||
(Token.MEMBER, (Token.ID, 'add'), None, (Token.CALL, [], None))
|
||||
]), None)]),
|
||||
(Token.EXPR, [(Token.ASSIGN, None, (Token.OPEXPR, [
|
||||
(Token.MEMBER, (Token.ID, 'add'), None, (Token.CALL, [], None))
|
||||
]), None)]),
|
||||
(Token.RETURN, (Token.EXPR, [(Token.ASSIGN, None, (Token.OPEXPR, [
|
||||
(Token.MEMBER, (Token.ID, 'add'), None, (Token.CALL, [], None))
|
||||
]), None)]))
|
||||
]))
|
||||
]
|
||||
self.assertEqual(list(traverse(list(jsi.statements()))), list(traverse(ast)))
|
||||
|
||||
def test_object(self):
|
||||
jsi = JSInterpreter('''
|
||||
|
@ -494,20 +494,22 @@ class JSInterpreter(object):
|
||||
# TODO support let
|
||||
peek_id, peek_value, peek_pos = token_stream.peek()
|
||||
if peek_id in _token_keys:
|
||||
token_stream.pop()
|
||||
if peek_id is Token.ID:
|
||||
# this
|
||||
if peek_value == 'this':
|
||||
token_stream.pop()
|
||||
return (Token.RSV, 'this')
|
||||
# function expr
|
||||
elif peek_value == 'function':
|
||||
return self._function(token_stream, stack_top - 1, True)
|
||||
# id
|
||||
else:
|
||||
token_stream.chk_id(last=True)
|
||||
token_stream.chk_id()
|
||||
token_stream.pop()
|
||||
return (Token.ID, peek_value)
|
||||
# literals
|
||||
else:
|
||||
token_stream.pop()
|
||||
return (peek_id, peek_value)
|
||||
# array
|
||||
elif peek_id is Token.SOPEN:
|
||||
|
Loading…
x
Reference in New Issue
Block a user