Skip to content

Commit 802436a

Browse files
authored
Merge pull request #114 from hinshun/string-chain
Allow zero-arg string functions to be used as a string expression
2 parents 37aeaae + 1a8f808 commit 802436a

1 file changed

Lines changed: 6 additions & 18 deletions

File tree

codegen/expr.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,12 @@ import (
1212
func (cg *CodeGen) EmitStringExpr(ctx context.Context, scope *parser.Scope, expr *parser.Expr) (string, error) {
1313
switch {
1414
case expr.Ident != nil, expr.Selector != nil:
15-
16-
obj := scope.Lookup(expr.Ident.Name)
17-
switch obj.Kind {
18-
case parser.DeclKind:
19-
switch n := obj.Node.(type) {
20-
case *parser.FuncDecl:
21-
return cg.EmitStringFuncDecl(ctx, scope, n, nil, noopAliasCallback, nil)
22-
case *parser.AliasDecl:
23-
return cg.EmitStringAliasDecl(ctx, scope, n, nil, nil)
24-
default:
25-
return "", errors.WithStack(ErrCodeGen{expr, errors.Errorf("unknown decl object")})
26-
}
27-
case parser.ExprKind:
28-
return obj.Data.(string), nil
29-
default:
30-
return "", errors.WithStack(ErrCodeGen{expr, errors.Errorf("unknown obj type")})
15+
sc, err := cg.EmitStringChainStmt(ctx, scope, expr, nil, nil, nil)
16+
if err != nil {
17+
return "", err
3118
}
19+
20+
return sc("")
3221
case expr.BasicLit != nil:
3322
if expr.BasicLit.Str != nil {
3423
return expr.BasicLit.Str.Unquoted(), nil
@@ -110,8 +99,7 @@ func (cg *CodeGen) EmitFilesystemExpr(ctx context.Context, scope *parser.Scope,
11099
return st, err
111100
}
112101

113-
st, err = so(st)
114-
return st, err
102+
return so(st)
115103
case expr.BasicLit != nil:
116104
return llb.Scratch(), errors.WithStack(ErrCodeGen{expr, errors.Errorf("fs expr cannot be basic lit")})
117105
case expr.FuncLit != nil:

0 commit comments

Comments
 (0)