Squirrel scripting language wrapper module

BlitzMax Forums/BlitzMax Programming/Squirrel scripting language wrapper module

Helios(Posted 2005) [#1]
Well ive been floating around these forums for ages, but never posted any of my work, so i might as well now.

This is a basic wrapper for the squirrel scripting languages, if you dont know what squirrel is you can get infomation on it here - http://squirrel-lang.org/
Ive also posted some examples lower in this post.

You can download the module here - www.savefile.com/files/6495778


It now contains a few examples to show things like precompilations and parameter masking, it also contains a type called TSquirrelEngine to make using squirrel a LOT easier, as it handles all the complex stack routines for you. Theres still a lot of things to implement though such as classes, arrays and such, but there coming along :).

Anyway enjoy, hope thats helpfull.

BlitzMax Code:
Rem

	Squirrel - Example 4 : Precompiling
	This shows how to precompile a script
	
	Created by Tim Leonard (Aka. Helios)
	Version 1.0

End Rem

Rem
	Framework
End Rem
Strict
Framework BRL.System
	Import BRL.retro
	Import DE.Squirrel
	
Rem
	Squirrel Example
End Rem

' Create a new squirrelengine
Global SE:TSquirrelEngine = New TSquirrelEngine

' Load the test file 
SE.LoadFile("Scripts\Example4.nut")

' Now lets dump the compile bytecode to a file
SE.DumpByteCode("Scripts\Example4.cnut")

' Kill Old Squirrel Engine
SE = Null
FlushMem()

' Now to show that this has dumped the file
' succcesfully, we will create another squirrel engine
' and make it run it

' Create new engine
SE:TSquirrelEngine = New TSquirrelEngine

' Register a simple function with
' a parameter mask and count
SE.RegisterGlobalFunction(func_Notify,"Notify",2,"sn")

' Now lets test if it reads out byte code 
' successfully, it will auromatically decide
' if the file is compiled or not and load it 
' appropriatly, you shhould notice a loading dip of about
' 10ms for a simple script when you precompile
SE.LoadFile("Scripts\Example4.cnut")
Se.Run()

Rem
	Our squirrel function, must always 
	have the prototype of FunctionName(State:Byte ptr)
	
	This is called with 2 paremeters the first being a string
	and the second being a integer
End Rem
Function Func_Notify(State:Byte Ptr)

	' Please not that when accessing paremeters 
	' always start at 0 as the TSquirrelEngine access 
	' them as zero-based
	Notify SE.GetStringParameter(0),SE.GetIntegerParameter(1)

End Function


Squirrel Code:
local table = {
	a = "10"
	subtable = {
		array = [1,2,3]
	},
	[10 + 123] = "expression index"
}

local array=[ 1, 2, 3, { a = 10, b = "string" } ];

foreach (i,val in array)
{
	::print("the type of val is"+typeof val);
}

/////////////////////////////////////////////


class Entity
{	
	constructor(etype,entityname)
	{
		name = entityname;
		type = etype;
	}
									
	x = 0;
	y = 0;
	z = 0;
	name = null;
	type = null;
}

function Entity::MoveTo(newx,newy,newz)
{
	x = newx;
	y = newy;
	z = newz;
}

class Player extends Entity {
	constructor(entityname)
	{
		Entity.constructor("Player",entityname)
	}
	function DoDomething()
	{
		::print("something");
	}
	
}


local newplayer = Player("da playar");

newplayer.MoveTo(100,200,300);	



