예제 #1
0
  def VarDecl(self, pnode):
    # type: (PNode) -> command__VarDecl
    """Transform an Oil assignment statement."""
    typ = pnode.typ
    children = pnode.children

    # TODO: Fill this in.
    lhs_type = None

    if typ == grammar_nt.oil_var:
      # oil_var: lvalue_list [type_expr] '=' testlist (Op_Semi | Op_Newline)

      #log('len(children) = %d', len(children))
      lvalue = self.Expr(children[0])  # could be a tuple
      #log('lvalue %s', lvalue)

      n = len(children)
      if n == 4:
        op_tok = children[1].tok
        rhs = children[2]
      elif n == 5:
        # TODO: translate type expression
        op_tok = children[2].tok
        rhs = children[3]

      else:
        raise AssertionError(n)

      # The caller should fill in the keyword token.
      return command.VarDecl(None, lvalue, lhs_type, op_tok, self.Expr(rhs))

    if typ == grammar_nt.oil_setvar:
      # oil_setvar: lvalue_list (augassign | '=') testlist (Op_Semi | Op_Newline)
      lvalue = self.Expr(children[0])  # could be a tuple
      op_tok = children[1].tok
      rhs = children[2]
      return command.VarDecl(None, lvalue, lhs_type, op_tok, self.Expr(rhs))

    nt_name = self.number2symbol[typ]
    raise AssertionError(
        "PNode type %d (%s) wasn't handled" % (typ, nt_name))
예제 #2
0
파일: expr_to_ast.py 프로젝트: grahamc/oil
  def MakeVarDecl(self, p_node):
    # type: (PNode) -> command__VarDecl
    """
    oil_var_decl: name_type_list '=' testlist end_stmt
    """
    typ = p_node.typ
    children = p_node.children
    assert typ == grammar_nt.oil_var_decl

    #log('len(children) = %d', len(children))
    lhs = self._NameTypeList(children[0])  # could be a tuple
    rhs = self.Expr(children[2])

    # The caller should fill in the keyword token.
    return command.VarDecl(None, lhs, rhs)