github.com/neugram/ng@v0.0.0-20180309130942-d472ff93d872/syntax/expr/expr.go (about)

     1  // Copyright 2015 The Neugram Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package expr defines data structures representing Neugram expressions.
     6  package expr
     7  
     8  import (
     9  	"neugram.io/ng/syntax/src"
    10  	"neugram.io/ng/syntax/tipe"
    11  	"neugram.io/ng/syntax/token"
    12  )
    13  
    14  type Expr interface {
    15  	expr()
    16  	Pos() src.Pos // implements syntax.Node
    17  }
    18  
    19  type Binary struct {
    20  	Position src.Pos
    21  	Op       token.Token // Add, Sub, Mul, Div, Rem, Pow, And, Or, Equal, NotEqual, Less, Greater
    22  	Left     Expr
    23  	Right    Expr
    24  }
    25  
    26  type Unary struct {
    27  	Position src.Pos
    28  	Op       token.Token // Not, Mul (deref), Ref, LeftParen, Range
    29  	Expr     Expr
    30  }
    31  
    32  type Bad struct {
    33  	Position src.Pos
    34  	Error    error
    35  }
    36  
    37  type Selector struct {
    38  	Position src.Pos
    39  	Left     Expr
    40  	Right    *Ident
    41  }
    42  
    43  type Slice struct {
    44  	Position src.Pos
    45  	Low      Expr
    46  	High     Expr
    47  	Max      Expr
    48  }
    49  
    50  type Index struct {
    51  	Position src.Pos
    52  	Left     Expr
    53  	Indicies []Expr
    54  }
    55  
    56  type TypeAssert struct {
    57  	Position src.Pos
    58  	Left     Expr
    59  	Type     tipe.Type // asserted type; nil means type switch X.(type)
    60  }
    61  
    62  type BasicLiteral struct {
    63  	Position src.Pos
    64  	Value    interface{} // string, *big.Int, *big.Float
    65  }
    66  
    67  type FuncLiteral struct {
    68  	Position        src.Pos
    69  	Name            string // may be empty
    70  	ReceiverName    string // if non-empty, this is a method
    71  	PointerReceiver bool
    72  	Type            *tipe.Func
    73  	ParamNames      []string
    74  	ResultNames     []string
    75  	Body            interface{} // *stmt.Block, breaking the package import cycle
    76  }
    77  
    78  type CompLiteral struct {
    79  	Position src.Pos
    80  	Type     tipe.Type
    81  	Keys     []Expr // TODO: could make this []string
    82  	Values   []Expr
    83  }
    84  
    85  type MapLiteral struct {
    86  	Position src.Pos
    87  	Type     tipe.Type
    88  	Keys     []Expr
    89  	Values   []Expr
    90  }
    91  
    92  type ArrayLiteral struct {
    93  	Position src.Pos
    94  	Type     *tipe.Array
    95  	Keys     []Expr // TODO: could make this []int
    96  	Values   []Expr
    97  }
    98  
    99  type SliceLiteral struct {
   100  	Position src.Pos
   101  	Type     *tipe.Slice
   102  	Keys     []Expr // TODO: could make this []int
   103  	Values   []Expr
   104  }
   105  
   106  type TableLiteral struct {
   107  	Position src.Pos
   108  	Type     *tipe.Table
   109  	ColNames []Expr
   110  	Rows     [][]Expr
   111  }
   112  
   113  // Type is not a typical Neugram expression. It is used only for when
   114  // types are passed as arguments to the builtin functions new and make.
   115  type Type struct {
   116  	Position src.Pos
   117  	Type     tipe.Type
   118  }
   119  
   120  type Ident struct {
   121  	Position src.Pos
   122  	Name     string
   123  	// Type tipe.Type
   124  }
   125  
   126  type Call struct {
   127  	Position   src.Pos
   128  	Func       Expr
   129  	Args       []Expr
   130  	Ellipsis   bool // last argument expands, e.g. f(x...)
   131  	ElideError bool
   132  }
   133  
   134  type Range struct {
   135  	Position src.Pos
   136  	Start    Expr
   137  	End      Expr
   138  	Exact    Expr
   139  }
   140  
   141  type ShellList struct {
   142  	Position src.Pos
   143  	AndOr    []*ShellAndOr
   144  }
   145  
   146  type ShellAndOr struct {
   147  	Position   src.Pos
   148  	Pipeline   []*ShellPipeline
   149  	Sep        []token.Token // '&&' or '||'. len(Sep) == len(Pipeline)-1
   150  	Background bool
   151  }
   152  
   153  type ShellPipeline struct {
   154  	Position src.Pos
   155  	Bang     bool
   156  	Cmd      []*ShellCmd // Cmd[0] | Cmd[1] | ...
   157  }
   158  
   159  type ShellCmd struct {
   160  	Position  src.Pos
   161  	SimpleCmd *ShellSimpleCmd // or:
   162  	Subshell  *ShellList
   163  }
   164  
   165  type ShellSimpleCmd struct {
   166  	Position src.Pos
   167  	Redirect []*ShellRedirect
   168  	Assign   []ShellAssign
   169  	Args     []string
   170  }
   171  
   172  type ShellRedirect struct {
   173  	Position src.Pos
   174  	Number   *int
   175  	Token    token.Token // '<', '<&', '>', '>&', '>>'
   176  	Filename string
   177  }
   178  
   179  type ShellAssign struct {
   180  	Position src.Pos
   181  	Key      string
   182  	Value    string
   183  }
   184  
   185  type Shell struct {
   186  	Position   src.Pos
   187  	Cmds       []*ShellList
   188  	TrapOut    bool // override os.Stdout, outer language collect it
   189  	DropOut    bool // send stdout to /dev/null (just an optimization)
   190  	ElideError bool
   191  
   192  	// FreeVars is a list of $-parameters referred to in this
   193  	// shell expression that are declared statically in the
   194  	// scope of the expression. Not all parameters have to be
   195  	// declared statically in the scope, as they may be
   196  	// referring to run time environment variables.
   197  	FreeVars []string
   198  
   199  	// TODO: Shell object for err := $$(stdin, stdout, stderr) cmd $$
   200  }
   201  
   202  func (e *Binary) expr()         {}
   203  func (e *Unary) expr()          {}
   204  func (e *Bad) expr()            {}
   205  func (e *Selector) expr()       {}
   206  func (e *Slice) expr()          {}
   207  func (e *BasicLiteral) expr()   {}
   208  func (e *FuncLiteral) expr()    {}
   209  func (e *CompLiteral) expr()    {}
   210  func (e *MapLiteral) expr()     {}
   211  func (e *ArrayLiteral) expr()   {}
   212  func (e *SliceLiteral) expr()   {}
   213  func (e *TableLiteral) expr()   {}
   214  func (e *Type) expr()           {}
   215  func (e *Ident) expr()          {}
   216  func (e *Call) expr()           {}
   217  func (e *Index) expr()          {}
   218  func (e *TypeAssert) expr()     {}
   219  func (e *ShellList) expr()      {}
   220  func (e *ShellAndOr) expr()     {}
   221  func (e *ShellPipeline) expr()  {}
   222  func (e *ShellSimpleCmd) expr() {}
   223  func (e *ShellRedirect) expr()  {}
   224  func (e *ShellAssign) expr()    {}
   225  func (e *ShellCmd) expr()       {}
   226  func (e *Shell) expr()          {}
   227  
   228  func (e *Binary) Pos() src.Pos         { return e.Position }
   229  func (e *Unary) Pos() src.Pos          { return e.Position }
   230  func (e *Bad) Pos() src.Pos            { return e.Position }
   231  func (e *Selector) Pos() src.Pos       { return e.Position }
   232  func (e *Slice) Pos() src.Pos          { return e.Position }
   233  func (e *BasicLiteral) Pos() src.Pos   { return e.Position }
   234  func (e *FuncLiteral) Pos() src.Pos    { return e.Position }
   235  func (e *CompLiteral) Pos() src.Pos    { return e.Position }
   236  func (e *MapLiteral) Pos() src.Pos     { return e.Position }
   237  func (e *ArrayLiteral) Pos() src.Pos   { return e.Position }
   238  func (e *SliceLiteral) Pos() src.Pos   { return e.Position }
   239  func (e *TableLiteral) Pos() src.Pos   { return e.Position }
   240  func (e *Type) Pos() src.Pos           { return e.Position }
   241  func (e *Ident) Pos() src.Pos          { return e.Position }
   242  func (e *Call) Pos() src.Pos           { return e.Position }
   243  func (e *Range) Pos() src.Pos          { return e.Position }
   244  func (e *Index) Pos() src.Pos          { return e.Position }
   245  func (e *TypeAssert) Pos() src.Pos     { return e.Position }
   246  func (e *ShellList) Pos() src.Pos      { return e.Position }
   247  func (e *ShellAndOr) Pos() src.Pos     { return e.Position }
   248  func (e *ShellPipeline) Pos() src.Pos  { return e.Position }
   249  func (e *ShellSimpleCmd) Pos() src.Pos { return e.Position }
   250  func (e *ShellRedirect) Pos() src.Pos  { return e.Position }
   251  func (e ShellAssign) Pos() src.Pos     { return e.Position }
   252  func (e *ShellCmd) Pos() src.Pos       { return e.Position }
   253  func (e *Shell) Pos() src.Pos          { return e.Position }