mirror of
https://github.com/LucasVbr/croissant.git
synced 2026-05-13 17:12:10 +00:00
refactor: Edit type name for consistency
This commit is contained in:
+18
-6
@@ -4,12 +4,12 @@ open Syntax
|
|||||||
|
|
||||||
(** [string_of_type t] returns a string representation of the type [t]. *)
|
(** [string_of_type t] returns a string representation of the type [t]. *)
|
||||||
let string_of_type = function
|
let string_of_type = function
|
||||||
| Type_Integer -> "Type_Integer"
|
| IntegerType -> "IntegerType"
|
||||||
| Type_Float -> "Type_Float"
|
| FloatType -> "FloatType"
|
||||||
| Type_Character -> "Type_Character"
|
| CharacterType -> "CharacterType"
|
||||||
| Type_String -> "Type_String"
|
| StringType -> "StringType"
|
||||||
| Type_Boolean -> "Type_Boolean"
|
| BooleanType -> "BooleanType"
|
||||||
| Type_Void -> "Type_Void"
|
| VoidType -> "VoidType"
|
||||||
|
|
||||||
(** [string_of_binary_operator op] returns a string representation of the binary operator [op]. *)
|
(** [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
|
||||||
@@ -69,3 +69,15 @@ 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
|
||||||
"SourceFile([" ^ String.concat ", " stmt_strings ^ "])"
|
"SourceFile([" ^ String.concat ", " stmt_strings ^ "])"
|
||||||
|
|
||||||
|
(** The signature of the module [PRINT]. *)
|
||||||
|
module type Print = sig
|
||||||
|
val string_of_type : Syntax._type -> string
|
||||||
|
val string_of_binary_operator : Syntax.binary_operator -> string
|
||||||
|
val string_of_unary_operator : Syntax.unary_operator -> string
|
||||||
|
val string_of_literal : Syntax.literal -> string
|
||||||
|
val string_of_expression : Syntax.expression -> string
|
||||||
|
val string_of_variable_declaration : Syntax.variable_declaration -> string
|
||||||
|
val string_of_statement : Syntax.statement -> string
|
||||||
|
val string_of_source_file : Syntax.source_file -> string
|
||||||
|
end
|
||||||
+6
-6
@@ -1,12 +1,12 @@
|
|||||||
(* lib/ast/syntax.ml *)
|
(* lib/ast/syntax.ml *)
|
||||||
|
|
||||||
type _type =
|
type _type =
|
||||||
| Type_Integer
|
| IntegerType
|
||||||
| Type_Float
|
| FloatType
|
||||||
| Type_Character
|
| CharacterType
|
||||||
| Type_String
|
| StringType
|
||||||
| Type_Boolean
|
| BooleanType
|
||||||
| Type_Void
|
| VoidType
|
||||||
|
|
||||||
type literal =
|
type literal =
|
||||||
| Integer of int
|
| Integer of int
|
||||||
|
|||||||
+6
-6
@@ -75,12 +75,12 @@ variable_declaration:
|
|||||||
| IDENTIFIER ":" tp { VariableDeclaration($3, Identifier($1), Literal(Null)) }
|
| IDENTIFIER ":" tp { VariableDeclaration($3, Identifier($1), Literal(Null)) }
|
||||||
|
|
||||||
tp:
|
tp:
|
||||||
| INTEGER_TYPE { Type_Integer }
|
| INTEGER_TYPE { IntegerType }
|
||||||
| FLOAT_TYPE { Type_Float }
|
| FLOAT_TYPE { FloatType }
|
||||||
| CHARACTER_TYPE { Type_Character }
|
| CHARACTER_TYPE { CharacterType }
|
||||||
| STRING_TYPE { Type_String }
|
| STRING_TYPE { StringType }
|
||||||
| BOOLEAN_TYPE { Type_Boolean }
|
| BOOLEAN_TYPE { BooleanType }
|
||||||
| VOID_TYPE { Type_Void }
|
| VOID_TYPE { VoidType }
|
||||||
|
|
||||||
expression:
|
expression:
|
||||||
| literal { Literal($1) }
|
| literal { Literal($1) }
|
||||||
|
|||||||
+123
-78
@@ -2,100 +2,145 @@
|
|||||||
|
|
||||||
open Alcotest
|
open Alcotest
|
||||||
open Ast.Syntax
|
open Ast.Syntax
|
||||||
open Ast.Print
|
|
||||||
|
module To_test = struct
|
||||||
|
let _type = Ast.Print.string_of_type
|
||||||
|
let literal = Ast.Print.string_of_literal
|
||||||
|
let binary_operator = Ast.Print.string_of_binary_operator
|
||||||
|
let unary_operator = Ast.Print.string_of_unary_operator
|
||||||
|
let expression = Ast.Print.string_of_expression
|
||||||
|
let statement = Ast.Print.string_of_statement
|
||||||
|
let source_file = Ast.Print.string_of_source_file
|
||||||
|
end
|
||||||
|
|
||||||
let test_string_of_type () =
|
let test_string_of_type () =
|
||||||
check string "int" "Type_Integer" (string_of_type Type_Integer);
|
let tests =
|
||||||
check string "float" "Type_Float" (string_of_type Type_Float);
|
|
||||||
check string "bool" "Type_Boolean" (string_of_type Type_Boolean);
|
|
||||||
check string "string" "Type_String" (string_of_type Type_String);
|
|
||||||
check string "char" "Type_Character" (string_of_type Type_Character);
|
|
||||||
check string "void" "Type_Void" (string_of_type Type_Void)
|
|
||||||
|
|
||||||
let test_string_of_literal () =
|
|
||||||
check string "42" "Integer(42)" (string_of_literal (Integer 42));
|
|
||||||
check string "3.14" "Float(3.14)" (string_of_literal (Float 3.14));
|
|
||||||
check string "true" "Boolean(true)" (string_of_literal (Boolean true));
|
|
||||||
check string "false" "Boolean(false)" (string_of_literal (Boolean false));
|
|
||||||
check string "hello" "String(\"hello\")" (string_of_literal (String "hello"));
|
|
||||||
check string "c" "Character('c')" (string_of_literal (Character 'c'));
|
|
||||||
check string "null" "Null" (string_of_literal Null)
|
|
||||||
|
|
||||||
let test_string_of_binary_operator () =
|
|
||||||
check string "+" "Add" (string_of_binary_operator Add);
|
|
||||||
check string "-" "Substract" (string_of_binary_operator Substract);
|
|
||||||
check string "*" "Multiply" (string_of_binary_operator Multiply);
|
|
||||||
check string "/" "Divide" (string_of_binary_operator Divide);
|
|
||||||
check string "&&" "AmpersandAmpersand"
|
|
||||||
(string_of_binary_operator AmpersandAmpersand);
|
|
||||||
check string "||" "BarBar" (string_of_binary_operator BarBar);
|
|
||||||
check string "==" "EqualsEquals" (string_of_binary_operator EqualsEquals);
|
|
||||||
check string "!=" "ExclamationEquals"
|
|
||||||
(string_of_binary_operator ExclamationEquals);
|
|
||||||
check string "<" "LessThan" (string_of_binary_operator LessThan);
|
|
||||||
check string "<=" "LessThanEquals" (string_of_binary_operator LessThanEquals);
|
|
||||||
check string ">" "GreaterThan" (string_of_binary_operator GreaterThan);
|
|
||||||
check string ">=" "GreaterThanEquals"
|
|
||||||
(string_of_binary_operator GreaterThanEquals)
|
|
||||||
|
|
||||||
let test_string_of_unary_operator () =
|
|
||||||
check string "-" "Negate" (string_of_unary_operator Negate);
|
|
||||||
check string "!" "Not" (string_of_unary_operator Not)
|
|
||||||
|
|
||||||
let test_string_of_expression () =
|
|
||||||
let expr = Literal (Integer 42) in
|
|
||||||
check string "42" "Literal(Integer(42))" (string_of_expression expr);
|
|
||||||
let expr = BinaryExpression (Add, Literal (Integer 1), Literal (Integer 2)) in
|
|
||||||
check string "1 + 2"
|
|
||||||
"BinaryExpression(Add, Literal(Integer(1)), Literal(Integer(2)))"
|
|
||||||
(string_of_expression expr);
|
|
||||||
let expr = UnaryExpression (Negate, Literal (Integer 42)) in
|
|
||||||
check string "-42" "UnaryExpression(Negate, Literal(Integer(42)))"
|
|
||||||
(string_of_expression expr);
|
|
||||||
let expr = Identifier "x" in
|
|
||||||
check string "x" "Identifier(\"x\")" (string_of_expression expr)
|
|
||||||
|
|
||||||
let test_string_of_statement () =
|
|
||||||
let stmt = ExpressionStatement (Literal (Integer 42)) in
|
|
||||||
check string "42;" "ExpressionStatement(Literal(Integer(42)))"
|
|
||||||
(string_of_statement stmt);
|
|
||||||
let stmt =
|
|
||||||
VariableStatement
|
|
||||||
[ VariableDeclaration (Type_Integer, Identifier "x", Literal Null) ]
|
|
||||||
in
|
|
||||||
check string "int x;"
|
|
||||||
"VariableStatement([VariableDeclaration(Type_Integer, Identifier(\"x\"), \
|
|
||||||
Literal(Null))])"
|
|
||||||
(string_of_statement stmt);
|
|
||||||
let stmt =
|
|
||||||
VariableStatement
|
|
||||||
[
|
[
|
||||||
VariableDeclaration (Type_Integer, Identifier "x", Literal (Integer 42));
|
("int", "IntegerType", IntegerType);
|
||||||
|
("float", "FloatType", FloatType);
|
||||||
|
("char", "CharacterType", CharacterType);
|
||||||
|
("string", "StringType", StringType);
|
||||||
|
("bool", "BooleanType", BooleanType);
|
||||||
|
("void", "VoidType", VoidType);
|
||||||
]
|
]
|
||||||
in
|
in
|
||||||
check string "int x = 42;"
|
List.iter
|
||||||
"VariableStatement([VariableDeclaration(Type_Integer, Identifier(\"x\"), \
|
(fun (name, expected, actual) ->
|
||||||
Literal(Integer(42)))])"
|
(check string) name expected (To_test._type actual))
|
||||||
(string_of_statement stmt)
|
tests
|
||||||
|
|
||||||
|
let test_string_of_literal () =
|
||||||
|
let tests =
|
||||||
|
[
|
||||||
|
("42", "Integer(42)", Integer 42);
|
||||||
|
("3.14", "Float(3.14)", Float 3.14);
|
||||||
|
("true", "Boolean(true)", Boolean true);
|
||||||
|
("false", "Boolean(false)", Boolean false);
|
||||||
|
("hello", "String(\"hello\")", String "hello");
|
||||||
|
("c", "Character('c')", Character 'c');
|
||||||
|
("null", "Null", Null);
|
||||||
|
]
|
||||||
|
in
|
||||||
|
List.iter
|
||||||
|
(fun (name, expected, actual) ->
|
||||||
|
(check string) name expected (To_test.literal actual))
|
||||||
|
tests
|
||||||
|
|
||||||
|
let test_string_of_binary_operator () =
|
||||||
|
let tests =
|
||||||
|
[
|
||||||
|
("+", "Add", Add);
|
||||||
|
("-", "Substract", Substract);
|
||||||
|
("*", "Multiply", Multiply);
|
||||||
|
("/", "Divide", Divide);
|
||||||
|
("&&", "AmpersandAmpersand", AmpersandAmpersand);
|
||||||
|
("||", "BarBar", BarBar);
|
||||||
|
("==", "EqualsEquals", EqualsEquals);
|
||||||
|
("!=", "ExclamationEquals", ExclamationEquals);
|
||||||
|
("<", "LessThan", LessThan);
|
||||||
|
("<=", "LessThanEquals", LessThanEquals);
|
||||||
|
(">", "GreaterThan", GreaterThan);
|
||||||
|
(">=", "GreaterThanEquals", GreaterThanEquals);
|
||||||
|
]
|
||||||
|
in
|
||||||
|
List.iter
|
||||||
|
(fun (name, expected, actual) ->
|
||||||
|
(check string) name expected (To_test.binary_operator actual))
|
||||||
|
tests
|
||||||
|
|
||||||
|
let test_string_of_unary_operator () =
|
||||||
|
let tests = [ ("-", "Negate", Negate); ("!", "Not", Not) ] in
|
||||||
|
List.iter
|
||||||
|
(fun (name, expected, actual) ->
|
||||||
|
(check string) name expected (To_test.unary_operator actual))
|
||||||
|
tests
|
||||||
|
|
||||||
|
let test_string_of_expression () =
|
||||||
|
let tests =
|
||||||
|
[
|
||||||
|
("42", "Literal(Integer(42))", Literal (Integer 42));
|
||||||
|
( "1 + 2",
|
||||||
|
"BinaryExpression(Add, Literal(Integer(1)), Literal(Integer(2)))",
|
||||||
|
BinaryExpression (Add, Literal (Integer 1), Literal (Integer 2)) );
|
||||||
|
( "-42",
|
||||||
|
"UnaryExpression(Negate, Literal(Integer(42)))",
|
||||||
|
UnaryExpression (Negate, Literal (Integer 42)) );
|
||||||
|
("x", "Identifier(\"x\")", Identifier "x");
|
||||||
|
]
|
||||||
|
in
|
||||||
|
List.iter
|
||||||
|
(fun (name, expected, actual) ->
|
||||||
|
(check string) name expected (To_test.expression actual))
|
||||||
|
tests
|
||||||
|
|
||||||
|
let test_string_of_statement () =
|
||||||
|
let tests =
|
||||||
|
[
|
||||||
|
( "42;",
|
||||||
|
"ExpressionStatement(Literal(Integer(42)))",
|
||||||
|
ExpressionStatement (Literal (Integer 42)) );
|
||||||
|
( "int x;",
|
||||||
|
"VariableStatement([VariableDeclaration(IntegerType, \
|
||||||
|
Identifier(\"x\"), Literal(Null))])",
|
||||||
|
VariableStatement
|
||||||
|
[ VariableDeclaration (IntegerType, Identifier "x", Literal Null) ] );
|
||||||
|
( "int x = 42;",
|
||||||
|
"VariableStatement([VariableDeclaration(IntegerType, \
|
||||||
|
Identifier(\"x\"), Literal(Integer(42)))])",
|
||||||
|
VariableStatement
|
||||||
|
[
|
||||||
|
VariableDeclaration
|
||||||
|
(IntegerType, Identifier "x", Literal (Integer 42));
|
||||||
|
] );
|
||||||
|
]
|
||||||
|
in
|
||||||
|
List.iter
|
||||||
|
(fun (name, expected, actual) ->
|
||||||
|
(check string) name expected (To_test.statement actual))
|
||||||
|
tests
|
||||||
|
|
||||||
let test_string_of_source_file () =
|
let test_string_of_source_file () =
|
||||||
let source_file =
|
let tests =
|
||||||
|
[
|
||||||
|
( "1; 2 + 3;",
|
||||||
|
"SourceFile([ExpressionStatement(Literal(Integer(1))), \
|
||||||
|
ExpressionStatement(BinaryExpression(Add, Literal(Integer(2)), \
|
||||||
|
Literal(Integer(3))))])",
|
||||||
SourceFile
|
SourceFile
|
||||||
[
|
[
|
||||||
ExpressionStatement (Literal (Integer 1));
|
ExpressionStatement (Literal (Integer 1));
|
||||||
ExpressionStatement
|
ExpressionStatement
|
||||||
(BinaryExpression (Add, Literal (Integer 2), Literal (Integer 3)));
|
(BinaryExpression (Add, Literal (Integer 2), Literal (Integer 3)));
|
||||||
|
] );
|
||||||
]
|
]
|
||||||
in
|
in
|
||||||
check string "1; 2 + 3;"
|
List.iter
|
||||||
"SourceFile([ExpressionStatement(Literal(Integer(1))), \
|
(fun (name, expected, actual) ->
|
||||||
ExpressionStatement(BinaryExpression(Add, Literal(Integer(2)), \
|
(check string) name expected (To_test.source_file actual))
|
||||||
Literal(Integer(3))))])"
|
tests
|
||||||
(string_of_source_file source_file)
|
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
let open Alcotest in
|
run "ast.print"
|
||||||
run "AST tests"
|
|
||||||
[
|
[
|
||||||
("string_of_type", [ test_case "type" `Quick test_string_of_type ]);
|
("string_of_type", [ test_case "type" `Quick test_string_of_type ]);
|
||||||
( "string_of_literal",
|
( "string_of_literal",
|
||||||
|
|||||||
@@ -11,16 +11,16 @@ SourceFile([
|
|||||||
Add,
|
Add,
|
||||||
BinaryExpression(
|
BinaryExpression(
|
||||||
Add,
|
Add,
|
||||||
Literal(Integer(4)
|
Literal(Integer(4)),
|
||||||
),
|
|
||||||
Literal(Integer(2))
|
Literal(Integer(2))
|
||||||
),
|
),
|
||||||
BinaryExpression(
|
BinaryExpression(
|
||||||
Multiply,
|
Multiply,
|
||||||
Literal(Integer(3)),
|
Literal(Integer(3)),
|
||||||
Literal(Integer(1))
|
Literal(Integer(1))
|
||||||
))
|
)
|
||||||
)]
|
)
|
||||||
)]
|
)
|
||||||
)
|
])
|
||||||
|
])
|
||||||
*/
|
*/
|
||||||
Reference in New Issue
Block a user