Lizard on FreeBasic

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
srvaldez
Posts: 3060
Joined: Sep 25, 2005 21:54

Lizard on FreeBasic

Post by srvaldez »

Lizard is an interesting project written in PureBasic https://www.purebasic.fr/english/viewtopic.php?t=76279
to follow this example download and extract the Lizard archive and copy the Lizard dll from the Library folder to the Lizard folder and save the following example in the Lizard folder

Code: Select all

#inclib "Lizard"

Enum Event
	Event_Print = 1
	Event_Message
	Event_Error
End Enum



extern "Windows"
	declare function Kernel_BindCallback(byval as integer, byval CallbackAddress as any ptr) as integer
'	Kernel_DuplicateExpression
	declare function Kernel_EvaluateExpression(byval p as any ptr) as any ptr
'	Kernel_FreeExpression
'	Kernel_GetValue
	declare function Kernel_Initialize(byval p as wstring ptr) as integer
	declare function Kernel_InputExpression(byval p as wstring ptr) as any ptr
	declare function Kernel_OutputExpression(byval p as any ptr) as wstring ptr
'	Kernel_Reset
'	Kernel_SetValue
	declare function Kernel_Terminate() as integer
'	declare function Kernel_Version() as integer

sub Callback_Print(byval Text as wstring ptr)
	Print *Text
End sub

sub Callback_Message(byval Text as wstring ptr, byval SymbolName as wstring ptr, byval ErrorName as wstring ptr)
	Print "  ";*SymbolName;".";*ErrorName;"  ";
	Print *Text
End sub

sub Callback_Error(byval Text as wstring ptr)
	Print "  Syntax error: \";*Text
End sub

end extern

dim as double tm
dim shared as wstring ptr input_
dim shared as wstring ptr w
dim shared as any ptr Expression
dim as string s
'=========================================================
input_=callocate(1024)

sub readfile(byref fl as string)
	dim as string s
	dim as long fn = FreeFile
	open fl for input as fn
	while not eof(fn)
		line input #fn, s
		if trim(s)="" then continue while
		*input_=s
		Expression=Kernel_InputExpression(input_)
		if Expression then
			Expression=Kernel_EvaluateExpression(Expression)
			if Expression then
				w=Kernel_OutputExpression(Expression)
				print *w
			end if
		end if
	wend
	close fn
end sub

if Kernel_Initialize(0) then
	Kernel_BindCallback(Event_Error, @Callback_Error)
	Kernel_BindCallback(Event_Message, @Callback_Message)
	Kernel_BindCallback(Event_Print, @Callback_Print)
else
	print "unable to initialize"
	sleep
	end
end if

*input_=" "
while *input_<>""
	line input "-> ",s
	if lcase(left(trim(s),8))="readfile" then
		s=trim(mid(trim(s),9))
		readfile(s)
	else
		*input_=s
		if *input_="" then exit while
		tm=timer
		Expression=Kernel_InputExpression(input_)
		if Expression then
			Expression=Kernel_EvaluateExpression(Expression)
			if Expression then
				w=Kernel_OutputExpression(Expression)
				print *w
			end if
		end if
		tm=timer-tm
		print tm
	end if
wend

Kernel_Terminate()
deallocate(input_)
example run, where "->" is the input prompt followed by the input, and next lines after is the output
an empty input ends the program
-> readfile examples\Example7_Script.txt
Null
0
Null
Null
Null
Null
Null
Null
Null
Null
-> Derive(Log(x), x)
1/x
elapsed time 0.0005979000125080347 seconds
-> 1-1/3+1/5+1/7
106/105
elapsed time 0.0004744000034406781 seconds
-> Calculate(Pi, 50)
3.1415926535897932384626433832795028841971693993751
elapsed time 0.0007534000324085355 seconds
Post Reply