feat: Implement eval methods for expressions

This commit is contained in:
Lucàs
2024-10-26 22:40:50 +02:00
parent b493c77d70
commit 9e201e8d16
28 changed files with 638 additions and 325 deletions
+39 -4
View File
@@ -1,5 +1,40 @@
type unary_operators = ArithmeticNegation | LogicalNegation
class virtual unaryOperator =
object
inherit Node.node
method virtual check_type : Types.t -> Types.t
method virtual eval : Literals.literal_value -> Literals.literal_value
end
let pp_unary_operators = function
| ArithmeticNegation -> "ArithmeticNegation"
| LogicalNegation -> "LogicalNegation"
class arithmeticNegation =
object
inherit unaryOperator
method to_string = "ArithmeticNegation"
method check_type (operand : Types.t) =
match operand with
| Types.IntegerType -> Types.IntegerType
| Types.FloatType -> Types.FloatType
| _ -> raise (Failure "ArithmeticNegation: operand must be a number")
method eval operand =
match operand with
| `Int i -> `Int (-i)
| `Float f -> `Float (-.f)
| _ -> raise (Failure "ArithmeticNegation: operand must be a number")
end
class logicalNegation =
object
inherit unaryOperator
method to_string = "LogicalNegation"
method check_type (operand : Types.t) =
match operand with
| Types.BooleanType -> Types.BooleanType
| _ -> raise (Failure "LogicalNegation: operand must be a boolean")
method eval operand =
match operand with
| `Bool b -> `Bool (not b)
| _ -> raise (Failure "LogicalNegation: operand must be a boolean")
end