PDA

View Full Version : LAPE Basics and modifications [Guide/Tut]



Enslaved
05-24-2013, 09:35 PM
Basic Tutorial/Guide to Lape :P
Examples from nielsie95's Thread Here (http://villavu.com/forum/showthread.php?t=68613)
Lape is 95% similar to simba, but with a few nice changes, but I will go over the basics again.

Summary:
Faster,
Better Parsing
More Versatile across platforms
Much larger potential
Does not work with current srl lib, but is hopefully going to work in Simba 1.0 with SRL 6
Same method for Including for compiler

current issues -

Giving variables a value in the declaration (i.e. var t := (getSystemTime() + 5000)) will cause everything after that line to disappear from the function list.
o Using “for var i := 0 to 9 do” does the same as above.
o When {$include_once file.simba} is used to include the same file more than once, it’s functions appears multiple times on the function list. This only applies if the file is included in two separate files. To recreate, download this package and open c.simba. Search “example” in the function list search bar, and you will see multiple versions of the same function.
o A type function should include the type in the function list. For example, instead of showing “setAngle”, the function list will show “TRSMinimap.setAngle”
When there is a compiling error in Lape, Simba doesn’t jump to that line or open the file that contains the error, it just prints the error in the debug box.



Your Base Types:

Integers: Normal whole numbers just like in PS

This splits up into (Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64) Lengths of how long they are, shorter length integers are faster to compute and take up less memory but cant store as much data

Floats: decimal Places


-Single - (4-byte) single-precision floating-point numbers ranging in value from -3.4028235E+38 through -1.401298E-45 for negative values and from 1.401298E-45 through 3.4028235E+38 for positive values. Single-precision numbers store an approximation of a real number.

-Double - 32 Bit data type

-Extended - 40-bit "extended precision" format

-Currency - 4 Decimal Places

Char(acters)
-Ansi - this is the standard list of 217 main characters
-Wide - A wide character is a computer character datatype that generally has a size greater than the traditional 8-bit character

Strings - enclosed in apostrophies : 'hello'

-Short strings - these are the basic ascii character based strings

-ANSI -a series of characters made up of ANSI Chars

-WIDE -a series of characters made up of WIDE Chars

-UNICODE -a series of characters made up of UNICODE Chars

Variants - a variable that can contain anything

Arrays- a container for different variables, can have different dimensions

Record- This complex data type corresponds to a row in a table. Each record consist of fields that form the columns of the table. A record is typically used to hold information about a fixed number of properties.

type
TPoint = record
x, y: Integer;
end;

const
tp: TPoint = [1,2];

var
t: TPoint;
begin
t := tp;
t := [,2];
WriteLn(t, ' :: ', tp);
end;


Assignments are now expressions, so you can now assign variables whist assigning a value to another variable at the same time:

var
a, b: Integer;
begin
a := b := 0;
a := 1 + (b := 2);
WriteLn('a: ', a, ' b: ', b);
end;


Enums - are a data type consisting of a set of named values called elements, members or enumerators of the type. The Boolean type, for example is often a pre-defined enumeration of the values FALSE and TRUE. Many languages allow the user to define new enumerated types. ( possibly fits in with records)

Pointers - Defined by @procedure sets a procedure to a variable/ in memory sense, it physically directs the counter to the set memory address.
Pointers

var
a: Integer;
b: ^Integer;
begin
a := 2;
b := @a;
b^ := 3;
WriteLn(a);
end;



Function Pointers- points towards a function and may be able to pass parameters

Var
thisvar := Function();
Function Reply() : Boolean;
Begin
Writeln('Hello');
Exit(True);
end;
Begin
thisvar := @reply;
If thisvar() then
Writeln('bye');
end;

Operations:

:=
Your definition operator for assigning data to variables

Whats New:
-Assigning default values.

procedure test(a: Integer = 1; b: string = 'a');
begin
WriteLn(a, ' -> ', b);
end;

begin
test;
test(,'hoi');
test(2);
end;


As said above, assignments are now also operators and multiple assignments can be made every "line"

=
Equals operator, will return 1 if the 2 values are equal - simple action handled by the Arithmetic logic Unit on your cpu

<>
Not Equals, opposite value of equlas operator

>
bigger than, returns 1 if left value is larger than right value. returns 0 if equal or below

>=
Bigger or equal to, Like ">" but returns 1 if equal

<
smaller than - ref to ">"

<=
smaller or equal to, Like "<" but returns 1 if equal

@
returns the address of a variable, or of a function, procedure, or method; that is, @ constructs a pointer to its operand.

----Math Operators----

^ or **
Power operator like the function pow(base,exponent)

+,-,*,/
Basic calculation operators

Mod, i.e a:= 6 mod 4 ; a would = 2 (returns the remainder)


