differentmatt / filbert

JavaScript parser of Python
Other
133 stars 27 forks source link

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

Closed dideler closed 9 years ago

dideler commented 9 years ago

"" 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.

differentmatt commented 9 years ago

We need to keep these empty Python objects as objects instead of literal expressions, so we can treat them as Python objects. For example, len([]) would break.

"" will need to be a new expression as well someday, which yields a Python string object. Then we can handle Python-specific string processing in #24.

dideler commented 9 years ago

That makes sense, thanks for answering.