Skip to content

Commit

Permalink
Introduce DeepRefl which checks for cycles, only fully implemented fo…
Browse files Browse the repository at this point in the history
…r O, uses unexposed V.Contains
  • Loading branch information
acook committed Mar 10, 2024
1 parent 7d6a647 commit 548f9c3
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func (b B) Refl() string {
return "[ " + str + " ]"
}

func (b B) DeepRefl(list V) (V, string) {
return list, b.Refl()
}

func (b B) PP() string {
str := b.Disassemble().Refl()
str = str[1:(len(str) - 1)]
Expand Down
3 changes: 3 additions & 0 deletions src/item_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

type datatypes interface {
Refl() string
DeepRefl(V) (V, string)
Value() interface{}
}

type sequence interface {
Refl() string
DeepRefl(V) (V, string)
Value() interface{}
Cat(sequence) sequence
App(datatypes) sequence
Expand All @@ -25,6 +27,7 @@ type byter interface {

type stackable interface {
Refl() string
DeepRefl(V) (V, string)
Value() interface{}
Push(datatypes)
Pop() datatypes
Expand Down
4 changes: 4 additions & 0 deletions src/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (m *Meta) Refl() string {
return str + ">"
}

func (m *Meta) DeepRefl(list V) (V, string) {
return list, m.Refl()
}

// for stack interface compatibility
// will panic if you try to push anything other than a stack
func (m *Meta) Push(i datatypes) {
Expand Down
4 changes: 4 additions & 0 deletions src/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func (n N) Refl() string {
return fmt.Sprint(n)
}

func (n N) DeepRefl(list V) (V, string) {
return list, n.Refl()
}

func (n N) Value() interface{} {
return int64(n)
}
Expand Down
66 changes: 65 additions & 1 deletion src/object.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package main

import (
"fmt"
"reflect"
)

type Object struct {
Slots map[W]datatypes
Parent *Object
Expand Down Expand Up @@ -64,15 +69,74 @@ func (o *Object) DeleGet(meta *Meta, w W) bool {
}

func (o Object) Refl() string {
str := ""

list := V{}
_, s := o.DeepRefl(list)
str += s

return str + ""
}

func (o Object) ShallowRefl() string {
str := "\033[32mO{ "

for k, v := range o.Slots {
str += k.Refl() + ":" + v.Refl() + " "
str += k.Refl() + ":"

switch t := v.(type) {
case *Object:
str += v.(*Object).SimpleRefl()
case N, C, T, B, OP:
str += v.Refl()
default:
str += "..."
str += fmt.Sprint(reflect.TypeOf(t))
str += "..."
}

str += " "
}

return str + "}\033[0m"
}

func (o Object) DeepRefl(list V) (V, string) {
str := "\033[32mO{ "

for k, v := range o.Slots {
str += k.Refl() + ":"

switch t := v.(type) {
case N, C, T, B, OP:
str += v.Refl()
default:
if list.Contains(v).Kind == "true" {
str += "..."
str += fmt.Sprint(reflect.TypeOf(t))
str += "..."
} else {
list = list.App(v).(V)
l, s := v.DeepRefl(list)
list = l
str += s
}
}

str += " \033[32m"
}

return list, str + "}\033[0m"
}

func (o Object) SimpleRefl() string {
str := "O#"

str = str + fmt.Sprint(len(o.Slots))

return str + ""
}

func (o Object) Value() interface{} {
return o.Slots
}
Expand Down
4 changes: 4 additions & 0 deletions src/octet.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func (c C) Refl() string {
return fmt.Sprintf("0x%0.2X", c)
}

func (c C) DeepRefl(list V) (V, string) {
return list, c.Refl()
}

func (c C) Value() interface{} {
return byte(c)
}
Expand Down
4 changes: 4 additions & 0 deletions src/opword.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ func (op OP) Refl() string {
return "`" + op.Print()
}

func (op OP) DeepRefl(list V) (V, string) {
return list, op.Refl()
}

func (op OP) Value() interface{} {
return uint8(op)
}
Expand Down
4 changes: 4 additions & 0 deletions src/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ PrintLoop:

return str + " |"
}

func (q Queue) DeepRefl(list V) (V, string) {
return list, q.Refl()
}
4 changes: 4 additions & 0 deletions src/rune.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func (r R) Refl() string {
return "\\u" + hex.EncodeToString(buf)
}

func (r R) DeepRefl(list V) (V, string) {
return list, r.Refl()
}

func (r R) Value() interface{} {
return rune(r)
}
Expand Down
4 changes: 4 additions & 0 deletions src/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ func (s *Stack) Refl() string {
return str + ">"
}

func (s *Stack) DeepRefl(list V) (V, string) {
return list, s.Refl()
}

func (s *Stack) ReflHeader() string {
str := ""

Expand Down
4 changes: 4 additions & 0 deletions src/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func (t Tag) Refl() string {
return t.Kind + "#" + t.Label
}

func (t Tag) DeepRefl(list V) (V, string) {
return list, t.Refl()
}

func (t Tag) Value() interface{} {
return t.Kind
}
Expand Down
4 changes: 4 additions & 0 deletions src/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func (t T) Refl() string {
return "'" + string(t) + "'"
}

func (t T) DeepRefl(list V) (V, string) {
return list, t.Refl()
}

func (t T) Value() interface{} {
return string(t)
}
Expand Down
17 changes: 17 additions & 0 deletions src/vector.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package main

import (
"reflect"
)

type V []datatypes

func (v V) Refl() string {
Expand All @@ -13,6 +17,10 @@ func (v V) Refl() string {
return str + ")"
}

func (v V) DeepRefl(list V) (V, string) {
return list, v.Refl()
}

func (v V) Value() interface{} {
return []datatypes(v)
}
Expand All @@ -36,6 +44,15 @@ func (v V) Rmo(n N) sequence {
return v
}

func (v V) Contains(d datatypes) *Tag {
for _, i := range v {
if reflect.DeepEqual(i, d) {
return NewTrue("contains")
}
}
return NewNil("contains")
}

func (v V) Len() N {
return N(len(v))
}
Expand Down
4 changes: 4 additions & 0 deletions src/word.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func (w W) Refl() string {
return "~" + w.Print()
}

func (w W) DeepRefl(list V) (V, string) {
return list, w.Refl()
}

func (w W) Value() interface{} {
return uint64(w)
}
Expand Down

0 comments on commit 548f9c3

Please sign in to comment.