Skip to content

Commit

Permalink
Merge pull request #414 from refaktor/multiliner
Browse files Browse the repository at this point in the history
improvements to test tool in info/, fixed all tests for base
  • Loading branch information
refaktor authored Nov 20, 2024
2 parents 703a04b + 240773a commit d7fe671
Show file tree
Hide file tree
Showing 10 changed files with 665 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
go version -m bin/rye
- name: Run Rye Tests
run: cd tests ; ../bin/rye main.rye test
run: cd info ; ../bin/rye main.rye test base
timeout-minutes: 1

- name: Build WASM
Expand Down
11 changes: 6 additions & 5 deletions cmd/rbit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,15 @@ func main() {

flag.Usage = func() {
fmt.Println("╭────────────────────────────────────────────────────────────────────────────────────────────---")
fmt.Println("│ \033[1mrbit - rye builtin info tool")
fmt.Println("│ \033[1mrbit - rye builtin info tool - https://ryelang.org")
fmt.Println("╰───────────────────────────────────────────────────────────────────────────────────────---")
fmt.Println("\n Usage: \033[1mparse\033[0m [\033[1moptions\033[0m] [\033[1mfilename\033[0m or \033[1mcommand\033[0m]")
fmt.Println("\n Usage: \033[1mparse\033[0m [\033[1moptions\033[0m] [\033[1mfilename\033[0m]")
flag.PrintDefaults()
fmt.Println("\033[33m rbit \033[36m# shows helo")
fmt.Println("\033[33m rbit \033[36m# shows this help")
fmt.Println("\033[33m rbit ../../evaldo/builtins.go > ../../info/base.info.rye \033[36m# generates the info file")
fmt.Println("\033[33m rbit -stats ../../evaldo/builtins.go \033[36m# gets bi coverage stats")
fmt.Println("\033[33m rbit -ls ../../evaldo/ \033[36m# lists bi files")
fmt.Println("\033[33m rbit -stats ../../evaldo/builtins.go \033[36m# gets builtins file stats")
fmt.Println("\033[33m rbit -ls ../../evaldo/ \033[36m# lists builtin files")
fmt.Println("\033[33m rbit -help \033[36m# shows this help")
fmt.Println("\033[0m\n Thank you for trying out \033[1mRye\033[22m ...")
fmt.Println("")
}
Expand Down
8 changes: 7 additions & 1 deletion env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package env

import (
"fmt"
"math"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -168,7 +169,12 @@ func (i Decimal) Equal(o Object) bool {
}
// fmt.Println(i.Value)
// fmt.Println(o.(Decimal).Value)
return i.Value == o.(Decimal).Value
const epsilon = 0.0000000000001 // math.SmallestNonzeroFloat64
if math.Abs(i.Value-o.(Decimal).Value) <= (epsilon) {
return true
} else {
return false
}
}

func (i Decimal) Dump(e Idxs) string {
Expand Down
68 changes: 43 additions & 25 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ func getFrom(ps *env.ProgramState, data any, key any, posMode bool) env.Object {
if posMode {
idx--
}
if len(s1.Data) >= int(idx)+1 {
v := s1.Data[idx]
if len(s1.Data) > int(idx) && idx >= 0 {
v := s1.Data[idx-1]
return env.ToRyeValue(v)
} else {
ps.FailureFlag = true
Expand All @@ -267,8 +267,8 @@ func getFrom(ps *env.ProgramState, data any, key any, posMode bool) env.Object {
if posMode {
idx--
}
if len(s1.Data) >= int(idx)+1 {
v := s1.Data[idx]
if len(s1.Data) > int(idx) && idx >= 0 {
v := s1.Data[idx-1]
return env.ToRyeValue(v)
} else {
ps.FailureFlag = true
Expand Down Expand Up @@ -410,7 +410,7 @@ var ShowResults bool
var builtins = map[string]*env.Builtin{
// Tests:
// equal { to-word "test" } 'test
// error { to-word "123" }
// error { to-word 123 }
"to-word": {
Argsn: 1,
Doc: "Tries to change a Rye value to a word with same name.",
Expand Down Expand Up @@ -490,7 +490,7 @@ var builtins = map[string]*env.Builtin{
// Tests:
// equal { to-string 'test } "test"
// equal { to-string 123 } "123"
// equal { to-string 123.4 } "123.4000"
// equal { to-string 123.4 } "123.400000"
// equal { to-string "test" } "test"
"to-string": { // ***
Argsn: 1,
Expand Down Expand Up @@ -536,7 +536,7 @@ var builtins = map[string]*env.Builtin{
},

// Tests:
// equal { dict [ "a" 1 "b" 2 "c" 3 ] |to-context |type? } 'context
// equal { dict [ "a" 1 "b" 2 "c" 3 ] |to-context |type? } 'ctx ; TODO - rename ctx to context in Rye
// ; equal { dict [ "a" 1 ] |to-context do\in { a } } '1
"to-context": { // ***
Argsn: 1,
Expand Down Expand Up @@ -642,7 +642,7 @@ var builtins = map[string]*env.Builtin{

// Tests:
// equal { to-file "example.txt" } %example.txt
// error { to-file 123 }
// equal { to-file 123 } %123
"to-file": { // ** TODO-FIXME: return possible failures
Argsn: 1,
Doc: "Tries to change Rye value to a file.",
Expand Down Expand Up @@ -1290,8 +1290,8 @@ var builtins = map[string]*env.Builtin{
},
},
// Tests:
// equal { 3 .odd } 0
// equal { 2 .odd } 1
// equal { 3 .even } 0
// equal { 2 .even } 1
"even": { // ***
Argsn: 1,
Doc: "Checks if a number is even.",
Expand Down Expand Up @@ -1345,10 +1345,10 @@ var builtins = map[string]*env.Builtin{
// Tests:
// equal { 1 + 1 } 2
// equal { 3 + 4 } 7
// equal { 5.6 + 7.8 } 13.4
// equal { 5.6 + 7.8 } 13.400000
// equal { "A" + "b" } "Ab"
// equal { "A" + 1 } "A1"
// equal { { 1 2 } { 3 4 } } { 1 2 3 4 }
// equal { { 1 2 } + { 3 4 } } { 1 2 3 4 }
"_+": { // **
Argsn: 2,
Doc: "Adds or joins two values together (Integers, Strings, Uri-s and Blocks)",
Expand Down Expand Up @@ -1960,7 +1960,28 @@ var builtins = map[string]*env.Builtin{
return obj
}
}

return arg0
},
},
"display\\custom": {
Argsn: 2,
Doc: "Work in progress Interactively displays a value.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
// This is temporary implementation for experimenting what it would work like at all
// later it should belong to the object (and the medium of display, terminal, html ..., it's part of the frontend)
term.SaveCurPos()
switch bloc := arg0.(type) {
case env.Spreadsheet:
obj, esc := term.DisplayTable(bloc, ps.Idx)
if !esc {
return obj
}
case *env.Spreadsheet:
obj, esc := term.DisplayTable(*bloc, ps.Idx)
if !esc {
return obj
}
}
return arg0
},
},
Expand Down Expand Up @@ -6412,7 +6433,7 @@ var builtins = map[string]*env.Builtin{
// Tests:
// equal { "abcde" .difference "cde" } "ab"
// equal { difference { 1 2 3 4 } { 2 4 } } { 1 3 }
// equal { difference list { "Bob" "Sal" "Joe" } list { "Joe" } } { "Bob" "Sal" }
// equal { difference list { "Bob" "Sal" "Joe" } list { "Joe" } } list { "Bob" "Sal" }
"difference": {
Argsn: 2,
Doc: "Finds the difference (values in first but not in second) of two values.",
Expand Down Expand Up @@ -6732,7 +6753,7 @@ var builtins = map[string]*env.Builtin{

// Tests:
// equal { split\many "192.0.0.1" "." } { "192" "0" "0" "1" }
// equal { split\many "abcde..e.q|bar" ".|" } { "abcd" "e" "q" "bar" }
// equal { split\many "abcd..e.q|bar" ".|" } { "abcd" "e" "q" "bar" }
// Args:
// * string
// * separator-set
Expand Down Expand Up @@ -6807,8 +6828,8 @@ var builtins = map[string]*env.Builtin{

// Tests:
// equal { first { 1 2 3 4 } } 1
// equal { rest "abcde" } "a"
// equal { rest list { 1 2 3 } } 1
// equal { first "abcde" } "a"
// equal { first list { 1 2 3 } } 1
"first": { // **
Argsn: 1,
Doc: "Accepts a Block, List or String and returns the first item.",
Expand Down Expand Up @@ -7025,7 +7046,7 @@ var builtins = map[string]*env.Builtin{
// Tests:
// equal { last { 1 2 } } 2
// equal { last "abcd" } "d"
// equal { list { 4 5 6 } } 6
// equal { last list { 4 5 6 } } 6
"last": { // **
Argsn: 1,
Doc: "Accepts a Block, List or String and returns the last value in it.",
Expand Down Expand Up @@ -7152,7 +7173,7 @@ var builtins = map[string]*env.Builtin{
},

// Tests:
// equal { dict { "a" 1 "b" 2 "c" 3 } |values } { 1 2 3 }
// equal { dict { "a" 1 "b" 2 "c" 3 } |values } list { 1 2 3 }
"values": { // **
Argsn: 1,
Doc: "Accepts a Dict and returns a List of just values.",
Expand Down Expand Up @@ -8150,7 +8171,8 @@ var builtins = map[string]*env.Builtin{
},
// Tests:
// equal { dict { "a" 1 "b" 2 "c" 3 } |keys } { "a" "b" "c" }
// equal { spreadsheet { 'a 'b 'c } { 1 2 3 } |keys } { 'a 'b 'c }
// equal { spreadsheet { "a" "b" "c" } { 1 2 3 } |keys } list { "a" "b" "c" }
// ; TODO -- doesn't work yet, .header? also has the same problem -- equal { spreadsheet { 'a 'b 'c } { 1 2 3 } |keys } { 'a 'b 'c }
"keys": {
Argsn: 1,
Doc: "Accepts Dict or Spreadsheet and returns a Block of keys or column names.", // TODO -- accept context also
Expand All @@ -8165,11 +8187,7 @@ var builtins = map[string]*env.Builtin{
}
return *env.NewBlock(*env.NewTSeries(keys))
case env.Spreadsheet:
keys := make([]env.Object, len(s1.Cols))
for i, k := range s1.Cols {
keys[i] = *env.NewString(k)
}
return *env.NewBlock(*env.NewTSeries(keys))
return s1.GetColumns()
default:
fmt.Println("Error")
}
Expand Down
38 changes: 0 additions & 38 deletions evaldo/builtins_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,44 +812,6 @@ var Builtins_math = map[string]*env.Builtin{
return *env.NewDecimal(fa * float64(math.Pi) / 180.0)
},
},
"is-near": {
Argsn: 2,
Doc: "Returns true if two decimals are close.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
fa, fb, errPos := assureFloats(arg0, arg1)
if errPos > 0 {
return MakeArgError(ps, errPos, []env.Type{env.IntegerType, env.BlockType}, "is-near")
}
const epsilon = 0.0000000000001 // math.SmallestNonzeroFloat64
if math.Abs(fa-fb) <= (epsilon) {
return env.NewInteger(1)
} else {
return env.NewInteger(0)
}
},
},
"near-zero": {
Argsn: 1,
Doc: "Returns true if a decimal is close to zero.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
var fa float64
switch a := arg0.(type) {
case env.Decimal:
fa = a.Value
case env.Integer:
fa = float64(a.Value)
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.BlockType}, "near-zero")
}
// const epsilon = math.SmallestNonzeroFloat64
const epsilon = 0.0000000000001 // math.SmallestNonzeroFloat64
if math.Abs(fa) <= epsilon {
return env.NewInteger(1)
} else {
return env.NewInteger(0)
}
},
},
"to-eyr": {
Argsn: 1,
Doc: "Math dialect to Eyr dialect",
Expand Down
Loading

0 comments on commit d7fe671

Please sign in to comment.