Code Monkey home page Code Monkey logo

tiny-synth's Introduction

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.

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:

<return_type>,<param_name>:<param_type>;
<functional_code>

This includes the following:

  • <return_type> - The return type of the C method to be generated.
  • <param_name>:<param_type> - The name and type of each parameter to be included in the generated C method. Can be repeated arbitratily many times (comma-separated).
  • <functional_code> - 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 <function_name>

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.