chore: state after where broken

This commit is contained in:
xuu 2024-12-03 15:12:39 -07:00
parent 7b9a71f84c
commit 127257c494
Signed by: xuu
GPG Key ID: 8B3B0604F164E04F

View File

@ -22,33 +22,66 @@ type result struct {
func (r result) String() string { return fmt.Sprintf("%#v", r) }
func run(read io.Reader) (*result, error) {
sum := 0
sum2 := 0
lexer := Lexer(read)
for typ, token := range lexer.Iter() {
active := true
if m := readMul(lexer); m != 0 {
fmt.Println(typ, string(token))
sum += m
for typ, _ := range lexer.Iter() {
// fmt.Println(typ, string(token))
switch {
case typ == "TokMUL":
if m := readMul(lexer); m != 0 {
sum += m
if active {
sum2 += m
}
}
case typ == "TokDONT":
if readDont(lexer) {
println("don't()", active)
active = false
}
case typ == "TokDO":
if readDo(lexer) {
println("do()", active)
active = true
}
}
}
return &result{sum, -1}, nil
return &result{sum, sum2}, nil
}
func Lexer(in io.Reader) *lexer {
seq := func(yield func(rune) bool) {
defer func() {
if err := recover(); err != nil {
fmt.Println("ERR", err)
return
}
fmt.Println("NO DEFER AND NO PANIC??")
}()
buf := make([]byte, 256)
s, _ := in.Read(buf)
buf = buf[:s]
for len(buf) > 0 {
for _, r := range string(buf) {
for i, r := range string(buf) {
println(i, r)
if i >= 72 {
fmt.Println("START")
}
if !yield(r) {
return
}
if i >= 72 {
fmt.Println("STOP")
}
}
s, _ := in.Read(buf)
buf = buf[:s]
@ -71,8 +104,8 @@ type lexer struct {
iter func() (rune, bool)
stop func()
buf []rune
token string
buf []rune
token string
literal []rune
}
@ -118,7 +151,24 @@ func (l *lexer) NextTok() bool {
case 'm':
l.loadString("mul")
l.token = "TokMUL"
if string(l.literal) != "mul" {
l.token = "TokILLEGAL"
return false
}
return true
case 'd':
l.loadString("don't")
switch string(l.literal) {
case "don't":
l.token = "TokDONT"
case "do":
l.token = "TokDO"
default:
l.token = "TokILLEGAL"
return false
}
return true
case '(':
l.loadRune("TokLPAREN")
return true
@ -162,7 +212,7 @@ func readMul(lex *lexer) int {
if !lex.NextTok() || lex.token != "TokLPAREN" {
return 0
}
if !lex.NextTok() || lex.token != "TokNUMBER" {
return 0
}
@ -183,3 +233,29 @@ func readMul(lex *lexer) int {
fmt.Println(a, "*", b)
return a * b
}
func readDont(lex *lexer) bool {
if lex.token != "TokDONT" || string(lex.literal) != "don't" {
return false
}
if !lex.NextTok() || lex.token != "TokLPAREN" {
return false
}
if !lex.NextTok() || lex.token != "TokRPAREN" {
return false
}
return true
}
func readDo(lex *lexer) bool {
if lex.token != "TokDO" || string(lex.literal) != "do" {
return false
}
if !lex.NextTok() || lex.token != "TokLPAREN" {
return false
}
if !lex.NextTok() || lex.token != "TokRPAREN" {
return false
}
return true
}