Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into feature/function_support
Browse files Browse the repository at this point in the history
  • Loading branch information
yumaikas committed Aug 10, 2024
2 parents 5d4d851 + 3aa2822 commit 4f361aa
Show file tree
Hide file tree
Showing 45 changed files with 778 additions and 659 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ docker run -ti refaktor/rye

### Building Rye from source

Use official documentation or lines below to install Golang 1.19.3 https://go.dev/doc/install (at the time of writing):
Use official documentation or lines below to install Golang 1.21.5 https://go.dev/doc/install (at the time of writing):

wget https://go.dev/dl/go1.21.5.linux-amd64.tar.gz
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.21.5.linux-amd64.tar.gz
Expand Down
75 changes: 60 additions & 15 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 @@ -2387,11 +2432,11 @@ var builtins = map[string]*env.Builtin{
return ps.Res
default:
ps.ErrorFlag = true
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "do-in")
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "do\\in")
}
default:
ps.ErrorFlag = true
return MakeArgError(ps, 1, []env.Type{env.CtxType}, "do-in")
return MakeArgError(ps, 1, []env.Type{env.CtxType}, "do\\in")
}

},
Expand Down Expand Up @@ -2420,11 +2465,11 @@ var builtins = map[string]*env.Builtin{
return ps.Res
default:
ps.ErrorFlag = true
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "do-in")
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "do\\in\\try")
}
default:
ps.ErrorFlag = true
return MakeArgError(ps, 1, []env.Type{env.CtxType}, "do-in")
return MakeArgError(ps, 1, []env.Type{env.CtxType}, "do\\in\\try")
}

},
Expand Down Expand Up @@ -2471,11 +2516,11 @@ var builtins = map[string]*env.Builtin{
return ps.Res
default:
ps.ErrorFlag = true
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "do-in")
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "do\\par")
}
default:
ps.ErrorFlag = true
return MakeArgError(ps, 1, []env.Type{env.CtxType}, "do-in")
return MakeArgError(ps, 1, []env.Type{env.CtxType}, "do\\par")
}

},
Expand Down Expand Up @@ -5064,7 +5109,7 @@ var builtins = map[string]*env.Builtin{
return ps.Res
}
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "recur-if\\1")
return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "recur-if")
}
},
},
Expand Down Expand Up @@ -6278,13 +6323,13 @@ var builtins = map[string]*env.Builtin{
case env.String:
return util.StringToFieldsWithQuoted(str.Value, sepa.Value, quote.Value)
default:
return MakeArgError(ps, 3, []env.Type{env.StringType}, "split-quoted")
return MakeArgError(ps, 3, []env.Type{env.StringType}, "split\\quoted")
}
default:
return MakeArgError(ps, 2, []env.Type{env.StringType}, "split-quoted")
return MakeArgError(ps, 2, []env.Type{env.StringType}, "split\\quoted")
}
default:
return MakeArgError(ps, 1, []env.Type{env.StringType}, "split-quoted")
return MakeArgError(ps, 1, []env.Type{env.StringType}, "split\\quoted")
}
},
},
Expand Down Expand Up @@ -6329,7 +6374,7 @@ var builtins = map[string]*env.Builtin{
}
return *env.NewBlock(*env.NewTSeries(spl2))
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType}, "split-every")
return MakeArgError(ps, 2, []env.Type{env.IntegerType}, "split\\every")
}
case env.Block:
switch sepa := arg1.(type) {
Expand All @@ -6341,10 +6386,10 @@ var builtins = map[string]*env.Builtin{
}
return *env.NewBlock(*env.NewTSeries(spl2))
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType}, "split-every")
return MakeArgError(ps, 2, []env.Type{env.IntegerType}, "split\\every")
}
default:
return MakeArgError(ps, 1, []env.Type{env.StringType, env.BlockType}, "split-every")
return MakeArgError(ps, 1, []env.Type{env.StringType, env.BlockType}, "split\\every")
}
},
},
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
6 changes: 3 additions & 3 deletions evaldo/builtins_eyr.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ var Builtins_eyr = map[string]*env.Builtin{
ps.Ser = ser
return ps.Res
default:
return MakeArgError(ps, 1, []env.Type{env.BlockType}, "eyr")
return MakeArgError(ps, 1, []env.Type{env.BlockType}, "eyr\\full")
}
},
},
Expand All @@ -323,10 +323,10 @@ var Builtins_eyr = map[string]*env.Builtin{
ps.Ser = ser
return ps.Res
default:
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "eyr-loop")
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "eyr\\loop")
}
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "eyr-loop")
return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "eyr\\loop")
}
},
},
Expand Down
4 changes: 2 additions & 2 deletions evaldo/builtins_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ var Builtins_math = map[string]*env.Builtin{
},
"near-zero": {
Argsn: 1,
Doc: "Returns true if two decimals are close.",
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) {
Expand All @@ -823,7 +823,7 @@ var Builtins_math = map[string]*env.Builtin{
case env.Integer:
fa = float64(a.Value)
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.BlockType}, "is-zero")
return MakeArgError(ps, 1, []env.Type{env.IntegerType, env.BlockType}, "near-zero")
}
// const epsilon = math.SmallestNonzeroFloat64
const epsilon = 0.0000000000001 // math.SmallestNonzeroFloat64
Expand Down
2 changes: 1 addition & 1 deletion evaldo/builtins_smtpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

