diff options
-rw-r--r-- | subex/parse.go | 11 | ||||
-rw-r--r-- | walk/atom.go | 119 | ||||
-rw-r--r-- | walk/value.go | 78 | ||||
-rw-r--r-- | walk/walk.go | 152 |
4 files changed, 0 insertions, 360 deletions
diff --git a/subex/parse.go b/subex/parse.go index a671e6d..7d0e586 100644 --- a/subex/parse.go +++ b/subex/parse.go @@ -22,17 +22,6 @@ func accept(l RuneReader, chars string) bool { return false } -func expectBracket(l RuneReader, ifLeft walk.AtomOLD, ifRight walk.AtomOLD) walk.AtomOLD { - switch l.Next() { - case '(': - return ifLeft - case ')': - return ifRight - default: - panic("Expected ( or )") - } -} - func isNumericRune(r rune) bool { return '0' <= r && r <= '9' || r == '.' } diff --git a/walk/atom.go b/walk/atom.go deleted file mode 100644 index 471f030..0000000 --- a/walk/atom.go +++ /dev/null @@ -1,119 +0,0 @@ -package walk - -import ( - "math" - "fmt" -) - -type AtomType int64 -const ( - AtomNull AtomType = iota - AtomBool - AtomNumber - AtomTerminal - AtomStringTerminal - AtomStringRune -) -type AtomOLD struct { - Typ AtomType - data uint64 -} -func NewAtomNull() AtomOLD { - return AtomOLD { - Typ: AtomNull, - data: 0, - } -} -func NewAtomBool(v bool) AtomOLD { - if v { - return AtomOLD { - Typ: AtomBool, - data: 1, - } - } else { - return AtomOLD { - Typ: AtomBool, - data: 0, - } - } -} -func (v AtomOLD) Bool() bool { - if v.Typ != AtomBool { - panic("Tried to use non-bool as bool") - } - return v.data == 1 -} -func NewAtomNumber(v float64) AtomOLD { - return AtomOLD { - Typ: AtomNumber, - data: math.Float64bits(v), - } -} -func (v AtomOLD) Number() float64 { - if v.Typ != AtomNumber { - panic("Tried to use non-number as number") - } - return math.Float64frombits(v.data) -} -func NewAtomTerminal(v ValueTerminal) AtomOLD { - return AtomOLD { - Typ: AtomTerminal, - data: uint64(v), - } -} -func (v AtomOLD) Terminal() ValueTerminal { - if v.Typ != AtomTerminal { - panic("Tried to use non-terminal as terminal") - } - return ValueTerminal(v.data) -} -func NewAtomStringTerminal() AtomOLD { - return AtomOLD { - Typ: AtomStringTerminal, - data: 0, - } -} -func NewAtomStringRune(v rune) AtomOLD { - return AtomOLD { - Typ: AtomStringRune, - data: uint64(v), - } -} -func (v AtomOLD) StringRune() rune { - if v.Typ != AtomStringRune { - panic("Tried to use non-stringrune as stringrune") - } - return rune(v.data) -} -func (v AtomOLD) String() string { - switch v.Typ { - case AtomNull: - return "null" - case AtomBool: - if v.data == 0 { - return "false" - } - return "true" - case AtomNumber: - return fmt.Sprintf("%v", math.Float64frombits(v.data)) - case AtomTerminal: - switch ValueTerminal(v.data) { - case MapBegin: - return "{" - case MapEnd: - return "}" - case ArrayBegin: - return "[" - case ArrayEnd: - return "]" - default: - panic("Invalid terminal atom") - } - case AtomStringTerminal: - return "\"" - case AtomStringRune: - return string(rune(v.data)) - default: - panic("Invalid atom type") - } -} diff --git a/walk/value.go b/walk/value.go deleted file mode 100644 index 4459d89..0000000 --- a/walk/value.go +++ /dev/null @@ -1,78 +0,0 @@ -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 -} diff --git a/walk/walk.go b/walk/walk.go index 65fac6e..aca6a8c 100644 --- a/walk/walk.go +++ b/walk/walk.go @@ -2,7 +2,6 @@ package walk import ( "fmt" - "math" "strings" "unicode/utf8" ) @@ -221,40 +220,6 @@ func (_ MapEndTerminal) edible() {} func (_ MapEndTerminal) atom() {} func (_ MapEndTerminal) terminal() {} -// int or string -type PathSegment interface {} - -type Path []PathSegment -func (path Path) ToWalkValues() []ValueOLD { - var values []ValueOLD - for _, segment := range path { - switch s := segment.(type) { - case int: - values = append(values, ValueNumber(s)) - case string: - values = append(values, ValueString(s)) - default: - panic("Invalid PathSegment") - } - } - return values -} - -func PathFromWalkValues(values []ValueOLD) Path { - var segments []PathSegment - for _, value := range values { - switch v := value.(type) { - case ValueNumber: - segments = append(segments, int(math.Round(float64(v)))) - case ValueString: - segments = append(segments, string(v)) - default: - panic("Invalid value in path") - } - } - return segments -} - type WalkItem struct { Value ValueList Path ValueList @@ -277,120 +242,3 @@ func (item WalkItem) Debug() string { } return builder.String() } - -func ConcatData(first []AtomOLD, second []AtomOLD) []AtomOLD { - res := make([]AtomOLD, 0, len(first) + len(second)) - res = append(res, first...) - res = append(res, second...) - return res -} - -func Atomise(in []ValueOLD) (out []AtomOLD) { - numAtoms := 0 - for _, value := range in { - switch v := value.(type) { - case ValueTerminal, ValueNull, ValueBool, ValueNumber: - numAtoms++ - case ValueString: - numAtoms += utf8.RuneCountInString(string(v)) + 2 - default: - panic("Invalid WalkValue") - } - } - out = make([]AtomOLD, 0, numAtoms) - for _, value := range in { - out = value.Atomise(out) - } - return out -} - -type CompoundError int - -const ( - CompoundRuneOutsideString CompoundError = iota - CompoundUnknownAtom - CompoundMissingEnd - CompoundInvalidStringAtom -) - -func (err CompoundError) Error() string { - switch err { - case CompoundRuneOutsideString: - return "Compound Error: Rune Outside String" - case CompoundUnknownAtom: - return "Compound Error: Unknown Atom" - case CompoundMissingEnd: - return "Compound Error: Missing End" - case CompoundInvalidStringAtom: - return "Compound Error: Invalid String Atom" - default: - panic("Invalid CompoundError") - } -} - -type CompoundResult struct { - value ValueOLD - error error -} - -func Compound(in []AtomOLD) (out []ValueOLD, error error) { - numValues := 0 - i := 0 - inString := false - for _, atom := range in { - switch atom.Typ { - case AtomNull, AtomBool, AtomNumber, AtomTerminal: - if !inString { - numValues++ - } - case AtomStringTerminal: - if inString { - numValues++ - } - inString = !inString - } - } - i = 0 - out = make([]ValueOLD, 0, numValues) - for { - if i >= len(in) { - break - } - atom := in[i] - i++ - switch atom.Typ { - case AtomNull: - out = append(out, ValueNull{}) - continue - case AtomBool: - out = append(out, ValueBool(atom.data != 0)) - continue - case AtomNumber: - out = append(out, ValueNumber(math.Float64frombits(atom.data))) - continue - case AtomTerminal: - out = append(out, ValueTerminal(atom.data)) - continue - case AtomStringRune: - return nil, CompoundRuneOutsideString - case AtomStringTerminal: - default: - return nil, CompoundUnknownAtom - } - // Handle string start - var builder strings.Builder - for { - if i >= len(in) { - return nil, CompoundMissingEnd - } - atom := in[i] - i++ - if atom.Typ == AtomStringTerminal { - break - } - builder.WriteString(atom.String()) - } - out = append(out, ValueString(builder.String())) - } - return out, nil -} |