mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-25 23:42:23 +00:00
compiler: process bool literals in a generic way
This commit is contained in:
parent
70d0ff869d
commit
aeaa4a8210
2 changed files with 1 additions and 30 deletions
|
@ -86,34 +86,12 @@ func countGlobals(f ast.Node) (i int64) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// isIdentBool looks if the given ident is a boolean.
|
|
||||||
func isIdentBool(ident *ast.Ident) bool {
|
|
||||||
return ident.Name == "true" || ident.Name == "false"
|
|
||||||
}
|
|
||||||
|
|
||||||
// isExprNil looks if the given expression is a `nil`.
|
// isExprNil looks if the given expression is a `nil`.
|
||||||
func isExprNil(e ast.Expr) bool {
|
func isExprNil(e ast.Expr) bool {
|
||||||
v, ok := e.(*ast.Ident)
|
v, ok := e.(*ast.Ident)
|
||||||
return ok && v.Name == "nil"
|
return ok && v.Name == "nil"
|
||||||
}
|
}
|
||||||
|
|
||||||
// makeBoolFromIdent creates a bool type from an *ast.Ident.
|
|
||||||
func makeBoolFromIdent(ident *ast.Ident, tinfo *types.Info) (types.TypeAndValue, error) {
|
|
||||||
var b bool
|
|
||||||
switch ident.Name {
|
|
||||||
case "true":
|
|
||||||
b = true
|
|
||||||
case "false":
|
|
||||||
b = false
|
|
||||||
default:
|
|
||||||
return types.TypeAndValue{}, fmt.Errorf("givent identifier cannot be converted to a boolean => %s", ident.Name)
|
|
||||||
}
|
|
||||||
return types.TypeAndValue{
|
|
||||||
Type: tinfo.ObjectOf(ident).Type(),
|
|
||||||
Value: constant.MakeBool(b),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// resolveEntryPoint returns the function declaration of the entrypoint and the corresponding file.
|
// resolveEntryPoint returns the function declaration of the entrypoint and the corresponding file.
|
||||||
func resolveEntryPoint(entry string, pkg *loader.PackageInfo) (*ast.FuncDecl, *ast.File) {
|
func resolveEntryPoint(entry string, pkg *loader.PackageInfo) (*ast.FuncDecl, *ast.File) {
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -610,14 +610,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
if isIdentBool(n) {
|
if tv := c.typeAndValueOf(n); tv.Value != nil {
|
||||||
value, err := makeBoolFromIdent(n, c.typeInfo)
|
|
||||||
if err != nil {
|
|
||||||
c.prog.Err = err
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
c.emitLoadConst(value)
|
|
||||||
} else if tv := c.typeAndValueOf(n); tv.Value != nil {
|
|
||||||
c.emitLoadConst(tv)
|
c.emitLoadConst(tv)
|
||||||
} else {
|
} else {
|
||||||
c.emitLoadVar(n.Name)
|
c.emitLoadVar(n.Name)
|
||||||
|
|
Loading…
Reference in a new issue