Skip to content

Commit

Permalink
lex colons and accept after line labels (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobertlo authored Nov 19, 2024
1 parent 81adcf1 commit cc1a492
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ func lexInput(l *lexer) lexStateFn {
fallthrough
case '>':
return l.emitConsume(token{tokAddressMode, string(l.nextRune)}, lexInput)
case ':':
return l.emitConsume(token{tokColon, ":"}, lexInput)
case '\x1a':
return l.consume(lexInput)
default:
Expand Down
30 changes: 30 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ func parseComment(p *parser) parseStateFn {
// parseLabels consumes text tokens until an op is read
// label text token: parseLabels
// op text token: parseOp
// colon: parseColon
// newline / comments: consume
// anyting else: nil
func parseLabels(p *parser) parseStateFn {
if p.nextToken.IsOp() {
Expand All @@ -229,6 +231,10 @@ func parseLabels(p *parser) parseStateFn {
return parseOp
}

if p.nextToken.typ == tokColon {
return parseColon
}

_, ok := p.symbols[p.nextToken.val]
if ok {
p.err = fmt.Errorf("line %d: symbol '%s' redefined", p.line, p.nextToken.val)
Expand All @@ -251,6 +257,30 @@ func parseLabels(p *parser) parseStateFn {
return parseLabels
}

// from: parseLabels
// newline or comments: parseColon
// op: parseOp
// anything else: nil
func parseColon(p *parser) parseStateFn {
p.next()

// just consume newlines and comments for now
if p.nextToken.typ == tokNewline || p.nextToken.typ == tokComment {
p.next()
return parseColon
}

if p.nextToken.IsOp() {
if p.nextToken.IsPseudoOp() {
return parsePseudoOp
}
return parseOp
}

p.err = fmt.Errorf("line %d: op expected after colon, got '%s'", p.line, p.nextToken)
return nil
}

// from: parseLabels
// comment: parseComment
// newline: parseLine
Expand Down
15 changes: 15 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ func TestParserPositive(t *testing.T) {
newlines: 1,
}},
},
{
input: "a: mov $0, $1 ; comment\n",
output: []sourceLine{{
line: 1,
labels: []string{"a"},
typ: lineInstruction,
op: "mov",
amode: "$",
a: []token{{typ: tokNumber, val: "0"}},
bmode: "$",
b: []token{{typ: tokNumber, val: "1"}},
comment: "; comment",
newlines: 1,
}},
},
{
input: "mov $ -1, $ 2 + 2\n",
output: []sourceLine{{
Expand Down
1 change: 1 addition & 0 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
tokNumber // (optionally) signed integer
tokExprOp // + - * / % ==
tokComma
tokColon
tokParenL
tokParenR
tokComment // includes semi-colon, no newline char
Expand Down

0 comments on commit cc1a492

Please sign in to comment.