metalanguage and language set

Quickstart

Build ngrease HEAD release

Check out the ngrease sources

First let's check out the ngrease sources from svn. Here we pipe the output to tail to keep this document short, but you may want to see all output and just write the svn part of this pipeline. You may also want to omit the -r option, that is used here to keep this document reproducible, and check out HEAD instead.

~/work $ svn co -r 562 https://ngrease.svn.sourceforge.net/svnroot/ngrease/trunk ngrease-svn | tail -n 1
Checked out revision 562. ~/work $ ls
ngrease-svn

Build and smoketest ngrease

Let's build an ngrease release.

~/work $ cd ngrease-svn/master-build

We are going to call this release HEAD.

~/work/ngrease-svn/master-build $ echo "ngrease-version=HEAD" > ../ngrease-release/release.properties

Now we can build the release. Again here we do some output processing to keep this document short. You may want to see all output and just write the ant part of this pipeline.

~/work/ngrease-svn/master-build $ ant simplified-release | tail -n 2 | grep -v "Total time"
BUILD SUCCESSFUL
(Output asserted)
~/work/ngrease-svn/master-build $ cd ..

Let's pipe a string to ngrease to smoketest it.

~/work/ngrease-svn $ echo string:hello | ngrease-release/target/ngrease-all-HEAD/bin/ngrease -f -
hello
(Output asserted)

Add ngrease binaries to PATH

Now let's add the ngrease binaries to PATH for convenience. Here we use the $(pwd) trick to keep the command portable. You can use a normal absolute path instead, if you wish.

~/work/ngrease-svn $ export PATH="$PATH:$(pwd)/ngrease-release/target/ngrease-all-HEAD/bin"

Since we used an absolute path we can leave the release directory.

~/work/ngrease-svn $ cd ..
~/work $ echo string:hello | ngrease -f -
hello
(Output asserted)

Ok, we are ready to move on and learn to use the tool.

Hello world

Write a hello world string.

~/work $ mkdir hello
~/work $ cd hello
~/work/hello $ $EDITOR hello.ngr
# ngrease 1

string {Hello " " World! "\n"}

Let's evaluate it.

~/work/hello $ ngrease -f hello.ngr
Hello World!
(Output asserted)
~/work/hello $ cd ..

Binary hello world

~/work $ mkdir binaryhello
~/work $ cd binaryhello
~/work/binaryhello $ $EDITOR binaryhello.ngr
# ngrease 1

hex-bytes {
48 65 6c 6c 6f 20 ca fe ba be
}
~/work/binaryhello $ ngrease -f binaryhello.ngr | od -tx1z
0000000 48 65 6c 6c 6f 20 ca fe ba be >Hello ....< 0000012
(Output asserted)
~/work/binaryhello $ cd ..

Directory and file generation

~/work $ mkdir filegeneration
~/work $ cd filegeneration
~/work/filegeneration $ $EDITOR hello-shar.ngr
# ngrease 1

$ (
context-chain {
$:default-context
$:evaluate:$:reference:
resource:"/net/sf/ngrease/languages/shar/context.ngr"
}
):with {
parent-dir:"."
$:transform {to:sh-source from:
directory {name:hello
file {name:world.txt
string {Hello " " World!}
}
}
}
}
~/work/filegeneration $ ngrease -f hello-shar.ngr
mkdir ./hello cat > ./hello/world.txt <<\TODO_UNIQUE_EOF_STRING Hello World! TODO_UNIQUE_EOF_STRING
(Output asserted)
~/work/filegeneration $ ngrease -f hello-shar.ngr | bash
~/work/filegeneration $ find hello
hello hello/world.txt ~/work/filegeneration $ cat hello/world.txt
Hello World!
(Output asserted)

TODO fix the extra trailing newline.

TODO productize ngrmount and document mounting a filesystem element.

~/work/filegeneration $ cd ..

Java HelloWorld

~/work $ mkdir javahello
~/work $ cd javahello
~/work/javahello $ $EDITOR java-hello-shar.ngr
# ngrease 1

$ (
context-chain {
$:default-context
$:evaluate:$:reference:resource:/net/sf/ngrease/languages/java/context.ngr
$:evaluate:$:reference:resource:/net/sf/ngrease/languages/shar/context.ngr
}
):with {
parent-dir:"."
$:transform {to {java-package-directory directory sh-source} from:
package {name:helloworld
class {
public name:Hello
method {
public static returns:void name:main
parameter {type:java.lang.String[] name:args}
body:
method-call {
field-ref {class-ref:System out}
println
string-constant:"Hello World!"
}
}
}
}
}
}
~/work/javahello $ ngrease -f java-hello-shar.ngr | bash
~/work/javahello $ cat helloworld/Hello.java
package helloworld; public class Hello { public static void main(java.lang.String[] args) { System.out.println("Hello World!"); } } ~/work/javahello $ javac helloworld/Hello.java
~/work/javahello $ java helloworld.Hello
Hello World!
(Output asserted)
~/work/javahello $ cd ..