<- Back to shtanton's homepage
aboutsummaryrefslogtreecommitdiff
path: root/walk/value.go
blob: 4459d8949fbe41067c11e5155a49254344bf4416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package walk

import (
	"fmt"
)

type ValueTerminal int
const (
	ArrayBegin ValueTerminal = iota
	ArrayEnd
	MapBegin
	MapEnd
)
func (value ValueTerminal) Atomise(in []AtomOLD) []AtomOLD {
	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 []AtomOLD) []AtomOLD {
	return append(in, NewAtomNull())
}
func (value ValueNull) String() string {
	return "null"
}

type ValueBool bool
func (value ValueBool) Atomise(in []AtomOLD) []AtomOLD {
	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 []AtomOLD) []AtomOLD {
	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 []AtomOLD) []AtomOLD {
	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 ValueOLD interface {
	// Append this values atoms to the input
	Atomise(in []AtomOLD) []AtomOLD
	String() string
}