feat: Support file as argument and better error message

This commit is contained in:
Lucàs
2024-07-03 17:05:54 +02:00
parent e3c26ba702
commit 6cbfb1c6a6
4 changed files with 42 additions and 44 deletions
+23 -5
View File
@@ -3,17 +3,35 @@
open Printf open Printf
open Ast.Print open Ast.Print
let get_lexbuf () =
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 = Lexing.from_channel stdin in let lexbuf = get_lexbuf () in
let res = let res =
try Parser.main Lexer.token lexbuf with try Parser.main Lexer.token lexbuf with
| Lexer.Error c -> | Lexer.Error c ->
fprintf stderr "Lexical error at line %d: Unknown character '%c'\n" let file_name = lexbuf.lex_curr_p.pos_fname
lexbuf.lex_curr_p.pos_lnum c; 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 exit 1
| Parser.Error -> | Parser.Error ->
fprintf stderr "Parse error at line %d:\n" lexbuf.lex_curr_p.pos_lnum; 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 exit 1
in in
let _ = res in let _ = res in
Printf.printf "%s\n" (string_of_source_file res) Printf.printf "%s\n" (string_of_source_file res)
+9 -9
View File
@@ -71,14 +71,14 @@ let string_of_source_file = function
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]. *) (** The signature of the module [Print]. *)
module type Print = sig module type Print = sig
val string_of_type : Syntax._type -> string val string_of_type : _type -> string
val string_of_binary_operator : Syntax.binary_operator -> string val string_of_binary_operator : binary_operator -> string
val string_of_unary_operator : Syntax.unary_operator -> string val string_of_unary_operator : unary_operator -> string
val string_of_literal : Syntax.literal -> string val string_of_literal : literal -> string
val string_of_expression : Syntax.expression -> string val string_of_expression : expression -> string
val string_of_variable_declaration : Syntax.variable_declaration -> string val string_of_variable_declaration : variable_declaration -> string
val string_of_statement : Syntax.statement -> string val string_of_statement : statement -> string
val string_of_source_file : Syntax.source_file -> string val string_of_source_file : source_file -> string
end end
+8 -5
View File
@@ -4,9 +4,8 @@
exception Error of char exception Error of char
} }
let line_comment = "//" [^ '\n']* let line_comment = "//" [^'.']*'\n'
let block_comment = "/*" [^'.']* "*/" let block_comment = "/*" [^'.']* "*/"
let comment = line_comment | block_comment
let letter = ['a'-'z' 'A'-'Z'] let letter = ['a'-'z' 'A'-'Z']
let digit = ['0'-'9'] let digit = ['0'-'9']
@@ -20,8 +19,12 @@ let char = "'" [^'.'] "'"
let string = '"' [^'.']* '"' let string = '"' [^'.']* '"'
rule token = parse rule token = parse
| [' ' '\t'] | comment { token lexbuf } | ' ' | '\t' { token lexbuf }
| ['\n'] { Lexing.new_line lexbuf; token lexbuf } | '\n' | line_comment { Lexing.new_line lexbuf; token lexbuf }
| block_comment as comment {
String.iter (fun c -> if c = '\n' then Lexing.new_line lexbuf) comment;
token lexbuf
}
| '+' { PLUS } | '+' { PLUS }
| '-' { MINUS } | '-' { MINUS }
+2 -25
View File
@@ -1,26 +1,3 @@
// Déclaration d'une variable de type entier /*Hello, World!*/
var a: entier = 4 + 2 + 3 * 1;
/* var a = 1;
SourceFile([
VariableStatement([
VariableDeclaration(
Type_Integer,
Identifier("a"),
BinaryExpression(
Add,
BinaryExpression(
Add,
Literal(Integer(4)),
Literal(Integer(2))
),
BinaryExpression(
Multiply,
Literal(Integer(3)),
Literal(Integer(1))
)
)
)
])
])
*/