From aeaa4a82106ffb227c1ab1e423b4bd4b2bfc52cf Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 19 May 2020 15:20:10 +0300 Subject: [PATCH] compiler: process bool literals in a generic way --- pkg/compiler/analysis.go | 22 ---------------------- pkg/compiler/codegen.go | 9 +-------- 2 files changed, 1 insertion(+), 30 deletions(-) diff --git a/pkg/compiler/analysis.go b/pkg/compiler/analysis.go index b79d1de85..09681d69e 100644 --- a/pkg/compiler/analysis.go +++ b/pkg/compiler/analysis.go @@ -86,34 +86,12 @@ func countGlobals(f ast.Node) (i int64) { 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`. func isExprNil(e ast.Expr) bool { v, ok := e.(*ast.Ident) 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. func resolveEntryPoint(entry string, pkg *loader.PackageInfo) (*ast.FuncDecl, *ast.File) { var ( diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index 54ca11b4d..5720f948b 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -610,14 +610,7 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor { return nil case *ast.Ident: - if isIdentBool(n) { - 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 { + if tv := c.typeAndValueOf(n); tv.Value != nil { c.emitLoadConst(tv) } else { c.emitLoadVar(n.Name)