Quantcast
Channel: Hacker News
Viewing all articles
Browse latest Browse all 25817

Gravity: a new programming language and VM written in C

$
0
0

README.md

Gravity Programming Language

Gravity is a powerful, dynamically typed, lightweight, embeddable programming language written in C without any external dependencies (except for stdlib). It is a class-based concurrent scripting language with modern Swift-like syntax.

Gravity supports procedural programming, object-oriented programming, functional programming and data-driven programming. Thanks to special built-in methods, it can also be used as a prototype-based programming language.

Gravity has been developed from scratch for the Creo project in order to offer an easy way to write portable code for the iOS and Android platforms. It is written in portable C code that can be compiled on any platform using a C99 compiler. The VM code is about 2K lines long, the multipass compiler code is about 3K lines and the shared code is about 2K lines long. The compiler and virtual machine combined add less than 200KB to the executable on a 64 bit system.

What Gravity code looks like

classVector {// instance variablesvar x =0;var y =0;var z =0;// constructorfuncinit (a, b, c) {if (!a) a =0;if (!b) b =0;if (!c) c =0;
        x = a; y = b; z = c;
    }// instance method (built-in operator overriding)func+ (v) {if (v isa Int) returnVector(x+v, y+v, z+v);elseif (v isa Vector) returnVector(x+v.x, y+v.y, z+v.z);return null;
    }// instance method (built-in String conversion overriding)funcString() {return"["+ x.String() +","+ y.String() +","+ z.String() +"]";
    }
}funcmain() {// initialize a new vector objectvar v1 =Vector(1,2,3);// initialize a new vector objectvar v2 =Vector(4,5,6);// call + function in the vector objectvar v3 = v1 + v2;// returns string "[5,7,9]"return v3.String();
 }

Features

  • multipass compiler
  • dynamic typing
  • classes and inheritance
  • higher order functions and classes
  • lexical scoping
  • coroutines (via fibers)
  • nested classes
  • closures
  • garbage collection
  • operator overriding
  • powerful embedding api
  • built-in unit tests
  • built-in JSON serializer/deserializer

Special thanks

Gravity was supported by a couple of open source projects. The inspiration for closures comes from the elegant Lua programming language; specifically from the document Closures in Lua. For fibers, upvalues handling and some parts of the garbage collector, my gratitude goes to Bob Nystrom and his excellent Wren programming language. A very special thanks should also go to my friend Andrea Donetti who helped me debugging and testing various aspects of the language.

Documentation

The Getting Started page is a guide for downloading and compiling the language. There is also a more extensive language documentation and an internals book, which is currently in progress.

Contributing

Contributions to Gravity are welcomed and encouraged!

License

Gravity is available under the permissive MIT license.


Viewing all articles
Browse latest Browse all 25817

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>