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
+1 -3
View File
@@ -1,6 +1,4 @@
;bin/dune
(executable
(name main)
(public_name croissant)
(libraries ast))
(libraries Croissant))
+22 -17
View File
@@ -1,18 +1,23 @@
(* bin/main.ml *)
open Printf
open Ast
exception Error of string
let () =
(* Array.iteri (fun i arg -> printf "%d: %s\n" i arg) Sys.argv; *)
let help_message = "Usage: croissant <file_path>\n"
and args_count = Array.length Sys.argv - 1 in
if args_count >= 2 then printf "%s" help_message
else if args_count = 1 then
let file_path = Sys.argv.(1) in
let ast = Print.string_of_source_file (Analyzer.analyze_file file_path) in
printf "%s\n" ast
else raise (Error "interpreter from stdin is not implemented yet")
(* TODO: Implement interpreter from stdin *)
let file_path = Sys.argv.(1) in
let file_stream = open_in file_path in
let lexbuf = Lexing.from_channel file_stream in
let ast =
try Analyzer.Parser.main Analyzer.Lexer.token lexbuf with
| Analyzer.Lexer.Error c ->
let file_name = lexbuf.lex_curr_p.pos_fname
and line_num = lexbuf.lex_curr_p.pos_lnum
and col_num = lexbuf.lex_curr_p.pos_cnum - lexbuf.lex_curr_p.pos_bol in
Printf.fprintf stderr "Fichier \"%s\", ligne %d, colonne %d\n%s: %s\n"
file_name line_num col_num "Erreur lexicale"
("Caractère '" ^ String.make 1 c ^ "' inconnu");
exit 1
| Analyzer.Parser.Error ->
let file_name = lexbuf.lex_curr_p.pos_fname
and line_num = lexbuf.lex_curr_p.pos_lnum
and col_num = lexbuf.lex_curr_p.pos_cnum - lexbuf.lex_curr_p.pos_bol in
Printf.fprintf stderr "Fichier \"%s\", ligne %d, colonne %d\n%s: %s\n"
file_name line_num col_num "Erreur syntaxique" "Syntaxe incorrecte";
exit 1
in
Printf.printf "%s\n" (Syntax.SourceFiles.pp_source_files ast)