Language Documentation/Tutorial

Crules has been specificity designed to be simple and intuitive for programmers, by making computing procedures easier while remaining dynamic and reusable. Designed with clean, clear syntax and keywords; along with a fully dynamic typing system; fast and dynamic data structures which makes it a great platform as a scripting engine which can be embedded into existing programs for a more high level logic computation or even as a fast and reliable application development platform, with a definite focus on being integrated with existing technologies.

For now in the early life of this language we will focus on explaining the language core until more advanced features (which will set it apart) are more stable and use-able, and since the nature of an early project the syntax for certain language constructs and semantics may change often!

Basic Interpreter Usage

So how do you start to work with this language, as in how do you write code? Well you have 2 choices, but first follow the guide Getting Crules to install the interpreter and get your computer all setup once this is complete you should have a working install of the language. Lets get rocking.. firstly put on some iron maiden and get your shell up!

To run crules as an interactive session simply:

% crules --help
% crules --version
# interactive session just do:
% crules
# or run on the main source file:
% crules source-code.crl

Done the interpreter usage is simple, its your code you run in it is the usage.

Language Tutorial

So lets begin with a straight in approach to the language, if your using this i will assume you at least have some good programming experience be it Python or C or Perl. Since the core features and semantics will work very similar to a mix of these languages!

Hello World

Lets do the customary 'Hello World' program though in dynamic languages its usually unhelpful since its usually a keyword:

% crules
>>> print( "Hello World!\n" );
Hello World!

Whao, that was complicated and mostly not very helpful to learn the language. So that let us print messages to the screen. Note '\n' for new line '\t' for tab, programmers familiar with other languages should know this. Note print is a function not a keyword like many other dynamic languages but more importantly its actually an internal alias call to the static object:

Stdio.print( "Hello World!\n" );

But take note this is strictly due to the implementation, and isn't part of the language specification, I believe it is important with respect to common sense there must be a clear separation between language extension/implementation and language specification to limit undefined or unexpected side-effects.

Expressions

The centre point in programming from the beginning of modern programming languages, has been Mathematics! Simply as a way for dynamic valued calculations on dynamically processed information. So lets make sure crules works right and do some simple math:

>>> 1 + 1;
2
>>> 2 * 3 + 1 - ( 3 / 2 );
5.500000

So if your familiar with other imperative procedural languages you get the idea, but this isn't helpful since this is hard coded data and I though the point was for languages to be dynamic so we need to introduce variables and the dynamic typing system!

Variables and Dynamic Typing

If you have worked with languages such as Perl or Python you have worked with a dynamic typing system, this is when variables are not typed statically typed similar to languages like C where you have to specifically set the type of a variable before assigning it and in all contexts where this variable is valid manipulation must result of the same type with the exception of some type casting.

Dynamic typing gives the ability to simply use an identifier to address any valid primitive in the language, and every context which is valid for this symbol can be assigned to any other symbol or user defined object. Primitives will be listed and described after a quick example:

>>> x = 3;
3
>>> x = 3 / 2 ;
1.500000
>>> x = true;
True
>>> x = false;
False
>>> x = 'c';
'c'
>>> x = "some string";
"some string"
>>> x = [ 1,2,3,4 ];
[ 1, 2, 3, 4 ]
>>> x = [ x, 2+3, ];
[ [ 1, 2, 3, 4 ], 5 ]
>>> x = y = z = true;
True

This was just a quick overview of some of the primitives within the language and this little table shows them and their respective valid lexical descriptions support for such things as Hex Digits will be added soon…

Primitive Type Lexical Token
Integer (0-9)+
Decimal {Integer}”.”{Integer}
Character '(a-zA_Z0-9_)'
String ”(a-zA-Z_)(a-zA_Z0-9_)*”
Boolean (true-false)
Nil (nil)

Functions

Functions is where a programmer should spend most of his time even if it isn't a fully functional language, but its the core on how stuff works even if your using a fully object orientated language you need to write your constructor functions and member functions.

For this I have created many different ways of defining functions each with very different side effects or none lets take a look at a basic function and its respective call:

defun print_something( x ) {
    print( x );
}
 
print_something( 2 );

So this will print 2 like it should! Lets look at something else just so you get the idea:

defun multiply( x,y ) {
  return ( x*y );
}
print( "x= <", (x=multiply( 3,4 )) ,">!\n" );

So ok now i think you see more correctly how parameters can be passed here you pass expressions which are evaluated on argument dispatch. Ok next I address a big problem with dynamic typing is allowing some static typing. It occurs in every big dynamically typed language API's want arguments to functions to be of certain types like Integers or Strings or some other user defined object.

So for example things like this can happen quite often:

defun random_function( x,y ) {
    x.some_member_function( y );
    return ( x+1 );
}

So ok do you know what will happen here because I sure don't because a dynamic type system allows anything to be anything we have no assurances that when entering a function were dealing with a symbol of a specific type which is why you see people getting frustrated quite often when they passed a slightly wrong type as the wrong parameter.

In Crules you can do something like this:

defun do_something( Int x, Int y, List z ) {
  z.append( x+y );
  return True;
}

So from this you know exactly what is going on you know x and y will be integers which can be added and z will be a mutable list which your appending do if we didnt have that typing the function would just have to hope the correct types would have been passed, this is not enforced in crules you can do as before and go as untyped example:

defun foo( x,y,z ) ....

The interesting thing about this is it has the effect or becoming this function signature:

defun foo( Object x, Object y, Object z ) ...

Since everything in crules is an object because it is an object orientated language you can pass in anything this is what goes on behind the scenes.

Builtin primitive types are:

Primitive Type Typing
Integer Int
Decimal Float
Character Char
String String
Boolean Bool
List List

You can of course create your own objects and those object types can become parameter types if wanted also.

Coming Soon more documentation

More documentation coming asap its being re-written

 
documentation.txt · Last modified: 2010/05/21 04:05 by redbrain
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki