Skip to content

Commit

Permalink
Merge pull request #307 from refaktor/allin
Browse files Browse the repository at this point in the history
fixed the ctrl-c (sleep/serve) issue https://github.com/refaktor/rye/…
  • Loading branch information
refaktor committed Aug 10, 2024
2 parents 3b92ce3 + 4b775e5 commit 3aa2822
Show file tree
Hide file tree
Showing 9 changed files with 702 additions and 576 deletions.
49 changes: 47 additions & 2 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ var builtins = map[string]*env.Builtin{
},
"_/": { // **
Argsn: 2,
Doc: "Divided two numbers.",
Doc: "Divide two numbers and return a decimal.",
Pure: true,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch a := arg0.(type) {
Expand Down Expand Up @@ -1305,6 +1305,51 @@ var builtins = map[string]*env.Builtin{
}
},
},
"_//": { // **
Argsn: 2,
Doc: "Divides two numbers and return truncated integer.",
Pure: true,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch a := arg0.(type) {
case env.Integer:
switch b := arg1.(type) {
case env.Integer:
if b.Value == 0 {
ps.FailureFlag = true
return MakeBuiltinError(ps, "Can't divide by Zero.", "_/")
}
return *env.NewInteger(a.Value / b.Value)
case env.Decimal:
if b.Value == 0.0 {
ps.FailureFlag = true
return MakeBuiltinError(ps, "Can't divide by Zero.", "_/")
}
return *env.NewInteger(a.Value / int64(b.Value))
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "_/")
}
case env.Decimal:
switch b := arg1.(type) {
case env.Integer:
if b.Value == 0 {
ps.FailureFlag = true
return MakeBuiltinError(ps, "Can't divide by Zero.", "_/")
}
return *env.NewInteger(int64(a.Value) / b.Value)
case env.Decimal:
if b.Value == 0.0 {
ps.FailureFlag = true
return MakeBuiltinError(ps, "Can't divide by Zero.", "_/")
}
return *env.NewInteger(int64(a.Value) / int64(b.Value))
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "_/")
}
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.DecimalType}, "_/")
}
},
},
"_=": { // ***
Argsn: 2,
Doc: "Checks if two Rye values are equal.",
Expand Down Expand Up @@ -7842,7 +7887,7 @@ func registerBuiltin(ps *env.ProgramState, word string, builtin env.Builtin) {
// in future a map will probably not be a map but an array and builtin will also support the Kind value

idxk := 0
if strings.Index(word, "//") > 0 {
if word != "_//" && strings.Index(word, "//") > 0 {
temp := strings.Split(word, "//")
word = temp[1]
idxk = ps.Idx.IndexWord(temp[0])
Expand Down
30 changes: 25 additions & 5 deletions evaldo/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ func DoRyeRepl(es *env.ProgramState, dialect string, showResults bool) { // here
fmt.Println(err)
return
}
defer keyboard.Close()

c := make(chan util.KeyEvent)
r := Repl{
Expand All @@ -332,31 +331,52 @@ func DoRyeRepl(es *env.ProgramState, dialect string, showResults bool) { // here
ml := util.NewMicroLiner(c, r.recieveMessage, r.recieveLine)
r.ml = ml

ctx := context.Background()
defer ctx.Done()
ctx, cancel := context.WithCancel(context.Background())

defer cancel()

// ctx := context.Background()
// defer os.Exit(0)
// defer ctx.Done()
defer keyboard.Close()
go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
// fmt.Println("Done")
return
default:
// fmt.Println("Select default")
r, k, keyErr := keyboard.GetKey()
if err != nil {
fmt.Println(keyErr)
break
}
if k == keyboard.KeyCtrlC {
ctx.Done()
// fmt.Println("Ctrl C 1")
cancel()
err1 := util.KillProcess(os.Getpid())
// err1 := syscall.Kill(os.Getpid(), syscall.SIGINT)
if err1 != nil {
fmt.Println(err.Error()) // TODO -- temprorary just printed
}
//ctx.Done()
// fmt.Println("")
// return
//break
// os.Exit(0)
}
c <- constructKeyEvent(r, k)
}
}
}(ctx)

_, err = ml.MicroPrompt("x> ", "", 0)
// fmt.Println("MICRO")
_, err = ml.MicroPrompt("x> ", "", 0, ctx)
if err != nil {
fmt.Println(err)
}
// fmt.Println("END")
}

/* THIS WAS DISABLED TEMP FOR WASM MODE .. 20250116 func DoGeneralInput(es *env.ProgramState, prompt string) {
Expand Down
4 changes: 2 additions & 2 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func parseOpword(v *Values, d Any) (Any, error) {
word := v.Token()
force := 0
var idx int
if len(word) == 1 || word == "<<" || word == "<-" || word == "<~" || word == ">=" || word == "<=" {
if len(word) == 1 || word == "<<" || word == "<-" || word == "<~" || word == ">=" || word == "<=" || word == "//" {
// onecharopwords < > + * ... their naming is equal to _< _> _* ...
idx = wordIndex.IndexWord("_" + word)
} else {
Expand Down Expand Up @@ -467,7 +467,7 @@ func newParser() *Parser { // TODO -- add string eaddress path url time
ONECHARWORDS <- < [<>*+-=/] >
NORMOPWORDS <- < ("_"[<>*+-=/]) >
PIPEARROWS <- ">>" / "~>" / "->"
OPARROWS <- "<<" / "<~" / "<-" / ">=" / "<="
OPARROWS <- "<<" / "<~" / "<-" / ">=" / "<=" / "//"
LETTER <- < [a-zA-Z^(` + "`" + `] >
LETTERORNUM <- < [a-zA-Z0-9-?=.\\!_+<>\]*()] >
LETTERORNUMNOX <- < [a-zA-Z0-9-?=.\\!_+\]*()] >
Expand Down
5 changes: 4 additions & 1 deletion main_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package main

import (
"context"
"fmt"
"regexp"
"strings"
Expand Down Expand Up @@ -99,7 +100,9 @@ func main() {
jsCallback = js.Global().Get("receiveMessageFromGo")
jsCallback2 = js.Global().Get("receiveLineFromGo")

ml.MicroPrompt("x> ", "", 0)
ctx := context.Background()

ml.MicroPrompt("x> ", "", 0, ctx)

/* for {
key := <-c
Expand Down
12 changes: 12 additions & 0 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,18 @@ func main_rye_repl(_ io.Reader, _ io.Writer, subc bool, here bool, lang string,
}
}

// c := make(chan os.Signal, 1)
// signal.Notify(c, os.Interrupt, syscall.SIGTERM)

//go func() {
// sig := <-c
// fmt.Println()
// fmt.Println("Captured signal:", sig)
// Perform cleanup or other actions here
// os.Exit(0)
//}()
//fmt.Println("Waiting for signal")

evaldo.DoRyeRepl(es, lang, evaldo.ShowResults)
}

Expand Down
15 changes: 12 additions & 3 deletions tests/basics.rye
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,23 @@ section "Working with numbers"
; TODO
}

`
group "+-*/"

group "+-*/ //"
""
{ { integer decimal } { integer decimal } }
{
}
equal { 5 / 2 } 2.5
equal { 5.0 / 2 } 2.5
equal { 5 / 2.0 } 2.5
equal { 5.0 / 2.0 } 2.5

equal { 5 // 2 } 2
equal { 5.0 // 2 } 2
equal { 5 // 2.0 } 2
equal { 5.0 // 2.0 } 2
}

`
group "!=><+"
""
{ { integer decimal } { integer decimal } }
Expand Down
Loading

0 comments on commit 3aa2822

Please sign in to comment.