diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index f02ede8..3eeae95 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,11 +4,12 @@
-
+
-
+
+
@@ -58,14 +59,6 @@
-
-
-
-
-
-
-
-
@@ -86,15 +79,6 @@
-
-
-
-
-
-
-
-
-
@@ -114,7 +98,8 @@
-
+
+
true
diff --git a/lexer/lexer.go b/lexer/lexer.go
index 5509c82..d18e96c 100644
--- a/lexer/lexer.go
+++ b/lexer/lexer.go
@@ -47,6 +47,8 @@ const (
GREATER_THAN // >
GREATER_THAN_TO_EQUAL // >=
AND // &&
+ SEMICOLON // ;
+ NEW_LINE // \n
)
var numReg = regexp.MustCompile(`\d`)
@@ -70,7 +72,12 @@ func NewLexer(input string) *Lexer {
func (l *Lexer) NextToken() Token {
tok := l.Scanner.Scan()
+ //var lastLine int
for tok == scanner.Comment || tok == scanner.EOF {
+ //if l.Scanner.Pos().Line != lastLine { // check if the line number has changed
+ // fmt.Println("Newline character found")
+ // lastLine = l.Scanner.Pos().Line // update lastLine to the current line number
+ //}
if tok == scanner.EOF {
return Token{Type: EOF, Value: ""}
}
@@ -94,6 +101,10 @@ func (l *Lexer) NextToken() Token {
}
return Token{Type: ASSIGN, Value: val}
+ case '\n': // \n
+ return Token{Type: NEW_LINE, Value: "\n"}
+ case ';':
+ return Token{Type: SEMICOLON, Value: ";"}
case '+':
if l.Scanner.Peek() == '=' {
l.NextToken()
diff --git a/lexer/lexerArithmeticOperations.go b/lexer/lexerArithmeticOperations.go
index cc24501..395609e 100644
--- a/lexer/lexerArithmeticOperations.go
+++ b/lexer/lexerArithmeticOperations.go
@@ -1,8 +1,8 @@
package lexer
import (
+ "RedoLanguage/std"
"fmt"
-
"github.com/Knetic/govaluate"
)
@@ -49,32 +49,72 @@ func IsMathExpression(curT Token, secondT Token, lexer *Lexer) bool { //10 +
func ReplaceAllIdentsWithValue(c Token, s Token, l *Lexer) (interface{}, error) {
return nil, nil
}
-func MathExpressionTokensToEnd(c Token, s Token, l *Lexer) ([]Token, error) {
+func TestMath() {
+ l := NewLexer("modulo(11,n)/n+10+10; 10")
+ c := l.NextToken()
+ str, _ := MathExpressionTokensToEnd(c, l)
+ fmt.Println(ParseArithmeticExpressions(str))
+}
+func MathExpressionTokensToEnd(c Token, l *Lexer) (string, error) {
var tokenArr []Token
+
RPcount, LPcount := 0, 0
- if s.Type == RPAREN {
- tokenArr = append(tokenArr, s)
- RPcount++
- }
- switch c.Type {
- case IDENTIFIER:
- if s.Type == LPAREN {
- //function
+ var finalStr string
+ for {
+ if c.Type == SEMICOLON || c.Type == NEW_LINE || c.Type == EOF || c.Type == COMMA {
+ break
}
- //variable
- case BOOL, NUMBER:
- tokenArr = append(tokenArr, c)
- case LPAREN:
- tokenArr = append(tokenArr, c)
- LPcount++
+ switch c.Type {
+ case IDENTIFIER:
+ s := l.NextToken()
+ if s.Type == LPAREN {
+ f, args, err := ParseFunctionCall(c, s, l)
+ if err != nil {
+ return "", err
+ }
+ out, ok := std.Functions[f]
- case RPAREN:
- return nil, fmt.Errorf("first token to a math expression can't be a ')'")
- }
- if LPcount != RPcount {
- return nil, fmt.Errorf("invalid left/right parentheses count")
+ if !ok {
+ return "", fmt.Errorf("'%s' function is not defined", c.Value)
+ }
+ o := out(args...)
+ if o == nil {
+ return "", fmt.Errorf("invalid function call: '%s' function does not return a value", c.Value)
+ }
+ finalStr += fmt.Sprint(o)
+ c = l.NextToken()
+ continue
+ } else {
+ va, ok := std.Variables[c.Value]
+ if !ok {
+ return "", fmt.Errorf("'%s' is not defined", c.Value)
+ }
+ finalStr += fmt.Sprint(va)
+ tokenArr = append(tokenArr, c)
+ }
+
+ case LPAREN:
+ LPcount++
+ finalStr += "("
+ tokenArr = append(tokenArr, c)
+ case RPAREN:
+ RPcount++
+ finalStr += ")"
+ tokenArr = append(tokenArr, c)
+ default:
+ finalStr += c.Value
+ tokenArr = append(tokenArr, c)
+ }
+
+ c = l.NextToken()
+ //if c.Type != RPAREN || c.Type != LPAREN {
+ // OperatorTurn = !OperatorTurn
+ //}
}
- return tokenArr, nil
+ //if LPcount != RPcount {
+ // return "", fmt.Errorf("invalid left/right parentheses count")
+ //}
+ return finalStr, nil
}
diff --git a/main.go b/main.go
index e0ae780..a70e722 100644
--- a/main.go
+++ b/main.go
@@ -9,7 +9,7 @@ import (
)
func main() {
- //
+ //lx.TestMath()
//n := lx.NewLexer(`(10+10)*10`)
//l := lx.NewLexer("10 + 10 * 10")
//
diff --git a/std/Variables.go b/std/Variables.go
index bb84304..570e092 100644
--- a/std/Variables.go
+++ b/std/Variables.go
@@ -1,3 +1,5 @@
package std
-var Variables = map[string]any{}
+var Variables = map[string]any{
+ //"n": 10,
+}