This shows you the differences between two versions of the page.
|
documentation [2010/05/21 03:45] redbrain |
documentation [2010/05/21 04:05] (current) redbrain |
||
|---|---|---|---|
| Line 84: | Line 84: | ||
| </code> | </code> | ||
| - | So take note of all of those different operations i haven't shown all the primitives being assigned but i think you get the idea, applying the '=' operator assigns and identifier to a primitive or to another symbol be it a list index address or simply a primitive. | + | 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 ^ | ^ Primitive Type ^ Lexical Token ^ | ||
| Line 94: | Line 94: | ||
| | Nil | (nil) | | | Nil | (nil) | | ||
| - | This is the basic basic primitives for now, integers will be filled out to have 'long integer' lexical tokens and Big-nums and multi-precision support in later releases documentation will be updated as these features become more stable. | ||
| - | |||
| - | What is this 'nil' type this is simply to have an initialized symbol to have basically no value, which is simply a way to, to have a variable within a certain context so it can be referenced in particular way. Example if you create a symbol within a loop or other similar construct it only exists within that procedure it was created as soon as the call stack is pop'd the symbol is marked as garbage and is impossible to reference again, so if you want to do some processing on data and assign a symbol the result from several call stack procedure's deep you can assign this nil symbol the result and it is still reference-able. Example: | ||
| - | |||
| - | <code Python> | ||
| - | x = nil; y = 0; z = 15; | ||
| - | while ( y < z ) { | ||
| - | q = z - y; | ||
| - | print "q= <",q,">!\n"; | ||
| - | if( y == 5 ) { | ||
| - | x = true; | ||
| - | break; | ||
| - | } | ||
| - | y = y + 1; | ||
| - | } | ||
| - | |||
| - | print "x=<",x,">!\n"; | ||
| - | </code> | ||
| - | |||
| - | This is kind of a silly procedure, but its simply to address the idea, that 'x' has been initialized so it is assessable within the loop but the data isn't lost but symbol q within the while context is, since the call stack has pop'd out of that procedure as the conditional y == 5 is inevitably going to evaluate true with a few iterations and break the loop context! | ||
| - | |||
| - | ==== Lists - Strings ==== | ||
| - | |||
| - | Lists and Strings are implemented in two very different ways, my original idea was to work like Haskell and simply have strings as lists which is a nice idea theoretically but potentially an awful thing to do. Due to the fact you will end up having to use a lot of memory for the code to manage the list of characters which isn't needed when they can be separate object but have the same operations! | ||
| - | |||
| - | But never the less the api on the 2 objects is very similar and you can use them in very different ways lets look at lists first. | ||
| - | |||
| - | <code python> | ||
| - | list = [ 1,2,3,4 ]; | ||
| - | print list,"\n"; | ||
| - | |||
| - | list = [ list , "some string!" ]; | ||
| - | print list,"\n"; | ||
| - | |||
| - | x = list[ 0 ]; | ||
| - | print x,"\n"; | ||
| - | </code> | ||
| - | |||
| - | So notice we can have lists inside lists and access lists just like it was an array in C! This is simply a call to the member function ' list.index( Int ) ' | ||
| ==== Functions ==== | ==== Functions ==== | ||
| - | Functions is where a programmer should spend most of his time even if it is 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. | + | 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 we 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: | + | 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: |
| <code python> | <code python> | ||
| defun print_something( x ) { | defun print_something( x ) { | ||
| - | print x; | + | print( x ); |
| } | } | ||
| Line 151: | Line 112: | ||
| <code python> | <code python> | ||
| - | defun multiply( x,y,z ) { | + | defun multiply( x,y ) { |
| - | z= x*y; | + | return ( x*y ); |
| - | return z; | + | |
| } | } | ||
| - | z = -1; | + | print( "x= <", (x=multiply( 3,4 )) ,">!\n" ); |
| - | x= multiply( 3,4,z ); | + | |
| - | + | ||
| - | print "x= <",x,"> z= <",z,">!\n"; | + | |
| </code> | </code> | ||
| - | Something i want people to notice is that since we assigned parameter 'z' the toplevel context variable 'z' we have actually changed the symbol, many modern dynamic languages don't allow this due to the fact they use environment contexts for each function call. I prefer having total control and hope programmers don't do silly things with the interpreter! | + | 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. |
| - | Things are no where near finished with functions lets take the example: | + | So for example things like this can happen quite often: |
| <code python> | <code python> | ||
| - | defun functor( ... ) { | + | defun random_function( x,y ) { |
| - | args= $$; | + | x.some_member_function( y ); |
| - | print args, "!\n"; | + | return ( x+1 ); |
| } | } | ||
| </code> | </code> | ||
| - | People familiar with perl will understand this and this: | + | 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. |
| - | <code python> | + | In Crules you can do something like this: |
| - | defun functor { | + | |
| - | args= $$; | + | |
| - | print args, "!\n"; | + | |
| - | } | + | |
| - | </code> | + | |
| - | + | ||
| - | Note these are exactly the same function simply the parameters are varadic, you access arguments (if any) via the '$$' key-value, which is a list you can manipulate. You can mix this example: | + | |
| <code python> | <code python> | ||
| - | defun functor( x,y, ... ) { | + | defun do_something( Int x, Int y, List z ) { |
| - | __args= $$; | + | z.append( x+y ); |
| - | for i in args { | + | return True; |
| - | print i; | + | |
| - | } | + | |
| - | ...... | + | |
| } | } | ||
| </code> | </code> | ||
| - | Lets take a closer look at handling parameters its nice we can have a dynamic type system so we can literally pass anything as an argument, but sometimes we want to be more forceful and explicitly say what the arguments should be as a pre-condition! | + | 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: |
| <code python> | <code python> | ||
| - | defun functor( Int x, Float y, String z ) { | + | defun foo( x,y,z ) .... |
| - | zz= x+y; | + | |
| - | print z,zz,"!\n"; | + | |
| - | } | + | |
| </code> | </code> | ||
| - | So we know that z could be some kind of message etc its a random functions just to show the idea, it gets more interesting when passing data-structures, we can have them work more like static arrays example: | + | The interesting thing about this is it has the effect or becoming this function signature: |
| <code python> | <code python> | ||
| - | defun max( Int [x] ) { | + | defun foo( Object x, Object y, Object z ) ... |
| - | m= x[0]; | + | |
| - | for i in x { | + | |
| - | if( i >= m ) { | + | |
| - | m = i; | + | |
| - | } | + | |
| - | } | + | |
| - | return m; | + | |
| - | } | + | |
| </code> | </code> | ||
| - | So you get the idea these are nice features but really they are the same idea: I will show some of the function work i am currently working towards but be warned these semantics are by no-means stable nor are they implemented because of this! | + | 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. |
| - | <code python> | + | Builtin primitive types are: |
| - | # simple linear velocity :-) | + | |
| - | f := u + (a * t); | + | |
| - | x = f( u=2, a=2.5, t=3 ); | + | |
| - | </code> | + | |
| - | These are runtime function definitions similar to Python lambda forms, and resemble simple algebraic substitutions which will be a nice way of defining functions, any parameter/variable you don't assign with in the given expression shall be left alone and will be a valid procedure if and only if the symbols un-touched are visible in the running context! Due to the nature of the semantics this is limited to one expression. | + | ^ Primitive Type ^ Typing ^ |
| - | + | | Integer | Int | | |
| - | ==== Loops ==== | + | | Decimal | Float | |
| - | + | | Character | Char | | |
| - | Where would an imperative language be without loops! In crules i only implement 2 looping methods 'for' and 'while'. Although every for loop can be implemented as a while loop same can't be said the other way around. My origional idea for for loops was to make them work like python iterators as in: | + | | String | String | |
| - | + | | Boolean | Bool | | |
| - | <code Python> | + | | List | List | |
| - | for i in [ 1,2,3,4 ]: | + | |
| - | print i | + | |
| - | </code> | + | |
| - | + | ||
| - | But this doesn't work for the very strong lets allow side affects approach in crules, which is a very crucial difference between Python, as in arguments can change their value if they want. So having iterators doesn't make sense if your to take the example: | + | |
| - | + | ||
| - | <code Python> | + | |
| - | list = [ 1,2,3,4 ] | + | |
| - | for i in list: | + | |
| - | list = 2; | + | |
| - | print i | + | |
| - | print list | + | |
| - | </code> | + | |
| - | + | ||
| - | Although this is something i doubt anyone in the world would ever think of doing in Python but its the idea that arguments should have the ability to change. So what would be valid out come of this? So python would output this: | + | |
| - | + | ||
| - | <code> | + | |
| - | >>> list = [ 1,2,3,4 ] | + | |
| - | >>> for i in list: | + | |
| - | ... list = 2 | + | |
| - | ... print i | + | |
| - | ... print list | + | |
| - | ... | + | |
| - | 1 | + | |
| - | 2 | + | |
| - | 2 | + | |
| - | 2 | + | |
| - | 3 | + | |
| - | 2 | + | |
| - | 4 | + | |
| - | 2 | + | |
| - | </code> | + | |
| - | + | ||
| - | This seems reasonable but implementation wise your going to have to clone the list which you iterate over which just isn't helpful since every runtime developer will tell you its a nightmare cutting down on object clones. Yes internally you can use Pointers to these things but most of the time for the abstract case this can cause all manor or problems. Lets take a look at another example which made me change my mind completely: | + | |
| - | + | ||
| - | <code Python> | + | |
| - | list = [ 1,2,3,4 ] | + | |
| - | for i in list: | + | |
| - | i = 2; | + | |
| - | + | ||
| - | print list | + | |
| - | </code> | + | |
| - | + | ||
| - | And look at the output python limits the side effects of procedures on arguments and personally i don't like this if i set the iterator to a value i expect the argument to change! But really its personal preference | + | |
| - | + | ||
| - | <code python> | + | |
| - | list = { 1,2,3,4,5 }; | + | |
| - | for i in list { | + | |
| - | print i,"\n"; | + | |
| - | } | + | |
| - | </code> | + | |
| - | + | ||
| - | Now for a while example: | + | |
| - | + | ||
| - | <code python> | + | |
| - | quit= false; i= 0; | + | |
| - | while( quit != true ) { | + | |
| - | if( i == 5 ) { | + | |
| - | quit = true; | + | |
| - | } | + | |
| - | else { i++; } | + | |
| - | } | + | |
| - | </code> | + | |
| - | + | ||
| - | Thats enough on loops i assume most of you are programmers looking at this, i will need more help building nicer documentation in the future! | + | |
| - | + | ||
| - | ==== Conditional Blocks (if-statements) ==== | + | |
| - | + | ||
| - | I have to admit i really love lolcats! :D So here is a little conditional! | + | |
| - | + | ||
| - | <code python> | + | |
| - | icanhascheezburger = true; | + | |
| - | + | ||
| - | if( icanhascheezburger ) { | + | |
| - | print "Yes can haz cheeze burger! :-)"; | + | |
| - | } | + | |
| - | else { | + | |
| - | print "No can haz cheeze burger! :'("; | + | |
| - | } | + | |
| - | </code> | + | |
| - | + | ||
| - | Lets do a more exciting example some random string matcher: | + | |
| - | + | ||
| - | <code python> | + | |
| - | from sys.crl import sys.readline; | + | |
| - | + | ||
| - | PASSWORD="icanhascheezburger"; | + | |
| - | + | ||
| - | prompt = ">>> "; | + | |
| - | input = sys.readline( prompt ); | + | |
| - | + | ||
| - | while ( input != PASSWORD ) { | + | |
| - | print "Password Incorrect! Try again..."; | + | |
| - | input = readline( prompt ); | + | |
| - | } | + | |
| - | </code> | + | |
| - | + | ||
| - | Yeah i know thats a terrible password handler but i just want to show how you can compare strings. | + | |
| - | + | ||
| - | ==== Object Orientation ==== | + | |
| - | + | ||
| - | Object Orientation is very important, and really has become the most prefered paradigm to program in, simply due to the fact building very clear/clean API's is very easy and easy to do well! Java has very good features in this area, and in crules i want to try and take some influence from this but remembering that this is a dynamic language some ideas many change lets take a look at basic Objects: | + | |
| - | + | ||
| - | <code python> | + | |
| - | class myclass { | + | |
| - | defun myclass( ) { | + | |
| - | } | + | |
| - | } | + | |
| - | </code> | + | |
| - | + | ||
| - | .. this area needs more work other featues are more important to me at the moment... | + | |
| - | + | ||
| - | ==== Lazy Evaluation and The Where Clause ==== | + | |
| - | + | ||
| - | This still needs a lot of work but i will give you some idea of what i mean here: | + | |
| - | + | ||
| - | <code python> | + | |
| - | defex max( Int [x] ) := j where { j=0; each i in x -> (j>=i) }; | + | |
| - | + | ||
| - | # or: | + | |
| - | + | ||
| - | defex even( Int [x] ) := j where { each i in x -> even( i ) }; | + | |
| - | </code> | + | |
| - | Beware this syntax needs alot of work still, and it also resembles haskell list comprehension. | + | You can of course create your own objects and those object types can become parameter types if wanted also. |
| - | ==== Rules and Dependencies ==== | + | === Coming Soon more documentation === |
| - | The funny thing in this section Rules was meant to be the focus of crules but I've found so many other parts of language design intrigue'd me much much more so this hasn't been worked on for some time! And unlikely to be worked on until Object Orientation is finalized for 1.0 when we have method overrides implemented to better understand how this can work. | + | More documentation coming asap its being re-written |