Mantra Programming Language Version 1.0a4 November 2, 2007 Released under BSD License http://www.linguamantra.org/ Terence Parr, parrt at cs usfca edu Mantra project lead and supreme dictator for life University of San Francisco CREDITS See http://www.linguamantra.org for the latest list. I am looking for volunteers to help me build the runtime library, which is pretty easy. All I'm doing is writing a nice library sitting on top of Java's low-level library. ------------------------------- INTRODUCTION Mantra (Hindi for "magical incantation") is a new dynamically-typed programming language that is syntactically similar to Java but with the flexibility and ease of programming of Ruby or Python. Mantra is not an interpreted language. Source code is translated to Java and so mantra programs integrate trivially with plain Java code. Mantra is meant as a research platform and to teach language design and implementation. Nonetheless, it should prove a nice little language. Mantra is implemented using ANTLR v3.1b1 and StringTemplate 3.1b1 (as yet unreleased). To be safe, use the libraries provided with the distribution. This release is the first alpha release. I do not have a good unit testing facility yet and so I cannot guarantee that mantra will do anything useful for you. In fact, it might wipe out all life on this planet. That said, it seems to be pretty useful at the moment has all of the runtime is written in mantra itself except for the object and MetaClass classes. ------------------------------- FEATURES Here some of the major features of Mantra: o Dynamically typed with type annotations. int add(int x, int y) { return x+y; } The type annotations make you think that mantra is statically typed but it is not. There are no typecasts and you are free to say {x.close()} where x is an integer if you want. The types buy you readability and Mantra can generate more efficient code for you. Mantra can also generate pre-and post-condition checks on the types of the incoming and outgoing objects automatically for you. o Real closures like Ruby; closures are like anonymous methods that you can pass around as first-class objects: list a = ["Tom", "Ter", "Tim"]; a:{string s | println(s);}; // map closure to elements of a a:println; // same thing but more terse o Streams of objects the map operator, ':', applies a closure or method to a stream of objects. Many objects know how to turn themselves into a stream such as files and strings. For example the following iterates over the characters in a string: "abc":{char c | print(c);}; o Subsets to copy data: list goodones = [1,99,20]; subset = names[goodones]; // yields a stream of objects o List comprehensions are easy using map: list a = ["Tom", "Ter", "Tim"]; list b = a:{string n where n!="Ter" | return n;}; o Mixins; like Ruby mixins. Mixins behave like abstract superclasses except that a class may include more than one mixin o Dynamic mixins and delegation; you can add methods to classes at runtime. int.mixin("toHex", { int self | return java {new mstring(Integer.toHexString(((mint)self).v))}; } ); println(32.toHex()); // emits "20" o Type annotations; you specify the type of variables without forcing static typing. The type information is used to generate faster code and make Mantra code more readable. o Pipes; hook the output of one actor to the input of another. The first and last element in the pipeline are sources and sinks of data. The following example computes the word frequency count from a file: dict wfreq = dict(); File f = File(filename); f => Words() => { string w | mutint c=wfreq[w]; if ( c is null ) wfreq[w]=mutint(1); else c++; }; The pipe operator also launches a thread for each operand of a pipe and calls the predefined main() on each object. The significance of POP lies in that you can create new functionality by piping actors together rather than adding methods to classes. Because the pipeline automatically opens and closes streams, you get a really terse statement sometimes. Here is a file copy: File("t") => File("u"); o Nice for-each with where clause: for (string filename in OS.dir(".") where File(filename).isDir()) { println(i+":"+filename); // i is implicitly-defined iteration index } o no "public static void main(String[] args)" required to execute some code. Here is a simple program: println("Hello, world!"); o trees are native data structure like: tree t = ^(1 2 3); 1 at root with two children: 2 and 3. o File extension is ".om" as in "auuuum", the chant. I love that. ------------------------------- INSTALLATION Untar the mantra-1.0a4.tar.gz file as follows: $ tar xf mantra-1.0a4.tar.gz $ cd mantra-1.0a4 $ ls CHANGES.txt README.txt lib src LICENSE.txt doc runtime tests $ In the lib dir, you'll see mantra-1.0a4.jar, which has all of the necessary support libs inside it, including: mantra-1.0a1/lib/antlr-2.7.7.jar mantra-1.0a1/lib/antlr-3.1b1.jar mantra-1.0a1/lib/stringtemplate-3.1b1.jar Add mantra-1.0a4.jar to your CLASSPATH environment variable. To run scripts, the child process launched from mantra.Tool needs to see the jar: $ export CLASSPATH="mantra-1.0a4.jar:$CLASSPATH" $ java mantra.Tool println("hi"); hi $ The following only works when you are translating to Java not running the result. (Mantra doesn't run generated classes...only scripts). $ java -cp .:mantra-1.0a4.jar mantra.Tool MyClass.om Mantra is written in Java and so you must have java installed on your machine before trying to use Mantra. ------------------------------- USAGE The main Java class is {mantra.Tool}. It translates Mantra source code to Java code. If the Mantra source is a program/script rather than a class or mixin, {mantra.Tool} also compiles and executes the resulting {Main.main()} method. Here is how to run a simple script using stdin: $ java mantra.Tool println("Hi"); Hi $ If that script were in Hi.om, then you'd do this: $ java mantra.Tool Hi.om Hi.om Hi $ To compile a class, do this: $ cat B.om class B { int x; foo() { println("B.foo()"); } } $ java mantra.Tool B.om B.om $ javac B.java $ ls -l B.* -rw-r--r-- 1 parrt staff 1220 Oct 5 18:24 B.class -rw-r--r-- 1 parrt staff 1578 Oct 5 18:24 B.java -rw-r--r-- 1 parrt staff 51 Oct 5 18:23 B.om $ ------------------------------- EXAMPLES In the distribution you will see an examples directory with some subdirectories. The examples are not very organized and I do not have the intended output for all of them entered yet. Nonetheless, they should give you a good idea of what his working at the moment. ------------------------------- DOCUMENTATION The only documentation that I have at the moment is the main webpage for mantra: http://www.linguamantra.org BUG DATABASE Please report bugs to parrt at cs usfca edu directly but you can view bugs here: http://www.antlr.org:8888/browse/MANTRA