// TeaScript includes
#include "teascript/version.h"
#include "teascript/Engine.hpp"
//(std includes missing...)
// This test code will add some variables (mutable and const) to the script context
// and then execute script code, which will use them.
void test_code1()
{
// create the TeaScript default engine.
teascript::Engine engine;
// add 2 integer variables a and b.
engine.AddVar( "a", 2 ); // variable a is mutable with value 2
engine.AddVar( "b", 3 ); // variable b is mutable with value 3
engine.AddConst( "hello", "Hello, World!" ); // variable hello is a const string.
// For Bool exists different named methods because many types could be implicit converted to bool by accident.
// like char * for example.
engine.AddBoolVar( "speak", true ); // variable speak is a Bool with value true.
// execute the script code passed as string. it computes new variable c based on values a and b.
// also it will print the content of the hello variable.
// finally it returns the value of variable c.
auto const res = engine.ExecuteCode( "const c := a + b\n"
"if( speak ) {\n"
" println( hello )\n"
"}\n"
"c\n"
);
// print the result.
std::cout << "c is " << res.GetAsInteger() << std::endl;
}
// this is our simple callback function which we will call from TeaScript code.
// The callback function signature is always ValueObject (*)( Context & )
teascript::ValueObject user_callback( teascript::Context &rContext )
{
// look up variable 'some_var'
auto const val = rContext.FindValueObject( "some_var" );
// print a message and the value of 'some_var'
std::cout << "Hello from user_callback! some_var = " << val.PrintValue() << std::endl;
// return 'nothing' aka NaV (Not A Value).
return {};
}
// This test code will register a C++ callback function and then executes a script
// which will call it.
void test_code2()
{
// create the TeaScript default engine.
teascript::Engine engine;
// register a function as a callback. can use arbitrary names.
engine.RegisterUserCallback( "call_me", user_callback );
// execute the script which will create variable 'some_var' and then call our callback function.
engine.ExecuteCode( "const some_var := \"Hello!\"\n"
"if( is_defined call_me and call_me is Function ) { // safety checks! \n"
" call_me( )\n"
"}\n"
);
}
// This is another callback function.
// It will create the sum of 2 passed parameters and return the result to the script.
teascript::ValueObject calc_sum( teascript::Context &rContext )
{
if( rContext.CurrentParamCount() != 2 ) { // this check could be relaxed also...
// in case of wrong parameter count, throw eval error with current source code location.
throw teascript::exception::eval_error( rContext.GetCurrentSourceLocation(), "Calling calc_sum: Wrong amount of parameters! Expecting 2.");
}
// get the 2 operands for the calculation.
auto const lhs = rContext.ConsumeParam();
auto const rhs = rContext.ConsumeParam();
// calculate the sum and return result as ValueObject.
return teascript::ValueObject( lhs.GetAsInteger() + rhs.GetAsInteger() );
}
// This test code will register a callback function and call it from a script with parameters.
void test_code3()
{
// create the TeaScript default engine.
teascript::Engine engine;
// register a function as a callback. can use arbitrary names.
engine.RegisterUserCallback( "sum", calc_sum );
// execute the script which will call our function
auto const res = engine.ExecuteCode( "sum( 1234, 4321 )\n" );
// print the result.
std::cout << "res is " << res.GetAsInteger() << std::endl;
}