Skip to content

Commit

Permalink
Merge pull request #241 from refaktor/modwords
Browse files Browse the repository at this point in the history
added format (sprintf) and prnf (printf), fixed enter-console bug
  • Loading branch information
refaktor authored Jun 15, 2024
2 parents 09ea117 + 726e34c commit 961a1f2
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 23 deletions.
2 changes: 1 addition & 1 deletion env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (i RyeCtx) Equal(o Object) bool {
if i.locked != oCtx.locked {
return false
}
return false
return true
}

func (i RyeCtx) Dump(e Idxs) string {
Expand Down
141 changes: 123 additions & 18 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ var builtins = map[string]*env.Builtin{
if found {
switch iintval := intval.(type) {
case env.Integer:
ctx.Set(arg.Index, *env.NewInteger(1 + iintval.Value))
ctx.Mod(arg.Index, *env.NewInteger(1 + iintval.Value))
return *env.NewInteger(1 + iintval.Value)
default:
return MakeBuiltinError(ps, "Value in word is not integer.", "inc!")
Expand All @@ -690,7 +690,7 @@ var builtins = map[string]*env.Builtin{
if found {
switch iintval := intval.(type) {
case env.Integer:
ctx.Set(arg.Index, *env.NewInteger(iintval.Value - 1))
ctx.Mod(arg.Index, *env.NewInteger(iintval.Value - 1))
return *env.NewInteger(1 + iintval.Value)
default:
return MakeBuiltinError(ps, "Value in word is not integer.", "inc!")
Expand Down Expand Up @@ -731,7 +731,7 @@ var builtins = map[string]*env.Builtin{

"set!": { // ***
Argsn: 2,
Doc: "Set word or words by deconstructing block",
Doc: "Set word to value or words by deconstructing a block",
Pure: false,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch words := arg1.(type) {
Expand All @@ -747,10 +747,10 @@ var builtins = map[string]*env.Builtin{
}
val := vals.Series.S[i]
// if it exists then we set it to word from words
res := ps.Ctx.Set(word.Index, val)
if res.Type() == env.ErrorType {
ps.Ctx.Mod(word.Index, val)
/* if res.Type() == env.ErrorType {
return MakeBuiltinError(ps, res.(env.Error).Message, "set")
}
}*/
default:
fmt.Println(word)
return MakeBuiltinError(ps, "Only words in words block", "set")
Expand All @@ -761,7 +761,8 @@ var builtins = map[string]*env.Builtin{
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "set")
}
case env.Word:
return ps.Ctx.Set(words.Index, arg0)
ps.Ctx.Mod(words.Index, arg0)
return arg0
default:
return MakeArgError(ps, 1, []env.Type{env.BlockType}, "set")
}
Expand Down Expand Up @@ -1409,6 +1410,53 @@ var builtins = map[string]*env.Builtin{
return arg0
},
},
"format": { // **
Argsn: 2,
Doc: "Formats a value according to Go-s sprintf format",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
var res string
switch arg := arg1.(type) {
case env.String:
switch val := arg0.(type) {
case env.String:
res = fmt.Sprintf(arg.Value, val.Value)
case env.Integer:
res = fmt.Sprintf(arg.Value, val.Value)
case env.Decimal:
res = fmt.Sprintf(arg.Value, val.Value)
// TODO make option with multiple values and block as second arg
default:
return MakeArgError(ps, 1, []env.Type{env.StringType, env.DecimalType, env.IntegerType}, "format")
}
return *env.NewString(res)
default:
return MakeArgError(ps, 2, []env.Type{env.StringType}, "format")
}
},
},
"prnf": { // **
Argsn: 2,
Doc: "Formats a value according to Go-s sprintf format and prn-s it",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch arg := arg1.(type) {
case env.String:
switch val := arg0.(type) {
case env.String:
fmt.Printf(arg.Value, val.Value)
case env.Integer:
fmt.Printf(arg.Value, val.Value)
case env.Decimal:
fmt.Printf(arg.Value, val.Value)
// TODO make option with multiple values and block as second arg
default:
return MakeArgError(ps, 1, []env.Type{env.StringType, env.DecimalType, env.IntegerType}, "format")
}
return arg0
default:
return MakeArgError(ps, 2, []env.Type{env.StringType}, "format")
}
},
},
"embed": { // **
Argsn: 2,
Doc: "Embeds a value in string.",
Expand Down Expand Up @@ -2235,8 +2283,8 @@ var builtins = map[string]*env.Builtin{
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch name := arg0.(type) {
case env.String:
/* ser := ps.Ser
ps.Ser = bloc.Series
ser := ps.Ser
/* ps.Ser = bloc.Series
EvalBlock(ps)
ps.Ser = ser */
//reader := bufio.NewReader(os.Stdin)
Expand Down Expand Up @@ -2268,7 +2316,7 @@ var builtins = map[string]*env.Builtin{

DoRyeRepl(ps, "do", ShowResults)
fmt.Println("-------------------------------------------------------------")
// ps.Ser = ser
ps.Ser = ser
return ps.Res
default:
return MakeArgError(ps, 1, []env.Type{env.StringType}, "enter-console")
Expand Down Expand Up @@ -2369,7 +2417,7 @@ var builtins = map[string]*env.Builtin{
},
},

"do\\in": { // **
"do\\inside": { // **
Argsn: 2,
Doc: "Takes a Context and a Block. It Does a block inside 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 {
Expand All @@ -2394,7 +2442,7 @@ var builtins = map[string]*env.Builtin{
},
},

"do\\in\\try": { // **
"do\\inside\\try": { // **
Argsn: 2,
Doc: "Takes a Context and a Block. It Does a block inside 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 {
Expand Down Expand Up @@ -2427,7 +2475,7 @@ var builtins = map[string]*env.Builtin{
},
},

"do\\par": { // **
"do\\in": { // **
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 {
Expand All @@ -2437,10 +2485,33 @@ var builtins = map[string]*env.Builtin{
case env.Block:
ser := ps.Ser
ps.Ser = bloc.Series
tempCtx := &ctx
for {
// fmt.Println("UP ->")
// do we change parents globally (and we have to fix them back) or just in local copy of a value? check
if tempCtx.Parent == ps.Ctx {
// fmt.Println("ISTI PARENT")
// temp = ctx.Parent
tempCtx.Parent = ps.Ctx.Parent
break
}
if tempCtx.Parent != nil {
tempCtx = tempCtx.Parent
} else {
break
}
}
//var temp *env.RyeCtx
// set argument's parent context to current parent context
// }
// set argument context as parent
temp := ps.Ctx.Parent
ps.Ctx.Parent = &ctx
EvalBlock(ps)
// if temp != nil {
ps.Ctx.Parent = temp
// ctx.Parent = temp
// }
ps.Ser = ser
return ps.Res
default:
Expand Down Expand Up @@ -2775,7 +2846,7 @@ var builtins = map[string]*env.Builtin{
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
return *ps.Ctx
},
},

Expand All @@ -2787,6 +2858,21 @@ var builtins = map[string]*env.Builtin{
},
},

"parent?": { // **
Argsn: 1,
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 {
switch c := arg0.(type) {
case env.RyeCtx:
return *c.Parent
case *env.RyeCtx:
return *c.Parent
default:
return MakeArgError(ps, 1, []env.Type{env.CtxType}, "parent?")
}
},
},

"ls": {
Argsn: 0,
Doc: "Lists words in current context",
Expand Down Expand Up @@ -2841,6 +2927,7 @@ var builtins = map[string]*env.Builtin{
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch s1 := arg0.(type) {
case env.RyeCtx:
s1.Parent = ps.Ctx // TODO ... this is temporary so ccp works, but some other method must be figured out as changing the parent is not OK
ps.Ctx = &s1
return s1
default:
Expand Down Expand Up @@ -5556,6 +5643,24 @@ var builtins = map[string]*env.Builtin{
}
},
},
"trim\\": {
Argsn: 2,
Doc: "Trims the String of specific characters.",
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 s1 := arg0.(type) {
case env.String:
switch s2 := arg1.(type) {
case env.String:
return *env.NewString(strings.Trim(s1.Value, s2.Value))
default:
return MakeArgError(ps, 2, []env.Type{env.StringType}, "trim\\")
}
default:
return MakeArgError(ps, 1, []env.Type{env.StringType}, "trim\\")
}
},
},

"replace": {
Argsn: 3,
Expand Down Expand Up @@ -6657,13 +6762,13 @@ var builtins = map[string]*env.Builtin{
case env.Integer:
newval = *env.NewString(oldval.Value + strconv.Itoa(int(s3.Value)))
}
ctx.Set(wrd.Index, newval)
ctx.Mod(wrd.Index, newval)
return newval
case env.Block: // TODO
// fmt.Println(123)
s := &oldval.Series
oldval.Series = *s.Append(arg0)
ctx.Set(wrd.Index, oldval)
ctx.Mod(wrd.Index, oldval)
return oldval
case env.List:
dataSlice := make([]any, 0)
Expand All @@ -6683,7 +6788,7 @@ var builtins = map[string]*env.Builtin{
combineList = append(combineList, env.ToRyeValue(v))
}
finalList := *env.NewList(combineList)
ctx.Set(wrd.Index, finalList)
ctx.Mod(wrd.Index, finalList)
return finalList
default:
return makeError(ps, "Type of tagword is not String or Block")
Expand Down Expand Up @@ -7227,7 +7332,7 @@ var builtins = map[string]*env.Builtin{
},

// BASIC ENV / Dict FUNCTIONS
"format": {
"format-": { // TODO ... format is not sprintf ... find a name
Argsn: 1,
Doc: "Accepts a Dict and returns formatted presentation of it as a string.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
Expand Down
6 changes: 3 additions & 3 deletions evaldo/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ func MaybeDisplayFailureOrError(es *env.ProgramState, genv *env.Idxs) {
fmt.Println("\x1b[0m")

// ENTER CONSOLE ON ERROR
es.ErrorFlag = false
es.FailureFlag = false
DoRyeRepl(es, "do", true)
// es.ErrorFlag = false
// es.FailureFlag = false
// DoRyeRepl(es, "do", true)
}
// cebelca2659- vklopi kontne skupine
}
Expand Down
2 changes: 1 addition & 1 deletion wasm/ryeshell/index2.html
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@
}
}

function sendLinesToTerminal(multilineString, delay = 1000, term) {
function sendLinesToTerminal(multilineString, delay = 600, term) {
const lines = multilineString.split("\n");
let index = 0;
let isPaused = false;
Expand Down

0 comments on commit 961a1f2

Please sign in to comment.