token and begin lexer
This commit is contained in:
parent
e03eb2ca2d
commit
47784287bb
1
cmd/main.go
Normal file
1
cmd/main.go
Normal file
@ -0,0 +1 @@
|
|||||||
|
package main
|
54
lexer/lexer.go
Normal file
54
lexer/lexer.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package lexer
|
||||||
|
|
||||||
|
import "robaertschi.xyz/robaertschi/thorgot/token"
|
||||||
|
|
||||||
|
type Lexer struct {
|
||||||
|
input string
|
||||||
|
ch byte
|
||||||
|
pos int
|
||||||
|
readPos int
|
||||||
|
|
||||||
|
// Loc
|
||||||
|
col int
|
||||||
|
line int
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(input string) Lexer {
|
||||||
|
lexer := Lexer{input: input}
|
||||||
|
|
||||||
|
lexer.readChar()
|
||||||
|
|
||||||
|
return lexer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Lexer) readChar() {
|
||||||
|
if l.readPos >= len(l.input) {
|
||||||
|
l.ch = 0
|
||||||
|
} else {
|
||||||
|
l.ch = l.input[l.readPos]
|
||||||
|
}
|
||||||
|
|
||||||
|
if l.ch == '\n' {
|
||||||
|
l.col = 0
|
||||||
|
l.line += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
l.pos = l.readPos
|
||||||
|
l.readPos += 1
|
||||||
|
l.col += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Lexer) makeToken(t token.TokenType, literal string) token.Token {
|
||||||
|
return token.Token{Token: t, Literal: literal, Loc: token.Loc{Line: l.line, Col: l.col}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *Lexer) NextToken() token.Token {
|
||||||
|
var token token.Token
|
||||||
|
|
||||||
|
switch l.ch {
|
||||||
|
case 0:
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return token
|
||||||
|
}
|
46
token/token.go
Normal file
46
token/token.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package token
|
||||||
|
|
||||||
|
type TokenType string
|
||||||
|
|
||||||
|
type Loc struct {
|
||||||
|
Line int
|
||||||
|
Col int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Token struct {
|
||||||
|
Token TokenType
|
||||||
|
Literal string
|
||||||
|
Loc Loc
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
Illegal TokenType = "Illegal"
|
||||||
|
Eof = "Eof"
|
||||||
|
|
||||||
|
EndLine = "EndLine"
|
||||||
|
|
||||||
|
Semicolon = "Semicolon" // ;
|
||||||
|
Colon = "Colon" // :
|
||||||
|
Equal = "Equal" // =
|
||||||
|
LBrace = "LBrace" // {
|
||||||
|
RBrace = "RBrace" // }
|
||||||
|
LParen = "LParen" // (
|
||||||
|
RParen = "RParen" // )
|
||||||
|
|
||||||
|
Identifier = "Identifier"
|
||||||
|
|
||||||
|
// Keywords
|
||||||
|
Fn = "Fn" // fn
|
||||||
|
)
|
||||||
|
|
||||||
|
var stringToToken = map[string]TokenType{
|
||||||
|
"fn": Fn,
|
||||||
|
}
|
||||||
|
|
||||||
|
func LookupKeyword(literal string) TokenType {
|
||||||
|
if token, ok := stringToToken[literal]; ok {
|
||||||
|
return token
|
||||||
|
}
|
||||||
|
|
||||||
|
return Identifier
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user