#include <string>
#include <iostream>
//include the ffcall lib
#include </usr/include/avcall.h>
#define FUNCTION_EXTERNAL 0
//... other machine functions
#define TABLE_1 1
#define TABLE_2 2
//... other tables
#define FUNCTION_1 1
#define FUNCTION_2 2
//... other C functions
#define STORE 1
#define NOSTORE 0
//Types:
#define TYPE_INT 0
//... other primitive types
//type codes: 0 int, 1 bool
int integer_storage;
int add(int a, int b)
{
std::cout << a + b << std::endl;
}
template<typename Arg, typename... Args>
void table1_findLookupFunction(const Arg& functionCode, int store, Args... args)
{
switch(functionCode)
{
case 1:
std::cout << "Table 1. Function code: " << functionCode << std::endl;
std::cout << "Function: Add" << std::endl;
if(store)
{
integer_storage = add(args...);
}
else
{
//call function
add(args...);
}
break;
default:
std::cout << "Couldn't find function :(" << std::endl;
}
}
template <typename ... Arg>
void table2_findLookupFunction(int functionCode, Arg... args)
{
std::cout << "Table 2. Function code: " << functionCode << std::endl;
}
template <typename ... Arg>
void findLookupTable(int tableCode, int functionCode, int store, Arg... args)
{
switch(tableCode)
{
case 1:
std::cout << "Table: " << tableCode << std::endl;
table1_findLookupFunction(functionCode, store, args...);
break;
case 2:
std::cout << "Table: " << tableCode << std::endl;
table2_findLookupFunction(functionCode, store, args...);
break;
default:
std::cout << "Couldn't find lookup table :(" << std::endl;
}
}
int main()
{
//interpreter goes here
int command[] = {FUNCTION_EXTERNAL, TABLE_1, FUNCTION_1, STORE, 2, TYPE_INT, 2, TYPE_INT, 2};
//loop over the commands, then...
if(command[0] = FUNCTION_EXTERNAL)
{
av_alist L;
av_start_void(L,&findLookupTable);
av_int(L,command[1]); //push table ID
av_int(L,command[2]); //push function ID
av_int(L,command[3]); //push store pseudo-boolean
for(int argIndex = 5;argIndex <= command[6];argIndex++)
{
std::cout << "Command: " << command[argIndex] << std::endl;
}
//av_int(L,10);
//av_double(L,3.14);
av_call(L);
}
//continue the loop
return 0;
}
//compile with g++ sample.cpp -I/usr/include -lavcall