c-scripting/examples/example1.c

76 lines
1.8 KiB
C

/*
** C Scripting Example #1
*/
#define SY__MAIN 1
#include "symbol_set.h"
#include "example1.h"
#include <stdio.h>
COMMAND_SCRIPT(foo, "Generates foo and increments the foo counter"){
printf("foo, ");
ctx->foo += 1;
}
COMMAND_SCRIPT(bar, "Generates bar and decrements the foo counter"){
printf("bar, ");
ctx->foo -= 1;
}
COMMAND_SCRIPT(baz,
"Generates 2 bars if the foo counter is positive, "
"generates 2 foos if the foo counter is negative, "
"otherwise generates baz"){
if (ctx->foo > 0){
printf("bar, bar, ");
}
else if (ctx->foo < 0){
printf("foo, foo, ");
}
else{
printf("baz, ");
}
}
int main(void){
// init sequence
U32 command_ids[] = {
SyID(EX1_COMMAND, foo), SyID(EX1_COMMAND, foo), SyID(EX1_COMMAND, baz),
SyID(EX1_COMMAND, bar), SyID(EX1_COMMAND, baz), SyID(EX1_COMMAND, bar),
SyID(EX1_COMMAND, foo), SyID(EX1_COMMAND, bar), SyID(EX1_COMMAND, baz),
};
// display the legend of rules
printf("RULES:\n");
for (SyEach(EX1_COMMAND, command)){
printf("%.*s -> %.*s\n",
(int)(command->name.size), command->name.str,
(int)(command->description.size), command->description.str);
}
printf("\n");
// display the sequence
printf("SEQUENCE:\n");
for (U32 i = 0; i < sizeof(command_ids)/sizeof(command_ids[0]); i += 1){
U32 id = command_ids[i];
EX1_Command *command = SyAddressFromID(EX1_COMMAND, id);
printf("%.*s, ",
(int)(command->name.size), command->name.str);
}
printf("\n\n");
// run the sequence
printf("OUTPUT:\n");
{
EX1_Ctx ctx = {0};
for (U32 i = 0; i < sizeof(command_ids)/sizeof(command_ids[0]); i += 1){
U32 id = command_ids[i];
EX1_Command *command = SyAddressFromID(EX1_COMMAND, id);
command->hook(&ctx);
}
}
printf("\n\n");
return(0);
}