Skip to content

Commit

Permalink
made math operator parser but need to add the parser to the interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
mrredo committed Apr 8, 2023
1 parent 29803dc commit 72d5f34
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 44 deletions.
25 changes: 5 additions & 20 deletions .idea/workspace.xml

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

11 changes: 11 additions & 0 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const (
GREATER_THAN // >
GREATER_THAN_TO_EQUAL // >=
AND // &&
SEMICOLON // ;
NEW_LINE // \n
)

var numReg = regexp.MustCompile(`\d`)
Expand All @@ -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: ""}
}
Expand All @@ -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()
Expand Down
84 changes: 62 additions & 22 deletions lexer/lexerArithmeticOperations.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package lexer

import (
"RedoLanguage/std"
"fmt"

"github.com/Knetic/govaluate"
)

Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func main() {
//
//lx.TestMath()
//n := lx.NewLexer(`(10+10)*10`)
//l := lx.NewLexer("10 + 10 * 10")
//
Expand Down
4 changes: 3 additions & 1 deletion std/Variables.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package std

var Variables = map[string]any{}
var Variables = map[string]any{
//"n": 10,
}

0 comments on commit 72d5f34

Please sign in to comment.