Pike

Implementation of programming language Pike

Pike is the original (and the only) interpreter of Pike language. It is licensed under GNU GPL, GNU LGPL and MPL, thus can be used for basically any purpose.

Examples:

Hello, World!:

Example for versions Pike 7.8
void main() 
{
    write("Hello, World!\n");
}

Fibonacci numbers:

Example for versions Pike 7.8

This example uses recursive definition of Fibonacci numbers.

int fibonacci(int n) 
{
    return ( n<=2 ? 1 : fibonacci(n-1) + fibonacci(n-2) );
}

void main() 
{
    for (int n=1; n<=16; n++)
        write(fibonacci(n)+", ");
    write("...\n");
}

Factorial:

Example for versions Pike 7.8

This example uses recursive factorial definition. Note that int data type is extended to bignum in case of overflow.

int factorial(int n) 
{
    return ( n<=1 ? 1 : n * factorial(n-1) );
}

void main() 
{
    for (int n=0; n<=16; n++)
        write(n+"! = "+factorial(n)+"\n");
}

Quadratic equation:

Example for versions Pike 7.8
void main() 
{
    int A = (int)Stdio.stdin->gets();
    if (A == 0) 
    {
        write("Not a quadratic equation.\n");
        return 0;
    }
    int B = (int)Stdio.stdin->gets();
    int C = (int)Stdio.stdin->gets();
    int D = (B*B-4*A*C);
    write(D+"\n");
    if (D == 0)
        write(sprintf("x = %f\n",-B/2.0/A));
    else if (D > 0) 
    {
        write(sprintf("x1 = %f\n", (-B+sqrt(D))/2.0/A));
        write(sprintf("x2 = %f\n", (-B-sqrt(D))/2.0/A));
    }
    else 
    {
        write(sprintf("x1 = (%f, %f)\n", -B/2.0/A, sqrt(-D)/2.0/A));
        write(sprintf("x1 = (%f, %f)\n", -B/2.0/A, -sqrt(-D)/2.0/A));
    }
}

CamelCase:

Example for versions Pike 7.8

This example implements character-by-character string processing. The only thing to note is that Pike provides no data type for characters. text[i] would return an integer — ASCII-code of the corresponding character. To get i-th character as a string variable, one has to use text[i..i] which is operation of extracting substring from a string.

void main() 
{
    string text = lower_case(Stdio.stdin->gets());
    string cc = "";
    int i, lastSpace = 1;

    for (i=0; i<sizeof(text); i++) 
    {
        if (text[i] >= 'a' && text[i] <= 'z') 
        {
            if (lastSpace == 1)
                cc += upper_case(text[i..i]);
            else
                cc += text[i..i];
            lastSpace = 0;
        }
        else
            lastSpace = 1;
    }
    write(cc+"\n");
}