Skip to content

Commit

Permalink
feat(lexer): adds skeleton for lexical analyser
Browse files Browse the repository at this point in the history
  • Loading branch information
lukepeterson committed Sep 16, 2024
1 parent bcc0aeb commit b3766bb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
51 changes: 32 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
package main

// import "github.com/lukepeterson/go8080assembler/assembler"
import (
"fmt"
"log"

"github.com/lukepeterson/go8080assembler/assembler"
"github.com/lukepeterson/go8080assembler/pkg/lexer"
)

func main() {
code := `
MVI A, 34h
MOV B, C
LDA, 1234h
HLT
`

assembler := assembler.New()

err := assembler.Assemble(code)
if err != nil {
log.Fatal(err)
}

for _, instruction := range assembler.ByteCode {
fmt.Printf("%02X ", instruction)
}
// code := `
// MVI A, 34h
// MOV B, C
// LDA, 1234h
// HLT
// `

// assembler := assembler.New()

// err := assembler.Assemble(code)
// if err != nil {
// log.Fatal(err)
// }

// for _, instruction := range assembler.ByteCode {
// fmt.Printf("%02X ", instruction)
// }

input := `
MVI A, 0x33
TEST: MOV B, C ; The first comment
LDA, 1234h ; The second comment
JMP TEST
; The Third comment with a colon ; here
`

myLexer := lexer.New(input)
fmt.Printf("myLexer: %v\n", myLexer)

}
34 changes: 34 additions & 0 deletions pkg/lexer/lexer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package lexer

type TokenType string

const (
COMMA TokenType = "COMMA"
COLON TokenType = "COLON"

IDENT TokenType = "IDENT"
NUMBER TokenType = "NUMBER"
REGISTER TokenType = "REGISTER"
MNEMONIC TokenType = "MNEMONIC"

COMMENT TokenType = "COMMENT"
EOF TokenType = "EOF"

UNKNOWN TokenType = "UNKNOWN"
)

type Token struct {
Type TokenType
Literal string
}

type Lexer struct {
input string
// position int
}

func New(input string) *Lexer {
lexer := &Lexer{input: input}
// lexer.readChar()
return lexer
}

0 comments on commit b3766bb

Please sign in to comment.