Skip to content

Commit

Permalink
Expand eagerly
Browse files Browse the repository at this point in the history
  • Loading branch information
zyedidia committed Aug 16, 2022
1 parent f6d199c commit 1016563
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
13 changes: 9 additions & 4 deletions expand/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@ func expand(r *bufio.Reader, rvar Resolver, rexpr Resolver, special byte) (strin
inExpr = false
value, err := rexpr(exprbuf.String())
if err != nil {
return "", err
buf.WriteString("$(")
buf.WriteString(exprbuf.String())
buf.WriteByte(')')
} else {
buf.WriteString(value)
}
buf.WriteString(value)
exprbuf.Reset()
continue
} else if inExpr {
Expand All @@ -100,9 +103,11 @@ func expand(r *bufio.Reader, rvar Resolver, rexpr Resolver, special byte) (strin
inVar = false
value, err := rvar(exprbuf.String())
if err != nil {
return "", err
buf.WriteByte('$')
buf.WriteString(exprbuf.String())
} else {
buf.WriteString(value)
}
buf.WriteString(value)
exprbuf.Reset()
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/full_build/test.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ ncpu = 1

[[builds]]

args = ["clean"]
output = """\
rm -f hello.o other.o hello
"""

[[builds]]

args = ["hello"]
output = """\
gcc -Wall -O2 -c hello.c -o hello.o
Expand Down
11 changes: 6 additions & 5 deletions vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ func NewLuaVM() *LuaVM {
vars: make(map[string]*lua.LTable),
}

rvar, rexpr := vm.ExpandFuncs()
lib := liblua.FromLibs(liblua.Knit)
L.SetGlobal("import", luar.New(L, func(pkg string) *lua.LTable {
return lib.Import(L, pkg)
}))
L.SetGlobal("_rule", luar.New(L, func(rule string, file string, line int) {
vm.addRule(rule, file, line)
vm.addRule(rule, file, line, rvar, rexpr)
}))
L.SetGlobal("rule", luar.New(L, func(rule string) {
dbg, ok := L.GetStack(1)
Expand All @@ -51,7 +52,7 @@ func NewLuaVM() *LuaVM {
file = dbg.Source
line = dbg.CurrentLine
}
vm.addRule(rule, file, line)
vm.addRule(rule, file, line, rvar, rexpr)
}))
L.SetGlobal("tostring", luar.New(L, func(v lua.LValue) string {
return LToString(v)
Expand Down Expand Up @@ -100,7 +101,6 @@ func NewLuaVM() *LuaVM {
return v
}))

rvar, rexpr := vm.ExpandFuncs()
// fn := func(name string) (string, error) {
// v := getVar(L, name)
// if v == nil {
Expand All @@ -121,9 +121,10 @@ func NewLuaVM() *LuaVM {
return vm
}

func (vm *LuaVM) addRule(rule string, file string, line int) {
func (vm *LuaVM) addRule(rule string, file string, line int, rvar, rexpr expand.Resolver) {
s, _ := expand.Expand(rule, rvar, rexpr)
vm.rules = append(vm.rules, LRule{
Contents: rule,
Contents: s,
File: file,
Line: line,
Env: getLocals(vm.L),
Expand Down

0 comments on commit 1016563

Please sign in to comment.