Logic Operators

Boolean operators
And
OR
XOR
NOT

Bitwise Operators
SHL
SHR

more info on bitwise operators:http://compsci.ca/v3/viewtopic.php?t=9893


More Features:

Overloading Functions

Function overloading or method overloading that allows creating several methods with the same name which differ from each other in the type of the input and the output of the function. It is simply defined as the ability of one function to perform different tasks.
(this also works for imported functions)

procedure Hoi(Name: string); overload;
begin
WriteLn('Hoi ', Name);
end;

procedure Hoi; overload;
begin
Hoi('folks');
end;

begin
Hoi;
end;


Overriding Functions:
This is when you what a certain function to do something else or give it a different execution pattern.

function IntToStr(i: Int64): string; override;
begin
Result := 'int ' + inherited;
end;

begin
Writeln(IntToStr(42));
end;


Else Clauses for while/ for loops:
You can now apply the else clause as shown below:

begin
for 1 to -1 with 2 do
WriteLn('Looping')
else
WriteLn('No loop.. Aww!');

while False do
{nothing}
else
Writeln('No loop again. Jeez.');
end;




Passing Values to exit statements:
Exit(Result)/Continue(Depth)/Break(Depth)

function Test: Integer;
var
i: Integer;
begin
for i := 1 to 10 do
begin
WriteLn('i = ', i);
repeat
if (i < 3) then
Continue(2)
else
Break(2);
until False;

WriteLn('Out of the loop');
end;
Exit(-1);
end;

begin
Test;
end;




Joining Records: nice feature

type
TRGBRecord = packed record
R, G, B: Byte;
end;
TRGB = union
Color: Integer;
RGB: TRGBRecord;
end;

var
c: TRGB;
begin
c.Color := 16777215 {clWhite};
WriteLn('R: ', c.RGB.R, ' G: ', c.RGB.G, ' B: ', c.RGB.B);
end;



Untyped parameters
This is very useful in some awkward to program places

procedure Untyped(const x);
begin
WriteLn(Integer(x));
end;

var
i = 1;
a = [1, 2, 3, 4];
begin
Untyped(i);
Untyped(a);
end;


Loose Syntaxing

{$X+}

WriteLn('hoi!');

begin
var i = 0;
for var a := 0 to 2 do
i := i + a * 2;
WriteLn(a);
end;


Type Functions:
Type functions

function Integer.Next: Integer;
begin
Result := Self + 1;
end;

begin
WriteLn(2.Next);
end;


Will be adding to this when i have time.

StickToTheScript
05-24-2013, 09:59 PM
This is pretty cool! I would like to try. Put me down if you need testing!

rj
05-25-2013, 04:53 PM
Thanks rep++ this is needed to push lape

ActualNoob
06-10-2013, 11:53 AM
nice tutorial enslaved. I have a few questions.
Is it possible to override the default operators for a "type" ive defined, for example '<', '='?
is it possible to have this sort of referencing?


type Person= record
name: String;
age: String;
Parent: ^Person;
end;

stata
08-29-2013, 06:08 PM
Great, but what's the syntax for declaring/using Enums? Are arrays always pointers as in C/Java or no?

Zyt3x
08-29-2013, 07:01 PM
Be careful when writing type functions. Making something like this could potentially mess up your script:
procedure Integer.setNext();
begin
self := self + 1;
end;

var
a := 2;

begin
writeLn(1); // 1
writeLn(a); // 2
a.setNext(); // a = 3
writeLn(a); // 3
3.setNext(); // 3 = 4
writeLn(3); // 4
end;

footballjds
08-29-2013, 08:10 PM
GREAT

minus comparing lape to simba :P

samerdl
10-16-2013, 01:57 PM
This works really well with Simba 1.0, the oldschool include now won't compile with simba 1.0 tho :(

great guide!

Flight
10-16-2013, 04:17 PM
I tried your examples for both Function Pointers as well as Loose Syntaxing, both gave compile errors. Are you sure this information is accurate?...

Sax
10-16-2013, 09:04 PM
Thanks! Was waiting for this. :D

Brandon
10-16-2013, 11:29 PM
I tried your examples for both Function Pointers as well as Loose Syntaxing, both gave compile errors. Are you sure this information is accurate?...

Because a lot of the information is missing or in-accurate..


@OP..

Float aka a Single is a 32-bit data type.. Why would a double be 32 then if it is guaranteed to be "double" the size of a float?

A double is a 64-bit data type (In Simba x32-x64).
An extended is 10 bytes in size and is an 80-bit data type.


In lape you can do:


begin
writeln(sizeof(Double) * 8); //it would print 64.
writeln(sizeof(Single) * 8); //it would print 32.
writeln(sizeof(Integer) * 8); //it would print 32.
writeln(sizeof(Extended) * 8); //it would print 80.
writeln(sizeof(Int64) = sizeof(Double)); //Will print true because both are 64-bit data types.
end.



