forked from Archives/fx
Add ruby reducers
parent
1b09c93447
commit
61eb6e5ec3
@ -0,0 +1,6 @@
|
||||
require 'json'
|
||||
x = JSON.parse(STDIN.read)
|
||||
|
||||
# Reducers %v
|
||||
|
||||
puts JSON.generate(x)
|
@ -0,0 +1,46 @@
|
||||
package reducer
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func CreateRuby(args []string) *exec.Cmd {
|
||||
cmd := exec.Command("ruby", "-e", ruby(args))
|
||||
return cmd
|
||||
}
|
||||
|
||||
//go:embed reduce.rb
|
||||
var templateRuby string
|
||||
|
||||
func ruby(args []string) string {
|
||||
rs := "\n"
|
||||
for i, a := range args {
|
||||
rs += "begin"
|
||||
switch {
|
||||
case a == ".":
|
||||
rs += `
|
||||
x = x
|
||||
`
|
||||
|
||||
default:
|
||||
rs += fmt.Sprintf(
|
||||
`
|
||||
x = lambda {|x| %v }.call(x)
|
||||
`, a)
|
||||
}
|
||||
// Generate a beautiful error message.
|
||||
rs += "rescue Exception => e\n"
|
||||
pre, post, pointer := trace(args, i)
|
||||
rs += fmt.Sprintf(
|
||||
` STDERR.puts "\n #{%q} #{%q} #{%q}\n %v\n\n#{e}\n"
|
||||
exit(1)
|
||||
`,
|
||||
pre, a, post,
|
||||
pointer,
|
||||
)
|
||||
rs += "end\n"
|
||||
}
|
||||
return fmt.Sprintf(templateRuby, rs)
|
||||
}
|
Loading…
Reference in New Issue