jweinst1
4/9/2018 - 12:02 AM

small chemical program reactor in c

small chemical program reactor in c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
Proto type for a Chemical reactor and chemical compounds.
For dev of chemical programming language
*/

//base portion of struct for polymorphism
#define ATOM Element elem;

//type of element
typedef enum
{
  Element_int,
  Element_add,
  Element_iadd
} Element;

// Base compound struct
typedef struct
{
  ATOM
} Compound;

// int compound
typedef struct
{
  ATOM
  int i;
} Compound_int;

//plus 
typedef struct
{
  ATOM
} Compound_add;


// i add struct
typedef struct
{
  ATOM
  int i;
} Compound_iadd;

/*CONSTRUCTOR METHODS*/

Compound_int* c_int_new(int num)
{
  Compound_int* ci = malloc(sizeof(Compound_int));
  ci->elem = Element_int;
  ci->i = num;
  return ci;
}

Compound_add* c_add_new(void)
{
  Compound_add* ci = malloc(sizeof(Compound_add));
  ci->elem = Element_add;
  return ci;
}

Compound_iadd* c_iadd_new(int num)
{
  Compound_iadd* ci = malloc(sizeof(Compound_int));
  ci->elem = Element_iadd;
  ci->i = num;
  return ci;
}

Compound* react(Compound* c1, Compound* c2)
{
  Compound* newcomp;
  switch(c1->elem)
  {
    case Element_int:
      if(c2->elem == Element_add)
      {
         newcomp = (Compound*)c_iadd_new(((Compound_int*)c1)->i);
         free(c1);
         free(c2);
      }
      break;
    case Element_iadd:
      if(c2->elem == Element_int)
      {
        newcomp = (Compound*)c_int_new(((Compound_iadd*)c1)->i + ((Compound_int*)c2)->i);
        free(c1);
        free(c2);
      }
      break;
  }
  return newcomp;
}



int main(void) {
  printf("Hello World\n");
  return 0;
}