Skip to content

Commit

Permalink
feat: declaring a variable name with the same name as a type
Browse files Browse the repository at this point in the history
  • Loading branch information
Molaryy committed Apr 9, 2024
1 parent 14206e9 commit 9de6362
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 54 deletions.
111 changes: 58 additions & 53 deletions Token/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,76 +34,81 @@ func handleString(nbLine int, value string) (string, bool) {
}

func getArithmeticOperation(firstValue int, operator string, secondValue int, nbLine int) int {
switch operator {
case "+": return firstValue + secondValue
case "-": return firstValue - secondValue
case "*": return firstValue * secondValue
case "/":
if secondValue == 0 {
Logger.Fatal(nbLine, "You tried to divise by 0 at ")
}
return firstValue / secondValue
case "%": return firstValue % secondValue
}
Logger.Fatal(nbLine, "An error has occured")
return 0
switch operator {
case "+":
return firstValue + secondValue
case "-":
return firstValue - secondValue
case "*":
return firstValue * secondValue
case "/":
if secondValue == 0 {
Logger.Fatal(nbLine, "You tried to divise by 0 at ")
}
return firstValue / secondValue
case "%":
return firstValue % secondValue
}
Logger.Fatal(nbLine, "An error has occured")
return 0
}

func handleNumericOperations(rows []string, tokens []string, nbLine int) {
tokensLen := len(tokens)
sum := 0
firstOp := true
var opt string
tokensLen := len(tokens)
sum := 0
firstOp := true
var opt string

for idx := 0; idx < tokensLen; {
fstValue, err := strconv.Atoi(tokens[idx])
if err != nil {
Logger.Fatal(nbLine, "Trying to do arithmetic operation with another type than an Int")
}
if idx + 1 >= tokensLen {
break
}
opt = tokens[idx + 1]
if idx + 2 >= tokensLen {
Logger.Fatal(nbLine, "There is no value after the aritmetic operation")
}
scdValue, err := strconv.Atoi(tokens[idx + 2])
if err != nil {
Logger.Fatal(nbLine, "Trying to do arithmetic operation with another type than an Int")
}
if firstOp {
sum += getArithmeticOperation(fstValue, opt, scdValue, nbLine)
firstOp = false
} else {
sum = getArithmeticOperation(sum, opt, scdValue, nbLine)
}
idx += 2
}
availableVariables[rows[1]] = Variable.Variable{IsNumeric: true, Value: sum, IsMutable: true}
for idx := 0; idx < tokensLen; {
fstValue, err := strconv.Atoi(tokens[idx])
if err != nil {
Logger.Fatal(nbLine, "Trying to do arithmetic operation with another type than an Int")
}
if idx+1 >= tokensLen {
break
}
opt = tokens[idx+1]
if idx+2 >= tokensLen {
Logger.Fatal(nbLine, "There is no value after the aritmetic operation")
}
scdValue, err := strconv.Atoi(tokens[idx+2])
if err != nil {
Logger.Fatal(nbLine, "Trying to do arithmetic operation with another type than an Int")
}
if firstOp {
sum += getArithmeticOperation(fstValue, opt, scdValue, nbLine)
firstOp = false
} else {
sum = getArithmeticOperation(sum, opt, scdValue, nbLine)
}
idx += 2
}
availableVariables[rows[1]] = Variable.Variable{IsNumeric: true, Value: sum, IsMutable: true}
}

func handleArithmeticOperations(tokens []string, rows []string, nbLine int) {
if _, err := strconv.Atoi(tokens[0]); err == nil {
handleNumericOperations(rows, tokens, nbLine)
if _, err := strconv.Atoi(tokens[0]); err == nil {
handleNumericOperations(rows, tokens, nbLine)
} else if strValue, isStr := handleString(nbLine, tokens[0]); isStr == true {
availableVariables[rows[1]] = Variable.Variable{IsString: true, Value: strValue}
availableVariables[rows[1]] = Variable.Variable{IsString: true, Value: strValue}
}
}

func handleVariableType(nbLine int, rows []string, indexEqualSign int) {
var tokenValues = rows[indexEqualSign+1:]
if (len(tokenValues) == singleVariableValue) {
if intValue, err := strconv.Atoi(tokenValues[0]); err == nil {
availableVariables[rows[1]] = Variable.Variable{IsNumeric: true, Value: intValue}
} else if strValue, isStr := handleString(nbLine, tokenValues[0]); isStr == true {
availableVariables[rows[1]] = Variable.Variable{IsString: true, Value: strValue}
}

if len(tokenValues) == singleVariableValue {
if intValue, err := strconv.Atoi(tokenValues[0]); err == nil {
availableVariables[rows[1]] = Variable.Variable{IsNumeric: true, Value: intValue}
} else if strValue, isStr := handleString(nbLine, tokenValues[0]); isStr == true {
availableVariables[rows[1]] = Variable.Variable{IsString: true, Value: strValue}
}
} else {
handleArithmeticOperations(tokenValues, rows, nbLine)
handleArithmeticOperations(tokenValues, rows, nbLine)
}
}

// TODO: need to handled when doing an arithmetic operation with the value of another variable.
func isLet(rows []string, nbLine int) {
indexEqualSign := slices.Index(rows, "=")
lenRows := len(rows)
Expand Down
1 change: 1 addition & 0 deletions examples/let/math/addition.leango
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
let test = 1 + 1 + 1 - 5
let let = 5
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
logger "github.com/sirupsen/logrus"
"leango/Logger"
"leango/Token"
"log"
"os"
Expand All @@ -27,10 +28,14 @@ func parseFile(filePath string) {
checkErr(err)
lines := strings.Split(string(data), "\n")

for _, line := range lines {
for idxLine, line := range lines {
if line != "" {
tokens = strings.FieldsFunc(line, Split)
for idx, token := range tokens {
_, varNameExists := tokenFunctions[token]
if idx > 0 && varNameExists {
Logger.Fatal(idxLine, "You can't name a variable with the same name as a type")
}
if Token.IsTokenAvailable(token) {
tokenFunctions[token](tokens, idx+1)
}
Expand All @@ -47,5 +52,6 @@ func main() {
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 9de6362

Please sign in to comment.