Skip to content

Commit

Permalink
Add string literal support to the lexer.
Browse files Browse the repository at this point in the history
  • Loading branch information
aleury committed Jan 15, 2024
1 parent c17d4ef commit dbc28fd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
16 changes: 16 additions & 0 deletions examples/print.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@data msg "hello world"

.run
SETX msg ; set X register to the address of msg
JUMP print

.done
HALT

.print
MOVX A ; move the value of address in X to A
JAEZ done ; jump to label 'done' if A = 0
OUTA ; print A
INCX ; increment address stored in X
JUMP print ; jump back to the start of .print to print the next character

16 changes: 16 additions & 0 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func (l *Lexer) NextToken() token.Token {
case l.ch == '\'':
char := l.readCharacter()
return l.newToken(token.CHAR, char)
case l.ch == '"':
str := l.readString()
return l.newToken(token.STRING, str)
case l.ch == 0:
return l.newToken(token.EOF, "")
case unicode.IsDigit(l.ch):
Expand Down Expand Up @@ -75,6 +78,19 @@ func (l *Lexer) readUntil(r rune) string {
return l.input[start:l.position]
}

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

func (l *Lexer) readCharacter() string {
start := l.position
l.readChar()
Expand Down
15 changes: 11 additions & 4 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ DECA
PSHA
POPA
MOVA X
MOVA Y
OUTA
HALT`
HALT
"test"
""`
tests := []struct {
expectedType token.TokenType
expectedLiteral string
Expand All @@ -69,9 +72,13 @@ HALT`
{token.OPCODE, "POPA", 20},
{token.OPCODE, "MOVA", 21},
{token.REGISTER, "X", 21},
{token.OPCODE, "OUTA", 22},
{token.OPCODE, "HALT", 23},
{token.EOF, "", 23},
{token.OPCODE, "MOVA", 22},
{token.REGISTER, "Y", 22},
{token.OPCODE, "OUTA", 23},
{token.OPCODE, "HALT", 24},
{token.STRING, "test", 25},
{token.STRING, "", 26},
{token.EOF, "", 26},
}

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 @@ -11,6 +11,7 @@ const (
IDENT = "IDENT"
INT = "INT"
CHAR = "CHAR"
STRING = "STRING"
)

var registers = map[string]TokenType{
Expand Down

0 comments on commit dbc28fd

Please sign in to comment.