stred-go

Stred: Streaming Tree Editor. Like sed but for JSON. This is the go implementation
git clone https://shtanton.xyz/git/stred-go.git
Log | Files | Refs | README

commit e832b30f31c56614afe689035af4e04a29e77896
parent bed0e712deda5038f52e495bacae003098df7a55
Author: Charlie Stanton <charlie@shtanton.xyz>
Date:   Fri, 21 Jul 2023 19:53:14 +0100

Adds an incredibly simple equality operator

Diffstat:
Msubex/arithmetic.go | 15+++++++++++++++
Msubex/parse.go | 4+++-
Msubex/subexast.go | 14++++++++++++++
3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/subex/arithmetic.go b/subex/arithmetic.go @@ -156,3 +156,18 @@ func notValues(values walk.ValueList) (notted walk.ValueList, err error) { } return notted, nil } + +// Returns true if all values are equal, false if not +func equalValues(values walk.ValueList) (walk.ValueList, error) { + if len(values) == 0 { + return walk.ValueList{walk.BoolScalar(true)}, nil + } + first := values[0] + for _, value := range values[1:] { + // TODO: Refine the equality check + if value != first { + return walk.ValueList{walk.BoolScalar(false)}, nil + } + } + return walk.ValueList{walk.BoolScalar(true)}, nil +} diff --git a/subex/parse.go b/subex/parse.go @@ -260,7 +260,7 @@ func parseSubex(l RuneReader, minPower int, runic bool) SubexAST { // case '[': // rangeParts := parseRangeSubex(l) // lhs = SubexASTRange {rangeParts} - case ')', ']', '"', '|', ';', '{', '+', '-', '*', '/', '!', '$': + case ')', ']', '"', '|', ';', '{', '+', '-', '*', '/', '!', '=', '$': l.Rewind() return SubexASTEmpty{} // case '=': @@ -366,6 +366,8 @@ func parseSubex(l RuneReader, minPower int, runic bool) SubexAST { lhs = SubexASTReciprocal {lhs} case r == '!' && minPower <= 4: lhs = SubexASTNot {lhs} + case r == '=' && minPower <= 4: + lhs = SubexASTEqual {lhs} case r == '$' && minPower <= 4: slot := l.Next() if slot == eof { diff --git a/subex/subexast.go b/subex/subexast.go @@ -395,6 +395,20 @@ func (ast SubexASTNot) String() string { return fmt.Sprintf("(%v)!", ast.Content) } +// Runs the content Subex and collects the output +// Replaces it with true if all output values are equal and false otherwise +type SubexASTEqual struct { + Content SubexAST +} +func (ast SubexASTEqual) compileWith(next SubexState, slotMap *SlotMap, runic bool) SubexState { + return &SubexCaptureBeginState { + next: ast.Content.compileWith(&SubexArithmeticEndState { + next: next, + calculate: equalValues, + }, slotMap, runic), + } +} + // Does nothing type SubexASTEmpty struct {} func (ast SubexASTEmpty) compileWith(next SubexState, slotMap *SlotMap, runic bool) SubexState {