//main.cpp
#include <cstdio>
#include <iostream>
using namespace std;
#include <squirrel.h>
SQInteger getCharForSquirrel(SQUserPointer fp)
{
int c = getc(static_cast<FILE*>(fp));
if (c == EOF) {
c = 0;
}
return static_cast<SQInteger>(c);
}
int main()
{
//初期化
HSQUIRRELVM v = ::sq_open(1024);
//コンパイル
char* filename = _SC("test.nut");
FILE* fp;
fopen_s(&fp, filename, "r");
//FILE* fp = fopen(filename, "r"); //こう書いても良い (警告が出るだけ
if (fp == NULL) {
cout << "error 1" << endl;
getchar();
return 1;
}
if (SQ_FAILED(::sq_compile(v, getCharForSquirrel, fp, filename, SQFalse))) {
fclose(fp);
cout << "error 2" << endl;
getchar();
return 2;
}
fclose(fp);
//C++ から Squirrel の関数を呼ぶ
int x = 10, y = 20, z = 0;
int top = ::sq_gettop(v);
::sq_pushroottable(v);
::sq_pushstring(v, _SC("foo"), -1);
if (SQ_FAILED(::sq_get(v, -2))) {
cout << "error 3" << endl;
getchar();
return 3;
}
::sq_pushroottable(v);
::sq_pushinteger(v, x);
::sq_pushinteger(v, y);
if (SQ_FAILED(::sq_call(v, 3, SQTrue, SQFalse))) {
cout << "error 4" << endl;
getchar();
return 4;
}
::sq_getinteger(v, -1, &z);
::sq_settop(v, top);
cout << "x = " << x << endl;
cout << "y = " << y << endl;
cout << "x + y = " << z << endl;
getchar();
//終了処理
::sq_pop(v, 1);
::sq_close(v);
return 0;
}
//test.nut
function foo(x, y) {
return (x + y);
}
#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <cstdarg>
using namespace std;
#undef _UNICODE
#include <squirrel.h>
#include <sqstdio.h>
namespace forSquirrel {
void print(HSQUIRRELVM v, const SQChar* s, ...)
{
va_list arglist;
va_start(arglist, s);
vprintf(s, arglist);
va_end(arglist);
}
SQInteger getChar(SQUserPointer fp)
{
int c = getc(static_cast<FILE*>(fp));
if (c == EOF) {
c = 0;
}
return static_cast<SQInteger>(c);
}
} //end of namespace forSquirrel
void call_func1(HSQUIRRELVM v, int n, float f, const SQChar* s)
{
int top = ::sq_gettop(v);
::sq_pushroottable(v);
::sq_pushstring(v, _SC("func1"), -1);
if (SQ_SUCCEEDED(::sq_get(v, -2))) {
::sq_pushroottable(v);
::sq_pushinteger(v, n);
::sq_pushfloat(v, f);
::sq_pushstring(v, s, -1);
::sq_call(v, 4, SQFalse, true);
}
::sq_settop(v, top);
}
int call_func2(HSQUIRRELVM v, int x, int y)
{
int top = ::sq_gettop(v);
::sq_pushroottable(v);
::sq_pushstring(v, _SC("func2"), -1);
int ret = 0;
if (SQ_SUCCEEDED(::sq_get(v, -2))) {
::sq_pushroottable(v);
::sq_pushinteger(v, x);
::sq_pushinteger(v, y);
::sq_call(v, 3, SQTrue, true);
::sq_getinteger(v, -1, &ret);
}
::sq_settop(v, top);
return ret;
}
int main()
{
//初期化
HSQUIRRELVM v = ::sq_open(1024);
::sq_seterrorhandler(v);
::sq_setprintfunc(v, forSquirrel::print);
::sq_pushroottable(v);
//コンパイル相当の作業
const char* filename = "../test.nut";//"C:\\home\\vc\\project\\sq_test1\\test.nut";
/*
//pattern 1
if (SQ_FAILED(::sqstd_dofile(v, filename, 0, true))) {
cout << "error" << endl;
getchar();
return 1;
}
*/
//pattern 2
/*
FILE* fp;
fopen_s(&fp, filename, "r");
if (fp == NULL) {
cout << "error 1" << endl;
getchar();
return 1;
}
if (SQ_FAILED(::sq_compile(v, forSquirrel::getChar, fp, filename, SQFalse))) {
fclose(fp);
cout << "error 2" << endl;
getchar();
return 2;
}
fclose(fp);
*/
//実行
call_func1(v, 1, 2.5, _SC("teststring"));
cout << call_func2(v, 10, 20) << endl;
//終了処理
::sq_pop(v, 1);
::sq_close(v);
//プログラムが一時停止するように書いておく
cout << "-- end --" << endl;
getchar();
return 0;
}
print("Welcome to Squirrel World!\n");
function func1(i, f, s) {
print("Call foo(), i = " + i + ", f = " + f + ", s = '" + s + "'\n");
}
function func2(x, y) {
return (x + y);
}
function func(x) {
return x * 2;
}
this.func <- function(x) {
return x * 2;
}
_SC(filename)
#ifdef _UNICODE
#define _SC(a) L##a
#else
#define _SC(a) a
#endif
紀行 (2009-03-13T13:53:11)
うきうきは結局入力が面倒で続かなかった
か (2009-03-15T03:57:12)
でも表をいちいち作らなくてもいいので,こっちをしばらく使ってみます (^^;