mirror of
https://github.com/LucasVbr/croissant.git
synced 2026-07-09 15:07:48 +00:00
refactor: Use analyzer to parse rawtext into ast
This commit is contained in:
@@ -3,4 +3,4 @@
|
|||||||
(executable
|
(executable
|
||||||
(name main)
|
(name main)
|
||||||
(public_name croissant)
|
(public_name croissant)
|
||||||
(libraries parser lexer ast))
|
(libraries analyzer ast))
|
||||||
|
|||||||
+11
-30
@@ -3,35 +3,16 @@
|
|||||||
open Printf
|
open Printf
|
||||||
open Ast.Print
|
open Ast.Print
|
||||||
|
|
||||||
let get_lexbuf () =
|
exception Error of string
|
||||||
if Array.length Sys.argv > 1 then (
|
|
||||||
let lexbuf = Lexing.from_channel (open_in Sys.argv.(1)) in
|
|
||||||
Lexing.set_filename lexbuf Sys.argv.(1);
|
|
||||||
lexbuf)
|
|
||||||
else
|
|
||||||
let lexbuf = Lexing.from_channel stdin in
|
|
||||||
Lexing.set_filename lexbuf "stdin";
|
|
||||||
lexbuf
|
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
let lexbuf = get_lexbuf () in
|
(* Array.iteri (fun i arg -> printf "%d: %s\n" i arg) Sys.argv; *)
|
||||||
let res =
|
let help_message = "Usage: croissant <file_path>\n"
|
||||||
try Parser.main Lexer.token lexbuf with
|
and args_count = Array.length Sys.argv - 1 in
|
||||||
| Lexer.Error c ->
|
if args_count >= 2 then printf "%s" help_message
|
||||||
let file_name = lexbuf.lex_curr_p.pos_fname
|
else if args_count = 1 then
|
||||||
and line_num = lexbuf.lex_curr_p.pos_lnum
|
let file_path = Sys.argv.(1) in
|
||||||
and col_num = lexbuf.lex_curr_p.pos_cnum - lexbuf.lex_curr_p.pos_bol in
|
let ast = string_of_source_file (Analyzer.analyze_file file_path) in
|
||||||
fprintf stderr "Fichier \"%s\", ligne %d, colonne %d\n%s: %s\n"
|
printf "%s\n" ast
|
||||||
file_name line_num col_num "Erreur lexicale"
|
else raise (Error "interpreter from stdin is not implemented yet")
|
||||||
("Caractère '" ^ String.make 1 c ^ "' inconnu");
|
(* TODO: Implement interpreter from stdin *)
|
||||||
exit 1
|
|
||||||
| 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
|
|
||||||
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
|
|
||||||
let _ = res in
|
|
||||||
Printf.printf "%s\n" (string_of_source_file res)
|
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
open Printf
|
||||||
|
|
||||||
|
let analyze lexbuf =
|
||||||
|
let ast =
|
||||||
|
try Parser.main Lexer.token lexbuf with
|
||||||
|
| 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
|
||||||
|
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
|
||||||
|
| 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
|
||||||
|
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
|
||||||
|
ast
|
||||||
|
|
||||||
|
let analyze_file file_name =
|
||||||
|
let file_stream = open_in file_name in
|
||||||
|
let lexbuf = Lexing.from_channel file_stream in
|
||||||
|
Lexing.set_filename lexbuf file_name;
|
||||||
|
analyze lexbuf
|
||||||
@@ -1,15 +1,16 @@
|
|||||||
;lib/dune
|
;lib/dune
|
||||||
|
|
||||||
(library
|
|
||||||
(name lexer)
|
|
||||||
(modules lexer)
|
|
||||||
(libraries parser))
|
|
||||||
|
|
||||||
(library
|
(library
|
||||||
(name parser)
|
(name parser)
|
||||||
(modules parser)
|
(modules parser)
|
||||||
(libraries ast))
|
(libraries ast))
|
||||||
|
|
||||||
|
(library
|
||||||
|
(name analyzer)
|
||||||
|
(modules lexer analyzer)
|
||||||
|
(libraries parser)
|
||||||
|
)
|
||||||
|
|
||||||
(menhir
|
(menhir
|
||||||
(modules parser))
|
(modules parser))
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
/*Hello, World!*/
|
/*Hello, World!*/
|
||||||
|
|
||||||
var a = 1;
|
var a : entier = 1;
|
||||||
Reference in New Issue
Block a user