🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

[ANN] Squirrel 1.0 release candidate 1(RC1) is out

Started by
16 comments, last by JD 19 years, 11 months ago
Well I got a problem again this time with not understanding the logic of calling a squirrel print() function from C. It turns out that I have to push the argument to print() while that should be handled by the vm. Why do I need to know what some user put into print() as an argument? He could have written print("hello world") and I have to push that from C side. Doesn't sound right does it? I don't understand the following section:

sq_pushroottable(v);	sq_pushstring(v, "print", -1);	sq_get(v, -2); //get the function from the root table	sq_pushroottable(v); //’this’ (function environment object)	sq_pushstring(v, "hello world", -1);	if(SQ_FAILED(sq_call(v, 2, 0)))		assert(0);	sq_pop(v,2); //pops the roottable and the function	


the sq_pushstring(v, "hello world", -1); this should be done by vm not? In the script I have print("hello") and the output is "hello world" the string I pushed onto the stack. Argg :)
Advertisement
I guess this is what you want to do. m'I right?

#include "stdafx.h"#include <squirrel.h>#include <sqstdio.h>#include <cassert>#include <cstdio>SQInteger file_lexfeedASCII(SQUserPointer file){	char c;		if(fread(&c, sizeof(c), 1, (FILE*)file) > 0)		return c;	return 0;}int compile_file(HSQUIRRELVM v, const char *filename){	FILE *file = fopen(filename, "rb");		if(file)	{		sq_compile(v, file_lexfeedASCII, file, filename, 1);		fclose(file);		return 1;	}	return 0;}int main(int argc, char* argv[]){	HSQUIRRELVM v;	v = sq_open(1024); // creates a VM with initial stack size 1024		// do some stuff with squirrel here	if(!compile_file(v, "script.txt"))		assert(0);		sq_pushroottable(v);	if(SQ_FAILED(sq_call(v, 2, 0))) //executes the script you just compiled	   assert(0);	sq_pop(v,2); //pops the roottable and the function			sq_close(v);		return 0;}


Alberto
-----------------------------The programming language Squirrelhttp://www.squirrel-lang.org
That's how I thought things should work but I get an error when I run it. I basically want to call function print("hello world") from the script and have it output "hello world" to standard output ie. DOS console. I thought I would have to invoke that function from C code but it seems that the script should do that by itself. If I write any function in script and I want to call it from C I understand that I need to push the function name and also its arguments on the stact and then invoke the sq_call(). Then that function is called in script and the script then pushes the results onto the stack that I then can get at from C. Likewise if I call a native function from within my script, my C function will have to pull the arguments from the stack, do the calculations and then push the results onto the stack so that the script can deal with it. Is this how it suppose to work?

The problem now I'm having is figuring out how to execute the script without writing any C stack code which I think how it suppose this to work. Say I write in script:

local a = 1;
local b = 2;
local c = a + b;
print(c);

and I want the script to execute what C code do I have to write for that to happen? I think sq_call() is reserved for the case when I want to invoke a user defined script function? Again, I'm confused, but thanks for hanging in with me :)
Quote:
- squirrel renamed to squirrelScript or similar for googling


Actually that would not help, as Google (and many other search engines) does not support wildcards, eg. '*script' or 'squirrel*'. So using that kind of joined word-name, it would be propably harder to find the site.

Now, if someone searched for a scripting language, Squirrel would not be the first, actually it seems that PHP and Python are the first ones. So popularity rules. When searching for squirrels, the first results are about squirrels. (Well, mostly, there seems to be even a porn site in the top 10 results!)

Squirrel script or squirrel scripting will lead the lost programmer to light.

PS. Sorry, i just had to make this reply, the proposal to change the name simply forced me to do this.
Maybe when more folks write about it then we get better google search results. Right now you get other scripting langs or things about squirrels and nuts. But I still think that squirrelScript is a better name than squirrel. Perhaps SQScript or something like that too.
Docs could be made easier to read if the info about a topic say arrays wasn't spread out in different places. It makes sense to have one topic on arrays and put everything into that including builtin functions. Those functions could still be put into api reference section. I would also like to see this example put into the docs:

#include "stdafx.h"#include <squirrel.h>#include <sqstdio.h>#include <cassert>#include <cstdio>#include <stdarg.h>void printfunc(HSQUIRRELVM v, const SQChar *s, ...){	va_list arglist;	va_start(arglist, s);	vprintf(s, arglist);		va_end(arglist);}int main(int argc, char* argv[]){	HSQUIRRELVM v;	v = sq_open(1024); // creates a VM with initial stack size 1024		sqstd_register_iolib(v);	sq_setprintfunc(v, printfunc);				sq_pushroottable(v);	if(SQ_FAILED(sqstd_dofile(v, "script.txt", 0, 0)))		assert(0);	sq_close(v);		return 0;}write in script.txt file:print("hello");


Ok, I'm going to play with some squirrel code and hopefully I'll decipher the stack issue and what to call and when maybe even looking into lua programming book.
I'm taking notes :). Thx for the feedback, few actually give me comments on the doc, I'm on it.

ciao
Alberto
-----------------------------The programming language Squirrelhttp://www.squirrel-lang.org
Sweet :) I'm going to transfer to forums on your website (not those on sourceforge since there is little input there) and stop writing here. I hope as I learn squirrel I will be able to provide more feedback to you. Other than that issue with setting up the environment so that it compiles my script I have not had much problems with actual learning of the squirrel lang. so far. Keep up the excellent work and thanks for all that hard work :)

This topic is closed to new replies.

Advertisement