Skip to content

Commit

Permalink
finished chapter 1
Browse files Browse the repository at this point in the history
  • Loading branch information
ZizouHuweidi committed Jul 16, 2023
1 parent 1d9c928 commit df50bca
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 34 deletions.
109 changes: 79 additions & 30 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,107 @@ func (l *Lexer) readChar() {
l.readPosition += 1
}

func (l *Lexer) readIdentifier() string {
position := l.position
for isLetter(l.ch) {
l.readChar()
}
return l.input[position:l.position]
}

func (l *Lexer) readNumber() string {
position := l.position
for isDigit(l.ch) {
l.readChar()
}
return l.input[position:l.position]
}

func isLetter(ch byte) bool {
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_'
}

func isDigit(ch byte) bool {
return '0' <= ch && ch <= '9'
}

func (l *Lexer) skipWhitespace() {
for l.ch == ' ' || l.ch == '\t' || l.ch == '\n' || l.ch == '\r' {
l.readChar()
}
}

func newToken(tokenType token.TokenType, ch byte) token.Token {
return token.Token{Type: tokenType, Literal: string(ch)}
}
func (l *Lexer) peekChar() byte {
if l.readPosition >= len(l.input) {
return 0
} else {
return l.input[l.readPosition]
}
}

func (l *Lexer) NextToken() token.Token {
var tok token.Token

l.skipWhitespace()

switch l.ch {
case '=':
tok = newToken(token.ASSIGN, l.ch)
if l.peekChar() == '=' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = token.Token{Type: token.EQ, Literal: literal}
} else {
tok = newToken(token.ASSIGN, l.ch)
}
case '+':
tok = newToken(token.PLUS, l.ch)
case '-':
tok = newToken(token.MINUS, l.ch)
case '!':
if l.peekChar() == '=' {
ch := l.ch
l.readChar()
literal := string(ch) + string(l.ch)
tok = token.Token{Type: token.NOT_EQ, Literal: literal}
} else {
tok = newToken(token.BANG, l.ch)
}
case '/':
tok = newToken(token.SLASH, l.ch)
case '*':
tok = newToken(token.ASTERISK, l.ch)
case '<':
tok = newToken(token.LT, l.ch)
case '>':
tok = newToken(token.GT, l.ch)
case ';':
tok = newToken(token.SEMICOLON, l.ch)
case '(':
tok = newToken(token.LPAREN, l.ch)
case ')':
tok = newToken(token.RPAREN, l.ch)
case ',':
tok = newToken(token.COMMA, l.ch)
case '+':
tok = newToken(token.PLUS, l.ch)
case '{':
tok = newToken(token.LBRACE, l.ch)
case '}':
tok = newToken(token.RBRACE, l.ch)
case '0':
case '(':
tok = newToken(token.LPAREN, l.ch)
case ')':
tok = newToken(token.RPAREN, l.ch)
case 0:
tok.Literal = ""
tok.Type = token.EOF
default:
if isLetter(l.ch) {
tok.Literal = l.readIdentifier()
tok.Type = token.LookupIdent(tok.Literal)
return tok
} else if isDigit(l.ch) {
tok.Type = token.INT
tok.Literal = l.readNumber()
return tok
} else {
tok = newToken(token.ILLEGAL, l.ch)
}
Expand All @@ -65,25 +136,3 @@ func (l *Lexer) NextToken() token.Token {
l.readChar()
return tok
}

func (l *Lexer) readIdentifier() string {
position := l.position
for isLetter(l.ch) {
l.readChar()
}
return l.input[position:l.position]
}

func isLetter(ch byte) bool {
return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_'
}

func (l *Lexer) skipWhitespace() {
for l.ch == ' ' || l.ch == '\t' || l.ch == '\n' || l.ch == '\r' {
l.readChar()
}
}

func newToken(tokenType token.TokenType, ch byte) token.Token {
return token.Token{Type: tokenType, Literal: string(ch)}
}
21 changes: 21 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import (
"fmt"
"os"
"os/user"

"github.com/zizouhuweidi/go-terpreter/repl"
)

func main() {
user, err := user.Current()
if err != nil {
panic(err)
}

fmt.Printf("Hello %s! This is the Monkey programming language!\n",
user.Username)
fmt.Printf("Feel free to type in commands\n")
repl.Start(os.Stdin, os.Stdout)
}
33 changes: 33 additions & 0 deletions repl/repl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package repl

import (
"bufio"
"fmt"
"io"

"github.com/zizouhuweidi/go-terpreter/lexer"
"github.com/zizouhuweidi/go-terpreter/token"
)

const PROMPT = ">> "

func Start(in io.Reader, out io.Writer) {
scanner := bufio.NewScanner(in)

for {
fmt.Printf(PROMPT)
scanned := scanner.Scan()
if !scanned {
return
}

line := scanner.Text()

l := lexer.New(line)

for tok := l.NextToken(); tok.Type != token.EOF; tok = l.NextToken() {
fmt.Printf("%+v\n", tok)
}
}

}
28 changes: 24 additions & 4 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ const (
INT = "INT" // 1343456

// Operators
ASSIGN = "="
PLUS = "+"
ASSIGN = "="
PLUS = "+"
MINUS = "-"
BANG = "!"
ASTERISK = "*"
SLASH = "/"

LT = "<"
GT = ">"

EQ = "=="
NOT_EQ = "!="

// Delimiters
COMMA = ","
Expand All @@ -31,11 +41,21 @@ const (
// Keywords
FUNCTION = "FUNCTION"
LET = "LET"
TRUE = "TRUE"
FALSE = "FALSE"
IF = "IF"
ELSE = "ELSE"
RETURN = "RETURN"
)

var keywords = map[string]TokenType{
"fn": FUNCTION,
"let": LET,
"fn": FUNCTION,
"let": LET,
"true": TRUE,
"false": FALSE,
"if": IF,
"else": ELSE,
"return": RETURN,
}

func LookupIdent(ident string) TokenType {
Expand Down

0 comments on commit df50bca

Please sign in to comment.