Simplify reducers and add fast path to js reducer

master
Anton Medvedev 2 years ago
parent bd02783f75
commit 751e5865da

@ -135,6 +135,12 @@ func main() {
return
}
if lang == "js" {
simplePath, ok := SplitSimplePath(args)
if ok {
output := GetBySimplePath(object, simplePath)
Echo(output, theme)
os.Exit(0)
}
vm, fn, err := CreateJS(args, fxrc)
if err != nil {
fmt.Println(err)

@ -19,13 +19,6 @@ func js(args []string, fxrc string) string {
for i, a := range args {
rs += " try {"
switch {
case a == ".":
rs += `
x = function ()
{ return this }
.call(x)
`
case flatMapRegex.MatchString(a):
code := fold(strings.Split(a, "[]"))
rs += fmt.Sprintf(
@ -100,6 +93,6 @@ func ReduceJS(vm *goja.Runtime, reduce goja.Callable, input interface{}, theme T
fmt.Print(output)
return 0
}
echo(object, theme)
Echo(object, theme)
return 0
}

@ -32,13 +32,6 @@ func nodejs(args []string, fxrc string) string {
for i, a := range args {
rs += " try {"
switch {
case a == ".":
rs += `
x = function ()
{ return this }
.call(x)
`
case flatMapRegex.MatchString(a):
code := fold(strings.Split(a, "[]"))
rs += fmt.Sprintf(

@ -17,20 +17,12 @@ var templatePython string
func python(args []string) string {
rs := "\n"
for i, a := range args {
rs += "try:"
switch {
case a == ".":
rs += `
x = x
`
default:
rs += fmt.Sprintf(
`
rs += fmt.Sprintf(
`try:
f = (lambda x: (%v))(x)
x = f(x) if callable(f) else f
`, a)
}
// Generate a beautiful error message.
rs += "except Exception as e:\n"
pre, post, pointer := trace(args, i)

@ -28,10 +28,10 @@ func GenerateCode(lang string, args []string, fxrc string) string {
}
func Reduce(input interface{}, lang string, args []string, theme Theme, fxrc string) int {
path, ok := splitPath(args)
path, ok := SplitSimplePath(args)
if ok {
output := getByPath(input, path)
echo(output, theme)
output := GetBySimplePath(input, path)
Echo(output, theme)
return 0
}
var cmd *exec.Cmd
@ -68,7 +68,7 @@ func Reduce(input interface{}, lang string, args []string, theme Theme, fxrc str
fmt.Print(string(output))
return 0
}
echo(object, theme)
Echo(object, theme)
if dec.InputOffset() < int64(len(output)) {
fmt.Print(string(output[dec.InputOffset():]))
}

@ -17,19 +17,10 @@ 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(
`
rs += fmt.Sprintf(
`begin
x = lambda {|x| %v }.call(x)
`, a)
}
// Generate a beautiful error message.
rs += "rescue Exception => e\n"
pre, post, pointer := trace(args, i)

@ -24,7 +24,7 @@ const (
singleQuoteEscape
)
func splitPath(args []string) ([]interface{}, bool) {
func SplitSimplePath(args []string) ([]interface{}, bool) {
path := make([]interface{}, 0)
for _, arg := range args {
s := ""
@ -180,7 +180,7 @@ func isProp(ch rune) bool {
return unicode.IsLetter(ch) || unicode.IsDigit(ch) || ch == '_' || ch == '$'
}
func getByPath(object interface{}, path []interface{}) interface{} {
func GetBySimplePath(object interface{}, path []interface{}) interface{} {
for _, get := range path {
switch get := get.(type) {
case string:

@ -83,7 +83,7 @@ func Test_splitPath(t *testing.T) {
}
for _, tt := range tests {
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
path, ok := splitPath(tt.args)
path, ok := SplitSimplePath(tt.args)
require.Equal(t, tt.want, path)
require.True(t, ok)
})
@ -130,7 +130,7 @@ func Test_splitPath_negative(t *testing.T) {
}
for _, tt := range tests {
t.Run(strings.Join(tt.args, " "), func(t *testing.T) {
path, ok := splitPath(tt.args)
path, ok := SplitSimplePath(tt.args)
require.False(t, ok, path)
})
}

@ -9,7 +9,7 @@ import (
. "github.com/antonmedv/fx/pkg/theme"
)
func echo(object interface{}, theme Theme) {
func Echo(object interface{}, theme Theme) {
if s, ok := object.(string); ok {
fmt.Println(s)
} else {

Loading…
Cancel
Save