mirror of
https://github.com/LucasVbr/croissant.git
synced 2026-12-05 15:20:50 +00:00
feat: Implement eval methods for expressions
This commit is contained in:
+57
-21
@@ -1,23 +1,59 @@
|
||||
open Literals
|
||||
open UnaryOperators
|
||||
open BinaryOperators
|
||||
class virtual expression =
|
||||
object
|
||||
inherit Node.node
|
||||
method virtual check_type : Environments.typeEnvironments -> Types.t
|
||||
method virtual eval : Environments.evalEnvironment -> Literals.literal_value
|
||||
end
|
||||
|
||||
type expressions =
|
||||
| Literal of literals
|
||||
| Identifier of string
|
||||
| UnaryExpression of unary_operators * expressions
|
||||
| BinaryExpression of binary_operators * expressions * expressions
|
||||
class literal (lit : Literals.literal) =
|
||||
object
|
||||
inherit expression
|
||||
method to_string = "Literal(" ^ lit#to_string ^ ")"
|
||||
method check_type _ = lit#check_type
|
||||
method eval _ = lit#value
|
||||
end
|
||||
|
||||
let rec pp_expressions = function
|
||||
| Literal l ->
|
||||
let pp_l = pp_literals l in
|
||||
Printf.sprintf "Literal(%s)" pp_l
|
||||
| Identifier i -> Printf.sprintf "Identifier(\"%s\")" i
|
||||
| UnaryExpression (op, e) ->
|
||||
let pp_op = pp_unary_operators op and pp_e = pp_expressions e in
|
||||
Printf.sprintf "UnaryExpression(%s, %s)" pp_op pp_e
|
||||
| BinaryExpression (op, e1, e2) ->
|
||||
let pp_op = pp_binary_operators op
|
||||
and pp_e1 = pp_expressions e1
|
||||
and pp_e2 = pp_expressions e2 in
|
||||
Printf.sprintf "BinaryExpression(%s, %s, %s)" pp_op pp_e1 pp_e2
|
||||
class identifier (name : string) =
|
||||
object
|
||||
inherit expression
|
||||
method to_string = "Identifier(" ^ name ^ ")"
|
||||
|
||||
method check_type env =
|
||||
try env#find name
|
||||
with Not_found ->
|
||||
raise (Failure ("Identifier '" ^ name ^ "' not found"))
|
||||
|
||||
method eval env = env#find name
|
||||
end
|
||||
|
||||
class unaryOperation (operator : UnaryOperators.unaryOperator)
|
||||
(operand : expression) =
|
||||
object
|
||||
inherit expression
|
||||
|
||||
method to_string =
|
||||
"UnaryOperation(" ^ operator#to_string ^ ", " ^ operand#to_string ^ ")"
|
||||
|
||||
method check_type env =
|
||||
let operand_type = operand#check_type env in
|
||||
operator#check_type operand_type
|
||||
|
||||
method eval env = operator#eval (operand#eval env)
|
||||
end
|
||||
|
||||
class binaryOperation (operator : BinaryOperators.binaryOperator)
|
||||
(left : expression) (right : expression) =
|
||||
object
|
||||
inherit expression
|
||||
|
||||
method to_string =
|
||||
"BinaryOperation(" ^ operator#to_string ^ ", " ^ left#to_string ^ ", "
|
||||
^ right#to_string ^ ")"
|
||||
|
||||
method check_type env =
|
||||
let left_type = left#check_type env in
|
||||
let right_type = right#check_type env in
|
||||
operator#check_type (left_type, right_type)
|
||||
|
||||
method eval env = operator#eval (left#eval env, right#eval env)
|
||||
end
|
||||
Reference in New Issue
Block a user