Skip to content

Commit

Permalink
Add a factorial parser ("OCaml + printf" by Rodionov Maxim and Zaikin…
Browse files Browse the repository at this point in the history
… Vladimir) (Kakadu#32)

* project initialization

Signed-off-by: Maxim Rodionov <[email protected]>

* chore: update .gitignore and .ocamlformat

Signed-off-by: Maxim Rodionov <[email protected]>

* feat: add AST

Signed-off-by: Maxim Rodionov <[email protected]>

* chore: fix linter's comments and delete unnecessary file

Signed-off-by: Maxim Rodionov <[email protected]>

* feat: add an example of representing a factorial using the written types

Signed-off-by: Maxim Rodionov <[email protected]>

* fix: an example of representing a factorial check by compiler

Signed-off-by: Maxim Rodionov <[email protected]>

* test: add cram test for output

Signed-off-by: Maxim Rodionov <[email protected]>

* refactor: update AST and an example of representing a factorial

Removed types related to printf, corrected documentation, changed definition of Exp_fun and Exp_apply.

Signed-off-by: Maxim Rodionov <[email protected]>

* docs: add authors' emails

Signed-off-by: Friend-zva <[email protected]>

* refactor: rename 'REPL.ml' to 'main.ml'

Signed-off-by: Friend-zva <[email protected]>

* fix: unused depends

Signed-off-by: Friend-zva <[email protected]>

* feat: add 'parse_constant'

Signed-off-by: Vladimir Zaikin <[email protected]>

* feat: add base 'parse_pattern', 'parse_expression' and 'parse_binding'

Signed-off-by: Vladimir Zaikin <[email protected]>

* docs: rename 'Str' -> 'Struct' and fix typos

Signed-off-by: Vladimir Zaikin <[email protected]>

* feat: add parsing of 'structure', 'structure_item' and refactor some functions
Added 'parse_structure', 'parse_struct_value', 'parse_value_binding_list', 'parse_ident' and renamed 'skip_many' to 'ws'.

Signed-off-by: Maxim Rodionov <[email protected]>

* feat: expand 'parse_expression' and 'parse_pattern'

Signed-off-by: Vladimir Zaikin <[email protected]>

* feat: add 'parse_expression'

Signed-off-by: Maxim Rodionov <[email protected]>

* refactor: 'chain_left_associative' and 'parse_exp_match'

Signed-off-by: Maxim Rodionov <[email protected]>

* refactor: some funs and args in parsing 'expression' and 'pattern'

Signed-off-by: Vladimir Zaikin <[email protected]>

* chore: react to zanuda warnings

Change execation parser.
Add 'expect_test' for parser to '/tests/test_parser.ml' and delete 'parser_binding.t'.
Add '.mli' for 'parser.ml' and 'test_parser.ml'.

Signed-off-by: Vladimir Zaikin <[email protected]>

* refactor: 'parse_pattern', update 'parser.mli' and remove unimplemented functions

Signed-off-by: Maxim Rodionov <[email protected]>

* feat: add parser tests

Signed-off-by: Vladimir Zaikin <[email protected]>

* feat: REPL

Signed-off-by: Maxim Rodionov <[email protected]>

* feat: error handling

Signed-off-by: Maxim Rodionov <[email protected]>

* feat: REPL

Signed-off-by: Maxim Rodionov <[email protected]>

* feat: error handling

Signed-off-by: Maxim Rodionov <[email protected]>

* refactor: delete dir 'main', add test for parser via REPL

Signed-off-by: Maxim Rodionov <[email protected]>

* chore: remove stanza 'modules' in dune files, edit the test

Signed-off-by: Maxim Rodionov <[email protected]>

* refactor: 'factorial.ml' -> 'factorial.txt'

Signed-off-by: Vladimir Zaikin <[email protected]>

* fix: missing spaces and types

Signed-off-by: Vladimir Zaikin <[email protected]>

* feat: 'parse_exp_let', 'ws1', rewrite 'parse_ident' and parse of tuple with let*

Signed-off-by: Maxim Rodionov <[email protected]>

* refactor: replace all liftn with let*

Signed-off-by: Maxim Rodionov <[email protected]>

* refactor: 'parse_expression' and some variable names

Signed-off-by: Vladimir Zaikin <[email protected]>

* feat: a test for 'Exp_let'

Signed-off-by: Vladimir Zaikin <[email protected]>

* feat: 'parse_exp_apply' and a test for it

Signed-off-by: Vladimir Zaikin <[email protected]>

* refactor: 'parse_exp_apply_op'

Signed-off-by: Vladimir Zaikin <[email protected]>

* docs: add more info to 'README.md'

Signed-off-by: Vladimir Zaikin <[email protected]>

* feat: 'parse_exp_construct', 'parse_pat_construct', some code refactor
Removed unnecessary code, added a function 'is_empty' due to linter comments.

Signed-off-by: Maxim Rodionov <[email protected]>

* test: for 'Exp_construct'

Signed-off-by: Maxim Rodionov <[email protected]>

* refactor: 'ws1' -> 'keyword' and some typos

Signed-off-by: Vladimir Zaikin <[email protected]>

* feat: 'parse_exp_sequence', 'parse_bool_exp', 'parse_bool_pat'

Signed-off-by: Maxim Rodionov <[email protected]>

* feat: prevent backtracking for 'parse_structure'

Signed-off-by: Vladimir Zaikin <[email protected]>

* feat: 'parse_chain_right_associative' and some typos

Signed-off-by: Maxim Rodionov <[email protected]>

* test: for 'parse_chain_right_associative' and for REPL

Signed-off-by: Maxim Rodionov <[email protected]>

* chore: correct the order of tests

Signed-off-by: Maxim Rodionov <[email protected]>

---------

Signed-off-by: Maxim Rodionov <[email protected]>
Signed-off-by: Friend-zva <[email protected]>
Signed-off-by: Vladimir Zaikin <[email protected]>
Co-authored-by: Maxim Rodionov <[email protected]>
  • Loading branch information
Friend-zva and RodionovMaxim05 authored Oct 19, 2024
1 parent 23f17f2 commit a9880f4
Show file tree
Hide file tree
Showing 19 changed files with 786 additions and 105 deletions.
3 changes: 3 additions & 0 deletions OCamlPrintf/OCamlPrintf.opam
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ homepage: "https://github.com/RodionovMaxim05/OCamlPrintf"
bug-reports: "https://github.com/RodionovMaxim05/OCamlPrintf"
depends: [
"dune" {>= "3.7"}
"base"
"angstrom"
"ppx_inline_test" {with-test}
"ppx_deriving"
"ppx_expect"
"bisect_ppx"
"odoc" {with-doc}
"ocamlformat" {build}
Expand Down
38 changes: 32 additions & 6 deletions OCamlPrintf/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
<a name="readme-top"></a>

# OCamlPrintf

OCaml interpreter with support for printf
`OCaml` interpreter with support for printf.

#### MiniML

- Standard data types: `bool`, `int`, `tuples`, `list` and `option`;
- Recursive functions;
- Comparisons of numbers and other arithmetic;
- Standard functions.

#### Printf

- Support for char, string types and operations with them;
- Support the formatted printing function.

## Progress
- [ ] AST
- [ ] Parser
- [ ] Type inference
- [ ] ...

- [x] [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree)
- [x] Factorial
- [x] MiniML
- [ ] Parser
- [x] Factorial
- [ ] MiniML
- [x] [REPL](https://en.wikipedia.org/wiki/Read–eval–print_loop)
- [ ] Type check / Type inference
- [ ] Interpreter

## Authors

- [@Friend-zva](https://github.com/Friend-zva) (Vladimir Zaikin)
- [@RodionovMaxim05](https://github.com/RodionovMaxim05) (Maxim Rodionov)
- [@RodionovMaxim05](https://github.com/RodionovMaxim05) (Maxim Rodionov)

## License

Distributed under the GNU GENERAL PUBLIC LICENSE. See [lisence](COPYING) for more information.

<p align="right">(<a href="#readme-top">back to top</a>)</p>
7 changes: 0 additions & 7 deletions OCamlPrintf/bin/dune

This file was deleted.

38 changes: 0 additions & 38 deletions OCamlPrintf/bin/main.ml

This file was deleted.

16 changes: 10 additions & 6 deletions OCamlPrintf/dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

(license LGPL-3.0-or-later)

(authors "Maxim Rodionov <[email protected]>" "Vladimir Zaikin <[email protected]>")
(authors
"Maxim Rodionov <[email protected]>"
"Vladimir Zaikin <[email protected]>")

(maintainers "Maxim Rodionov <[email protected]>" "Vladimir Zaikin <[email protected]>")
(maintainers
"Maxim Rodionov <[email protected]>"
"Vladimir Zaikin <[email protected]>")

(bug_reports "https://github.com/RodionovMaxim05/OCamlPrintf")

Expand All @@ -22,11 +26,11 @@
(version 0.1)
(depends
dune
base
angstrom
(ppx_inline_test :with-test)
ppx_deriving
ppx_expect
bisect_ppx
(odoc :with-doc)
(ocamlformat :build)
; base
; After adding dependencies to 'dune' files and the same dependecies here too
))
(ocamlformat :build)))
32 changes: 16 additions & 16 deletions OCamlPrintf/lib/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ type pattern =
| Pat_any (** The pattern [_] *)
| Pat_var of ident (** A variable pattern such as [x] *)
| Pat_constant of constant (** Patterns such as [1], ['a'], ["true"] *)
| Pat_tuple of pattern list (** Patterns [(P1, ..., Pn)] *)
| Pat_tuple of pattern list (** Patterns [(P1, ... , Pn)] *)
| Pat_construct of ident * pattern option
(** [Pat_construct(C, args)] represents:
- [C] when [args] is [None],
- [C] when [args] is [None],
- [C P] when [args] is [Some P] *)
[@@deriving show { with_path = false }]

Expand All @@ -45,31 +45,31 @@ and expression =
| Exp_ident of ident (** Identifier such as [x] *)
| Exp_constant of constant (** Expressions constant such as [1], ['a'], ["true"] *)
| Exp_let of rec_flag * value_binding list * expression
(** [Exp_let(flag, [(P1,E1) ; ... ; (Pn,En)], E)] represents:
- [let P1 = E1 and ... and Pn = EN in E] when [flag] is [Nonrecursive],
- [let rec P1 = E1 and ... and Pn = EN in E] when [flag] is [Recursive]. *)
(** [Exp_let(flag, [(P1, E1); ... ; (Pn, En)], E)] represents:
- [let P1 = E1 and ... and Pn = En in E] when [flag] is [Nonrecursive],
- [let rec P1 = E1 and ... and Pn = En in E] when [flag] is [Recursive]. *)
| Exp_fun of pattern list * expression
(** [Exp_fun([P1; ...; Pn], E)] represents [fun P1 ... Pn -> E] *)
(** [Exp_fun([P1; ... ; Pn], E)] represents [fun P1 ... Pn -> E] *)
| Exp_apply of expression * expression list
(** [Exp_apply(E0, [E1; ...; En])] represents [E0 E1 ... En] *)
(** [Exp_apply(E0, [E1; ... ; En])] represents [E0 E1 ... En] *)
| Exp_match of expression * case list (** [match E0 with P1 -> E1 | ... | Pn -> En] *)
| Exp_tuple of expression list (** Expressions [(E1, ..., En)] *)
| Exp_tuple of expression list (** Expressions [(E1, ... , En)] *)
| Exp_construct of ident * expression option
(** [Exp_construct(C, exp)] represents:
- [C] when [exp] is [None],
- [C E] when [exp] is [Some E],
- [C (E1, ..., En)] when [exp] is [Some (Exp_tuple[E1;...;En])] *)
- [C] when [exp] is [None],
- [C E] when [exp] is [Some E],
- [C (E1, ... , En)] when [exp] is [Some (Exp_tuple[E1; ... ; En])] *)
| Exp_ifthenelse of expression * expression * expression option
(** [if E1 then E2 else E3] *)
| Exp_sequence of expression * expression (** [E1; E2] *)
[@@deriving show { with_path = false }]

type structure_item =
| Str_eval of expression (** [E] *)
| Str_value of rec_flag * value_binding list
(** [Str_value(rec, [(P1, E1 ; ... ; (Pn, En))])] represents:
- [let P1 = E1 and ... and Pn = En] when [rec] is [Nonrecursive],
- [let rec P1 = E1 and ... and Pn = En ] when [rec] is [Recursive]. *)
| Struct_eval of expression (** [E] *)
| Struct_value of rec_flag * value_binding list
(** [Struct_value(flag, [(P1, E1); ... ; (Pn, En))])] represents:
- [let P1 = E1 and ... and Pn = En] when [flag] is [Nonrecursive],
- [let rec P1 = E1 and ... and Pn = En] when [flag] is [Recursive]. *)
[@@deriving show { with_path = false }]

type structure = structure_item list [@@deriving show { with_path = false }]
2 changes: 1 addition & 1 deletion OCamlPrintf/lib/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(library
(name ocaml_printf_lib)
(public_name OCamlPrintf.Lib)
(modules Ast)
(libraries base angstrom)
(preprocess
(pps ppx_deriving.show))
(instrumentation
Expand Down
Loading

0 comments on commit a9880f4

Please sign in to comment.