Skip to content

Commit

Permalink
add modulo
Browse files Browse the repository at this point in the history
Signed-off-by: Flipez <code@brauser.io>
  • Loading branch information
Flipez committed Feb 3, 2022
1 parent 8793156 commit adc91a1
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func TestEvalIntegerExpression(t *testing.T) {
{"3 * (3 * 3) + 10", 37},
{"(5 + 10 * 2 + 15 / 3) * 2 + -10", 50},
{"5 ➕ 5 ➕ 5 ➕ 5 - 10", 10},
{"5 % 5", 0},
{"5 % 4", 1},
}

for _, tt := range tests {
Expand Down
5 changes: 5 additions & 0 deletions evaluator/infix.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func evalIntegerInfix(operator string, left, right object.Object) object.Object
return object.NewErrorFormat("devision by zero not allowed")
}
return object.NewInteger(leftVal / rightVal)
case "%":
if rightVal == 0 {
return object.NewErrorFormat("devision by zero not allowed")
}
return object.NewInteger(leftVal % rightVal)
case "<":
return nativeBoolToBooleanObject(leftVal < rightVal)
case "<=":
Expand Down
3 changes: 3 additions & 0 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func (l *Lexer) NextToken() token.Token {
case '*':
tok.Type = token.ASTERISK
tok.Literal = string(l.ch)
case '%':
tok.Type = token.PERCENT
tok.Literal = string(l.ch)
case '<':
if l.peekChar() == '=' {
ch := l.ch
Expand Down
5 changes: 5 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestNextToken(t *testing.T) {
{"foo": "bar"}
5 <= 10 >= 5;
4 % 3;
`

tests := []struct {
Expand Down Expand Up @@ -126,6 +127,10 @@ func TestNextToken(t *testing.T) {
{token.GT_EQ, ">="},
{token.INT, "5"},
{token.SEMICOLON, ";"},
{token.INT, "4"},
{token.PERCENT, "%"},
{token.INT, "3"},
{token.SEMICOLON, ";"},
{token.EOF, ""},
}

Expand Down
3 changes: 3 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
LESSGREATER // > or <
SUM // +
PRODUCT // *
MODULO // %
PREFIX // -X or !X
CALL // myFcuntion(X)
INDEX // array[index]
Expand All @@ -33,6 +34,7 @@ var precedences = map[token.TokenType]int{
token.MINUS: SUM,
token.SLASH: PRODUCT,
token.ASTERISK: PRODUCT,
token.PERCENT: MODULO,
token.LPAREN: CALL,
token.PERIOD: CALL,
token.LBRACKET: INDEX,
Expand Down Expand Up @@ -87,6 +89,7 @@ func New(l *lexer.Lexer, imports map[string]struct{}) *Parser {
p.registerInfix(token.MINUS, p.parseInfix)
p.registerInfix(token.SLASH, p.parseInfix)
p.registerInfix(token.ASTERISK, p.parseInfix)
p.registerInfix(token.PERCENT, p.parseInfix)
p.registerInfix(token.EQ, p.parseInfix)
p.registerInfix(token.NOT_EQ, p.parseInfix)
p.registerInfix(token.PERIOD, p.parseMethodCall)
Expand Down
1 change: 1 addition & 0 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
BANG = "!"
ASTERISK = "*"
SLASH = "/"
PERCENT = "%"

LT = "<"
LT_EQ = "<="
Expand Down

0 comments on commit adc91a1

Please sign in to comment.