Skip to content

Commit

Permalink
improvements to new loader, wasm main and some builtins
Browse files Browse the repository at this point in the history
  • Loading branch information
refaktor committed Feb 12, 2024
1 parent a77732e commit 0edfcf1
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
71 changes: 71 additions & 0 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -4372,6 +4372,61 @@ var builtins = map[string]*env.Builtin{
},
},

"mul": { // **
Argsn: 1,
Doc: "Accepts a Block or List of values and returns the sum.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
var sum float64 = 1
switch block := arg0.(type) {
case env.Block:
l := block.Series.Len()
onlyInts := true
for i := 0; i < l; i++ {
obj := block.Series.Get(i)
switch val1 := obj.(type) {
case env.Integer:
sum *= float64(val1.Value)
case env.Decimal:
sum *= val1.Value
onlyInts = false
default:
return MakeBuiltinError(ps, "Block type should be Integer or Decimal.", "sum")
}
}
if onlyInts {
return *env.NewInteger(int64(sum))
} else {
return *env.NewDecimal(sum)
}
case env.List:
l := len(block.Data)
onlyInts := true
for i := 0; i < l; i++ {
obj := block.Data[i]
switch val1 := obj.(type) {
case int64:
sum *= float64(val1)
case float64:
sum *= val1
onlyInts = false
default:
return MakeBuiltinError(ps, "List type should be Integer or Decimal.", "sum")
}
}
if onlyInts {
return *env.NewInteger(int64(sum))
} else {
return *env.NewDecimal(sum)
}

case env.Vector:
return *env.NewDecimal(block.Value.Sum())
default:
return MakeArgError(ps, 1, []env.Type{env.BlockType, env.VectorType}, "sum")
}
},
},

"sort!": { // **
Argsn: 1,
Doc: "Accepts a block of values and returns maximal value.",
Expand Down Expand Up @@ -4470,6 +4525,22 @@ var builtins = map[string]*env.Builtin{

// end of collections exploration

"recur-if": { //recur1-if
Argsn: 2,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch cond := arg0.(type) {
case env.Integer:
if cond.Value > 0 {
ps.Ser.Reset()
return nil
} else {
return ps.Res
}
default:
return MakeArgError(ps, 1, []env.Type{env.IntegerType}, "recur-if\\1")
}
},
},
//test if we can do recur similar to clojure one. Since functions in rejy are of fixed arity we would need recur1 recur2 recur3 and recur [ ] which is less optimal
//otherwise word recur could somehow be bound to correct version or args depending on number of args of func. Try this at first.
"recur-if\\1": { //recur1-if
Expand Down
7 changes: 6 additions & 1 deletion main_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package main

import (
"fmt"
"regexp"
"strings"
"syscall/js"

"github.com/refaktor/rye/contrib"
Expand Down Expand Up @@ -141,13 +143,16 @@ func RyeEvalShellLine(this js.Value, args []js.Value) any {
subc := false

code := args[0].String()
comment := regexp.MustCompile(`\s*;`)
codes := comment.Split(code, 2) //--- just very temporary solution for some comments in repl. Later should probably be part of loader ... maybe?
code1 := strings.Trim(codes[0], "\t")

if ES == nil {
return "Error: Rye is not initialized"
}

ps := ES
block := loader.LoadStringNEW(" "+code+" ", sig, ps)
block := loader.LoadStringNEW(" "+code1+" ", sig, ps)
switch val := block.(type) {
case env.Block:

Expand Down

0 comments on commit 0edfcf1

Please sign in to comment.