From 9de6362648f7222e895ea3fc34059190127e24f9 Mon Sep 17 00:00:00 2001 From: mohammed Date: Tue, 9 Apr 2024 20:36:22 +0200 Subject: [PATCH] feat: declaring a variable name with the same name as a type --- Token/handler.go | 111 ++++++++++++++++-------------- examples/let/math/addition.leango | 1 + main.go | 8 ++- 3 files changed, 66 insertions(+), 54 deletions(-) diff --git a/Token/handler.go b/Token/handler.go index 0cb8fbd..a855ef1 100644 --- a/Token/handler.go +++ b/Token/handler.go @@ -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) diff --git a/examples/let/math/addition.leango b/examples/let/math/addition.leango index 24250ad..1baa2c2 100644 --- a/examples/let/math/addition.leango +++ b/examples/let/math/addition.leango @@ -1 +1,2 @@ let test = 1 + 1 + 1 - 5 +let let = 5 diff --git a/main.go b/main.go index 8dccbba..c1e2dc4 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "fmt" logger "github.com/sirupsen/logrus" + "leango/Logger" "leango/Token" "log" "os" @@ -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) } @@ -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]) }