Skip to content

Commit

Permalink
Merge pull request #233 from refaktor/modwords
Browse files Browse the repository at this point in the history
improvements around contexts and math context
  • Loading branch information
refaktor authored Jun 3, 2024
2 parents 2a781b5 + 782eb78 commit 2fd2e4d
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 8 deletions.
2 changes: 1 addition & 1 deletion env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ type LiveEnv struct {
func NewLiveEnv() *LiveEnv {
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Println("Error creating watcher:", err)
// fmt.Println("Error creating watcher:", err)
return nil
}
// defer watcher.Close()
Expand Down
39 changes: 34 additions & 5 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2422,6 +2422,34 @@ var builtins = map[string]*env.Builtin{
},
},

"do\\par": { // **
Argsn: 2,
Doc: "Takes a Context and a Block. It Does a block in current context but with parent a given Context.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch ctx := arg0.(type) {
case env.RyeCtx:
switch bloc := arg1.(type) {
case env.Block:
ser := ps.Ser
ps.Ser = bloc.Series
temp := ps.Ctx.Parent
ps.Ctx.Parent = &ctx
EvalBlock(ps)
ps.Ctx.Parent = temp
ps.Ser = ser
return ps.Res
default:
ps.ErrorFlag = true
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "do-in")
}
default:
ps.ErrorFlag = true
return MakeArgError(ps, 1, []env.Type{env.CtxType}, "do-in")
}

},
},

"capture-stdout": { // **
Argsn: 1,
Doc: "Takes a block of code and does (runs) it.",
Expand Down Expand Up @@ -2705,23 +2733,23 @@ var builtins = map[string]*env.Builtin{

// CONTEXT

"current-ctx": { // **
"current-ctx": { // ** deprecated ... current from now on
Argsn: 0,
Doc: "Returns current context.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
return *ps.Ctx
},
},

"current-ctx!!": { // **
"current": { // **
Argsn: 0,
Doc: "Returns current context.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
return ps.Ctx
},
},

"parent-ctx": { // **
"parent": { // **
Argsn: 0,
Doc: "Returns parent context of the current context.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand Down Expand Up @@ -2934,7 +2962,7 @@ var builtins = map[string]*env.Builtin{
},
},

"extend": { // ** exclamation mark, because it as it is now extends/changes the source context too .. in place
"extends": { // ** add one with exclamation mark, which it as it is now extends/changes the source context too .. in place
Argsn: 2,
Doc: "Extends a context with a new context in place and returns it.",
Pure: false,
Expand All @@ -2946,7 +2974,8 @@ var builtins = map[string]*env.Builtin{
ser := ps.Ser
ctx := ps.Ctx
ps.Ser = bloc.Series
ps.Ctx = ctx0.Copy() // make new context with no parent
// ps.Ctx = ctx0.Copy() // make new context with no parent
ps.Ctx = env.NewEnv(&ctx0) // make new context with no parent
EvalBlock(ps)
rctx := ps.Ctx
ps.Ctx = ctx
Expand Down
55 changes: 55 additions & 0 deletions evaldo/builtins_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@ var Builtins_math = map[string]*env.Builtin{
return *env.NewDecimal(math.Mod(fa, fb))
},
},
"pow": {
Argsn: 2,
Doc: "Return the power of",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
fa, fb, errPos := assureFloats(arg0, arg1)
if errPos > 0 {
return MakeArgError(ps, errPos, []env.Type{env.IntegerType, env.BlockType}, "mod")
}
return *env.NewDecimal(math.Pow(fa, fb))
},
},
"log2": {
Argsn: 1,
Doc: "Return binary logarithm of x",
Expand All @@ -128,6 +139,20 @@ var Builtins_math = map[string]*env.Builtin{
}
},
},
"sq": {
Argsn: 1,
Doc: "Return the sine of the radian argument.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch val := arg0.(type) {
case env.Integer:
return *env.NewDecimal(math.Pow(float64(val.Value), 2.0))
case env.Decimal:
return *env.NewDecimal(math.Pow(val.Value, 2.0))
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.BlockType}, "mod")
}
},
},
"sin": {
Argsn: 1,
Doc: "Return the sine of the radian argument.",
Expand Down Expand Up @@ -398,6 +423,36 @@ var Builtins_math = map[string]*env.Builtin{
}
},
},
"round\\to": {
Argsn: 2,
Doc: "Round to a number of digits.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch val := arg0.(type) {
case env.Decimal:
switch precision := arg1.(type) {
case env.Integer:
ratio := math.Pow(10, float64(precision.Value))
return env.NewDecimal(math.Round(val.Value*ratio) / ratio)
default:
return MakeArgError(ps, 2, []env.Type{env.IntegerType, env.DecimalType}, "dim")
}
default:
return MakeArgError(ps, 1, []env.Type{env.DecimalType}, "dim")
}
},
},
"round": {
Argsn: 1,
Doc: "Round to nearest integer.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch val := arg0.(type) {
case env.Decimal:
return env.NewDecimal(math.Round(val.Value))
default:
return MakeArgError(ps, 1, []env.Type{env.DecimalType}, "dim")
}
},
},
"erf": {
Argsn: 1,
Doc: "Returns the error function of value.",
Expand Down
16 changes: 14 additions & 2 deletions evaldo/builtins_spreadsheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,13 +840,25 @@ func GenerateColumn(ps *env.ProgramState, s env.Spreadsheet, name env.Word, extr
switch w := word.(type) {
case env.Word:
val, er := s.GetRowValue(ps.Idx.GetWord(w.Index), row)
if val == nil {
val = ""
}
// fmt.Println(val)
if er != nil {
return nil
}
if firstVal == nil {
firstVal = val.(env.Object)
var ok bool
firstVal, ok = val.(env.Object)
if !ok {
firstVal = *env.NewString(val.(string))
}
}
val1, ok := val.(env.Object)
if !ok {
val1 = *env.NewString(val.(string))
}
ctx.Set(w.Index, val.(env.Object))
ctx.Set(w.Index, val1)
}
}
// execute the block of code injected with first value
Expand Down
19 changes: 19 additions & 0 deletions evaldo/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,25 @@ func DoRyeRepl(es *env.ProgramState, dialect string, showResults bool) { // here
EvalBlockInj(es, prevResult, true)
} else if dialect == "eyr" {
Eyr_EvalBlock(es, stack, true)
} else if dialect == "math" {
idxx, _ := es.Idx.GetIndex("math")
s1, ok := es.Ctx.Get(idxx)
if ok {
switch ss := s1.(type) {
case env.RyeCtx: /* */
es.Ctx = &ss
// return s1
}
}
res := DialectMath(es, block1)
switch block := res.(type) {
case env.Block:
stack := NewEyrStack()
ser := es.Ser
es.Ser = block.Series
Eyr_EvalBlock(es, stack, false)
es.Ser = ser
}
}

MaybeDisplayFailureOrError(es, genv)
Expand Down

0 comments on commit 2fd2e4d

Please sign in to comment.