Missed an example for enums which would be:



type Interface = (CHAT, INVENTORY, SETTINGS, MINIMAP);


Procedure Switch(I: Interface);
Begin
Case I Of
CHAT: writeln('Chat Interface');
INVENTORY: writeln('Inventory Interface');
SETTINGS: writeln('Settings Interface');
MINIMAP: writeln('Minimap Interface');
end;
End;

begin
Switch(Interface.CHAT); //Enum via Non-Global Namespace access.
Switch(Settings); //Global Namespace enum access.
Switch(1); //Non-strongly typed enum. Integer can be implicitly casted to an enum.
writeln(Ord(Interface.CHAT));
end.



Pointers - Defined by @procedure sets a procedure to a variable/ in memory sense, it physically directs the counter to the set memory address.

Isn't right :S.. Not sure what counter but that isn't what a pointer does. A pointer holds the memory address of an object, record, procedure, function, variable, etc.. Anything.. A pointer also takes up space on the stack in order to hold the address.

You can find out how many bytes it takes up by doing:

writeln(sizeof(Pointer));



Next, this example below is incorrect.. It is missing the return type of the function pointer.. It's also assigning to a variable of unknown type instead of declaring that variable of func-pointer type.

Var
thisvar := Function(); //This line is wrong.. Cannot assign thisvar to an anonymous function aka a Lambda.. Also declaration of the function pointer is incorrect..
Function Reply() : Boolean;
Begin
Writeln('Hello');
Exit(True);
End;

Begin
thisvar := @reply;
If thisvar() then
Writeln('bye');
End;



It should be:


Var
thisvar: Function(): Boolean; //Function return type fixed and the := is changed to semi-colon.
Function Reply() : Boolean;
Begin
Writeln('Hello');
Exit(True);
End;

Begin
thisvar := @reply;
If thisvar() then
Writeln('bye');
End;



@ constructs a pointer to its operand.

No it does not.. @ only gives the address of its operand. Simply using @ alone does nothing. Nothing points to anything there. You need to do something with the address it gives you. Maybe assign it to a pointer or pass it to a function or dereference it.



Math Operators ^ or ** acts like pow(X, Exponent)

No it does not.. ^ operator only does that in C or C++ or other languages.. In Pascal, that ^ operator means to either create a pointer or to derefence a pointer..


var
Ptr: ^Integer; //The ^ before the Integer means it is a pointer to an integer.
begin
Ptr^ := 3; //The ^ after the variable name means to dereference the pointer.. aka retrieve the value being pointed to..
//Assigning to a dereferenced pointer changes the value being pointed to.
end.


That is it.. no power operator. Also, in the most recent Simba, ** as power operator is broken.

Example:

begin
writeln((2 ** 2)); //Exception in Script: Operator "Power" not compatible with types at line 2, column 14
end.



Not, And, Or, XOR are BITWISE operators.. Or, And, and Not operators can be both logical and bitwise.

Example:

Function IsOdd(Value: Integer): Boolean;
Begin
Result := (Value And 1); //Bitwise And.
End;

Begin
writeln(IsOdd(2)); //False. 2 is not an odd number.
writeln(Not 1); //Bitwise Complement.

//if (A and B) then.. //Logical And.
//if (Not A and B) then.. //Logical Negation.
End.



The definition you have as a union is also incorrect. A union is not for "Joining" anything.. A union is for bit-level access. Usually used for accessing the different bits of a value. Using a union directly can also mess you up if you don't take into account "endianness".


Your union is thus equal to doing:

Function GetRValue(Colour: Integer): Byte;
Begin
Result := (Byte)(Colour);
End;

Function GetGValue(Colour: Integer): Byte;
Begin
Result := (Byte)((Colour) shr 8);
End;

Function GetBValue(Colour: Integer): Byte;
Begin
Result := (Byte)((Colour) shr 16);
End;


var
Colour: Integer;
Begin
Colour := 16777215;
writeln(GetRValue(Colour));
writeln(GetGValue(Colour));
writeln(GetBValue(Colour));
End.


The shifting allows you to access different bits of that 32 bit integer.. The union does exactly this.. Allowing you to NOT have to write out all that crap.. So again, a union isn't for magically joining records or classes or anything. It's actually for accessing individual bits and some other things..



Your loose syntax example should actually be:

{$X+}

WriteLn('hoi!');

begin
var a, i = 0;
for a := 0 to 2 do
i := i + a * 2;
WriteLn(a);
end;



I'm not going to rate this tutorial.. You should fix the mistakes though.. Other than that, good job. You probably already knew all this stuff but it's just to make it clear for everyone else..