c_cpp 尝试goto状态转换表达式引擎
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 尝试goto状态转换表达式引擎相关的知识,希望对你有一定的参考价值。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
/*
* Prototype for cell based vm
* uses only arrays and blocks of 32-bit signed integer.
*/
// macro to get length of cells machine
#define CellMachine_LEN(cm) (cm->head - cm->cells)
enum CellCode {
CellCode_STOP,
CellCode_END,
CellCode_INT,
CellCode_SUM
};
struct CellMachine {
int size;
int* base;
int* head;
const int* end;
int* cells;
};
struct CellMachine* CellMachine_new(int size)
{
struct CellMachine* mach = malloc(sizeof(struct CellMachine));
mach->size = size;
mach->cells = malloc(sizeof(int) * size);
mach->head = mach->cells;
mach->end = mach->head + size;
mach->base = mach->cells;
memset(mach->cells, 0, size * sizeof(int));
return mach;
}
void CellMachine_reset(struct CellMachine* mach)
{
mach->base = mach->cells;
mach->head = mach->cells;
}
// Method that runs code on the machine.
void CellMachine_run(struct CellMachine* mach, int* code)
{
CellMachine_reset(mach);
goto START_STATE;
START_STATE:
switch(*code++)
{
case CellCode_STOP:
goto STOP_STATE;
case CellCode_SUM:
goto SUM_STATE;
case CellCode_END:
fprintf(stderr, "Error got end code but no expression to end\n");
return;
default:
goto ERR_STATE;
}
SUM_STATE:
switch(*code++)
{
case CellCode_STOP:
goto STOP_STATE;
case CellCode_INT:
*(mach->head++) = *code++;
*(mach->head++) = *code++;
goto SUM_STATE;
case CellCode_END:
goto START_STATE;
default:
goto ERR_STATE;
}
STOP_STATE:
printf("Reached stop code\n");
return;
ERR_STATE:
fprintf(stderr, "Reached unknowned error code: %d \n", *(code - 1));
return;
}
void CellMachine_del(struct CellMachine* mach)
{
free(mach->cells);
free(mach);
}
int main(int argc, char const *argv[])
{
struct CellMachine* machine = CellMachine_new(10);
CellMachine_del(machine);
return 0;
}
以上是关于c_cpp 尝试goto状态转换表达式引擎的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp goto.c
c_cpp 使用goto语句来解决阶乘
c_cpp 在c中使用goto进行错误处理
DFA 最小化
(转载)虚幻引擎3--UnrealScript教程章节一:19.return和goto
c_cpp [JudgeGirl] 65.表达树 - 我在C中首次尝试OOP