var Builtins_smtpd = map[string]*env.Builtin{

"new-smtpd": {
"smtp-server": {
Argsn: 1,
Doc: "Creates SMTP server",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
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
25 changes: 9 additions & 16 deletions examples/adventofcode/2022/3/main.rye
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,18 @@

read\lines %rucksacks.txt :lines
|fold 'priority 0 {
.length? / 2 :mid ,
.split-every mid |pass { .first :left } |second
|intersect left |fold 'priority1 0 {
.length? / 2 |to-integer ::mid ,
.split\every mid |pass { .first ::left } |second
|intersection left |unique |fold 'priority1 0 {
.get-priority + priority1
} |+ priority
} |+ priority
} |print

; part 2

lines .split-every 3 |fold 'priority 0 {
-> 0 :line0 ,
-> 1 :line1 ,
-> 2 |intersect line1 |intersect line0
|get-priority + priority
lines .split\every 3 |fold 'priority 0 {
-> 0 ::line0 ,
-> 1 ::line1 ,
-> 2 |intersection line1 |intersection line0
|unique |get-priority + priority
} |print







24 changes: 12 additions & 12 deletions examples/adventofcode/2022/3/main2.rye
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@

; part 1

read\lines %rucksacks.txt :lines |add-up {
.length? / 2 :mid ,
.split-every mid
|with { .first :left , .second }
|intersect left |add-up { .get-priority }
} |print
read\lines %rucksacks.txt :lines |map {
.length? / 2 |to-integer ::mid ,
.split\every mid
|with { .first ::left , .second }
|intersection left |unique .map { .get-priority } |sum
} |sum |print

; part 2

lines .split-every 3 |add-up {
-> 0 :line0 ,
-> 1 :line1 ,
-> 2 |intersect line1 |intersect line0
|get-priority
} |print
lines .split\every 3 |map {
-> 0 ::line0 ,
-> 1 ::line1 ,
-> 2 |intersection line1 |intersection line0
|unique .get-priority
} |sum |print



Expand Down
2 changes: 1 addition & 1 deletion examples/examples-wip/main_0.6.rye
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ handle-api: fn { w r } {
}
}

new-server ":8080"
http-server ":8080"
|handle "/api" ?handle-api
|serve
2 changes: 1 addition & 1 deletion examples/examples-wip/websockets_chat.rye
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ hub: fn { e } {

go-with event ?hub

new-server ":8080"
http-server ":8080"
|handle-ws "/chat" ?handle-chat
|serve
2 changes: 1 addition & 1 deletion examples/examples-wip/websockets_chat_A.rye
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ hub: fn { e } {

go-with event ?hub

new-server ":8080"
http-server ":8080"
|handle-ws "/chat" ?handle-chat
|serve
2 changes: 1 addition & 1 deletion examples/examples-wip/websockets_chat_b.rye
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ hub: fn { e } {

go-with event ?hub

new-server ":8080"
http-server ":8080"
|handle-ws "/chat" ?handle-chat
|serve
2 changes: 1 addition & 1 deletion examples/examples-wip/websockets_chat_c.rye
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ hub: fn { ch } {

go-with events ?hub

new-server ":8080"
http-server ":8080"
|handle-ws "/chat" ?handle-chat
|serve
Loading

0 comments on commit 4f361aa

Please sign in to comment.