nodes.mx raw

   1  package syntax
   2  
   3  import "mxc/token"
   4  
   5  type Node interface {
   6  	Pos() token.Pos
   7  	SetPos(token.Pos)
   8  	aNode()
   9  }
  10  
  11  type node struct {
  12  	pos token.Pos
  13  }
  14  
  15  func (n *node) Pos() (p token.Pos) { return n.pos }
  16  func (n *node) SetPos(pos token.Pos) { n.pos = pos }
  17  func (*node) aNode()           {}
  18  
  19  type File struct {
  20  	Pragma    Pragma
  21  	PkgName   *Name
  22  	DeclList  []Decl
  23  	EOF       token.Pos
  24  	GoVersion string
  25  	node
  26  }
  27  
  28  type (
  29  	Decl interface {
  30  		Node
  31  		aDecl()
  32  	}
  33  
  34  	ImportDecl struct {
  35  		Group        *Group
  36  		Pragma       Pragma
  37  		LocalPkgName *Name
  38  		Path         *BasicLit
  39  		decl
  40  	}
  41  
  42  	ConstDecl struct {
  43  		Group    *Group
  44  		Pragma   Pragma
  45  		NameList []*Name
  46  		Type     Expr
  47  		Values   Expr
  48  		decl
  49  	}
  50  
  51  	TypeDecl struct {
  52  		Group      *Group
  53  		Pragma     Pragma
  54  		Name       *Name
  55  		TParamList []*Field
  56  		Alias      bool
  57  		Type       Expr
  58  		decl
  59  	}
  60  
  61  	VarDecl struct {
  62  		Group    *Group
  63  		Pragma   Pragma
  64  		NameList []*Name
  65  		Type     Expr
  66  		Values   Expr
  67  		decl
  68  	}
  69  
  70  	FuncDecl struct {
  71  		Pragma     Pragma
  72  		Recv       *Field
  73  		Name       *Name
  74  		TParamList []*Field
  75  		Type       *FuncType
  76  		Body       *BlockStmt
  77  		decl
  78  	}
  79  )
  80  
  81  type decl struct{ node }
  82  
  83  func (*decl) aDecl() {}
  84  
  85  type Group struct {
  86  	_ int32
  87  }
  88  
  89  func NewName(pos token.Pos, value string) (n *Name) {
  90  	n := &Name{}
  91  	n.pos = pos
  92  	n.Value = value
  93  	return n
  94  }
  95  
  96  type (
  97  	Expr interface {
  98  		Node
  99  		typeInfo
 100  		aExpr()
 101  	}
 102  
 103  	BadExpr struct {
 104  		expr
 105  	}
 106  
 107  	Name struct {
 108  		Value string
 109  		expr
 110  	}
 111  
 112  	BasicLit struct {
 113  		Value string
 114  		Kind  token.LitKind
 115  		Bad   bool
 116  		expr
 117  	}
 118  
 119  	CompositeLit struct {
 120  		Type     Expr
 121  		ElemList []Expr
 122  		NKeys    int32
 123  		Rbrace   token.Pos
 124  		expr
 125  	}
 126  
 127  	KeyValueExpr struct {
 128  		Key, Value Expr
 129  		expr
 130  	}
 131  
 132  	FuncLit struct {
 133  		Type *FuncType
 134  		Body *BlockStmt
 135  		expr
 136  	}
 137  
 138  	ParenExpr struct {
 139  		X Expr
 140  		expr
 141  	}
 142  
 143  	SelectorExpr struct {
 144  		X   Expr
 145  		Sel *Name
 146  		expr
 147  	}
 148  
 149  	IndexExpr struct {
 150  		X     Expr
 151  		Index Expr
 152  		expr
 153  	}
 154  
 155  	SliceExpr struct {
 156  		X     Expr
 157  		Index [3]Expr
 158  		Full  bool
 159  		expr
 160  	}
 161  
 162  	AssertExpr struct {
 163  		X    Expr
 164  		Type Expr
 165  		expr
 166  	}
 167  
 168  	TypeSwitchGuard struct {
 169  		Lhs *Name
 170  		X   Expr
 171  		expr
 172  	}
 173  
 174  	Operation struct {
 175  		Op   token.Operator
 176  		X, Y Expr
 177  		expr
 178  	}
 179  
 180  	CallExpr struct {
 181  		Fun     Expr
 182  		ArgList []Expr
 183  		HasDots bool
 184  		expr
 185  	}
 186  
 187  	ListExpr struct {
 188  		ElemList []Expr
 189  		expr
 190  	}
 191  
 192  	ArrayType struct {
 193  		Len  Expr
 194  		Elem Expr
 195  		expr
 196  	}
 197  
 198  	SliceType struct {
 199  		Elem Expr
 200  		expr
 201  	}
 202  
 203  	DotsType struct {
 204  		Elem Expr
 205  		expr
 206  	}
 207  
 208  	StructType struct {
 209  		FieldList []*Field
 210  		TagList   []*BasicLit
 211  		expr
 212  	}
 213  
 214  	Field struct {
 215  		Name *Name
 216  		Type Expr
 217  		node
 218  	}
 219  
 220  	InterfaceType struct {
 221  		MethodList []*Field
 222  		expr
 223  	}
 224  
 225  	FuncType struct {
 226  		ParamList  []*Field
 227  		ResultList []*Field
 228  		expr
 229  	}
 230  
 231  	MapType struct {
 232  		Key, Value Expr
 233  		expr
 234  	}
 235  
 236  	ChanType struct {
 237  		Dir  ChanDir
 238  		Elem Expr
 239  		expr
 240  	}
 241  )
 242  
 243  type Type interface {
 244  	Underlying() Type
 245  	String() string
 246  }
 247  
 248  type typeInfo interface {
 249  	SetTypeInfo(TypeAndValue)
 250  	GetTypeInfo() TypeAndValue
 251  }
 252  
 253  type ConstVal = interface{ String() string }
 254  
 255  type TypeAndValue struct {
 256  	Type  Type
 257  	Value ConstVal
 258  	exprFlags
 259  }
 260  
 261  type exprFlags uint16
 262  
 263  func (f exprFlags) IsVoid() (ok bool) { return f&1 != 0 }
 264  func (f exprFlags) IsType() (ok bool) { return f&2 != 0 }
 265  func (f exprFlags) IsBuiltin() (ok bool) { return f&4 != 0 }
 266  func (f exprFlags) IsValue() (ok bool) { return f&8 != 0 }
 267  func (f exprFlags) IsNil() (ok bool) { return f&16 != 0 }
 268  func (f exprFlags) Addressable() (ok bool) { return f&32 != 0 }
 269  func (f exprFlags) Assignable() (ok bool) { return f&64 != 0 }
 270  func (f exprFlags) HasOk() (ok bool) { return f&128 != 0 }
 271  func (f exprFlags) IsRuntimeHelper() (ok bool) { return f&256 != 0 }
 272  
 273  func (f *exprFlags) SetIsVoid()          { *f |= 1 }
 274  func (f *exprFlags) SetIsType()          { *f |= 2 }
 275  func (f *exprFlags) SetIsBuiltin()       { *f |= 4 }
 276  func (f *exprFlags) SetIsValue()         { *f |= 8 }
 277  func (f *exprFlags) SetIsNil()           { *f |= 16 }
 278  func (f *exprFlags) SetAddressable()     { *f |= 32 }
 279  func (f *exprFlags) SetAssignable()      { *f |= 64 }
 280  func (f *exprFlags) SetHasOk()           { *f |= 128 }
 281  func (f *exprFlags) SetIsRuntimeHelper() { *f |= 256 }
 282  
 283  type typeAndValue struct {
 284  	tv TypeAndValue
 285  }
 286  
 287  func (x *typeAndValue) SetTypeInfo(tv TypeAndValue) {
 288  	x.tv = tv
 289  }
 290  func (x *typeAndValue) GetTypeInfo() (t TypeAndValue) {
 291  	return x.tv
 292  }
 293  
 294  type expr struct {
 295  	node
 296  	typeAndValue
 297  }
 298  
 299  func (*expr) aExpr() {}
 300  
 301  type ChanDir uint32
 302  
 303  const (
 304  	_ ChanDir = iota
 305  	SendOnly
 306  	RecvOnly
 307  )
 308  
 309  type (
 310  	Stmt interface {
 311  		Node
 312  		aStmt()
 313  	}
 314  
 315  	SimpleStmt interface {
 316  		Stmt
 317  		aSimpleStmt()
 318  	}
 319  
 320  	EmptyStmt struct {
 321  		simpleStmt
 322  	}
 323  
 324  	LabeledStmt struct {
 325  		Label *Name
 326  		Stmt  Stmt
 327  		stmt
 328  	}
 329  
 330  	BlockStmt struct {
 331  		List   []Stmt
 332  		Rbrace token.Pos
 333  		stmt
 334  	}
 335  
 336  	ExprStmt struct {
 337  		X Expr
 338  		simpleStmt
 339  	}
 340  
 341  	SendStmt struct {
 342  		Chan, Value Expr
 343  		simpleStmt
 344  	}
 345  
 346  	DeclStmt struct {
 347  		DeclList []Decl
 348  		stmt
 349  	}
 350  
 351  	AssignStmt struct {
 352  		Op       token.Operator
 353  		Lhs, Rhs Expr
 354  		simpleStmt
 355  	}
 356  
 357  	BranchStmt struct {
 358  		Tok    token.Token
 359  		Label  *Name
 360  		Target Stmt
 361  		stmt
 362  	}
 363  
 364  	CallStmt struct {
 365  		Tok     token.Token
 366  		Call    Expr
 367  		DeferAt Expr
 368  		stmt
 369  	}
 370  
 371  	ReturnStmt struct {
 372  		Results Expr
 373  		stmt
 374  	}
 375  
 376  	IfStmt struct {
 377  		Init SimpleStmt
 378  		Cond Expr
 379  		Then *BlockStmt
 380  		Else Stmt
 381  		stmt
 382  	}
 383  
 384  	ForStmt struct {
 385  		Init SimpleStmt
 386  		Cond Expr
 387  		Post SimpleStmt
 388  		Body *BlockStmt
 389  		stmt
 390  	}
 391  
 392  	SwitchStmt struct {
 393  		Init   SimpleStmt
 394  		Tag    Expr
 395  		Body   []*CaseClause
 396  		Rbrace token.Pos
 397  		stmt
 398  	}
 399  
 400  	SelectStmt struct {
 401  		Body   []*CommClause
 402  		Rbrace token.Pos
 403  		stmt
 404  	}
 405  )
 406  
 407  type (
 408  	RangeClause struct {
 409  		Lhs Expr
 410  		Def bool
 411  		X   Expr
 412  		simpleStmt
 413  	}
 414  
 415  	CaseClause struct {
 416  		Cases Expr
 417  		Body  []Stmt
 418  		Colon token.Pos
 419  		node
 420  	}
 421  
 422  	CommClause struct {
 423  		Comm  SimpleStmt
 424  		Body  []Stmt
 425  		Colon token.Pos
 426  		node
 427  	}
 428  )
 429  
 430  type stmt struct{ node }
 431  
 432  func (stmt) aStmt() {}
 433  
 434  type simpleStmt struct {
 435  	stmt
 436  }
 437  
 438  func (simpleStmt) aSimpleStmt() {}
 439  
 440  type CommentKind uint32
 441  
 442  const (
 443  	Above CommentKind = iota
 444  	Below
 445  	Left
 446  	Right
 447  )
 448  
 449  type Comment struct {
 450  	Kind CommentKind
 451  	Text string
 452  	Next *Comment
 453  }
 454