mirror of
https://github.com/antonmedv/fx
synced 2024-11-01 21:40:20 +00:00
Add ruby reducers
This commit is contained in:
parent
1b09c93447
commit
61eb6e5ec3
@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func CreateNodejs(args []string) *exec.Cmd {
|
||||
cmd := exec.Command("node", "-e", GenerateCode(args))
|
||||
cmd := exec.Command("node", "-e", nodejs(args))
|
||||
cmd.Env = os.Environ()
|
||||
cmd.Env = append(cmd.Env, "NODE_OPTIONS=--max-old-space-size=8192")
|
||||
return cmd
|
||||
@ -19,7 +19,7 @@ func CreateNodejs(args []string) *exec.Cmd {
|
||||
//go:embed reduce.js
|
||||
var templateJs string
|
||||
|
||||
func GenerateCodeNodejs(args []string) string {
|
||||
func nodejs(args []string) string {
|
||||
rs := "\n"
|
||||
for i, a := range args {
|
||||
rs += " try {"
|
||||
|
@ -7,14 +7,14 @@ import (
|
||||
)
|
||||
|
||||
func CreatePython(bin string, args []string) *exec.Cmd {
|
||||
cmd := exec.Command(bin, "-c", GenerateCode(args))
|
||||
cmd := exec.Command(bin, "-c", python(args))
|
||||
return cmd
|
||||
}
|
||||
|
||||
//go:embed reduce.py
|
||||
var templatePython string
|
||||
|
||||
func GenerateCodePython(args []string) string {
|
||||
func python(args []string) string {
|
||||
rs := "\n"
|
||||
for i, a := range args {
|
||||
rs += "try:"
|
||||
@ -27,7 +27,7 @@ func GenerateCodePython(args []string) string {
|
||||
default:
|
||||
rs += fmt.Sprintf(
|
||||
`
|
||||
f = (lambda json: (%v))(x)
|
||||
f = (lambda x: (%v))(x)
|
||||
x = f(x) if callable(f) else f
|
||||
`, a)
|
||||
}
|
||||
|
@ -18,11 +18,13 @@ func GenerateCode(args []string) string {
|
||||
if !ok {
|
||||
lang = "node"
|
||||
}
|
||||
switch {
|
||||
case lang == "node":
|
||||
return GenerateCodeNodejs(args)
|
||||
case strings.HasPrefix(lang, "python"):
|
||||
return GenerateCodePython(args)
|
||||
switch lang {
|
||||
case "node":
|
||||
return nodejs(args)
|
||||
case "python", "python3":
|
||||
return python(args)
|
||||
case "ruby":
|
||||
return ruby(args)
|
||||
default:
|
||||
panic("unknown lang")
|
||||
}
|
||||
@ -34,11 +36,13 @@ func Reduce(object interface{}, args []string, theme Theme) {
|
||||
if !ok {
|
||||
lang = "node"
|
||||
}
|
||||
switch {
|
||||
case lang == "node":
|
||||
switch lang {
|
||||
case "node":
|
||||
cmd = CreateNodejs(args)
|
||||
case strings.HasPrefix(lang, "python"):
|
||||
case "python", "python3":
|
||||
cmd = CreatePython(lang, args)
|
||||
case "ruby":
|
||||
cmd = CreateRuby(args)
|
||||
default:
|
||||
panic("unknown lang")
|
||||
}
|
||||
|
6
pkg/reducer/reduce.rb
Normal file
6
pkg/reducer/reduce.rb
Normal file
@ -0,0 +1,6 @@
|
||||
require 'json'
|
||||
x = JSON.parse(STDIN.read)
|
||||
|
||||
# Reducers %v
|
||||
|
||||
puts JSON.generate(x)
|
46
pkg/reducer/ruby.go
Normal file
46
pkg/reducer/ruby.go
Normal file
@ -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
Block a user