refactor: Split ast into ast/syntax and ast/print

This commit is contained in:
Lucàs
2024-07-02 18:13:58 +02:00
parent 273bb2efe0
commit ce66b67cde
2 changed files with 25 additions and 11 deletions
+13 -11
View File
@@ -1,33 +1,35 @@
(* lib/ast.ml *) (* lib/ast/print.ml *)
type binary_operator = Add | Substract | Multiply | Divide open Syntax
type expression =
| IntegerLiteral of int
| BinaryExpression of binary_operator * expression * expression
type statement = ExpressionStatement of expression
type source_file = SourceFile of statement list
(* Print AST *)
(** [string_of_binary_operator op] returns a string representation of the binary operator [op]. *)
let string_of_binary_operator = function let string_of_binary_operator = function
| Add -> "Add" | Add -> "Add"
| Substract -> "Substract" | Substract -> "Substract"
| Multiply -> "Multiply" | Multiply -> "Multiply"
| Divide -> "Divide" | Divide -> "Divide"
(** [string_of_unary_operator op] returns a string representation of the unary operator [op]. *)
let string_of_unary_operator = function Negate -> "Negate"
(** [string_of_expression e] returns a string representation of the expression [e]. *)
let rec string_of_expression = function let rec string_of_expression = function
| IntegerLiteral i -> "IntegerLiteral(" ^ string_of_int i ^ ")" | IntegerLiteral i -> "IntegerLiteral(" ^ string_of_int i ^ ")"
| UnaryExpression (op, e) ->
"UnaryExpression("
^ string_of_unary_operator op
^ ", " ^ string_of_expression e ^ ")"
| BinaryExpression (op, e1, e2) -> | BinaryExpression (op, e1, e2) ->
"BinaryExpression(" "BinaryExpression("
^ string_of_binary_operator op ^ string_of_binary_operator op
^ ", " ^ string_of_expression e1 ^ ", " ^ string_of_expression e2 ^ ")" ^ ", " ^ string_of_expression e1 ^ ", " ^ string_of_expression e2 ^ ")"
(** [string_of_statement s] returns a string representation of the statement [s]. *)
let string_of_statement = function let string_of_statement = function
| ExpressionStatement e -> | ExpressionStatement e ->
"ExpressionStatement(" ^ string_of_expression e ^ ")" "ExpressionStatement(" ^ string_of_expression e ^ ")"
(** [string_of_source_file f] returns a string representation of the source file [f]. *)
let string_of_source_file = function let string_of_source_file = function
| SourceFile stmts -> | SourceFile stmts ->
let stmt_strings = List.map string_of_statement stmts in let stmt_strings = List.map string_of_statement stmts in
+12
View File
@@ -0,0 +1,12 @@
(* lib/ast/syntax.ml *)
type binary_operator = Add | Substract | Multiply | Divide
type unary_operator = Negate
type expression =
| IntegerLiteral of int
| UnaryExpression of unary_operator * expression
| BinaryExpression of binary_operator * expression * expression
type statement = ExpressionStatement of expression
type source_file = SourceFile of statement list