From 47784287bb468060a37c4fd21a0407d44592cd01 Mon Sep 17 00:00:00 2001 From: Robin Date: Tue, 12 Nov 2024 20:42:23 +0100 Subject: [PATCH] token and begin lexer --- cmd/main.go | 1 + lexer/lexer.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ token/token.go | 46 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 cmd/main.go create mode 100644 lexer/lexer.go create mode 100644 token/token.go diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1 @@ +package main diff --git a/lexer/lexer.go b/lexer/lexer.go new file mode 100644 index 0000000..be84f4d --- /dev/null +++ b/lexer/lexer.go @@ -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 +} diff --git a/token/token.go b/token/token.go new file mode 100644 index 0000000..073102b --- /dev/null +++ b/token/token.go @@ -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 +}