Skip to content

Commit

Permalink
eval infix operations for ints; support modulo
Browse files Browse the repository at this point in the history
  • Loading branch information
Houcine EL ADDALI committed Dec 19, 2023
1 parent cb776ee commit 4f4ae9c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
2 changes: 2 additions & 0 deletions internal/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func (l *Lexer) NextToken() token.Token {
}
case '/':
test = token.CreateToken(token.SLASH, string(l.char))
case '%':
test = token.CreateToken(token.MODULO, string(l.char))
case '*':
test = token.CreateToken(token.STAR, string(l.char))
case '!':
Expand Down
3 changes: 1 addition & 2 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func InitParser(l *lexer.Lexer) *Parser {
token.MINUS,
token.SLASH,
token.STAR,
token.MODULO,

token.EQUAL,
token.NOT_EQUAL,
Expand Down Expand Up @@ -213,7 +214,6 @@ func (p *Parser) parseForLoopExpression() ast.Expression {
exp.InitStm = p.parseDefStmtInForLoop()

if !p.expectedNextToken(token.CreateToken(token.S_COLON, ";")) {
fmt.Println("HEEEERE")
return nil
}
p.Next() // advance to the condition expression
Expand All @@ -222,7 +222,6 @@ func (p *Parser) parseForLoopExpression() ast.Expression {
if !p.expectedNextToken(token.CreateToken(token.S_COLON, ";")) {
return nil
}

p.Next() // advance to post iteration
exp.PostIteration = p.parseExpression(LOWEST)

Expand Down
1 change: 1 addition & 0 deletions internal/parser/parser_cte.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var precedences = map[token.TokenType]int{
token.MINUS: SUM,
token.SLASH: PRODUCT,
token.STAR: PRODUCT,
token.MODULO: PRODUCT,

token.LP: CALL,

Expand Down
36 changes: 33 additions & 3 deletions internal/runtime/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,42 @@ func Eval(node ast.Node) types.ObjectJIPL {
case *ast.PostfixExpression:
operand := Eval(node.Left)
return evalPostfixExpression(node.Operator, operand)
case *ast.InfixExpression:
leftOperand := Eval(node.Left)
rightOperand := Eval(node.Right)
return evalInfixExpression(node.Operator,leftOperand,rightOperand)
default:
return nil
}
}

func evalInfixExpression(operator string, leftOperand, rightOperand types.ObjectJIPL) types.ObjectJIPL {
if leftOperand.GetType() != types.T_INTEGER ||
rightOperand.GetType() !=types.T_INTEGER {
return nil
}
return evalIntInfixExpression(operator,leftOperand,rightOperand)
}


func evalIntInfixExpression(operator string, left, right types.ObjectJIPL) types.ObjectJIPL {
intObjRight := right.(*types.Integer)
intObjLeft := left.(*types.Integer)
switch operator {
case "+":
return &types.Integer{Val: intObjLeft.Val + intObjRight.Val}
case "-":
return &types.Integer{Val: intObjLeft.Val - intObjRight.Val}
case "*":
return &types.Integer{Val: intObjLeft.Val * intObjRight.Val}
case "/":
return &types.Integer{Val: intObjLeft.Val / intObjRight.Val}
case "%":
return &types.Integer{Val: intObjLeft.Val % intObjRight.Val}
default:
return nil
}
}

func evalPostfixExpression(operator string, operand types.ObjectJIPL) types.ObjectJIPL{
switch operator {
Expand All @@ -40,7 +70,7 @@ func evalPostfixExpression(operator string, operand types.ObjectJIPL) types.Obje
}

func evalIncrementPostfix(operand types.ObjectJIPL) types.ObjectJIPL {
if operand.GetType() != types.T_ITNTEGER {
if operand.GetType() != types.T_INTEGER {
return nil
}
intObj := operand.(*types.Integer)
Expand All @@ -49,7 +79,7 @@ func evalIncrementPostfix(operand types.ObjectJIPL) types.ObjectJIPL {
}

func evalDecrementPostfix(operand types.ObjectJIPL) types.ObjectJIPL{
if operand.GetType() != types.T_ITNTEGER {
if operand.GetType() != types.T_INTEGER{
return nil
}
intObj := operand.(*types.Integer)
Expand Down Expand Up @@ -78,7 +108,7 @@ func evalPrefixExpression(operator string, operand types.ObjectJIPL) types.Objec
}

func evalMinusPrefix(operand types.ObjectJIPL) types.ObjectJIPL {
if operand.GetType() != types.T_ITNTEGER {
if operand.GetType() != types.T_INTEGER {
return nil
}
intObj := operand.(*types.Integer)
Expand Down
1 change: 1 addition & 0 deletions internal/token/token_ctes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
NOT_EQUAL = 47 // !=
INCREMENT = 50 // ++
DECREMENT = 51 // --
MODULO = 56 // %

/*Comparators operators*/
LT = 52 // <
Expand Down
4 changes: 2 additions & 2 deletions internal/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func (intObj *Integer) ToString() string {
}

func (intObj *Integer) GetType() TypeObj {
return T_ITNTEGER
return T_INTEGER
}

// cte of types
const (
T_ITNTEGER = "INTEGER"
T_INTEGER = "INTEGER"
T_BOOLEAN = "BOOLEAN"
T_UNDEFINED = "UNDEFINED"
)
Expand Down

0 comments on commit 4f4ae9c

Please sign in to comment.