Vectrex(Posted 2005) [#2]
cool, I really like Squirrel


Filax(Posted 2005) [#3]
It's like LUA ?


Helios(Posted 2005) [#4]

It's like LUA ?


Feature wise yes, syntax wise no.


Filax(Posted 2005) [#5]
Ok ! Good job :)


Warpy(Posted 2005) [#6]
aaauug, curly braces! hiss! boo! It looks a lot like C.


Helios(Posted 2005) [#7]
Hum, can someone help me with something? I trying to get function parameters from the stack with sq_getstring but i always seem to get a nullexception error, and i cant work out how to get it to work. Heres another download - http://www.nexuschat.com/uploader/uploads/SquirrelModule_v2.zip
That contains the new TSquirrelEngine type and an examples folder, the example in that shows the error that happens, if anyone could help it would be greatly appreciated :).

EDIT: nvm, found the problem, just need a couple of var's added to the function declarations.


Helios(Posted 2005) [#8]
Released a more updated version.

You can get it here
www.nexuschat.com/uploader/uploads/SquirrelModule_v2.zip

It now contains a few examples to show things like precompilations and parameter masking, it also contains a type called TSquirrelEngine to make using squirrel a LOT easier, as it handles all the complex stack routines for you. Theres still a lot of things to implement though such as classes, arrays and such, but there coming along , and so far most of what is in lua is now implemented, its just squirrel specific that needs doing. Its also fixed a lot of bugs and redesigned a lot of things from Version 1.

~ Enjoy


Garred(Posted 2005) [#9]
Hi, I want use your library in my game, but I have a little problem.
How can I modify a Blitzmax variable or object from a Squirrel program?
I think calling Blitzmax functions from Squirrel is one way, but there are any more ways?
Thx, i like this module a lot


Helios(Posted 2005) [#10]
Do you mean like a Set/GetGlobal command or as in using pointers in your script to directly modify something?


Garred(Posted 2005) [#11]
Using pointers in my script to directly modify something. It's possible?


Helios(Posted 2005) [#12]
Well you can pass pointers(or userdata as its known) into squirrel, but you would need some functions to modify them as as far as i know userdata is read-only.
But if you want get/setglobal functions, you will have to wait a bit, ive so far worked out how to set globals, but i cant for the life of me work out how to get them.


Kanati(Posted 2005) [#13]
looks good... I'll have to take a look at this and squirrel.


deps(Posted 2005) [#14]
Got an error while building the module for 1.14 in OSX 10.3.9:
Compile Error: Unable to convert from 'String' to 'Byte Ptr'
[/Applications/BlitzMax/mod/de.mod/squirrel.mod/squirrel.bmx;380;3]
Build Error: failed to compile /Applications/BlitzMax/mod/de.mod/squirrel.mod/squirrel.bmx


Edit:

This change compiled, but don't know if it leaks memory or works at all. :)
	Rem
		bbdoc: Used to load and compile a string 
	End Rem
	Method LoadString(Buffer:String,SourceName:String)
		Local bptr:Byte Ptr = buffer.ToCString()
		Local sptr:Byte Ptr = sourcename.ToCString()
		'Return sq_compilebuffer(State,Buffer.ToCString(),Len(Buffer),SourceName,1)
		Local Ret:Int = sq_compilebuffer(State,bptr,Len(Buffer),sptr,1)
		MemFree bptr
		MemFree sptr
		Return ret
	End Method


yet another edit:
Not sure if it is related to the above fix (But I guess it is)
All examples compiles just fine and doesn't crash. But nothing happends when I run them. :P

last edit:
...And that's because a path in a sane operating system uses forward slashes. Everything seemes to work just fine now if I change every \ to / in the examples.


TommyBear(Posted 2006) [#15]
wow! Nice. This looks better than LUA I must say.


N(Posted 2006) [#16]
Believe it or not, part of it is based on Lua.


TommyBear(Posted 2006) [#17]
Well not really. It's totally different code. Sure it exposes a stack to the embedder (like other script engines), but otherwise the code is completely different. It may have been *inspired* by LUA, but they don't share code.

One nice thing is that you can easily alter the lexer to create new keywords, and change the syntax around... nice.


TommyBear(Posted 2006) [#18]
BTW Helios... what happened to your module? Can you upload it again?

Cheers!


Helios(Posted 2006) [#19]
Give me a second, just got to re-upload it, i changed the domain of my site ;D.


N(Posted 2006) [#20]
It may have been *inspired* by LUA, but they don't share code.


Nope, its table code is based on Lua's. They state it clearly on the front page of their site.


Garred(Posted 2006) [#21]
Squirrel is based on Lua, but internally only. Syntax is very different (better in my opinion)


TommyBear(Posted 2006) [#22]
My bad then... I didn't look at the table code.


N(Posted 2006) [#23]
*Smack*


ozak(Posted 2006) [#24]
So. Where could I download the module again?


Helios(Posted 2006) [#25]
Sorry I had to reformat my pc a couple of days ago so minGW isn't installed so I was unable to compile it, you'll have to do it yourself, so I cant be sure theres no bugs in it. My website is also having probs so ive had to upload it to a free file hoster.

Download it here - http://www.savefile.com/files/6429950


Diablo(Posted 2006) [#26]
i get complie error cause you used superstrict and didn't give some consts a var type (%). just tho i say this if anyone else was getting this problem. gj btw :D

edit: also i couldnt find a list called EngineList.

edit2: i think it needs to be called list instead

edit3: I think you need to change the error bit as well cause its a function:
If T.State = State
				TSquirrelEngine.ErrorDescription = Description
				TSquirrelEngine.ErrorSourcename  = SourceName
				TSquirrelEngine.ErrorLine		 = Line
				TSquirrelEngine.ErrorColumn		 = Column
			EndIf


edit4: You need to change the def of the above vars aswell to be global instead.

NOTE I dont know if this is how you should fix it but after the doing what i said above the module complied (i dont know if other things (internally) will get effected)

edit5: you need to change the examples as well to binphx instead of de.

thats not too much :D


Helios(Posted 2006) [#27]
Whoops, sorry, told you it probably had bugs in it. Heres a *hopefully* fixed one.

http://www.savefile.com/files/6495778


Diablo(Posted 2006) [#28]
for some stange reson example 3 doesnt do anything.

edit1: example four does somthing but i dont think GetGlobalString() is working correctly because it doesntprint anything out.


Helios(Posted 2006) [#29]
>_<, sorry another problem, that was caused by me testing the global variable code, i forgot to remove it, but it should work fine if you remove the GetGlobal asn SetGlobal lines from example 3 & 4. I apologise for all this when i get mingwa installed and sorted out ill fix it up and release it properly.


Diablo(Posted 2006) [#30]
kool, at lest your showing your still working on it :D


TommyBear(Posted 2006) [#31]
Excellent... thank you Helios.... Hey Mark... I think this might be one for the public modules.


Diablo(Posted 2006) [#32]
so how goes this - the download is no longer on save file :(

File ID is not valid



Helios(Posted 2006) [#33]

so how goes this


Not working on it at the moment, been creating my own scripting language :) (Which is going awesome, almost fully functional at the moment, and pretty fast.)


- the download is no longer on save file :(


Yeh im sorry, SaveFile.com auto deletes the files if there not downloaded for n amount of time.

Ill upload it again if you want it back up.


Diablo(Posted 2006) [#34]
Ill upload it again if you want it back up.

not for me, i have a copy.

Not working on it at the moment, been creating my own scripting language :) (Which is going awesome, almost fully functional at the moment, and pretty fast.)
cool!!! look forwed to seeing it.


TommyBear(Posted 2006) [#35]
Hey does anyone still have a copy of this module?? Can someone post a link to it?


N(Posted 2006) [#36]
Well, I don't have this module, but here's the source to my Squirrel module (haven't released it since I figured nobody would care with the existence of Lua).




Cajun17(Posted 2006) [#37]
I was about to write my own module, but now I don't have to. Thanks fellas.


TommyBear(Posted 2006) [#38]
Oh nice Noel... thanks for that. Does the string stuff function correctly?


TommyBear(Posted 2006) [#39]
Has anyone tested squirrel on Mac? PPC?


N(Posted 2006) [#40]
I haven't tested it thoroughly yet as I decided not to use it for my project, so I'm afraid I can't say whether or not it works on a Mac or if the string implementation works.


Klin(Posted 2009) [#41]
Hello.
First: Sorry for my bad english.
Well, I want to get squirrel wrapper. But the download links are dead.
My question: Has everyone the Squirrel Wrapper on the PC and can upload it, plz? :)

THX
Klin

P.S. Sorry for this old thread. But i don't want open a new thread.


N(Posted 2009) [#42]
My question: Has everyone the Squirrel Wrapper on the PC and can upload it, plz? :)
Look up...