feat: support variable declaration statement

This commit is contained in:
Lucàs
2024-07-03 00:18:17 +02:00
parent 69e88fd5fd
commit 9d3fa71e34
6 changed files with 131 additions and 4 deletions
+17
View File
@@ -2,6 +2,15 @@
open Syntax
(** [string_of_type t] returns a string representation of the type [t]. *)
let string_of_type = function
| Type_Integer -> "Type_Integer"
| Type_Float -> "Type_Float"
| Type_Character -> "Type_Character"
| Type_String -> "Type_String"
| Type_Boolean -> "Type_Boolean"
| Type_Void -> "Type_Void"
(** [string_of_binary_operator op] returns a string representation of the binary operator [op]. *)
let string_of_binary_operator = function
| Add -> "Add"
@@ -42,10 +51,18 @@ let rec string_of_expression = function
^ string_of_binary_operator op
^ ", " ^ string_of_expression e1 ^ ", " ^ string_of_expression e2 ^ ")"
let string_of_variable_declaration = function
| VariableDeclaration (t, id, e) ->
"VariableDeclaration(" ^ string_of_type t ^ ", " ^ string_of_expression id
^ ", " ^ string_of_expression e ^ ")"
(** [string_of_statement s] returns a string representation of the statement [s]. *)
let string_of_statement = function
| ExpressionStatement e ->
"ExpressionStatement(" ^ string_of_expression e ^ ")"
| VariableStatement decls ->
let decl_strings = List.map string_of_variable_declaration decls in
"VariableStatement([" ^ String.concat ", " decl_strings ^ "])"
(** [string_of_source_file f] returns a string representation of the source file [f]. *)
let string_of_source_file = function
+15 -1
View File
@@ -1,5 +1,13 @@
(* lib/ast/syntax.ml *)
type _type =
| Type_Integer
| Type_Float
| Type_Character
| Type_String
| Type_Boolean
| Type_Void
type literal =
| Integer of int
| Float of float
@@ -30,5 +38,11 @@ type expression =
| UnaryExpression of unary_operator * expression
| BinaryExpression of binary_operator * expression * expression
type statement = ExpressionStatement of expression
type variable_declaration =
| VariableDeclaration of _type * expression * expression
type statement =
| ExpressionStatement of expression
| VariableStatement of variable_declaration list
type source_file = SourceFile of statement list