All the classes needed to load, parse and run OwlSL script files can be found within the namespace "owlsl".
The class method cwl.open(path) loads and parses the given file.
The class method cwl.compile() compiles it into bytecode.
And then to run a method from a script file you'd invoke cwl.call(classname, method_name) or cwl.static_call(classname, method_name).
So, a c++ application that loads an OwlSL script file and runs a class method statically would look like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// main.cpp | |
#include <owlsl/cwl.h> | |
int main() | |
{ | |
owlsl::cwl cwl; | |
if (cwl.open("program1.c")) | |
{ | |
if (cwl.compile()) | |
{ | |
cwl.script.static_call("Program", "Main()"); | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// program1.c | |
class Program | |
{ | |
void Main() | |
{ | |
} | |
} |
OwlSL currently comes with a few types of it's own like: number (double), text (wide char string), bool, list<T> (std::deque<T>) and ref<T> (kind of a shared pointer) but you can define your own and make them work in the way you want.
The folowing script shows their usage:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// program1.c | |
class test | |
{ | |
number num; | |
void set_number(number val) | |
{ | |
num = val; | |
} | |
number get_number() | |
{ | |
return num; | |
} | |
} | |
class Program | |
{ | |
void Main() | |
{ | |
bool condition = 1>0; | |
list<test> lst; | |
if (condition) | |
{ | |
for (number n=0; n<10; ++n) | |
{ | |
lst.add(test()); | |
ref<test> r = lst.at(n); | |
r->set_number(n); | |
lst.print(r->get_number().text()); | |
} | |
} | |
} | |
} |
You may note that the type list<T> has a method print which doesn't really makes sense, it's a hardcoded method that went there for testing purporses and will be removed shortly. So for the sake of being correct, lets expose our own C++ class for handling console output. So in main.cpp we will add:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// main.cpp | |
#include <owlsl/cwl.h> | |
class console | |
{ | |
public: | |
void print_line (const owlsl::text& text) | |
{ | |
std::wcout << text.wstring() <<std::endl; | |
} | |
static void inscribe (owlsl::sco* sco) | |
{ | |
m_type = sco->vm.new_type("console"); | |
sco->constructor(m_type, "console()", &owlsl::type::base::alloc<console>); | |
sco->destructor(m_type, &owlsl::type::base::dalloc<console>); | |
sco->method(m_type, "void print_line(text)", &console::script_print_line); | |
} | |
static void script_print_line (owlsl::vm* vm) | |
{ | |
owlsl::text t = *vm->stack_cast<owlsl::text>(0); // parameter | |
console* c = vm->stack_cast<console>(-1); // this | |
c->print_line(t); | |
vm->pop(2); | |
} | |
protected: | |
static int m_type; | |
}; | |
int console::m_type = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// main.cpp (continued) | |
int main() | |
{ | |
owlsl::cwl cwl; | |
console::inscribe(&cwl.sco); | |
if (cwl.open("program1.c")) | |
{ | |
if (cwl.compile()) | |
{ | |
cwl.script.static_call("Program", "Main()"); | |
} | |
else | |
{ | |
std::wcout << L"[" << cwl.last_error().line << L", " << cwl.last_error().column << L"] " | |
<< cwl.last_error().description.wstring() <<std::endl; | |
} | |
} | |
else | |
{ | |
std::wcout << L"[" << cwl.last_error().line << L", " << cwl.last_error().column << L"] " | |
<< cwl.last_error().description.wstring() <<std::endl; | |
} | |
std::cin.get(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// program1.c | |
class test | |
{ | |
number num; | |
void set_number(number val) | |
{ | |
num = val; | |
} | |
number get_number() | |
{ | |
return num; | |
} | |
} | |
class Program | |
{ | |
void Main() | |
{ | |
console con; | |
list<test> lst; | |
if (1>0) | |
{ | |
for (number n=0; n<10; ++n) | |
{ | |
lst.add(test()); | |
ref<test> r = lst.at(n); | |
r->set_number(n); | |
con.print_line(r->get_number().text()); | |
} | |
} | |
} | |
} |