https://github.com/sr-lab/tiny-synth
An experiment in synthesis of C code from a functional language.
https://github.com/sr-lab/tiny-synth
c code-generation functional-language python refinement synthesis
Last synced: 3 months ago
JSON representation
An experiment in synthesis of C code from a functional language.
- Host: GitHub
- URL: https://github.com/sr-lab/tiny-synth
- Owner: sr-lab
- License: mit
- Created: 2017-01-26T12:28:50.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-27T11:18:58.000Z (almost 9 years ago)
- Last Synced: 2025-01-11T15:34:16.866Z (about 1 year ago)
- Topics: c, code-generation, functional-language, python, refinement, synthesis
- Language: Python
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tiny Synth
An experiment in synthesis of C code from a functional language.
The basic idea of this very simple and naive Python program is to transform a nameless functional language that looks a bit like this:
```
int,bin:int;
(add (rotate_right bin 1) (rotate_left (bitwise_and bin 1) 15)))
```
Into C code that can be compiled using [TinyC](http://bellard.org/tcc/).
## Structure
A specification file (`.spec` extension) is written in a nameless functional language with one function per file, with the function's name being the name of the file without extension. The basic structure of a specification file looks like this:
```
,:;
```
This includes the following:
* `` - The return type of the C method to be generated.
* `:` - The name and type of each parameter to be included in the generated C method. Can be repeated arbitratily many times (comma-separated).
* `` - The functional code to compile as the body of the generated C method.
A primitive file (`.pc` extension) is written as a very small piece of C code with substitution placeholders for each argument of the function that corresponds to that primitive. For example `add.pc` contains this:
```
(%1 + %2)
```
## Usage
Invoke the program on the command line like this:
```
python tiny-synth.py
```
Where `function_name` is the name of the function you want to compile.
## Reason
With functional programs being easier to reason about in general than imperative programs, I thought it'd a be a fun little experiment to try to synthesise some imperative code from functional code.
## Limitations
This isn't a serious development tool, and comes with some serious limitations and caveats. I'm sure there are a billion and one ways this could behave differently than expected.