Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gcsl numbers #2239

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions pkg/gcs/ast/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,6 @@ func lexText(l *lexer) stateFn {
l.backup()
return lexNumber
case r == '-':
// if next item is a number then lex number
n := l.next()
if isNumeric(n) {
// backup twice
l.backup()
l.backup()
return lexNumber
}
// other wise it's a - sign
l.backup()
l.emit(ItemMinus)
case r == '>':
if n := l.next(); n == '=' {
Expand Down Expand Up @@ -409,11 +399,6 @@ func isAlphaNumeric(r rune) bool {
return r == '_' || r == '-' || unicode.IsLetter(r) || unicode.IsDigit(r) || r == '%'
}

// is Numeric reports whether r is a digit
func isNumeric(r rune) bool {
return unicode.IsDigit(r)
}

// atTerminator reports whether the input is at valid termination character to
// appear after an identifier. Breaks .X.Y into two pieces. Also catches cases
// like "$x+2" not being acceptable without a space, in case we decide one
Expand Down
3 changes: 2 additions & 1 deletion pkg/gcs/ast/lex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ func TestBasicToken(t *testing.T) {
// end for
{Typ: itemRightBrace, Val: "}"},
// misc tests
{Typ: itemNumber, Val: "-1"},
{Typ: ItemMinus, Val: "-"},
{Typ: itemNumber, Val: "1"},
{Typ: itemNumber, Val: "1"},
{Typ: ItemMinus, Val: "-"},
{Typ: ItemMinus, Val: "-"},
Expand Down
30 changes: 21 additions & 9 deletions pkg/gcs/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,27 @@ func neq(l, r *number) *number {
}

func add(l, r *number) *number {
return &number{
ival: l.ival + r.ival,
fval: l.fval + r.fval,
n := &number{
isFloat: l.isFloat || r.isFloat,
}
if n.isFloat {
n.fval = ntof(l) + ntof(r)
} else {
n.ival = l.ival + r.ival
}
return n
}

func mul(l, r *number) *number {
return &number{
ival: l.ival * r.ival,
fval: l.fval * r.fval,
n := &number{
isFloat: l.isFloat || r.isFloat,
}
if n.isFloat {
n.fval = ntof(l) * ntof(r)
} else {
n.ival = l.ival * r.ival
}
return n
}

func div(l, r *number) *number {
Expand All @@ -99,9 +107,13 @@ func div(l, r *number) *number {
}

func sub(l, r *number) *number {
return &number{
ival: l.ival - r.ival,
fval: l.fval - r.fval,
n := &number{
isFloat: l.isFloat || r.isFloat,
}
if n.isFloat {
n.fval = ntof(l) - ntof(r)
} else {
n.ival = l.ival - r.ival
}
return n
}
2 changes: 1 addition & 1 deletion pkg/gcs/sysfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ func (e *Eval) executeAction(c *ast.CallExpr, env *Env) (Obj, error) {
if v.Typ() != typNum {
return nil, fmt.Errorf("map params should evaluate to a number, got %v", v.Inspect())
}
params[k] = int(v.(*number).ival)
params[k] = int(ntof(v.(*number)))
}

charKey := keys.Char(char.ival)
Expand Down
Loading