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
+50 -11
View File
@@ -1,13 +1,52 @@
open Expressions
class virtual statement =
object
inherit Node.node
method virtual check_type : Environments.typeEnvironments -> Types.t
method virtual eval : Environments.evalEnvironment -> Literals.literal_value
end
type statements =
| SequenceStatement of statements * statements
| ExpressionStatement of expressions
class sequence (left : statement) (right : statement) =
object
inherit statement
let rec pp_statements = function
| SequenceStatement (stmt1, stmt2) ->
let pp_stmt1 = pp_statements stmt1 and pp_stmt2 = pp_statements stmt2 in
Printf.sprintf "SequenceStatement(%s, %s)" pp_stmt1 pp_stmt2
| ExpressionStatement expr ->
let pp_expr = pp_expressions expr in
Printf.sprintf "ExpressionStatement(%s)" pp_expr
method to_string =
"Sequence(" ^ left#to_string ^ ", " ^ right#to_string ^ ")"
method check_type env =
let _ = left#check_type env in
right#check_type env
method eval env =
let _ = left#eval env in
right#eval env
end
class expression (expression : Expressions.expression) =
object
inherit statement
method to_string = "Expression(" ^ expression#to_string ^ ")"
method check_type env = expression#check_type env
method eval env = expression#eval env
end
class variableDeclaration (name : string) (tp : Types.t)
(expression : Expressions.expression) =
object
inherit statement
method to_string =
"VariableDeclaration(" ^ name ^ ", " ^ Types.string_of_t tp ^ ", "
^ expression#to_string ^ ")"
method check_type env =
let expression_type = expression#check_type env in
if tp = expression_type then (
env#add name expression_type;
Types.VoidType)
else raise (Failure "VariableDeclaration: type mismatch")
method eval env =
let value = expression#eval env in
env#add name value;
value
end