Skip to content

Commit

Permalink
refactor(*): refaz o lexer e melhora a performance
Browse files Browse the repository at this point in the history
  • Loading branch information
natanfeitosa committed Feb 12, 2024
1 parent 23cae2b commit 794c9bd
Show file tree
Hide file tree
Showing 17 changed files with 518 additions and 389 deletions.
5 changes: 5 additions & 0 deletions compartilhado/compartilhado.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package compartilhado

func ContemApenasAlfaNum(str string) bool {
return ContemApenasDigitos(str) || ContemApenasLetras(str)
}
8 changes: 4 additions & 4 deletions compartilhado/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ContemApenasDigitos(str string) bool {
return true
}

func IndiceCaractereParaByte(str string, indice int) int {
func IndiceCaraterParaByte(str string, indice int) int {
byteIndex := 0
for i := 0; i < indice; i++ {
_, tamanho := utf8.DecodeRuneInString(str[byteIndex:])
Expand All @@ -32,8 +32,8 @@ func IndiceCaractereParaByte(str string, indice int) int {
return byteIndex
}

func ObtemLetraPorIndice(str string, indice int) string {
byteIndex := IndiceCaractereParaByte(str, indice)
func ObtemCaraterPorIndice(str string, indice int) string {
byteIndex := IndiceCaraterParaByte(str, indice)
letra, _ := utf8.DecodeRuneInString(str[byteIndex:])
return string(letra)
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ go 1.21.1

require (
github.com/peterh/liner v1.2.2
github.com/rivo/uniseg v0.4.4
github.com/spf13/cobra v1.7.0
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.17.0 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRM
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
71 changes: 71 additions & 0 deletions lexer/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package lexer

var tokensSimples = map[string]TokenType{
"\n": TokenNovaLinha,

"=": TokenIgual,
"+": TokenMais,
"-": TokenMenos,
"*": TokenAsterisco,
"**": TokenPotencia,
"/": TokenDivisao,
"//": TokenDivisaoInteira,
"%": TokenModulo,
"<": TokenMenorQue,
"<=": TokenMenorOuIgual,
"==": TokenIgualIgual,
"!=": TokenDiferente,
">": TokenMaiorQue,
">=": TokenMaiorOuIgual,
"(": TokenAbreParenteses,
")": TokenFechaParenteses,
";": TokenPontoEVirgula,
",": TokenVirgula,
"{": TokenAbreChaves,
"}": TokenFechaChaves,
"[": TokenAbreColchetes,
"]": TokenFechaColchetes,
":": TokenDoisPontos,

// Reatribuicao
"+=": TokenMaisIgual,
"-=": TokenMenosIgual,
"*=": TokenAsteriscoIgual,
"/=": TokenBarraIgual,

"|": TokenBitABitOu,
"^": TokenBitABitExOu,
"&": TokenBitABitE,
"~": TokenBitABitNao,
"<<": TokenDeslocEsquerda,
">>": TokenDeslocDireita,

".": TokenPonto,
}

var tokensIdentificadores = map[string]TokenType{
"se": TokenSe,
"senao": TokenSenao,
"enquanto": TokenEnquanto,
"para": TokenPara,
"retorne": TokenRetorne,
"pare": TokenPare,
"continue": TokenContinue,

"de": TokenDe,
"importe": TokenImporte,

"Verdadeiro": TokenVerdadeiro,
"Falso": TokenFalso,
"Nulo": TokenNulo,

"var": TokenVar,
"const": TokenConst,
"func": TokenFunc,

// Operadores booleanos

"ou": TokenBoolOu,
"e": TokenBoolE,
"nao": TokenBoolNao,
}
Loading

0 comments on commit 794c9bd

Please sign in to comment.