项目作者: metasepi

项目描述 :
Translate IDIOMATIC C into human-readable ATS
高级语言: Haskell
项目地址: git://github.com/metasepi/idiomaticca.git
创建时间: 2019-01-30T06:30:42Z
项目社区:https://github.com/metasepi/idiomaticca

开源协议:GNU Affero General Public License v3.0

下载


Idiomaticca: translate IDIOMATIC C into readable ATS

Build Status

What’s this?

ATS is a statically typed programming language that unifies implementation with formal specification. It can safely use pointer and powerfully capture invariant in program.

However, there are already many C language) codes, which shape unsafe application. Should we manually rewrite the code with ATS to get safe application? No, it needs so much human resource.

This program is a translator from C code, which is written by idiomatic manner, to ATS code, which is human-readable. The translated code should be maintained by human, not only for C compiler. You may manually add some formal specifications to the code after translating.

What’s IDIOMATIC C code?

It means the C code written by idiomatic manner.
Following will be supported.

Requirements

We are testing this program on Debian GNU/Linux.

Install

Get source code of the idiomaticca:

  1. $ git clone https://github.com/metasepi/idiomaticca.git
  2. $ cd idiomaticca

Then install it using stack:

  1. $ make install
  2. $ which idiomaticca
  3. /home/YourName/.local/bin/idiomaticca

Usage

Start from a simple example:

  1. $ cat regress/noinc/loop_for/main.c
  1. int sum1(int n) {
  2. int i, sum = 0;
  3. for (i = 1; i <= n; i = i + 1) {
  4. sum = sum + i;
  5. }
  6. return sum;
  7. }
  8. int sum2(int n) {
  9. int i, sum = 0;
  10. for (i = 1; i <= n; i++) {
  11. sum = sum + i;
  12. }
  13. return sum;
  14. }
  15. int main() {
  16. return sum1(5) - sum2(5);
  17. }

Translate the C code into ATS:

  1. $ idiomaticca trans regress/noinc/loop_for/main.c > main.dats
  2. $ cat main.dats
  1. #include "share/atspre_staload.hats"
  2. staload UN = "prelude/SATS/unsafe.sats"
  3. fun sum1(n : int) : int =
  4. let
  5. var n: int = n
  6. in
  7. let
  8. var i: int
  9. var sum: int = 0
  10. fun loop_for(i : int, n : int, sum : int) : (int, int, int) =
  11. let
  12. var i: int = i
  13. var n: int = n
  14. var sum: int = sum
  15. in
  16. if i <= n then
  17. let
  18. val () = sum := sum + i
  19. val () = i := i + 1
  20. in
  21. loop_for(i, n, sum)
  22. end
  23. else
  24. (i, n, sum)
  25. end
  26. val () = i := 1
  27. val (i9a_i, i9a_n, i9a_sum) = loop_for(i, n, sum)
  28. val () = i := i9a_i
  29. val () = n := i9a_n
  30. val () = sum := i9a_sum
  31. in
  32. sum
  33. end
  34. end
  35. fun sum2(n : int) : int =
  36. let
  37. var n: int = n
  38. in
  39. let
  40. var i: int
  41. var sum: int = 0
  42. fun loop_for(i : int, n : int, sum : int) : (int, int, int) =
  43. let
  44. var i: int = i
  45. var n: int = n
  46. var sum: int = sum
  47. in
  48. if i <= n then
  49. let
  50. val () = sum := sum + i
  51. val () = i := i + 1
  52. in
  53. loop_for(i, n, sum)
  54. end
  55. else
  56. (i, n, sum)
  57. end
  58. val () = i := 1
  59. val (i9a_i, i9a_n, i9a_sum) = loop_for(i, n, sum)
  60. val () = i := i9a_i
  61. val () = n := i9a_n
  62. val () = sum := i9a_sum
  63. in
  64. sum
  65. end
  66. end
  67. implement main () =
  68. sum1(5) - sum2(5)

Compile the translated ATS code and run it:

  1. $ patscc main.dats
  2. $ ls a.out main*
  3. a.out* main.dats main_dats.c
  4. $ ./a.out
  5. $ echo $?
  6. 0

idiomaticca command line option will be changed, and not stable today.

The other examples are found at regress directory.

Contributing

Please send pull request, and write your name and reachable e-mail address in CONTRIBUTORS.md.

You also feel free to post some issues or mailing list.

Acknowledgements

The idiomaticca translator is inspired by Jamey Sharp’s Corrode translator.

License

AGPL-3 or later.

Copyright (c) 2019 Metasepi team.

FAQ

What license will be applied to my ATS code translated by idiomaticca?

You can apply your own license to your ATS code.

Example: If you translate your C code published under BSD license, you can publish your translated ATS code under BSD license.