Skip to content

Commit

Permalink
Add parentheses to the tokenizer and IsNumber() method
Browse files Browse the repository at this point in the history
  • Loading branch information
hugolgst committed Apr 19, 2020
1 parent 99aa803 commit 928bc99
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

# Dependency directories (remove the comment below to include it)
# vendor/
.idea
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package main

func main() {
import "./tokenizer"

func main() {
example := "TÄRNÖ BEHÅLLAREThis message will be printedBEHÅLLARE"
tokenizer.Tokenize(example)
}
10 changes: 10 additions & 0 deletions tokenizer/check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tokenizer

import "regexp"

// IsNumber checks if the given character is a number and returns the condition
func IsNumber(character string) bool {
numberRegex := regexp.MustCompile(`\d`)

return numberRegex.Match([]byte(character))
}
13 changes: 13 additions & 0 deletions tokenizer/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tokenizer

import (
"testing"
)

func TestIsNumber(t *testing.T) {
number := "1"

if !IsNumber(number) {
t.Errorf("IsNumber() failed.")
}
}
30 changes: 30 additions & 0 deletions tokenizer/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,39 @@ type Token struct {
Value string
}

const (
LeftParentheses = "("
RightParentheses = ")"

Space = " "
)

// Tokenize takes the given code, tokenize it and returns an array of Tokens
func Tokenize(code string) (tokens []Token) {
var currentIndex int

// Iterate each character of the code
for currentIndex < len(code) {
character := string(code[currentIndex])

// Add parentheses tokens
if character == LeftParentheses || character == RightParentheses {
tokens = append(tokens, Token{
Type: "PARENTHESES",
Value: character,
})

currentIndex++
continue
}

// Skip the spaces
if character == Space {
currentIndex++
continue
}
}

return
}

0 comments on commit 928bc99

Please sign in to comment.