Skip to content

Commit

Permalink
Merge pull request #413 from refaktor/multiliner
Browse files Browse the repository at this point in the history
updates to rbit tool, info / tests tool
  • Loading branch information
refaktor authored Nov 19, 2024
2 parents 8cd5d47 + 84545f2 commit 703a04b
Show file tree
Hide file tree
Showing 12 changed files with 885 additions and 470 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ console_*.rye.enc

buildtemp/main.rye
*.bson
cmd/parse_builtins/parse_builtins
cmd/parse_builtins/parse_builtinscmd/rbit/rbit
1 change: 1 addition & 0 deletions cmd/rbit/build
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go build
67 changes: 60 additions & 7 deletions cmd/parse_builtins/main.go → cmd/rbit/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"fmt"
"go/ast"
"go/parser"
Expand Down Expand Up @@ -119,18 +120,66 @@ func outputInfo(infos []builtinInfo) {
fmt.Println("}\n")
}

func outputStats(cnt counters) {
fmt.Println("stats {\n") // name
fmt.Printf("\tfunctions \t%d\n", cnt.functions)
fmt.Printf("\ttested-functions\t%d\n", cnt.tested_functions)
fmt.Printf("\ttests \t%d\n", cnt.tests)
fmt.Printf("\texamples \t%d\n", cnt.examples)
fmt.Printf("\n")
fmt.Printf("\ttest-coverage \t%.1f%%\n", 100*float64(cnt.tested_functions)/float64(cnt.functions))
fmt.Printf("\ttests-per-func \t%.1f\n", float64(cnt.tests)/float64(cnt.tested_functions))
fmt.Println("}\n")
}

var (
// fileName = flag.String("fiimle", "", "Path to the Rye file (default: none)")
stats = flag.Bool("stats", false, "Show stats about builtins file")
ls = flag.Bool("ls", false, "List builtins files")
help = flag.Bool("help", false, "Displays this help message.")
)

