Thursday, December 3, 2009

Turbo grammar

Surprise, surprise!
Do you guess what Santa will post today? Yes! that's right, the grammar for Turbo!
Abstract definition of a application|console|library in Turbo

application|console|library IDENTIFIER_NAME
{
        // use external libraries
        using LIBRARY_NAME1;
        using LIBRARY_NAME2;

        // include files as resource
        resource RESOURCE_NAME in "[PATH\]FILENAME.EXTENSION";

        // define constants
        const CONST_NAME = CONST_VALUE;

        const CONST_ARRAY_NAME = [VALUE_LIST];

        // enumeration
        enum {ENUM_NAME[, ENUM_NAME_LIST}

        // define variables
        var VAR_NAME[, VAR_NAME_LIST];

        // define class
        class CLASS_NAME ( BASE_CLASS_NAME )
        {
                // define class constants
                const CONST_NAME = CONST_VALUE;

                // define class variables
                var VAR_NAME[, VAR_NAME_LIST];

                // define class functions
                function FUNCTION_NAME( PARAM[, PARAM_NAME_LIST)
                {
                        // define function constants
                        const CONST_NAME = CONST_VALUE;

                        // define function variables
                        var VAR_NAME[, VAR_NAME_LIST];

                        return RETURN_VALUE;
                }
        }

        // define application's MAIN function, reserved word!
        // this will be executed only in application|console mode
        // for library use "initialization" simulate "main"
        function main
        {
                // do something
        }
}

But wait! that's some JavaScript(ECMA Script) combined with C#?!
Yup! and a bit of Delphi(Pascal) if you continue reading.
After searching the net for hours I came to the conclusion that C-like languages are more welcomed by masses.

Here is how statements will be defined in Turbo
STATEMENTS
-- IF
        if (CONDITION)
                STATEMENT;

        if (CONDITION)
        {
                STATEMENT_LIST;
        }
        else
        {
                STATEMENT_LIST;
        }


-- FOR
        for (IDENTIFIER = VALUE; CONDITION; UPDATE_EXPRESSION)
                STATEMENT;

        for (IDENTIFIER = VALUE; CONDITION; UPDATE_EXPRESSION)
        {
                STATEMENT_LIST;
        }


-- WHILE
        while (CONDITION)
                STATEMENT;

        while (CONDITION)
        {
                STATEMENT_LIST;
        }


-- DO
        do
        {
                STATEMENT_LIST;
        }
        while (CONDITION);


-- SWITCH
        switch (IDENTIFIER|VALUE)
        {
                CASE_VALUE: STATEMENT;

                default: STATEMENT;
        }

        switch (IDENTIFIER|VALUE)
        {
                CASE_VALUE:
                {
                        STATEMENT_LIST;
                }

                default:
                {
                        STATEMENT;
                }
        }


-- TRY
        try
        {
                STATEMENT_LIST;
        }

        try
        {
                STATEMENT_LIST;
        }
        except
        {
                DO_ON_EXCEPTION;
        }

        try
        {
                STATEMENT_LIST;
        }
        finally
        {
                DO_FINALLY;
        }

        try
        {
                STATEMENT_LIST;
        }
        except
        {
                DO_ON_EXCEPTION;
        }
        finally
        {
                DO_FINALLY;
        }
But wait, there's more! initialization and finalization of application|console|library
INITIALIZATION/FINALIZATION
executes a statement or statement list at the initialization or
finalization of the application|console|library.

ex:

initialization
        STATEMENT;

initialization
{
        STATEMENT_LIST;
}

finalization
        STATEMENT;

finalization
{
        STATEMENT_LIST;
}
Even if you love labels or not, there will be goto's!
LABELS
using labels is possible by writing "label" keyword before the label name
label LABEL_NAME:

use "goto LABEL_NAME;" to jump to a label
How about embeding binary or text files?
RESOURCES
you can embed external files(binary files, text files, etc.) by using
the following syntax

resource RESOURCE_NAME in "[PATH\]FILENAME.EXTENSION";

RESOURCE_NAME -- will be used to access resource
Here they come, who? the operators!
OPERATORS
!       factorial, retrieves the factorial value of IDENTIFIER
ex:     a!

^       power, BASE^EXPONENT
ex:     a ^ b

not     negate
ex:     not a, negate value of a

and     logial AND
ex:     a and b, a and b is true

or      logical OR
ex:     a or b, a or b is true

xor     exclusive OR
ex:     a xor b

shl     bitwise shift left
ex:     a shl b, shift a's bits to the left by b

shr     bitwise shift right
ex:     a shr b, shift a's bits to the right by b

mod     modulo
ex:     a mod b, the remain of dividing a by b

div     integer division
ex:     a div b, divide a by b and return a integer value

%       percent left of right
ex:     a % b is equal to a * b / 100

*       multiply
ex:     a * b

/       divide
ex:     a / b

+       add
ex:     a + b

-       subtract
ex:     a - b

=       equal
ex:     a = b, check equality

>       greater than
ex:     a > b

<       less than
ex:     a < b

>=      greater or equal
ex:     a >= b

<=      less or equal
ex:     a <= b

:=      assignment
ex:     a := b

*=      assignment of multiplication
ex:     a *= b, same as a := a * b

/=      assignment of division
ex:     a /= b, same as a := a / b

+=      assignment by addition
ex:     a += b, same as a := a + b;

-=      assignment by subtraction
ex:     a -= b, same as a := a - b;

IDENTIFIER++    increment IDENTIFIER's VALUE
IDENTIFIER--    decrement IDENTIFIER's VALUE
And who can forget about the good old comments?
COMMENTS
-- single line
        // comment

-- multiline
        /* comment line 1
           comment line 2
                ...
           comment line n */
STRINGS
-- strings are between double quotes
"this is a simple string"
CHARACTERS
characters can be represented as single "a" or
#128 -- 128 is it's ordinal value
characters can be concatenated with strings
ex: "this is a simple string" + #128 = "this is a simple string €"
But where are the numbers?
NUMBERS
-- integer 1, 2, 3
-- hex $FFAACC
-- float 123.456
Any comments?

No comments:

Post a Comment

Blogroll(General programming and Delphi feeds)