Skip to content

Commit

Permalink
fix bug in forExpander
Browse files Browse the repository at this point in the history
  • Loading branch information
bobertlo committed Dec 5, 2024
1 parent 311553a commit 3400e64
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
3 changes: 2 additions & 1 deletion forexpand.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func (f *forExpander) run() {
for state := forLine; state != nil; {
state = state(f)
}

// add an extra EOF in case we end without one
// we don't want to block on reading from the channel
f.tokens <- token{tokEOF, ""}
Expand Down Expand Up @@ -145,7 +146,7 @@ func forConsumeLabels(f *forExpander) forStateFn {
f.next()
return forConsumeLabels
}
} else if f.nextToken.typ == tokNewline || f.nextToken.typ == tokComment {
} else if f.nextToken.typ == tokNewline || f.nextToken.typ == tokComment || f.nextToken.typ == tokColon {
f.next()
return forConsumeLabels
} else {
Expand Down
20 changes: 1 addition & 19 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,6 @@ type sourceLine struct {
newlines int
}

func subExprSymbol(expr []token, label string, value []token) []token {
output := make([]token, 0, len(expr))
for _, tok := range expr {
if tok.typ == tokText && tok.val == label {
output = append(output, value...)
} else {
output = append(output, tok)
}
}
return output
}

func (line sourceLine) subSymbol(label string, value []token) sourceLine {
// output := make()
line.a = subExprSymbol(line.a, label, value)
line.b = subExprSymbol(line.b, label, value)
return line
}

type parser struct {
lex tokenReader

Expand Down Expand Up @@ -129,6 +110,7 @@ func (p *parser) validateSymbols() error {
for symbol, i := range p.references {
_, ok := p.symbols[symbol]
if !ok {
fmt.Println(p.lines)
return fmt.Errorf("line %d: symbol '%s' undefined", i, symbol)
}
}
Expand Down
18 changes: 18 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,24 @@ func TestParserPositive(t *testing.T) {
},
},
},
{
input: "label:dat $0, $0\n",
output: []sourceLine{
{
line: 1,
codeLine: 0,
labels: []string{"label"},
typ: lineInstruction,
op: "dat",
amode: "$",
a: []token{{typ: tokNumber, val: "0"}},
bmode: "$",
b: []token{{typ: tokNumber, val: "0"}},
comment: "",
newlines: 1,
},
},
},
}

runParserTests(t, "parser positive", testCases)
Expand Down

0 comments on commit 3400e64

Please sign in to comment.