jweinst1
11/19/2017 - 4:50 AM

uses evaluate expressions as a language in C

uses evaluate expressions as a language in C

#include <stdio.h>
#include <string.h>
//system of functional nodes

#define Wind_BASE struct WindNode* next; \
      enum WindType type;


enum WindType
{
	WindType_INT,
	WindType_ADD
};


//base type struct for inheritance
struct WindNode
{
	Wind_BASE
};


struct WindAdd
{
	Wind_BASE
	struct WindNode* first;
	struct WindNode* second;
};


struct WindInt
{
	Wind_BASE
	int val;
};

//method to add and return result of add node
int addTest(struct WindNode* node)
{
	if(node->type == WindType_ADD)
	{
		struct WindNode* a = ((struct WindAdd*)node)->first;
		struct WindNode* b = ((struct WindAdd*)node)->second;
		return ((struct WindInt*)a)->val + ((struct WindInt*)b)->val;
	}
	else return 0;
}


int main(int argc, char const *argv[])
{
	//init phase
	struct WindAdd add;
	struct WindAdd* addp = &add;
	add.type = WindType_ADD;

	struct WindInt a;
	struct WindInt* ap = &a;
	a.val = 3;
	struct WindInt b;
	struct WindInt* bp = &b;
	b.val = 3;

	add.first = (struct WindNode*)ap;
	add.second = (struct WindNode*)bp;

	//treats an ASt-like addition node as computation
	printf("The result is %d\n", addTest((struct WindNode*)addp));
	return 0;
}