Jamplate

Examples


This page contains example jamplate files and the output of executing them. All the examples below are available at the examples repository and has been tested using the Jamplate Executable.



Hello World

This example will print Hello World to the console.


The program file main.jamplate:

    #message 'Hello World'

Execution of main.jamplate:

    C:\test> jamplate main.jamplate
    Hello World


99 bottles of beer on the wall

This example is an implementation of the 99 bottles of beer. This implementation will print the song to the console.


The program file main.jamplate:

    #declare $i 99
    #declare $noun 'bottles'

    #while $i > 0
        #message $i ' ' $noun " of beer on the wall,\n"
        #message $i ' ' $noun " of beer.\n"
        #message "Take one down, pass it around,\n"

        #declare $i $i - 1

        #if $i == 0
            #message "No bottles of beer on the wall.\n\n"
        #else
            #if $i == 1
                #declare $noun 'bottle'
            #endif

            #message $i ' ' $noun " of beer on the wall.\n\n"
        #endif
    #endwhile

    #message "No bottles of beer on the wall,\n"
    #message "No bottles of beer.\n"
    #message "Go to the store, buy some more,\n"
    #message "99 bottles of beer on the wall.\n"

Execution of main.jamplate:

    C:\test> jamplate main.jamplate
    99 bottles of beer on the wall,
    99 bottles of beer.
    Take one down, pass it around,
    98 bottles of beer on the wall.

    98 bottles of beer on the wall,
    98 bottles of beer.
    Take one down, pass it around,
    97 bottles of beer on the wall.

    ...

    2 bottles of beer on the wall,
    2 bottles of beer.
    Take one down, pass it around,
    1 bottle of beer on the wall.

    1 bottle of beer on the wall,
    1 bottle of beer.
    Take one down, pass it around,
    No bottles of beer on the wall.

    No bottles of beer on the wall,
    No bottles of beer.
    Go to the store, buy some more,
    99 bottles of beer on the wall.


Quine

A program that prints its own sourcecode (Quine). This implementation will print its sourcecode to the console.


The program file main.jamplate:

    #declare input '#message "#declare input " "\'" input "\'" "\n" input'
    #message "#declare input " "\'" input "\'" "\n" input

Execution of main.jamplate:

    C:\test> jamplate main.jamplate
    #declare input '#message "#declare input " "\'" input "\'" "\n" input'
    #message "#declare input " "\'" input "\'" "\n" input


Fibonacci sequence

This example will take an input as an argument and print the Fibonacci Sequence of it to the console.


A header file src/fibonacci.jh:

    #if $p == 0
        #declare $r 0
    #elif $p == 1
        #declare $r 1
    #else
        #for $ort [$rt]
            #declare $p $p - 1
            #include __PATH__
            #declare $rt $r

            #declare $p $p - 1
            #include __PATH__
            #declare $rt $rt + $r

            #declare $p $p + 2
            #declare $r $rt
            #declare $rt $ort
        #endfor
    #endif

The program file src/main.jamplate:

    #declare $p $input
    #include __DIR__ '/fibonacci.jh'
    #message $r

Execution of src:

    C:\test> jamplate src $input=0
    0
    C:\test> jamplate src $input=1
    1
    C:\test> jamplate src $input=2
    1
    C:\test> jamplate src $input=3
    2
    C:\test> jamplate src $input=4
    3
    C:\test> jamplate src $input=5
    5
    C:\test> jamplate src $input=6
    8
    C:\test> jamplate src $input=7
    13
    C:\test> jamplate src $input=8
    21
    C:\test> jamplate src $input=9
    34
    C:\test> jamplate src $input=10
    55
    C:\test> jamplate src $input=11
    89
    C:\test> jamplate src $input=12
    144


Factorial

This example will take an input as an argument and print the Factorial of it to the console.


The program file main.jamplate:

    #declare $p $input - 1 
    #declare $r $input
    
    #while $p > 0
        #declare $r $r * $p
        #declare $p $p - 1
    #endwhile
    
    #message $r

Execution of main.jamplate:

    C:\test> jamplate main.jamplate $input=0
    0
    C:\test> jamplate main.jamplate $input=1
    1
    C:\test> jamplate main.jamplate $input=2
    2
    C:\test> jamplate main.jamplate $input=3
    6
    C:\test> jamplate main.jamplate $input=4
    24
    C:\test> jamplate main.jamplate $input=5
    120
    C:\test> jamplate main.jamplate $input=6
    720
    C:\test> jamplate main.jamplate $input=7
    5040


Truth machine

This example is an implementation of the truth machine. This implementation will take the input as an argument when executing the program and print the answer to the console.


The program file main.jamplate:

    #if $input == 0
        #message 0
    #elif $input == 1
        #while true
            #message 1
        #endwhile
    #else
        #error 'Invalid input: ' $input
    #endif


Execution of main.jamplate:

    C:\test> jamplate main.jamplate $input=0
    0
    C:\test> jamplate main.jamplate $input=1
    111111111111111111111111111111111111111111111111111111111111111111111111111111111 ...