<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/walk/value.go
diff options
context:
space:
mode:
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
+}