refactor: Syntax with unit tests

This commit is contained in:
Lucàs
2024-07-08 16:44:42 +02:00
parent 6df363c78d
commit 2e76ccf9fc
39 changed files with 460 additions and 771 deletions
+7
View File
@@ -0,0 +1,7 @@
type binary_operators = Add | Subtract | Multiply | Divide
let pp_binary_operators = function
| Add -> "Add"
| Subtract -> "Subtract"
| Multiply -> "Multiply"
| Divide -> "Divide"
+21
View File
@@ -0,0 +1,21 @@
open Literals
open UnaryOperators
open BinaryOperators
type expressions =
| Literal of literals
| UnaryExpression of unary_operators * expressions
| BinaryExpression of binary_operators * expressions * expressions
let rec pp_expressions = function
| Literal l ->
let pp_l = pp_literals l in
Printf.sprintf "Literal(%s)" pp_l
| 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
+15
View File
@@ -0,0 +1,15 @@
type literals =
| Integer of int
| Float of float
| Character of char
| String of string
| Boolean of bool
| Null
let pp_literals = function
| Integer i -> Printf.sprintf "Integer(%d)" i
| Float f -> Printf.sprintf "Float(%f)" f
| Character c -> Printf.sprintf "Character('%c')" c
| String s -> Printf.sprintf "String(\"%s\")" s
| Boolean b -> Printf.sprintf "Boolean(%b)" b
| Null -> "Null"
+8
View File
@@ -0,0 +1,8 @@
open Statements
type source_files = SourceFile of statements
let pp_source_files = function
| SourceFile stmts ->
let pp_stmt = pp_statements stmts in
Printf.sprintf "SourceFile(%s)" pp_stmt
+13
View File
@@ -0,0 +1,13 @@
open Expressions
type statements =
| SequenceStatement of statements * statements
| ExpressionStatement of expressions
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
+15
View File
@@ -0,0 +1,15 @@
type types =
| IntegerType
| FloatType
| CharacterType
| StringType
| BooleanType
| VoidType
let pp_types = function
| IntegerType -> "IntegerType"
| FloatType -> "FloatType"
| CharacterType -> "CharacterType"
| StringType -> "StringType"
| BooleanType -> "BooleanType"
| VoidType -> "VoidType"
+5
View File
@@ -0,0 +1,5 @@
type unary_operators = ArithmeticNegation | LogicalNegation
let pp_unary_operators = function
| ArithmeticNegation -> "ArithmeticNegation"
| LogicalNegation -> "LogicalNegation"
+10
View File
@@ -0,0 +1,10 @@
(library
(name Syntax)
(modules
Types
Literals
BinaryOperators
UnaryOperators
Expressions
Statements
SourceFiles))