nodes.mx raw

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