mirror of
https://github.com/LucasVbr/croissant.git
synced 2026-05-13 17:12:10 +00:00
feat: Implement eval methods for expressions
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user