Skip to content

Commit

Permalink
Add an asterik token to support deferencing registers in move instruc…
Browse files Browse the repository at this point in the history
…tion.
  • Loading branch information
aleury committed Jan 20, 2024
1 parent dd6ee2c commit cab9632
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
35 changes: 19 additions & 16 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func New(reader io.Reader) (*Lexer, error) {
return nil, err
}
l := &Lexer{input: []rune(string(input)), line: 1}
l.readChar()
l.readRune()
return l, nil
}

Expand All @@ -41,10 +41,13 @@ func (l *Lexer) NextToken() token.Token {
case l.currentRune == '"':
str := l.readString()
return l.newToken(token.STRING, str)
case l.currentRune == '*':
l.readRune()
return l.newToken(token.ASTERISK, "*")
case l.currentRune == '-':
if l.peekChar() == '>' {
l.readChar()
l.readChar()
if l.peekRune() == '>' {
l.readRune()
l.readRune()
return l.newToken(token.ARROW, "->")
}
return l.newToken(token.ILLEGAL, string(l.currentRune))
Expand Down Expand Up @@ -78,39 +81,39 @@ func (l *Lexer) newToken(kind token.TokenType, literal string) token.Token {
func (l *Lexer) readUntil(r rune) string {
start := l.position
for l.currentRune != r && l.currentRune != 0 {
l.readChar()
l.readRune()
}
return string(l.input[start:l.position])
}

func (l *Lexer) readString() string {
position := l.position + 1
for {
l.readChar()
l.readRune()
if l.currentRune == '"' || l.currentRune == 0 {
break
}
}
// consume closing quote
l.readChar()
l.readRune()
return string(l.input[position : l.position-1])
}

func (l *Lexer) readCharacter() string {
start := l.position
l.readChar()
l.readChar()
l.readChar()
l.readRune()
l.readRune()
l.readRune()
return string(l.input[start:l.position])
}

func (l *Lexer) readIdentifier() string {
start := l.position
if l.currentRune == '.' {
l.readChar()
l.readRune()
}
for unicode.IsLetter(l.currentRune) {
l.readChar()
l.readRune()
}
return string(l.input[start:l.position])
}
Expand All @@ -120,17 +123,17 @@ func (l *Lexer) skipWhitespace() {
if l.currentRune == '\n' {
l.line++
}
l.readChar()
l.readRune()
}
}

func (l *Lexer) readChar() {
l.currentRune = l.peekChar()
func (l *Lexer) readRune() {
l.currentRune = l.peekRune()
l.position = l.nextRuneIndex
l.nextRuneIndex = l.position + 1
}

func (l *Lexer) peekChar() rune {
func (l *Lexer) peekRune() rune {
if l.nextRuneIndex >= len(l.input) {
return 0
}
Expand Down
16 changes: 11 additions & 5 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ PSHA
POPA
MOVE A -> X
MOVE A -> Y
MOVE *A -> X
OUTA
HALT
"test"
Expand Down Expand Up @@ -78,11 +79,16 @@ HALT
{token.REGISTER, "A", 22},
{token.ARROW, "->", 22},
{token.REGISTER, "Y", 22},
{token.INSTRUCTION, "OUTA", 23},
{token.INSTRUCTION, "HALT", 24},
{token.STRING, "test", 25},
{token.STRING, "", 26},
{token.EOF, "", 26},
{token.INSTRUCTION, "MOVE", 23},
{token.ASTERISK, "*", 23},
{token.REGISTER, "A", 23},
{token.ARROW, "->", 23},
{token.REGISTER, "X", 23},
{token.INSTRUCTION, "OUTA", 24},
{token.INSTRUCTION, "HALT", 25},
{token.STRING, "test", 26},
{token.STRING, "", 27},
{token.EOF, "", 27},
}

l := newLexerFromString(input)
Expand Down
1 change: 1 addition & 0 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
CHAR = "CHAR"
STRING = "STRING"
ARROW = "ARROW"
ASTERISK = "ASTERISK"
)

var registers = map[string]TokenType{
Expand Down

0 comments on commit cab9632

Please sign in to comment.