mirror of
https://github.com/LucasVbr/croissant.git
synced 2026-05-13 17:12:10 +00:00
40 lines
1.2 KiB
OCaml
40 lines
1.2 KiB
OCaml
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
|
|
|
|
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 |