Skip to content

Commit

Permalink
still making math operators i will make them work on a paper then mak…
Browse files Browse the repository at this point in the history
…e it with a real plan
  • Loading branch information
mrredo committed Apr 8, 2023
1 parent 9e67f74 commit 29803dc
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 68 deletions.
26 changes: 21 additions & 5 deletions .idea/workspace.xml

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

83 changes: 44 additions & 39 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
@@ -1,54 +1,56 @@
package lexer

import (
"regexp"
"strings"
"text/scanner"
)

type TokenType int

const (
EOF TokenType = iota
LPAREN
RPAREN
COMMA
IDENTIFIER
NUMBER
STRING
EQUAL
ASSIGN
VAR
BOOL
MODULO
DIVIDE
PLUS
MULTIPLY
SUBTRACT
PLUS_ASSIGN
SUBTRACT_ASSIGN
MULTIPLY_ASSIGN
DIVIDE_ASSIGN
MODULO_ASSIGN
LEFT_SHIFT_ASSIGN
RIGHT_SHIFT_ASSIGN
BITWISE_AND_ASSIGN
BITWISE_XOR_ASSIGN
PLUS_PLUS
SUBTRACT_SUBTRACT
BITWISE_XOR
LEFT_SHIFT
RIGHT_SHIFT
BITWISE_AND
EQUAL_TO
NOT_EQUAL_TO
LESS_THAN
LESS_THAN_OR_EQUAL
GREATER_THAN
GREATER_THAN_TO_EQUAL
AND
L
EOF TokenType = iota // end of file
LPAREN // (
RPAREN // )
COMMA // ,
IDENTIFIER // variable identifier
NUMBER // numeric literal
STRING // string literal
EQUAL // ==
ASSIGN // =
VAR // var keyword
BOOL // boolean literal
MODULO // %
DIVIDE // /
PLUS // +
MULTIPLY // *
SUBTRACT // -
PLUS_ASSIGN // +=
SUBTRACT_ASSIGN // -=
MULTIPLY_ASSIGN // *=
DIVIDE_ASSIGN // /=
MODULO_ASSIGN // %=
LEFT_SHIFT_ASSIGN // <<=
RIGHT_SHIFT_ASSIGN // >>=
BITWISE_AND_ASSIGN // &=
BITWISE_XOR_ASSIGN // ^=
PLUS_PLUS // ++
SUBTRACT_SUBTRACT // --
BITWISE_XOR // ^
LEFT_SHIFT // <<
RIGHT_SHIFT // >>
BITWISE_AND // &
EQUAL_TO // ==
NOT_EQUAL_TO // !=
LESS_THAN // <
LESS_THAN_OR_EQUAL // <=
GREATER_THAN // >
GREATER_THAN_TO_EQUAL // >=
AND // &&
)

var numReg = regexp.MustCompile(`\d`)

type Token struct {
Type TokenType
Value string
Expand Down Expand Up @@ -102,7 +104,10 @@ func (l *Lexer) NextToken() Token {
if l.Scanner.Peek() == '=' {
l.NextToken()
return Token{Type: SUBTRACT_ASSIGN, Value: "-="}
} else if numReg.MatchString(string(l.Scanner.Peek())) {
return Token{Type: NUMBER, Value: "-" + l.NextToken().Value}
}

return Token{Type: SUBTRACT, Value: val}
case '*':
if l.Scanner.Peek() == '=' {
Expand Down
52 changes: 32 additions & 20 deletions lexer/lexerArithmeticOperations.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,35 @@ func IsMathExpression(curT Token, secondT Token, lexer *Lexer) bool { //10 +
return false
}

//func ReplaceAllIdentsWithValue(c Token, s Token, l *Lexer) (interface{}, error) {
// switch c.Type {
// case IDENTIFIER:
// if s.Type == RPAREN {
// funName, args, err := ParseFunctionCall(c, s, l)
// if err != nil {
// return nil, err
// }
//
// }
// va, ok := std.Variables[c.Value]
// if !ok {
// return nil, fmt.Errorf("'%s' is not defined", c.Value)
// }
// return va, nil
//
// default:
// return nil, nil
// }
//}
func ReplaceAllIdentsWithValue(c Token, s Token, l *Lexer) (interface{}, error) {
return nil, nil
}
func MathExpressionTokensToEnd(c Token, s Token, l *Lexer) ([]Token, 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
}

//variable
case BOOL, NUMBER:
tokenArr = append(tokenArr, c)
case LPAREN:
tokenArr = append(tokenArr, c)
LPcount++

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")
}
return tokenArr, nil
}
24 changes: 20 additions & 4 deletions lexer/lexerVariableOperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,45 @@ it detects its a expression, then u can to lexer.NextToken()
func IsVariableExpression(curT Token, exp Token, lexer *Lexer) bool {
//pk := lexer.Scanner.Peek()

return curT.Type == IDENTIFIER && (exp.Type == PLUS_ASSIGN || exp.Type == SUBTRACT_ASSIGN || exp.Type == MULTIPLY_ASSIGN || exp.Type == DIVIDE_ASSIGN || exp.Type == MODULO_ASSIGN) //pk != '(' //(pk == '+' || pk == '-' || pk == '*' || pk == '/' || pk == '%' || pk == '=')
return curT.Type == IDENTIFIER && (exp.Type == ASSIGN || exp.Type == PLUS_ASSIGN || exp.Type == SUBTRACT_ASSIGN || exp.Type == MULTIPLY_ASSIGN || exp.Type == DIVIDE_ASSIGN || exp.Type == MODULO_ASSIGN) //pk != '(' //(pk == '+' || pk == '-' || pk == '*' || pk == '/' || pk == '%' || pk == '=')
}
func ParseVariableAssigningExpression(key Token, expression Token, value Token, lexer *Lexer) (output int, err error) {
exp := expression
if value.Type != NUMBER && value.Type != IDENTIFIER {
return 0, fmt.Errorf("expected an integer, but got '%s'", value.Value)
if value.Type != NUMBER && value.Type != IDENTIFIER && value.Type != BOOL {
if exp.Type != ASSIGN {
return 0, fmt.Errorf("expected an integer, but got '%s'", value.Value)
}

}
if exp.Type != ASSIGN && value.Type == BOOL {
return 0, fmt.Errorf("booleans only support '=' operator for assigning")
}
if key.Type != IDENTIFIER {
return 0, fmt.Errorf("expected an identifier, but got '%s'", key.Value)
}

k, ok := std.Variables[key.Value]
if value.Type == BOOL && expression.Type == ASSIGN {
std.Variables[key.Value] = value.Value == "true"
return 0, err
}
if !ok {
return 0, fmt.Errorf("'%s' is not defined", key.Value)
}
if reflect.TypeOf(k).String() != "int" {
return 0, fmt.Errorf("can not do math operations on a non integer '%s'", key.Value)
}
vals, err := ParseExpression(value, lexer)
valI := vals.(int)

valI, ok := vals.(int)

if err != nil {

return 0, err
}
if !ok {
return 0, nil
}
switch exp.Type {
case PLUS_ASSIGN:
std.Variables[key.Value] = k.(int) + valI
Expand Down

0 comments on commit 29803dc

Please sign in to comment.