solved a number of problems highlighted by the syntax analyzer

This commit is contained in:
Celestino Amoroso 2024-12-27 07:46:11 +01:00
parent 24e31997fc
commit cca3b76baa
6 changed files with 29 additions and 28 deletions

View File

@ -126,34 +126,36 @@ func (f *FractionType) ToString(opt FmtOpt) string {
if opt&MultiLine == 0 { if opt&MultiLine == 0 {
sb.WriteString(fmt.Sprintf("%d:%d", f.num, f.den)) sb.WriteString(fmt.Sprintf("%d:%d", f.num, f.den))
} else { } else {
var s, num string var sign, num string
if f.num < 0 && opt&TTY == 0 { if f.num < 0 && opt&TTY == 0 {
num = strconv.FormatInt(-f.num, 10) num = strconv.FormatInt(-f.num, 10)
s = "-" sign = "-"
} else { } else {
num = strconv.FormatInt(f.num, 10) num = strconv.FormatInt(f.num, 10)
} }
den := strconv.FormatInt(f.den, 10) den := strconv.FormatInt(f.den, 10)
size := max(len(num), len(den)) size := max(len(num), len(den))
if opt&TTY != 0 { if opt&TTY != 0 {
sb.WriteString(fmt.Sprintf("\x1b[4m%[1]*s\x1b[0m\n", -size, fmt.Sprintf("%[1]*s", (size+len(num))/2, s+num))) sNum := fmt.Sprintf("\x1b[4m%[1]*s\x1b[0m\n", -size, fmt.Sprintf("%[1]*s", (size+len(num))/2, sign+num))
sb.WriteString(sNum)
} else { } else {
if len(s) > 0 { if len(sign) > 0 {
sb.WriteString(" ") sb.WriteString(" ")
} }
sb.WriteString(fmt.Sprintf("%[1]*s", -size, fmt.Sprintf("%[1]*s", (size+len(num))/2, num))) sb.WriteString(fmt.Sprintf("%[1]*s", -size, fmt.Sprintf("%[1]*s", (size+len(num))/2, num)))
sb.WriteByte('\n') sb.WriteByte('\n')
if len(s) > 0 { if len(sign) > 0 {
sb.WriteString(s) sb.WriteString(sign)
sb.WriteByte(' ') sb.WriteByte(' ')
} }
sb.WriteString(strings.Repeat("-", size)) sb.WriteString(strings.Repeat("-", size))
sb.WriteByte('\n') sb.WriteByte('\n')
if len(s) > 0 { if len(sign) > 0 {
sb.WriteString(" ") sb.WriteString(" ")
} }
} }
sb.WriteString(fmt.Sprintf("%[1]*s", -size, fmt.Sprintf("%[1]*s", (size+len(den))/2, den))) sDen := fmt.Sprintf("%[1]*s", size, fmt.Sprintf("%[1]*s", (size+len(den))/2, den))
sb.WriteString(sDen)
} }
return sb.String() return sb.String()

View File

@ -5,7 +5,7 @@
package expr package expr
import ( import (
"errors" // "errors"
"fmt" "fmt"
) )
@ -43,6 +43,6 @@ func errNoOperation(name string) error {
return fmt.Errorf("no %s() function defined in the data-source", name) return fmt.Errorf("no %s() function defined in the data-source", name)
} }
func errInvalidDataSource() error { // func errInvalidDataSource() error {
return errors.New("invalid data-source") // return errors.New("invalid data-source")
} // }

View File

@ -20,7 +20,7 @@ func evalContextValue(ctx ExprContext, opTerm *term) (v any, err error) {
var childValue any var childValue any
var sourceCtx ExprContext var sourceCtx ExprContext
if opTerm.children == nil || len(opTerm.children) == 0 { if len(opTerm.children) == 0 {
sourceCtx = ctx sourceCtx = ctx
} else if opTerm.children[0].symbol() == SymVariable && opTerm.children[0].source() == "global" { } else if opTerm.children[0].symbol() == SymVariable && opTerm.children[0].source() == "global" {
sourceCtx = globalCtx sourceCtx = globalCtx

View File

@ -460,7 +460,7 @@ func (scanner *scanner) parseNumber(firstCh byte) (tk *Token) {
tk = scanner.makeErrorToken(err) tk = scanner.makeErrorToken(err)
} else { } else {
var value any var value any
err = scanner.sync(err) // TODO: Check this function _ = scanner.sync(err) // TODO: Check this function
txt := sb.String() txt := sb.String()
if sym == SymFloat { if sym == SymFloat {
value, err = strconv.ParseFloat(txt, 64) value, err = strconv.ParseFloat(txt, 64)

View File

@ -59,8 +59,7 @@ func TestFractionToStringMultiline(t *testing.T) {
} }
} }
// TODO Check this test: the output string ends with a space func TestToStringMultilineTty(t *testing.T) {
func _TestToStringMultilineTty(t *testing.T) {
source := newFraction(-1, 2) source := newFraction(-1, 2)
want := "\x1b[4m-1\x1b[0m\n 2" want := "\x1b[4m-1\x1b[0m\n 2"
got := source.ToString(MultiLine | TTY) got := source.ToString(MultiLine | TTY)

View File

@ -8,18 +8,18 @@ import (
"testing" "testing"
) )
func _TestImportPlugin(t *testing.T) { // func TestImportPlugin(t *testing.T) {
t.Setenv("PLUGINS", "${HOME}/go/src/git.portale-stac.it/go") // t.Setenv("PLUGINS", "${HOME}/go/src/git.portale-stac.it/go")
t.Setenv("EXPR_PLUGIN_PATH","${PLUGINS}/expr-json-plugin:${PLUGINS}/expr-csv-plugin") // t.Setenv("EXPR_PLUGIN_PATH","${PLUGINS}/expr-json-plugin:${PLUGINS}/expr-csv-plugin")
gotCount, gotErr := importPluginFromSearchPath("json") // gotCount, gotErr := importPluginFromSearchPath("json")
if gotCount != 1 { // if gotCount != 1 {
t.Errorf("Import count: got=%d, want=1", gotCount) // t.Errorf("Import count: got=%d, want=1", gotCount)
} // }
if gotErr != nil { // if gotErr != nil {
t.Errorf("importPlugin() failed: %v", gotErr) // t.Errorf("importPlugin() failed: %v", gotErr)
} // }
} // }
func TestPluginExists(t *testing.T) { func TestPluginExists(t *testing.T) {
name := "json" name := "json"