PDA

View Full Version : It works for me.



danbaron
20-11-2011, 09:56
I tried Free Pascal (FPC Version: 2.4.2).

It is a compiler.

It can make DLLs.

I guess that means, theoretically, it could be called by thinBasic.

-----------------------------------------------------------------------------------------------

(I didn't see there was a forum for it at thinBasic, until I looked to upload this post.)

I haven't the vaguest notion about SDKs.


I definitely need to have a serious run with it and see if I can make a thinBasic SDK for Free Pascal in order to be able to develop thinBasic module under Free Pascal.

http://www.thinbasic.com/community/showthread.php?8349-Free-Pascal-2.2.0-released
(http://www.thinbasic.com/community/showthread.php?8349-Free-Pascal-2.2.0-released)
If it happens, it would be nice.
I think if I knew how to make a DLL and call it from thinBasic, I would be incentivized.
For me, I don't want PowerBasic, and, I don't want C.
I would like Fortran or Free Pascal - if I knew how to make an SDK, and it was easy, I would try to do one or both myself.

-----------------------------------------------------------------------------------------------

website:

http://www.freepascal.org/
(http://www.freepascal.org/)
documentation:
There are 7 documents in PDF files.
(The Run-Time Library (RTL) units reference manual, is 1739 pages.)

http://www.freepascal.org/docs.var
(http://www.freepascal.org/docs.var)
IDE = Lazurus

http://www.lazarus.freepascal.org/
(http://www.lazarus.freepascal.org/)
Free Pascal + Lazurus --> installer (76.2 MB)

http://sourceforge.net/projects/lazarus/files/
(http://sourceforge.net/projects/lazarus/files/)
Test:
(compiled to RAM, and run from IDE)


' code ----------------------------------------------------------------------------------------------------------------------

program test;

uses sysutils,dateutils;

const
// n equals one billion
n=1000000000;

var
i:longint;
f:extended;
t1,t2: tdatetime;

begin
t1:=now;

// Calculate the square roots.
for i:=1 to n do
begin
f:=sqrt(i);
end;

// Write the statistics.
t2:=now;
f:= millisecondsbetween(t2,t1)/1000;
writeln('time to calculate the square roots of 1 to ',n,':');
writeln('elapsed seconds = ',f);
writeln;

// Write some square root values.
for i:=1 to 9 do
begin
writeln(i,' ',sqrt(i));
end;

readln;
end.

' output --------------------------------------------------------------------------------------------------------------------

time to calculate the square roots of 1 to 1000000000:
elapsed seconds = 2.1450000000000000E+0001

1 1.0000000000000000E+0000
2 1.4142135623730950E+0000
3 1.7320508075688773E+0000
4 2.0000000000000000E+0000
5 2.2360679774997897E+0000
6 2.4494897427831781E+0000
7 2.6457513110645906E+0000
8 2.8284271247461901E+0000
9 3.0000000000000000E+0000

ErosOlmi
20-11-2011, 10:22
If you want to have an insode about thinBasic modules ...
in thinBasic Journal issue number 2 (http://www.thinbasic.com/community/showthread.php?9450-ThinBasic-Journal-Issue-2) at page 32 I started to explain what is a thinBasic module and how it works

Than go into your thinBasic installation directory and check under directory \thinBasic\SDK\
You will find SDK.ZIP file where there are examples on how to create a thinBasic module but unfortunately not for Pascal. We had for Delphi in the past.
If there will be real interest I can have a look and revamp it in some way.

Ciao
Eros

jack
20-11-2011, 16:58
Dan
there's an example on how to make a dll in the demo\win32 folder.
but it won't compile as is, the compiler does not like the string function length by itself so you need to assign it to a variable, writing it out probably would work also, here's the modified testdll.pas
the change is in the procedure p1


{
Copyright (c) 1998 by Pierre Muller
Win32 DLL usage example. It needs dlltest.pp
}
library testdll;
function GetModuleFileName(hModule:longint;lpszPath:pchar;cchPath:longint):longint;
stdcall; external 'kernel32' name 'GetModuleFileNameA';
procedure beep(ID:longint);
stdcall; external 'user32' name 'MessageBeep';
var
teststr : string;
procedure P1(var s:string);export;
var
p:array[0..255] of char;
l:integer;
begin
l:=length(s);
getmodulefilename(Hinstance,@p,255);
writeln('DLL: Hello, I''m DLL ',pchar(@p));
end;
procedure P2(x:longint);export;
begin
writeln('DLL: Argument X=',x);
writeln('DLL: New teststr="',teststr,'"');
end;
procedure P3(var t);export;
var
p : pointer;
begin
p:=Addr(T);
p:=p;
end;
procedure P4(x1:pointer);export;
begin
Inc(x1);
end;
procedure NewExit;
begin
beep(0);
writeln('DLL: Exit from testdll');
end;
exports
P1 index 1,
P2 name 'Proc2',
P3,
P4 resident,
teststr name 'FPC_string';
begin
writeln('DLL: HInstance ',Hinstance,' PrevInst ',Hprevinst,' DLLReason ',DLLreason,' DLLParam ',DLLparam);
teststr:='DLL init done';
exitproc:=@newExit;
end.

danbaron
20-11-2011, 21:51
Thanks Johan.

I found it.

I think I see the change,

"l:=length(s);".

----------------------------------------------

So far, making a thinBasic module looks complicated to me.

Under the example for PowerBasic, I see 6 files called, "UserDefinedLib", with different extensions.

As of now, from my viewpoint, it looks like magic.

But many modules exist, so, apparently, people do it.

Dan

Aurel
21-11-2011, 00:20
Im still not sure what is thinBasic modul...
Is this some kind of static lib?

ErosOlmi
21-11-2011, 15:11
So far, making a thinBasic module looks complicated to me.
Under the example for PowerBasic, I see 6 files called, "UserDefinedLib", with different extensions.
As of now, from my viewpoint, it looks like magic.
But many modules exist, so, apparently, people do it.

The only two important files are:
thinCore.inc
Consider it as a C header file. Is the interface exposed by thinBasic Core engine to all modules. It contains all the functions avaibale in thinCore.dll (thinBasic Core engine) and thanls to those interfaces your module functions will be able to be executed by a running script.
UserDefinedLib.bas
Is the example module created to show how to create a thinbasic module.
Just copy it into another one and personalize it


Im still not sure what is thinBasic modul...
Is this some kind of static lib?
This is one of those case reading something about is important.
I strongly suggest to read from page 32 of thinBasic Journal issue number 2.

In any case nothing strange. A thinBasic module is a standard 32bit DLL that implements 2 important functions: LoadLocalSymbols and possibly, but not mandatory, UnLoadLocalSymbols
The rest is done by the continuous interaction between Core engine (thinCore.dll) and developed module functions during script execution.

If you are interested, please give me a very easy function (described with its syntax you would like to have in thinbasic) you would like to have in an hypotetical module and I will write it for you explaining all the steps.

Ciao
Eros

danbaron
21-11-2011, 20:41
Very good.

I was hoping to receive such a reply from you.

I'm slowly working on things which are compiled.

This gives me hope that they will be able to be used with thinBasic.

And therefore, my motivation is increased.

I will bookmark this thread.

Thank you.
Dan.

ErosOlmi
21-11-2011, 21:07
Dan,

Pascal is my preferred programming language after Basic.

I'm also motivated to see if FreePascal can be used for developing thinBasic modules. It seems to have all we need to interface numbers, I need to test about dynamic string direct compatibility or some transformations are needed.

I've just downloaded Lazarus + FreePascal (http://www.lazarus.freepascal.org/)
My first step will be how to use DLLs from FreePascal. This will open the road to use thinCore.dll

I cannot promise anything at the moment because I will work on this in my spare time but I will try.

Ciao
Eros

ErosOlmi
22-11-2011, 01:22
Dear Dan,

I had some time and I developed a extremely base example of a thinBasic module developed with Free Pascal.
Module implements a single function called HalfNumber whose syntax is the following:

n = HalfNumber(AnyNumericExpression) AS Extended

In order to keep things as simple as possible, for the moment I've implemented a function that expects just a numeric expression as input.
As soon as you have got the whole picture we can see how to develop a new keyword that expects additional parameters.

Attached all the needed files:

a Lazarus project you can open and compile or change as you need.
It produces the DLL module.
Attention: if you compile source code by manual, library MUST be compiled using -WR directive (Generate a relocatable library. This library can be moved To another location In memory If the ImageBase address it wants Is already In use)
a thinBasic example using such a module
Here the source code of the Free Pascal module just to have a look at it before downloading.


library FreePascalExample;


{$mode objfpc}{$H+}


uses
Classes, Windows
{ you can add units after this };


{$R *.res}




// ------------------------------------------------------
// thinBasic_LoadSymbol_Delphi is used to declare new keywords in thinBasic language
function thinBasic_LoadSymbol_Delphi(SymbolName: string; ReturnCode: LongInt; FunctionOrSubPointer: pointer; ForceOverWrite: LongInt = 0): LongInt; stdcall; external 'thinCore.dll';
// ------------------------------------------------------


// Tell thinCore to parse a single numeric expression
// ------------------------------------------------------
function thinBasic_Parse1Number (): Extended; stdcall; external 'thinCore.dll' ;
// ------------------------------------------------------




// ------------------------------------------------------
// Ok, here we are with our first new thinBasic new keyword function
// Syntax of the function will be: n = HalfNumber(AnyNumericExpression) AS Extended
function Exec_HalfNumber(): Extended; stdcall;
var
MyExt: Extended;
begin
//thinBasic_Parse1Number will parse any numeric expression it will encounter
//passing back the result of the operation
MyExt := thinBasic_Parse1Number;
//Now that we have a number, we can get its half and return result
Exec_HalfNumber := MyExt / 2;
end;


// ------------------------------------------------------
// LoadLocalSymbols is automatically executed by thinCore
// consider LoadLocalSymbols as entry point of the module
// when "Uses" key is encountered
// LoadLocalSymbols is the place where all internal module functions
// must be declared to thinCore using "thinBasic_LoadSymbol_Delphi" function
// LoadLocalSymbols can also be used to initialize any variable or element needed by module
// LoadLocalSymbols IS MANDATORY and MUST be present in all thinBasic module
// ------------------------------------------------------
function LoadLocalSymbols(sPath: WideString): Longint; cdecl;
begin
// messagebox(0, Pointer(sPath),'Hello World from module. Path of module is',MB_OK or MB_ICONSTOP);


// here we are telling that the module implements a new thinBasic function called "HalfNumber"
// and it is connected to Exec_HalfNumber function
thinBasic_LoadSymbol_Delphi ('HalfNumber', 10, @Exec_HalfNumber, 0);


// Just return zero
LoadLocalSymbols := 0;
end;




// ------------------------------------------------------
// UnLoadLocalSymbols is executed by thinCore when thinBasic is going
// to unload from memory
// Use UnLoadLocalSymbols to execute any needed module clean up
// UnLoadLocalSymbols is NOT mandatory to exist
// ------------------------------------------------------
function UnLoadLocalSymbols(): Longint; cdecl;
begin
UnLoadLocalSymbols := 0;
end;




exports
LoadLocalSymbols, UnLoadLocalSymbols;
end.





Hope this can help to start.
Ciao
Eros

danbaron
22-11-2011, 08:26
Thank you, I downloaded the folder (I don't know how you do that stuff so fast.).

I'm already working on something, and, I want to get it to work first.

http://en.wikipedia.org/wiki/Toom–Cook_multiplication
(http://en.wikipedia.org/wiki/Toom%E2%80%93Cook_multiplication)
First I started it with C, then, I abandoned it and went to PowerBasic, then, I abandoned it and went to Fortran (gfortran).

Gfortran works good (with Code::Blocks, http://darmar.vgtu.lt/, multi-file projects work approximately perfectly), and, I think before long I should have something to show.

But, anyway, Toom-Cook is not the fastest way of multiplying big integers, Schonhage-Strassen is faster.

http://en.wikipedia.org/wiki/Schönhage–Strassen_algorithm
(http://en.wikipedia.org/wiki/Sch%C3%B6nhage%E2%80%93Strassen_algorithm)
I am doing Toom-Cook instead of Schonhage-Strassen, because, I understand Toom-Cook, and, I don't understand Schonhage-Strassen.

Schonhage-Strassen uses the Fast Fourier Transform (FFT), hopefully, in the future I will be able to implement it.

Both Toom-Cook and Schonhage-Strassen function recursively, so, to me, they are not so easy to do.

I am just feeling my way along.

I want to get Toom-Cook to work so that, at least I have something to start with.

At some point, I would like to implement all of the math functions for arbitrary size large numbers (both integers and floating point), besides arithmetic, also, trig, exponential, log, etc.

Whether or not being able to calculate the sine of a number to 10,000 decimal places has practical value, people are fascinated by being able to do it.

So, the whole thing is interesting to me, it's not clear that I can do it, so, it's a challenge, like climbing a mountain.

Making it (a little at a time) into a thinBasic module, is an additional challenge.

----------------------------------------------------------------------------------------------------------------------------------------------

The first language I ever used at the university to make big projects was Turbo Pascal.

There, they didn't care what language you used, they only cared about the output you got.

Then, I abandoned Turbo Pascal for Modula-2.

Modula-2 is basically Turbo Pascal with units.

Both Pascal and Modula-2 were created by Niklaus Wirth.

http://en.wikipedia.org/wiki/Niklaus_Wirth
(http://en.wikipedia.org/wiki/Niklaus_Wirth)
----------------------------------------------------------------------------------------------------------------------------------------------

I think that for sure I can do everything I want to, using Free Pascal.

So, if we can get it to cooperate with thinBasic, then, I say, very good.

(Of course, once you get something to work in one language, it's pretty easy to translate it into another.)

Dan

kryton9
22-11-2011, 08:31
I have switched to freepascal from delphi when I use object pascal. It is nice to see you support it Eros.

Have fun Dan.

zak
22-11-2011, 17:47
this is great Eros, i am always wants to make a module.
i will try with lazarus or delphi 7 withiin a few days.
i suggest to make a copy of your file thinBasic_FreePascalExample.zip with a meaningful title to the pascal/delphi section so the occasional visitors searching for delphi/pascal will find it easily. it is valuable.