2018-03-30 16:15:06 +00:00
|
|
|
package vm
|
|
|
|
|
|
|
|
import "encoding/json"
|
|
|
|
|
|
|
|
// StackOutput holds information about the stack, used for pretty printing
|
|
|
|
// the stack.
|
|
|
|
type stackItem struct {
|
|
|
|
Value interface{} `json:"value"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
|
2019-11-26 16:36:01 +00:00
|
|
|
func appendToItems(items *[]stackItem, val StackItem, seen map[StackItem]bool) {
|
|
|
|
if arr, ok := val.Value().([]StackItem); ok {
|
|
|
|
if seen[val] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
seen[val] = true
|
|
|
|
intItems := make([]stackItem, 0, len(arr))
|
|
|
|
for _, v := range arr {
|
|
|
|
appendToItems(&intItems, v, seen)
|
|
|
|
}
|
|
|
|
*items = append(*items, stackItem{
|
|
|
|
Value: intItems,
|
|
|
|
Type: val.String(),
|
|
|
|
})
|
|
|
|
|
|
|
|
} else {
|
|
|
|
*items = append(*items, stackItem{
|
|
|
|
Value: val,
|
|
|
|
Type: val.String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 15:26:59 +00:00
|
|
|
func stackToArray(s *Stack) []stackItem {
|
2018-11-26 15:56:45 +00:00
|
|
|
items := make([]stackItem, 0, s.Len())
|
2019-11-26 16:36:01 +00:00
|
|
|
seen := make(map[StackItem]bool)
|
2019-11-26 16:53:30 +00:00
|
|
|
s.IterBack(func(e *Element) {
|
2019-11-26 16:36:01 +00:00
|
|
|
appendToItems(&items, e.value, seen)
|
2018-03-30 16:15:06 +00:00
|
|
|
})
|
2019-10-29 15:26:59 +00:00
|
|
|
return items
|
|
|
|
}
|
2018-03-30 16:15:06 +00:00
|
|
|
|
2019-10-29 15:26:59 +00:00
|
|
|
func buildStackOutput(s *Stack) string {
|
|
|
|
b, _ := json.MarshalIndent(stackToArray(s), "", " ")
|
2018-03-30 16:15:06 +00:00
|
|
|
return string(b)
|
|
|
|
}
|