c_cpp Reactionwanguage由jweinst12创建 - https://repl.it/@jweinst12/ReactionLanguage

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp Reactionwanguage由jweinst12创建 - https://repl.it/@jweinst12/ReactionLanguage相关的知识,希望对你有一定的参考价值。

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

/** Reaction Language Prototype
 * Small Prototype of a reaction based Language
 * Based off binary objects, and reacting one in a direction
 * Lists of Elements are reacted towards another.
 * [+ 1] -> [2, 2] would produce [3, 3]
 */
 
#define RxList_SIZE 500
 
// struct that holds elements for reactions.
typedef struct
{
  unsigned char elems[RxList_SIZE];
} RxList;

// Typed enum that allows individual binary elements in reactions
// to be recognized by their type.
typedef enum
{
  RxType_stop,
  RxType_bool,
  RxType_ind,
  RxType_print
} RxType;

// gets the element at some index in the list of elements
unsigned char* Rx_ind(unsigned char** elems, int index)
{
  unsigned char* got = *elems;
  while(index--)
  {
    switch(*got)
    {
      case RxType_bool:
          got += 2;
          break;
      default:
          got++;
    }
  }
  return got;
}

// prints some binary element to stdout
void Rx_print_elem(unsigned char* elem)
{
  switch(*elem)
  {
    case RxType_bool:
        elem++;
        *elem ? puts("true") : puts("false");
        return;
    default:
       fprintf(stderr, "Unknown element %u, exiting.\n", *elem);
       exit(2);
  }
}

// Function that handles print reactions
void Rx_print(unsigned char** target, unsigned char** react)
{
  unsigned char* prItem;
  switch(**react)
  {
    case RxType_ind:
        *react += 1;
        prItem = Rx_ind(target, *(int*)(*react));
        *react += sizeof(int); // advances past index num
        Rx_print_elem(prItem);
        
        return;
    default:
       fprintf(stderr, "Not printable arg, exiting.\n");
       exit(2);
  }
}

void Rx_react(RxList* target, RxList* reaction, RxList* result)
{
  unsigned char* resultWrite = result->elems;
  unsigned char* reactRead = reaction->elems;
  unsigned char* targetRead = target->elems;
  while(*reactRead != RxType_stop)
  {
    switch(*reactRead)
    {
      case RxType_stop:
          return;
      case RxType_print:
          reactRead++;
          Rx_print(&targetRead, &reactRead);
          break;
      default:
         fprintf(stderr, "Unknown element %u, exiting.\n", *reactRead);
         exit(2);
    }
  }
}

int main(void) {
  printf("---Prototype-Test----\n");
  
  RxList l1;
  RxList l2;
  RxList l3;
  
  l1.elems[0] = RxType_bool;
  l1.elems[1] = 1;
  
  l2.elems[0] = RxType_print;
  l2.elems[1] = RxType_ind;
  l2.elems[2] = 0;
  l2.elems[3] = 0;
  l2.elems[4] = 0;
  l2.elems[5] = 0;
  l2.elems[6] = RxType_stop;
  
  Rx_react(&l1, &l2, &l3);
  printf("---------Done--------\n");
  return 0;
}

以上是关于c_cpp Reactionwanguage由jweinst12创建 - https://repl.it/@jweinst12/ReactionLanguage的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 由n个元素旋转数组

c_cpp 添加由链接列表表示的两个数字

c_cpp MIDI控制器由jardous

大数的阶乘

c_cpp 由fiddle.jyt.io创建的要点

c_cpp Ç由相对路径获得绝对路径的.c