github.com/DQNEO/babygo@v0.0.3/lib/ast/ast.go (about)

     1  package ast
     2  
     3  import (
     4  	"github.com/DQNEO/babygo/lib/token"
     5  )
     6  
     7  var Con ObjKind = "Con"
     8  var Typ ObjKind = "Typ"
     9  var Var ObjKind = "Var"
    10  var Fun ObjKind = "Fun"
    11  var Pkg ObjKind = "Pkg"
    12  
    13  type Signature struct {
    14  	Params  *FieldList
    15  	Results *FieldList
    16  }
    17  
    18  type ObjKind string
    19  
    20  func (ok ObjKind) String() string {
    21  	return string(ok)
    22  }
    23  
    24  type Object struct {
    25  	Kind ObjKind
    26  	Name string
    27  	Decl interface{} // *ValueSpec|*FuncDecl|*TypeSpec|*Field|*AssignStmt
    28  	Data interface{}
    29  }
    30  
    31  type Expr interface{}
    32  
    33  type Field struct {
    34  	Names  []*Ident
    35  	Type   Expr
    36  	Offset int
    37  }
    38  
    39  type FieldList struct {
    40  	List []*Field
    41  }
    42  
    43  type Ident struct {
    44  	Name string
    45  	Obj  *Object
    46  }
    47  
    48  type Ellipsis struct {
    49  	Elt Expr
    50  }
    51  
    52  type BasicLit struct {
    53  	Kind  token.Token // token.INT, token.CHAR, or token.STRING
    54  	Value string
    55  }
    56  
    57  type CompositeLit struct {
    58  	Type Expr
    59  	Elts []Expr
    60  }
    61  
    62  type KeyValueExpr struct {
    63  	Key   Expr
    64  	Value Expr
    65  }
    66  
    67  type ParenExpr struct {
    68  	X Expr
    69  }
    70  
    71  type SelectorExpr struct {
    72  	X   Expr
    73  	Sel *Ident
    74  }
    75  
    76  type IndexExpr struct {
    77  	X     Expr
    78  	Index Expr
    79  }
    80  
    81  type SliceExpr struct {
    82  	X      Expr
    83  	Low    Expr
    84  	High   Expr
    85  	Max    Expr
    86  	Slice3 bool
    87  }
    88  
    89  type CallExpr struct {
    90  	Fun      Expr   // function expression
    91  	Args     []Expr // function arguments; or nil
    92  	Ellipsis token.Pos
    93  }
    94  
    95  type StarExpr struct {
    96  	X Expr
    97  }
    98  
    99  type UnaryExpr struct {
   100  	X  Expr
   101  	Op token.Token
   102  }
   103  
   104  type BinaryExpr struct {
   105  	X  Expr
   106  	Y  Expr
   107  	Op token.Token
   108  }
   109  
   110  type TypeAssertExpr struct {
   111  	X    Expr
   112  	Type Expr // asserted type; nil means type switch X.(type)
   113  }
   114  
   115  // Type nodes
   116  type ArrayType struct {
   117  	Len Expr
   118  	Elt Expr
   119  }
   120  
   121  type StructType struct {
   122  	Fields *FieldList
   123  }
   124  
   125  type InterfaceType struct {
   126  	Methods []string
   127  }
   128  
   129  type MapType struct {
   130  	Key   Expr
   131  	Value Expr
   132  }
   133  
   134  type FuncType struct {
   135  	Params  *FieldList
   136  	Results *FieldList
   137  }
   138  
   139  type Stmt interface{}
   140  
   141  type DeclStmt struct {
   142  	Decl Decl
   143  }
   144  
   145  type ExprStmt struct {
   146  	X Expr
   147  }
   148  
   149  type IncDecStmt struct {
   150  	X   Expr
   151  	Tok token.Token
   152  }
   153  
   154  type AssignStmt struct {
   155  	Lhs     []Expr
   156  	Tok     token.Token
   157  	Rhs     []Expr
   158  	IsRange bool
   159  }
   160  
   161  type ReturnStmt struct {
   162  	Results []Expr
   163  }
   164  
   165  type BranchStmt struct {
   166  	Tok   token.Token
   167  	Label string
   168  }
   169  
   170  type BlockStmt struct {
   171  	List []Stmt
   172  }
   173  
   174  type IfStmt struct {
   175  	Init Stmt
   176  	Cond Expr
   177  	Body *BlockStmt
   178  	Else Stmt
   179  }
   180  
   181  type CaseClause struct {
   182  	List []Expr
   183  	Body []Stmt
   184  }
   185  
   186  type SwitchStmt struct {
   187  	Init Expr
   188  	Tag  Expr
   189  	Body *BlockStmt
   190  	// lableExit string
   191  }
   192  
   193  type TypeSwitchStmt struct {
   194  	Assign Stmt
   195  	Body   *BlockStmt
   196  }
   197  
   198  type ForStmt struct {
   199  	Init Stmt
   200  	Cond Expr
   201  	Post Stmt
   202  	Body *BlockStmt
   203  }
   204  
   205  type RangeStmt struct {
   206  	Key   Expr
   207  	Value Expr
   208  	X     Expr
   209  	Body  *BlockStmt
   210  	Tok   token.Token
   211  }
   212  
   213  type ImportSpec struct {
   214  	Path *BasicLit
   215  }
   216  
   217  type ValueSpec struct {
   218  	Names  []*Ident
   219  	Type   Expr
   220  	Values []Expr
   221  }
   222  
   223  type TypeSpec struct {
   224  	Name   *Ident
   225  	Assign bool // isAlias
   226  	Type   Expr
   227  }
   228  
   229  // Pseudo interface for *ast.Decl
   230  // *GenDecl | *FuncDecl
   231  type Decl interface {
   232  }
   233  
   234  type Spec interface{}
   235  
   236  type GenDecl struct {
   237  	Specs []Spec
   238  }
   239  
   240  type FuncDecl struct {
   241  	Recv *FieldList
   242  	Name *Ident
   243  	Type *FuncType
   244  	Body *BlockStmt
   245  }
   246  
   247  type File struct {
   248  	Name       *Ident
   249  	Imports    []*ImportSpec
   250  	Decls      []Decl
   251  	Unresolved []*Ident
   252  	Scope      *Scope
   253  }
   254  
   255  type Scope struct {
   256  	Outer   *Scope
   257  	Objects map[string]*Object
   258  }
   259  
   260  func NewScope(outer *Scope) *Scope {
   261  	return &Scope{
   262  		Outer:   outer,
   263  		Objects: make(map[string]*Object),
   264  	}
   265  }
   266  
   267  func (s *Scope) Insert(obj *Object) {
   268  	if s == nil {
   269  		panic("s sholud not be nil\n")
   270  	}
   271  	s.Objects[obj.Name] = obj
   272  }
   273  
   274  func (s *Scope) Lookup(name string) *Object {
   275  	obj, ok := s.Objects[name]
   276  	if !ok {
   277  		return nil
   278  	}
   279  
   280  	return obj
   281  }