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 return
} }
if lang == "js" { 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) vm, fn, err := CreateJS(args, fxrc)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)

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

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

@ -17,20 +17,12 @@ var templatePython string
func python(args []string) string { func python(args []string) string {
rs := "\n" rs := "\n"
for i, a := range args { for i, a := range args {
rs += "try:" rs += fmt.Sprintf(
switch { `try:
case a == ".":
rs += `
x = x
`
default:
rs += fmt.Sprintf(
`
f = (lambda x: (%v))(x) f = (lambda x: (%v))(x)
x = f(x) if callable(f) else f x = f(x) if callable(f) else f
`, a) `, a)
}
// Generate a beautiful error message. // Generate a beautiful error message.
rs += "except Exception as e:\n" rs += "except Exception as e:\n"
pre, post, pointer := trace(args, i) 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 { func Reduce(input interface{}, lang string, args []string, theme Theme, fxrc string) int {
path, ok := splitPath(args) path, ok := SplitSimplePath(args)
if ok { if ok {
output := getByPath(input, path) output := GetBySimplePath(input, path)
echo(output, theme) Echo(output, theme)
return 0 return 0
} }
var cmd *exec.Cmd var cmd *exec.Cmd
@ -68,7 +68,7 @@ func Reduce(input interface{}, lang string, args []string, theme Theme, fxrc str
fmt.Print(string(output)) fmt.Print(string(output))
return 0 return 0
} }
echo(object, theme) Echo(object, theme)
if dec.InputOffset() < int64(len(output)) { if dec.InputOffset() < int64(len(output)) {
fmt.Print(string(output[dec.InputOffset():])) fmt.Print(string(output[dec.InputOffset():]))
} }

@ -17,19 +17,10 @@ var templateRuby string
func ruby(args []string) string { func ruby(args []string) string {
rs := "\n" rs := "\n"
for i, a := range args { for i, a := range args {
rs += "begin" rs += fmt.Sprintf(
switch { `begin
case a == ".":
rs += `
x = x
`
default:
rs += fmt.Sprintf(
`
x = lambda {|x| %v }.call(x) x = lambda {|x| %v }.call(x)
`, a) `, a)
}
// Generate a beautiful error message. // Generate a beautiful error message.
rs += "rescue Exception => e\n" rs += "rescue Exception => e\n"
pre, post, pointer := trace(args, i) pre, post, pointer := trace(args, i)

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

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

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

Loading…
Cancel
Save