Skip to content

Commit

Permalink
add: logger + error handling let variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Molaryy committed Mar 16, 2024
1 parent 44d4e82 commit 8e87b3f
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 39 deletions.
7 changes: 7 additions & 0 deletions .idea/discord.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/leango.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 41 additions & 21 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package Logger

import (
log "github.com/sirupsen/logrus"
)

func Fatal(nbLine int, mes string) {
log.WithFields(log.Fields{
"line": nbLine,
}).Fatal(mes)
}

func Info(nbLine int, mes string) {
log.WithFields(log.Fields{
"line": nbLine,
}).Fatal(mes)
}
39 changes: 26 additions & 13 deletions Token/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package Token

import (
"fmt"
"leango/Logger"
"leango/Variable"
"slices"
"strconv"
)

var availableTokens = []string{"let", "const", "lean"}
var availableMutableVariables = make(map[string]Variable.Variable)
var availableNonMutableVariables = make(map[string]Variable.Variable)
var availableVariables = make(map[string]Variable.Variable)

func TokenHandler() map[string]func([]string, int) {
var TokenMapFunction = make(map[string]func([]string, int))
Expand All @@ -19,20 +20,32 @@ func TokenHandler() map[string]func([]string, int) {
return TokenMapFunction
}

func isLet(rows []string, line int) {
index := slices.Index(rows, "=")
func isLet(rows []string, nbLine int) {
indexEqualSign := slices.Index(rows, "=")
lenRows := len(rows)
var tokenValue []string

if len(rows) == 1 {
panic(fmt.Sprintf("Nothing found after the declaration of let at line %d", line))
if lenRows < 2 {
Logger.Fatal(nbLine, "Nothing found after the declaration of let")
}

if index == -1 {
panic(fmt.Sprintf("A value was not set for %s", rows[1]))
fmt.Println(indexEqualSign)
if indexEqualSign == 2 && lenRows == 3 {
Logger.Fatal(nbLine, fmt.Sprintf("A value was not set for %s", rows[1]))
}
if _, varNameExists := availableVariables[rows[1]]; varNameExists {
Logger.Fatal(nbLine, fmt.Sprintf("A variable aleady exists with the same name %s: line %d", rows[1], nbLine))
}
if indexEqualSign == 2 {
tokenValue = rows[indexEqualSign+1:]

if intValue, err := strconv.Atoi(tokenValue[0]); err == nil {
availableVariables[rows[1]] = Variable.Variable{IsNumeric: true, Value: intValue}
} else {
availableVariables[rows[1]] = Variable.Variable{IsNumeric: true, Value: intValue}
}
} else {
availableVariables[rows[1]] = Variable.Variable{Value: nil}
}

value := rows[index+1:]
fmt.Println(value)
fmt.Println("Is a let variable and the value is = ")
}

func isConst(rows []string, line int) {
Expand Down
8 changes: 4 additions & 4 deletions Variable/variable.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package Variable

type Variable struct {
isNumeric bool
isString bool
isArray bool
value any
IsNumeric bool
IsString bool
IsMutable bool
Value any
}
2 changes: 1 addition & 1 deletion examples/foo.leango
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let foo = 1
let foo
let bar = foo + 1
const john = 90

Expand Down
1 change: 1 addition & 0 deletions examples/let/errors/no_value_after_equal_sing.leango
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let foo =
2 changes: 2 additions & 0 deletions examples/let/errors/same_name_variables.leango
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let foo
let foo
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module leango

go 1.22.0

require (
github.com/sirupsen/logrus v1.9.3 // indirect
golang.org/x/sys v0.18.0 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.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.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
logger "github.com/sirupsen/logrus"
"leango/Token"
"log"
"os"
Expand Down Expand Up @@ -42,5 +43,8 @@ func main() {
if len(os.Args) != 2 {
log.Default().Fatal("Not enough arguments\n./leango [filepath]")
}
if !(strings.HasSuffix(os.Args[1], ".leango")) {
logger.WithFields(logger.Fields{}).Fatal(fmt.Sprintf("Incorrect file extension [%s]", os.Args[1]))
}
parseFile(os.Args[1])
}

0 comments on commit 8e87b3f

Please sign in to comment.