Skip to content

Commit

Permalink
improved cli argument handling, help. improved eyr dialect
Browse files Browse the repository at this point in the history
  • Loading branch information
refaktor committed Mar 17, 2024
1 parent acef6b0 commit 270760e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
6 changes: 3 additions & 3 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ var builtins = map[string]*env.Builtin{
ps.FailureFlag = true
return MakeBuiltinError(ps, "Can't divide by Zero.", "_/")
}
return *env.NewInteger(a.Value / b.Value)
return *env.NewDecimal(float64(a.Value) / float64(b.Value))
case env.Decimal:
if b.Value == 0.0 {
ps.FailureFlag = true
Expand Down Expand Up @@ -6873,9 +6873,9 @@ var builtins = map[string]*env.Builtin{
case env.Vector:
return *env.NewInteger(int64(s1.Value.Len()))
default:
fmt.Println("Error")
fmt.Println(s1)
return MakeArgError(ps, 2, []env.Type{env.StringType, env.DictType, env.ListType, env.BlockType, env.SpreadsheetType, env.VectorType}, "range")
}
return nil
},
},
"ncols": {
Expand Down
15 changes: 10 additions & 5 deletions evaldo/builtins_eyr.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (s *EyrStack) Pop() env.Object {
}

func Eyr_CallBuiltin(bi env.Builtin, ps *env.ProgramState, arg0_ env.Object, toLeft bool, stack *EyrStack) *env.ProgramState {
arg0 := bi.Cur0 //env.Object(bi.Cur0)
arg1 := bi.Cur1
arg0 := bi.Cur0 //env.Object(bi.Cur0)
var arg1 env.Object // := bi.Cur1

if bi.Argsn > 0 && bi.Cur0 == nil {
//fmt.Println(" ARG 1 ")
Expand All @@ -71,6 +71,10 @@ func Eyr_CallBuiltin(bi env.Builtin, ps *env.ProgramState, arg0_ env.Object, toL
return ps
}
arg0 = stack.Pop()
if bi.Argsn == 1 {
ps.Res = bi.Fn(ps, arg0, nil, nil, nil, nil)
stack.Push(ps.Res)
}
}
if bi.Argsn > 1 && bi.Cur1 == nil {
//evalExprFn(ps, true) // <---- THESE DETERMINE IF IT CONSUMES WHOLE EXPRESSION OR NOT IN CASE OF PIPEWORDS .. HM*... MAYBE WOULD COULD HAVE A WORD MODIFIER?? a: 2 |add 5 a:: 2 |add 5 print* --TODO
Expand All @@ -83,10 +87,11 @@ func Eyr_CallBuiltin(bi env.Builtin, ps *env.ProgramState, arg0_ env.Object, toL
//fmt.Println(ps.Res)

arg1 = stack.Pop()
if bi.Argsn == 2 {
ps.Res = bi.Fn(ps, arg0, arg1, nil, nil, nil)
stack.Push(ps.Res)
}
}
ps.Res = bi.Fn(ps, arg1, arg0, nil, nil, nil)

stack.Push(ps.Res)
return ps
}

Expand Down
73 changes: 71 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"flag"
"regexp"
"sort"

Expand Down Expand Up @@ -47,7 +48,75 @@ var CODE []any
// main function. Dispatches to appropriate mode function
//

// NEW FLASGS HANDLING

var (
// fileName = flag.String("file", "", "Path to the Rye file (default: none)")
do = flag.String("do", "", "Evaluates code after it loads a file or last save.")
silent = flag.Bool("silent", false, "Console doesn't display return values")
// quit = flag.Bool("quit", false, "Quits after executing.")
console = flag.Bool("console", false, "Enters console after a file is evaluated.")
help = flag.Bool("help", false, "Displays this help message.")
)

func main() {
flag.Usage = func() {
fmt.Println("╭────────────────────────────────────────────────────────────────╮")
fmt.Println("│ \033[1mRye\033[0m language. Visit \033[36mhttps://ryelang.org\033[0m to find out more. │")
fmt.Println("╰────────────────────────────────────────────────────────────────╯")
fmt.Println("\n Usage: \033[1mrye\033[0m [\033[1mfilename\033[0m or \033[1mcommand\033[0m] [\033[1moptions\033[0m]")
fmt.Println("\n To enter \033[1mRye console\033[0m provide no filename or command.")
fmt.Println("\n \033[1mFilename:\033[0m (optional)")
fmt.Println(" [filename] \n Executes a Rye file")
fmt.Println(" . \n Executes a main.rye in current directory")
fmt.Println(" [some/path]/.\n Executes a main.rye on some path")
fmt.Println("\n \033[1mCommands:\033[0m (optional)")
fmt.Println(" cont\n Continue console from the last save")
fmt.Println(" here\n Starts in Rye here mode")
fmt.Println("\n \033[1mOptions\033[0m (optional)")
flag.PrintDefaults()
fmt.Println("")
}
// Parse flags
flag.Parse()

evaldo.ShowResults = !*silent

// Check for --help flag
if flag.NFlag() == 0 && flag.NArg() == 0 {
main_rye_repl(os.Stdin, os.Stdout, true, false)
} else {
// Check for --help flag
if *help {
flag.Usage()
os.Exit(0)
}

// Check for subcommands (cont) and handle them
if flag.NArg() > 0 {
if os.Args[1] == "cont" {
fmt.Println("CONT")
var code string
if *do != "" {
code = *do
}
ryeFile := findLastConsoleSave()
main_rye_file(ryeFile, false, true, true, code)
} else if os.Args[1] == "here" {
main_rye_repl(os.Stdin, os.Stdout, true, true)
} else {
ryeFile := dotsToMainRye(os.Args[1])
main_rye_file(ryeFile, false, true, *console, "")
}
} else {
main_rye_repl(os.Stdin, os.Stdout, true, false)
}
}
}

// END OF NEW FLAGS

func main_OLD() {
evaldo.ShowResults = true

// TODO -- this is still handled totally ad-hoc because we are experimenting, but slowly it should
Expand All @@ -60,7 +129,7 @@ func main() {
main_rysh()
} else if os.Args[1] == "here" {
main_rye_repl(os.Stdin, os.Stdout, true, true)
} else if os.Args[1] == "--hr" {
} else if os.Args[1] == "--silent" {
evaldo.ShowResults = false
main_rye_repl(os.Stdin, os.Stdout, true, false)
} else if os.Args[1] == "cont" {
Expand All @@ -73,7 +142,7 @@ func main() {
} else if len(os.Args) >= 3 {
if os.Args[1] == "ryk" {
main_ryk()
} else if os.Args[1] == "--hr" {
} else if os.Args[1] == "--silent" {
ryeFile := dotsToMainRye(os.Args[2])
evaldo.ShowResults = false
main_rye_file(ryeFile, false, true, false, "")
Expand Down
2 changes: 1 addition & 1 deletion tests/misc.rye
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ section "Failure handling functions"
{ }
{
equal { 100 / 0 |fix { 99 } |+ 1 } 100
equal { 100 / 1 |fix { 99 } |+ 1 } 101
equal { 100 / 1 |fix { 99 } |+ 1 } 101.0000
}

group "assert-equal"
Expand Down

0 comments on commit 270760e

Please sign in to comment.