compiler: process bool literals in a generic way

This commit is contained in:
Evgenii Stratonikov 2020-05-19 15:20:10 +03:00
parent 70d0ff869d
commit aeaa4a8210
2 changed files with 1 additions and 30 deletions

View file

@ -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 (

View file

@ -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)