From b983c8a6626f9347ca1674bae8ec0847cd3549aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luc=C3=A0s?= <86352901+LucasVbr@users.noreply.github.com> Date: Tue, 2 Jul 2024 18:17:20 +0200 Subject: [PATCH] feat: Support Unary Operation #1 --- bin/main.ml | 4 ++-- dune-project | 8 ++++---- lib/parser.mly | 8 ++++++-- test/ast/dune | 6 ++++++ 4 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 test/ast/dune diff --git a/bin/main.ml b/bin/main.ml index 9ac74ad..f9cfbc1 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -1,7 +1,7 @@ (* bin/main.ml *) open Printf -open Ast +open Ast.Print let () = let lexbuf = Lexing.from_channel stdin in @@ -16,4 +16,4 @@ let () = exit 1 in let _ = res in - Printf.printf "%s\n" (string_of_source_file res) \ No newline at end of file + Printf.printf "%s\n" (string_of_source_file res) diff --git a/dune-project b/dune-project index f2b2546..55af80a 100644 --- a/dune-project +++ b/dune-project @@ -3,16 +3,16 @@ (name croissant) (generate_opam_files true) (source (github LucasVbr/croissant)) -(authors "LucasVbr") -(maintainers "LucasVbr") -(license LICENSE) +(authors "LucasVbr ") +(maintainers "LucasVbr ") +(license MIT) ;(documentation https://url/to/documentation) (package (name croissant) (synopsis "A short synopsis") (description "A longer description") - (depends ocaml dune alcotest menhir ocamlformat) + (depends ocaml dune menhir ocamlformat (alcotest :with-test)) (tags ("Custom programming language" "French") ) diff --git a/lib/parser.mly b/lib/parser.mly index 78a4d8d..ae04cdf 100644 --- a/lib/parser.mly +++ b/lib/parser.mly @@ -1,6 +1,6 @@ /* lib/parser.mly */ %{ - open Ast + open Ast.Syntax %} %token INT @@ -19,7 +19,7 @@ %left "+" "-" %left "*" "/" -//%nonassoc UMINUS +%nonassoc UMINUS %start main %type main @@ -38,12 +38,16 @@ statement: expression: | literal { $1 } + | unary_expression { $1 } | binary_expression { $1 } | "(" expression ")" { $2 } literal: | INT { IntegerLiteral($1) } +unary_expression: + | MINUS expression %prec UMINUS { UnaryExpression(Negate, $2) } + binary_expression: | e1=expression PLUS e2=expression { BinaryExpression(Add, e1, e2) } | e1=expression MINUS e2=expression { BinaryExpression(Substract, e1, e2) } diff --git a/test/ast/dune b/test/ast/dune new file mode 100644 index 0000000..9368d14 --- /dev/null +++ b/test/ast/dune @@ -0,0 +1,6 @@ +;test/ast/dune + +(test + (name print) + (libraries alcotest ast) +) \ No newline at end of file