github.com/nevalang/neva@v0.23.1-0.20240507185603-7696a9bb8dda/internal/compiler/sourcecode/typesystem/helper.go (about)

     1  package typesystem
     2  
     3  import "github.com/nevalang/neva/internal/compiler/sourcecode/core"
     4  
     5  // Helper is just a namespace for helper functions to avoid conflicts with entity types.
     6  // It's a stateless type and it's safe to share it between goroutines.
     7  type Helper struct{}
     8  
     9  // any base type def (without body) that has type parameters allows recursion
    10  func (h Helper) BaseDefWithRecursionAllowed(params ...Param) Def {
    11  	return Def{
    12  		Params:   params,
    13  		BodyExpr: nil,
    14  	}
    15  }
    16  
    17  // Do not pass empty string as a name to avoid Body.Empty() == true
    18  func (h Helper) BaseDef(params ...Param) Def {
    19  	return Def{Params: params}
    20  }
    21  
    22  func (h Helper) Def(body Expr, params ...Param) Def {
    23  	return Def{
    24  		Params:   params,
    25  		BodyExpr: &body,
    26  	}
    27  }
    28  
    29  // Do not pass empty string as a name to avoid inst.Empty() == true
    30  func (h Helper) Inst(ref string, args ...Expr) Expr {
    31  	if args == nil {
    32  		args = []Expr{} // makes easier testing because resolver returns non-nil args for native types
    33  	}
    34  	return Expr{
    35  		Inst: &InstExpr{
    36  			Ref:  core.EntityRef{Name: ref},
    37  			Args: args,
    38  		},
    39  	}
    40  }
    41  
    42  func (h Helper) Enum(els ...string) Expr {
    43  	if els == nil { // for !lit.Empty()
    44  		els = []string{}
    45  	}
    46  	return Expr{
    47  		Lit: &LitExpr{Enum: els},
    48  	}
    49  }
    50  
    51  func (h Helper) Union(els ...Expr) Expr {
    52  	if els == nil { // for !lit.Empty()
    53  		els = []Expr{}
    54  	}
    55  	return Expr{
    56  		Lit: &LitExpr{Union: els},
    57  	}
    58  }
    59  
    60  func (h Helper) Struct(structure map[string]Expr) Expr {
    61  	if structure == nil { // for !lit.Empty()
    62  		structure = map[string]Expr{}
    63  	}
    64  	return Expr{
    65  		Lit: &LitExpr{
    66  			Struct: structure,
    67  		},
    68  	}
    69  }
    70  
    71  func (h Helper) ParamWithNoConstr(name string) Param {
    72  	return Param{
    73  		Name: name,
    74  		Constr: Expr{
    75  			Inst: &InstExpr{
    76  				Ref: core.EntityRef{Name: "any"},
    77  			},
    78  		},
    79  	}
    80  }
    81  
    82  func (h Helper) Param(name string, constr Expr) Param {
    83  	return Param{
    84  		Name:   name,
    85  		Constr: constr,
    86  	}
    87  }
    88  
    89  type DefaultStringer string
    90  
    91  func (ds DefaultStringer) String() string { return string(ds) }
    92  
    93  func (h Helper) Trace(ss ...string) Trace {
    94  	var t *Trace
    95  	for _, s := range ss {
    96  		tmp := NewTrace(t, core.EntityRef{Name: s})
    97  		t = &tmp
    98  	}
    99  	return *t
   100  }