<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/walk/value.go
diff options
context:
space:
mode:
authorCharlie Stanton <charlie@shtanton.xyz>2023-04-25 13:55:26 +0100
committerCharlie Stanton <charlie@shtanton.xyz>2023-04-25 13:55:26 +0100
commit6a4e25b1c691846185e9dd698c1558981089738c (patch)
tree7a5031ae4a8d21b48836f98f7fe7629d276df0ac /walk/value.go
parenta28312895aecd8643c1705c0a316d1b99107dc0d (diff)
downloadstred-go-6a4e25b1c691846185e9dd698c1558981089738c.tar
Refactor Atom and Value code out of walk.go and into separate files
Diffstat (limited to 'walk/value.go')
-rw-r--r--walk/value.go78
1 files changed, 78 insertions, 0 deletions
diff --git a/walk/value.go b/walk/value.go
new file mode 100644
index 0000000..2e2c3c9
--- /dev/null
+++ b/walk/value.go
@@ -0,0 +1,78 @@
+package walk
+
+import (
+ "fmt"
+)
+
+type ValueTerminal int
+const (
+ ArrayBegin ValueTerminal = iota
+ ArrayEnd
+ MapBegin
+ MapEnd
+)
+func (value ValueTerminal) Atomise(in []Atom) []Atom {
+ return append(in, NewAtomTerminal(value))
+}
+func (value ValueTerminal) String() string {
+ switch value {
+ case ArrayBegin:
+ return "["
+ case ArrayEnd:
+ return "]"
+ case MapBegin:
+ return "{"
+ case MapEnd:
+ return "}"
+ default:
+ panic("Unknown TerminalValue")
+ }
+}
+
+type ValueNull struct {}
+func (value ValueNull) Atomise(in []Atom) []Atom {
+ return append(in, NewAtomNull())
+}
+func (value ValueNull) String() string {
+ return "null"
+}
+
+type ValueBool bool
+func (value ValueBool) Atomise(in []Atom) []Atom {
+ return append(in, NewAtomBool(bool(value)))
+}
+func (value ValueBool) String() string {
+ if value {
+ return "true"
+ } else {
+ return "false"
+ }
+}
+
+type ValueNumber float64
+func (value ValueNumber) Atomise(in []Atom) []Atom {
+ return append(in, NewAtomNumber(float64(value)))
+}
+func (value ValueNumber) String() string {
+ v := float64(value)
+ return fmt.Sprintf("%f", v)
+}
+
+type ValueString string
+func (value ValueString) Atomise(in []Atom) []Atom {
+ in = append(in, NewAtomStringTerminal())
+ for _, char := range value {
+ in = append(in, NewAtomStringRune(char))
+ }
+ in = append(in, NewAtomStringTerminal())
+ return in
+}
+func (value ValueString) String() string {
+ return fmt.Sprintf("\"%s\"", string(value))
+}
+
+type Value interface {
+ // Append this values atoms to the input
+ Atomise(in []Atom) []Atom
+ String() string
+}