func main() {

flag.Usage = func() {
fmt.Println("╭────────────────────────────────────────────────────────────────────────────────────────────---")
fmt.Println("│ \033[1mrbit - rye builtin info tool")
fmt.Println("╰───────────────────────────────────────────────────────────────────────────────────────---")
fmt.Println("\n Usage: \033[1mparse\033[0m [\033[1moptions\033[0m] [\033[1mfilename\033[0m or \033[1mcommand\033[0m]")
flag.PrintDefaults()
fmt.Println("\033[33m rbit \033[36m# shows helo")
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[0m\n Thank you for trying out \033[1mRye\033[22m ...")
fmt.Println("")
}
// Parse flags
flag.Parse()
args := flag.Args()

if flag.NFlag() == 0 && flag.NArg() == 0 {
flag.Usage()
os.Exit(0)
} else if *ls {
fmt.Println("TODO 1")
} else {
doParsing(args)
}
// asd
}

func doParsing(args []string) {
/// ###

infoList := make([]builtinInfo, 0)

// Check if a filename is provided as an argument
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <filename>")
if len(args) < 1 {
fmt.Println("File argument missing")
return
}

// Get the filename from the first argument
filename := os.Args[1]
filename := args[0]

// Create a new token file set
fset := token.NewFileSet()
Expand All @@ -157,6 +206,7 @@ func main() {
if key, ok := kv.Key.(*ast.BasicLit); ok {
// Extract the key

c.functions = c.functions + 1
/// fmt.Printf("Key: %s\n", key.Value)
// TODO NEXT - parse key into two values
info.name = key.Value
Expand All @@ -165,7 +215,6 @@ func main() {
if comment != "" {
/// fmt.Printf("Comment above key: %s\n", strings.TrimSpace(comment))
info = parseCommentsAboveKey(comment, &info)
c.functions = c.functions + 1
if len(info.tests) > 0 {
c.tested_functions = c.tested_functions + 1
c.tests = c.tests + len(info.tests)
Expand All @@ -184,7 +233,11 @@ func main() {

// fmt.Println("===================================================")

outputInfo(infoList)
if *stats {
outputStats(c)
} else {
outputInfo(infoList)
}

// fmt.Println("===================================================")

Expand Down
131 changes: 7 additions & 124 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,9 +907,9 @@ var builtins = map[string]*env.Builtin{
},
},

"unset!!": { // ***
"unset!": { // ***
Argsn: 1,
Doc: "Unset a word in current context, only meant to be used in console.",
Doc: "Unset a word in current context, only meant to be used in console",
Pure: false,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch word := arg0.(type) {
Expand Down Expand Up @@ -977,7 +977,7 @@ var builtins = map[string]*env.Builtin{
},
},

"get_": { // *** find a name or decide on order of naming with generic words clashes with
"pick": {
Argsn: 1,
Doc: "Returns value of the word in context",
Pure: true,
Expand Down Expand Up @@ -1107,7 +1107,7 @@ var builtins = map[string]*env.Builtin{
// VALUES

// Tests:
// equal { dict { "a" 123 } -> "b" } 123
// equal { dict { "a" 123 } -> "a" } 123
"dict": { // ***
Argsn: 1,
Doc: "Constructs a Dict from the Block of key and value pairs.",
Expand Down Expand Up @@ -6412,7 +6412,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" } { "Joe" } } { "Bob" "Sal" }
// equal { difference list { "Bob" "Sal" "Joe" } list { "Joe" } } { "Bob" "Sal" }
"difference": {
Argsn: 2,
Doc: "Finds the difference (values in first but not in second) of two values.",
Expand Down Expand Up @@ -8087,8 +8087,8 @@ var builtins = map[string]*env.Builtin{
},
// Tests:
// equal { { } .is-empty } 1
// equal { dict .is-empty } 1
// equal { spreadsheet { } { } .is-empty } 1
// equal { dict { } |is-empty } 1
// equal { spreadsheet { } { } |is-empty } 1
"is-empty": { // **
Argsn: 1,
Doc: "Accepts a collection (String, Block, Dict, Spreadsheet) and returns it's length.", // TODO -- accept context
Expand Down Expand Up @@ -8148,23 +8148,6 @@ var builtins = map[string]*env.Builtin{
}
},
},
// Args:
// * sheet
"ncols": {
Doc: "Accepts a Spreadsheet and returns number of columns.",
Argsn: 1,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s1 := arg0.(type) {
case env.Dict:
case env.Block:
case env.Spreadsheet:
return *env.NewInteger(int64(len(s1.Cols)))
default:
fmt.Println("Error")
}
return nil
},
},
// 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 }
Expand Down Expand Up @@ -8194,106 +8177,6 @@ var builtins = map[string]*env.Builtin{
},
},

// Tests:
// equal { spreadsheet { 'a } { 1 2 3 } .col-sum 'a } 6
"col-sum": {
Argsn: 2,
Doc: "Accepts a spreadsheet and a column name and returns a sum of a column.", // TODO -- let it accept a block and list also
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
var name string
switch s1 := arg0.(type) {
case env.Spreadsheet:
switch s2 := arg1.(type) {
case env.Word:
name = ps.Idx.GetWord(s2.Index)
case env.String:
name = s2.Value
default:
ps.ErrorFlag = true
return env.NewError("second arg not string")
}
r := s1.Sum(name)
if r.Type() == env.ErrorType {
ps.ErrorFlag = true
}
return r

default:
ps.ErrorFlag = true
return env.NewError("first arg not spreadsheet")
}
},
},

// Tests:
// equal { spreadsheet { 'a } { 1 2 3 } .col-avg 'a } 2.
"col-avg": {
Argsn: 2,
Doc: "Accepts a spreadsheet and a column name and returns a sum of a column.", // TODO -- let it accept a block and list also
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
var name string
switch s1 := arg0.(type) {
case env.Spreadsheet:
switch s2 := arg1.(type) {
case env.Word:
name = ps.Idx.GetWord(s2.Index)
case env.String:
name = s2.Value
default:
ps.ErrorFlag = true
return env.NewError("second arg not string")
}
r, err := s1.Sum_Just(name)
if err != nil {
ps.ErrorFlag = true
return env.NewError(err.Error())
}
n := s1.NRows()
return *env.NewDecimal(r / float64(n))

default:
ps.ErrorFlag = true
return env.NewError("first arg not spreadsheet")
}
},
},

// Tests:
// equal { spreadsheet { 'a } { 1 2 3 } .A1 } 1

"A1": {
Argsn: 1,
Doc: "Accepts a Spreadsheet and returns the first row first column cell.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s0 := arg0.(type) {
case env.Spreadsheet:
r := s0.Rows[0].Values[0]
return env.ToRyeValue(r)

default:
ps.ErrorFlag = true
return env.NewError("first arg not spreadsheet")
}
},
},
// Tests:
// equal { spreadsheet { 'a } { 1 2 3 } .B1 } 2
"B1": {
Argsn: 1,
Doc: "Accepts a Spreadsheet and returns the first row second column cell.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s0 := arg0.(type) {
case env.Spreadsheet:
r := s0.Rows[0].Values[1]
return env.ToRyeValue(r)

default:
ps.ErrorFlag = true
return env.NewError("first arg not spreadsheet")
}
},
},

/* Terminal functions .. move to it's own later */

// Tests:
Expand Down
Loading

0 comments on commit 703a04b

Please sign in to comment.