diff options
author | Charlie Stanton <charlie@shtanton.xyz> | 2023-07-19 11:57:59 +0100 |
---|---|---|
committer | Charlie Stanton <charlie@shtanton.xyz> | 2023-07-19 11:57:59 +0100 |
commit | 8cf10efe3b5a1bcc70bc6e5590ee63fd5eb00c5b (patch) | |
tree | 7a16883c17c2bdcc49b2f9d4f333dfc76c66248f /main | |
parent | 3c34366bdd5d817a184d6b1c901d03a16b6faa4b (diff) | |
download | stred-go-8cf10efe3b5a1bcc70bc6e5590ee63fd5eb00c5b.tar |
Huge refactor to a more value based system, doing away with terminals. Also introduces unit testing
Diffstat (limited to 'main')
-rw-r--r-- | main/command.go | 12 | ||||
-rw-r--r-- | main/lex.go | 2 | ||||
-rw-r--r-- | main/main.go | 8 | ||||
-rw-r--r-- | main/parse.go | 54 |
4 files changed, 14 insertions, 62 deletions
diff --git a/main/command.go b/main/command.go index ef48596..5a898e2 100644 --- a/main/command.go +++ b/main/command.go @@ -1,8 +1,8 @@ package main import ( - "main/walk" "main/subex" + "main/walk" "fmt" ) @@ -46,7 +46,7 @@ func (cmd AppendNextCommand) exec(state *ProgramState) { if err != nil { panic("Missing next value") } - state.value = walk.ConcatData(state.value, nextItem.Value) + state.value = append(state.value, nextItem.Value...) state.path = nextItem.Path state.pc++ } @@ -72,12 +72,12 @@ func (cmd DeletePathCommand) String() string { return "D" } -func runSubex(state subex.Transducer, in []walk.Atom) (out []walk.Atom, error bool) { - atomsOut, error := subex.RunTransducer(state, in) +func runSubex(state subex.Transducer, in walk.ValueList) (walk.ValueList, bool) { + out, error := subex.RunTransducer(state, in) if error { return nil, true } - return atomsOut, false + return out, false } type SubstituteValueCommand struct { @@ -193,7 +193,7 @@ func (cmd SwapPathCommand) String() string { type AppendPathCommand struct {} func (cmd AppendPathCommand) exec(state *ProgramState) { - state.path = walk.ConcatData(state.path, state.value) + state.path = append(state.path, state.value...) state.pc++ } func (cmd AppendPathCommand) String() string { diff --git a/main/lex.go b/main/lex.go index 198c346..496abd0 100644 --- a/main/lex.go +++ b/main/lex.go @@ -180,7 +180,7 @@ func lexCommand(l *lexer) stateFunc { case '}': l.emit(TokenRBrace) return lexCommand - case 's', 'S', 'f', 'F', 'l', 'L', 'a', 'A': + case 's', 'S': l.emit(TokenCommand) return lexSubstitution case ':', 'b': diff --git a/main/main.go b/main/main.go index a506954..8e8c369 100644 --- a/main/main.go +++ b/main/main.go @@ -4,13 +4,13 @@ import ( "os" "bufio" "main/walk" - "main/json_array" + "main/json" ) type Program []Command type ProgramState struct { - path, value, xreg, yreg, zreg []walk.Atom + path, value, xreg, yreg, zreg walk.ValueList in walk.StredReader out walk.StredWriter program []Command @@ -45,8 +45,8 @@ func main() { stdout := bufio.NewWriter(os.Stdout) state := ProgramState { - in: json_array.NewJSONArrayReader(stdin), - out: json_array.NewJSONArrayWriter(stdout), + in: json.NewJSONReader(stdin), + out: json.NewJSONWriter(stdout), program: program, } diff --git a/main/parse.go b/main/parse.go index cbbfb9a..141ae7e 100644 --- a/main/parse.go +++ b/main/parse.go @@ -71,61 +71,13 @@ func (p *parser) parseBasicCommand(commands []Command, commandChar rune) []Comma return append(commands, NextCommand{}) case 'N': return append(commands, AppendNextCommand{}) - case 's', 'S', 'f', 'F', 'l', 'L', 'a', 'A': + case 's', 'S': ast := p.parseSubex() - switch commandChar { - case 'f': - ast = subex.SubexASTConcat { - First: ast, - Second: subex.SubexASTRepeat { - Content: subex.SubexASTCopyAny{}, - Acceptable: []subex.ConvexRange{{Start: -1, End: 0}}, - }, - } - case 'F': - ast = subex.SubexASTConcat { - First: subex.SubexASTStore { - Slot: '_', - Match: ast, - }, - Second: subex.SubexASTRepeat { - Content: subex.SubexASTCopyAny{}, - Acceptable: []subex.ConvexRange{{Start: -1, End: 0}}, - }, - } - case 'l': - ast = subex.SubexASTConcat { - First: subex.SubexASTRepeat { - Content: subex.SubexASTCopyAny{}, - Acceptable: []subex.ConvexRange{{Start: 0, End: -1}}, - }, - Second: ast, - } - case 'L': - ast = subex.SubexASTConcat { - First: subex.SubexASTRepeat { - Content: subex.SubexASTCopyAny{}, - Acceptable: []subex.ConvexRange{{Start: 0, End: -1}}, - }, - Second: subex.SubexASTStore { - Slot: '_', - Match: ast, - }, - } - case 'a', 'A': - ast = subex.SubexASTRepeat { - Acceptable: []subex.ConvexRange{{Start: -1, End: 0}}, - Content: subex.SubexASTOr { - First: ast, - Second: subex.SubexASTCopyAny{}, - }, - } - } subex := subex.CompileTransducer(ast) switch commandChar { - case 's', 'a': + case 's': return append(commands, SubstituteValueCommand {subex}, JumpCommand {len(commands) + 3}) - case 'S', 'f', 'F', 'l', 'L', 'A': + case 'S': return append(commands, SubstitutePathCommand {subex}, JumpCommand {len(commands) + 3}) default: panic("Unreachable!?!?") |