Skip to content

Commit

Permalink
serialize state part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
rokostik committed Feb 18, 2024
1 parent 82a4187 commit b97d956
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 48 deletions.
92 changes: 62 additions & 30 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,20 +1113,6 @@ func (i Function) Print(e Idxs) string {
return "[" + pure + "Function(" + strconv.FormatInt(int64(i.Argsn), 10) + ")]"
}

// func (i Function) Dump(e Idxs) Block {
// // LONG DISPLAY OF FUNCTION NODES return "[Function: " + i.Spec.Inspect(e) + ", " + i.Body.Inspect(e) + "]"
// ser := make([]Object, 0)
// idx, found := e.GetIndex("fn")
// if !found {
// goto ENE // TODO
// }
// ser = append(ser, Word{idx})
// ser = append(ser, i.Spec)
// ser = append(ser, i.Body)
// ENE:
// return *NewBlock(*NewTSeries(ser))
// }

func (i Function) Trace(msg string) {
fmt.Print(msg + " (function): ")
fmt.Println(i.Spec)
Expand Down Expand Up @@ -1157,7 +1143,28 @@ func (i Function) Equal(o Object) bool {
}

func (i Function) Dump(e Idxs) string {
return fmt.Sprintf("\"serlization of %s is not yet supported\" ", i.Inspect(e))
var b strings.Builder
b.WriteString("fn { ")
for _, obj := range i.Spec.Series.GetAll() {
if obj != nil {
b.WriteString(obj.Dump(e))
b.WriteString(" ")
} else {
b.WriteString("'nil ")
}
}
b.WriteString("} { ")
for _, obj := range i.Body.Series.GetAll() {
if obj != nil {
b.WriteString(obj.Dump(e))
b.WriteString(" ")
} else {
b.WriteString("'nil ")
}
}
b.WriteString("}")

return b.String()
}

//
Expand Down Expand Up @@ -1247,8 +1254,8 @@ func (i Builtin) Equal(o Object) bool {
}

func (i Builtin) Dump(e Idxs) string {
// TODO
return fmt.Sprintf("\"serlization of %s is not yet supported\" ", i.Inspect(e))
// Serializing builtins is not supported
return ""
}

//
Expand Down Expand Up @@ -1343,9 +1350,14 @@ func (i Error) Equal(o Object) bool {
if i.Message != oError.Message {
return false
}
if i.Parent != oError.Parent {
if (i.Parent == nil) != (oError.Parent == nil) {
return false
}
if i.Parent != nil && oError.Parent != nil {
if !i.Parent.Equal(oError.Parent) {
return false
}
}
if len(i.Values) != len(oError.Values) {
return false
}
Expand All @@ -1358,8 +1370,11 @@ func (i Error) Equal(o Object) bool {
}

func (i Error) Dump(e Idxs) string {
// TODO
return fmt.Sprintf("\"serlization of %s is not yet supported\" ", i.Inspect(e))
if i.Parent == nil {
return fmt.Sprintf("failure { %d \"%s\" }", i.Status, i.Message)
} else {
return fmt.Sprintf("wrap\\failure { %d \"%s\" } %s", i.Status, i.Message, i.Parent.Dump(e))
}
}

//
Expand Down Expand Up @@ -1494,8 +1509,15 @@ func (i CPath) Equal(o Object) bool {
}

func (i CPath) Dump(e Idxs) string {
// TODO
return fmt.Sprintf("\"serlization of %s is not yet supported\" ", i.Inspect(e))
var b strings.Builder
b.WriteString(i.Word1.Dump(e))
if i.Cnt > 1 {
b.WriteString("/" + i.Word2.Dump(e))
}
if i.Cnt > 2 {
b.WriteString("/" + i.Word3.Dump(e))
}
return b.String()
}

//
Expand Down Expand Up @@ -1551,8 +1573,8 @@ func (i Native) Equal(o Object) bool {
}

func (i Native) Dump(e Idxs) string {
// TODO
return fmt.Sprintf("\"serlization of %s is not yet supported\" ", i.Inspect(e))
// Serializing natives is not supported
return ""
}

//
Expand Down Expand Up @@ -1810,8 +1832,13 @@ func (i List) Equal(o Object) bool {
}

func (i List) Dump(e Idxs) string {
// TODO
return fmt.Sprintf("\"serlization of %s is not yet supported\" ", i.Inspect(e))
var b strings.Builder
b.WriteString("list { ")
for _, v := range i.Data {
b.WriteString(ToRyeValue(v).Dump(e) + " ")
}
b.WriteString("}")
return b.String()
}

//
Expand Down Expand Up @@ -1947,8 +1974,8 @@ func (i Converter) Equal(o Object) bool {
}

func (i Converter) Dump(e Idxs) string {
// TODO
return fmt.Sprintf("\"serlization of %s is not yet supported\" ", i.Inspect(e))
// Serializing converters is not supported
return ""
}

//
Expand Down Expand Up @@ -2090,6 +2117,11 @@ func (i Vector) Equal(o Object) bool {
}

func (i Vector) Dump(e Idxs) string {
// TODO
return fmt.Sprintf("\"serlization of %s is not yet supported\" ", i.Inspect(e))
var b strings.Builder
b.WriteString("vector { ")
for _, v := range i.Value {
b.WriteString(fmt.Sprintf("%f ", v))
}
b.WriteString("}")
return b.String()
}
48 changes: 30 additions & 18 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,15 +665,10 @@ var builtins = map[string]*env.Builtin{

"dump": { // *** currently a concept in testing ... for getting a code of a function, maybe same would be needed for context?
Argsn: 1,
Doc: "Retunrs (dumps) content of a function.",
Doc: "Returns (dumps) Rye code representing the object.",
Pure: true,
Fn: func(env1 *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch d := arg0.(type) {
case env.Function:
return env.NewString(d.Dump(*env1.Idx))
default:
return MakeArgError(env1, 1, []env.Type{env.FunctionType}, "dump")
}
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
return env.NewString(arg0.Dump(*ps.Idx))
},
},

Expand Down Expand Up @@ -2212,9 +2207,9 @@ var builtins = map[string]*env.Builtin{
if found {
return val
}
return MakeBuiltinError(ps, "Value not found.", "evalu")
return MakeBuiltinError(ps, "Value not found.", "vals")
default:
return MakeArgError(ps, 1, []env.Type{env.BlockType, env.WordType}, "evalu")
return MakeArgError(ps, 1, []env.Type{env.BlockType, env.WordType}, "vals")
}
},
},
Expand Down Expand Up @@ -2247,7 +2242,7 @@ var builtins = map[string]*env.Builtin{
ps.Ser = ser
return *env.NewBlock(*env.NewTSeries(res))
default:
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "eval\\with")
return MakeArgError(ps, 2, []env.Type{env.BlockType}, "vals\\with")
}
},
},
Expand Down Expand Up @@ -4605,13 +4600,23 @@ var builtins = map[string]*env.Builtin{
case env.Block:
var doc string
if args.Series.Len() > 0 {
var hasDoc bool
switch a := args.Series.S[len(args.Series.S)-1].(type) {
case env.String:
doc = a.Value
hasDoc = true
//fmt.Println("DOC DOC")
// default:
//return MakeBuiltinError(ps, "Series type should be string.", "fn")
}
for i, o := range args.Series.GetAll() {
if i == len(args.Series.S)-1 && hasDoc {
break
}
if o.Type() != env.WordType {
return MakeBuiltinError(ps, "Function arguments should be words", "fn")
}
}
}
switch body := arg1.(type) {
case env.Block:
Expand Down Expand Up @@ -6197,13 +6202,7 @@ var builtins = map[string]*env.Builtin{
//fmt.Println("FAIL")
ps.FailureFlag = true
ps.ReturnFlag = true
switch val := arg0.(type) {
case env.String: // todo .. make Error type .. make error construction micro dialect, return the error wrapping error that caused it
return env.NewError(val.Value)
case env.Integer: // todo .. make Error type .. make error construction micro dialect, return the error wrapping error that caused it
return env.NewError1(int(val.Value))
}
return arg0
return MakeRyeError(ps, arg0, nil)
},
},

Expand All @@ -6226,6 +6225,19 @@ var builtins = map[string]*env.Builtin{
},
},

"wrap\\failure": {
Argsn: 2,
Doc: "Wraps an Error with another Error. Accepts String as message, Integer as code, or block for multiple parameters and Error as arguments.",
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
switch er := arg1.(type) {
case *env.Error:
return MakeRyeError(ps, arg0, er)
default:
return MakeArgError(ps, 2, []env.Type{env.ErrorType}, "wrap\\error")
}
},
},

"code?": { // **
AcceptFailure: true,
Argsn: 1,
Expand Down

0 comments on commit b97d956

Please sign in to comment.