the first prototype for an evaluatable language
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
// Prototype for evaluatable expression
// ....... structure .........
// <val> <op> <args> ... <op>
// Or, other form is
// <val> <op> <args> <term> ... <new exp>
#define TYPE_STOP 0
#define TYPE_SET 1
#define TYPE_INT 2
#define TYPE_PLUS 3
#define TYPE_SUB 4
#define TYPE_PRINT 5
#define TYPE_INT_SIZE (sizeof(int) + 1)
typedef unsigned char eval_t;
void eval_exp(eval_t* exp)
{
eval_t* current = NULL;
eval_t op = 0;
goto STATE_BASE;
STATE_BASE:
switch(*exp)
{
case TYPE_INT:
current = exp;
exp += TYPE_INT_SIZE;
goto STATE_INT_CUR;
case TYPE_STOP:
return;
default:
goto STATE_UNKNOWN_BYTE;
}
STATE_INT_CUR:
switch(*exp)
{
case TYPE_INT:
eval_exp(exp);
goto STATE_BASE;
case TYPE_PLUS:
exp++;
goto STATE_PLUS_INT_OP;
case TYPE_SUB:
exp++;
goto STATE_SUB_INT_OP;
case TYPE_STOP:
exp++;
// This type eval has finished
goto STATE_BASE;
default:
fprintf(stderr, "Op Error: Unsupported op %u for type <int>\n", *exp);
exit(1);
}
STATE_PLUS_INT_OP:
switch(*exp)
{
case TYPE_INT:
exp++;
*(int*)(current + 1) += *(int*)(exp);
exp += sizeof(int);
goto STATE_INT_CUR;
case TYPE_PLUS:
goto STATE_DUP_OP;
case TYPE_SUB:
goto STATE_BAD_OP_PLUS;
case TYPE_STOP:
goto STATE_INT_CUR;
}
STATE_UNKNOWN_BYTE:
fprintf(stderr, "Unknown byte: %u found in expression, exiting...\n", *exp);
exit(1);
STATE_DUP_OP:
fprintf(stderr, "Found duplicate of op %u, exiting...\n", *exp);
exit(1);
STATE_BAD_OP_PLUS:
fprintf(stderr, "Found illegal op %u, for <plus> op, exiting...\n", *exp);
exit(1);
}
int main(void) {
printf("Hello World\n");
return 0;
}