Giter Site home page Giter Site logo

claudiouzelac / evilml Goto Github PK

View Code? Open in Web Editor NEW

This project forked from akabe/evilml

0.0 0.0 0.0 380 KB

A compiler from ML to C++ template language

Makefile 0.11% OCaml 41.89% CSS 1.18% HTML 0.43% JavaScript 55.23% C++ 0.63% Standard ML 0.54%

evilml's Introduction

Evil ML

Evil ML is a joke compiler from ML to C++ template language (not ordinary C++ code).

C++ template is a higher-order pure functional programming language traditionally used for compile-time computation, while its syntax is verbose and hard to use. ML, a higher-order functional programming language, is simple, practical and easy to understand, so that we jokingly implemented this compiler. You can easily use black magic in C++ template programming. Online demo is here.

P.S. constexpr (supported C++11 or above) is useful. Why don't you use it?

Features

  • OCaml-like higher-order pure functional language (Hindley-Milner polymorphism, no value restriction).
  • Type inference is performed. Most type annotations are automatically inferred.
  • Variant, a flexible and strong data structure, is supported.
  • You can write raw C++ code in (*! ... *) in top level.

Difference from OCaml:

  • Strings have type char list (type string does not exist).
  • Module system and separate compilation are not supported.
  • User-defined operators are not allowed.
  • type keyword in top level can only define variant types. You cannot declare aliases of types and records.
  • Pattern match is only performed by match. Patterns cannot appear in formal arguments and l.h.s. of let bindings.
  • Exhaustivity checking of pattern matching is not implemented. (future work)
  • Identifiers are defined as regular expression [a-zA-Z_][a-zA-Z0-9_]*. Primes cannot be used, and names that begin __ (double underscores) are reserved by this compiler. Identifiers of data constructors begin capital letters.
  • Top-level shadowing of identifiers (variables, types, and constructors) is prohibited.

Install

$ ./configure
$ make
$ make install

Demo: quick sort

examples/quicksort/qsort.ml implements quick sort of a list of 8 elements. You can compile the ML program into C++ template as follows:

$ cd examples/quicksort
$ evilml qsort.ml

Generated qsort.cpp works well:

$ g++ qsort.cpp
$ ./a.out
1  2  3  4  5  6  7  8

In order to make sure that sorting is executed in compile time, we suggest to use g++ -S qsort.cpp and open qsort.s:

...
	movl	$1, 4(%esp)   ; pass 1 to printf
	movl	$.LC0, (%esp)
	call	printf
	movl	$2, 4(%esp)   ; pass 2 to printf
	movl	$.LC0, (%esp)
	call	printf
	movl	$3, 4(%esp)   ; pass 3 to printf
	movl	$.LC0, (%esp)
	call	printf
	movl	$4, 4(%esp)   ; pass 4 to printf
	movl	$.LC0, (%esp)
	call	printf
	movl	$5, 4(%esp)   ; pass 5 to printf
	movl	$.LC0, (%esp)
	call	printf
	movl	$6, 4(%esp)   ; pass 6 to printf
	movl	$.LC0, (%esp)
	call	printf
	movl	$7, 4(%esp)   ; pass 7 to printf
	movl	$.LC0, (%esp)
	call	printf
	movl	$8, 4(%esp)   ; pass 8 to printf
	movl	$.LC1, (%esp)
	call	printf
...

(Of course, you can use std::cout to print integers, however we make use of printf for readable assembly code.)

Bugs

  • let rec diverge _ = diverge () should be infinite loop, but generated C++ code causes compilation error. let rec diverge n = diverge (n+1) passes C++ compilation. (I don't know the formal definition of reduction rules of C++ template expressions.)
  • C++03 template prohibits operation of float-point values, so that this compiler outputs wrong code.

evilml's People

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.