mirror of
https://github.com/tildearrow/furnace.git
synced 2024-11-01 18:42:40 +00:00
Momo: stack machine, part 3
This commit is contained in:
parent
a4e634ecd6
commit
5f7925ceb4
1 changed files with 50 additions and 9 deletions
|
@ -62,6 +62,9 @@ static const char* stackInsNames[]={
|
||||||
"end", "push", "push n", "add", "sub", "mul", "div", "mod", "cmp eq", "cmp ne",
|
"end", "push", "push n", "add", "sub", "mul", "div", "mod", "cmp eq", "cmp ne",
|
||||||
"cmp gt", "cmp lt", "cmp ge", "cmp le", "cmp and", "cmp or", "beq", "bne", "exit"
|
"cmp gt", "cmp lt", "cmp ge", "cmp le", "cmp and", "cmp or", "beq", "bne", "exit"
|
||||||
};
|
};
|
||||||
|
static unsigned char stackTakesArg[]={
|
||||||
|
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0
|
||||||
|
};
|
||||||
|
|
||||||
struct StackData {
|
struct StackData {
|
||||||
unsigned char ins;
|
unsigned char ins;
|
||||||
|
@ -222,6 +225,7 @@ unsigned int runStackMachine(struct StackData* data, size_t count, unsigned int
|
||||||
\
|
\
|
||||||
/* push operation if pending */ \
|
/* push operation if pending */ \
|
||||||
if (state[curState].curOp[0]) { \
|
if (state[curState].curOp[0]) { \
|
||||||
|
unsigned char isCompare=0; \
|
||||||
printf("PENDING OP: %s\n",state[curState].curOp); \
|
printf("PENDING OP: %s\n",state[curState].curOp); \
|
||||||
if (strcmp(state[curState].curOp,"+")==0) { \
|
if (strcmp(state[curState].curOp,"+")==0) { \
|
||||||
data[*pc].ins=MOMO_STACK_ADD; \
|
data[*pc].ins=MOMO_STACK_ADD; \
|
||||||
|
@ -244,42 +248,60 @@ unsigned int runStackMachine(struct StackData* data, size_t count, unsigned int
|
||||||
data[*pc].param=0; \
|
data[*pc].param=0; \
|
||||||
(*pc)++; \
|
(*pc)++; \
|
||||||
} else if (strcmp(state[curState].curOp,"&&")==0) { \
|
} else if (strcmp(state[curState].curOp,"&&")==0) { \
|
||||||
data[*pc].ins=MOMO_STACK_CMP_AND; \
|
/* handled later */ \
|
||||||
data[*pc].param=0; \
|
|
||||||
(*pc)++; \
|
|
||||||
} else if (strcmp(state[curState].curOp,"||")==0) { \
|
} else if (strcmp(state[curState].curOp,"||")==0) { \
|
||||||
data[*pc].ins=MOMO_STACK_CMP_OR; \
|
/* handled later */ \
|
||||||
data[*pc].param=0; \
|
|
||||||
(*pc)++; \
|
|
||||||
} else if (strcmp(state[curState].curOp,">")==0) { \
|
} else if (strcmp(state[curState].curOp,">")==0) { \
|
||||||
data[*pc].ins=MOMO_STACK_CMP_GT; \
|
data[*pc].ins=MOMO_STACK_CMP_GT; \
|
||||||
data[*pc].param=0; \
|
data[*pc].param=0; \
|
||||||
(*pc)++; \
|
(*pc)++; \
|
||||||
|
isCompare=1; \
|
||||||
} else if (strcmp(state[curState].curOp,"<")==0) { \
|
} else if (strcmp(state[curState].curOp,"<")==0) { \
|
||||||
data[*pc].ins=MOMO_STACK_CMP_LT; \
|
data[*pc].ins=MOMO_STACK_CMP_LT; \
|
||||||
data[*pc].param=0; \
|
data[*pc].param=0; \
|
||||||
(*pc)++; \
|
(*pc)++; \
|
||||||
|
isCompare=1; \
|
||||||
} else if (strcmp(state[curState].curOp,">=")==0) { \
|
} else if (strcmp(state[curState].curOp,">=")==0) { \
|
||||||
data[*pc].ins=MOMO_STACK_CMP_GE; \
|
data[*pc].ins=MOMO_STACK_CMP_GE; \
|
||||||
data[*pc].param=0; \
|
data[*pc].param=0; \
|
||||||
(*pc)++; \
|
(*pc)++; \
|
||||||
|
isCompare=1; \
|
||||||
} else if (strcmp(state[curState].curOp,"<=")==0) { \
|
} else if (strcmp(state[curState].curOp,"<=")==0) { \
|
||||||
data[*pc].ins=MOMO_STACK_CMP_LE; \
|
data[*pc].ins=MOMO_STACK_CMP_LE; \
|
||||||
data[*pc].param=0; \
|
data[*pc].param=0; \
|
||||||
(*pc)++; \
|
(*pc)++; \
|
||||||
} else if (strcmp(state[curState].curOp,"<=")==0) { \
|
isCompare=1; \
|
||||||
} else if (strcmp(state[curState].curOp,"==")==0) { \
|
} else if (strcmp(state[curState].curOp,"==")==0) { \
|
||||||
data[*pc].ins=MOMO_STACK_CMP_EQ; \
|
data[*pc].ins=MOMO_STACK_CMP_EQ; \
|
||||||
data[*pc].param=0; \
|
data[*pc].param=0; \
|
||||||
(*pc)++; \
|
(*pc)++; \
|
||||||
|
isCompare=1; \
|
||||||
} else if (strcmp(state[curState].curOp,"!=")==0) { \
|
} else if (strcmp(state[curState].curOp,"!=")==0) { \
|
||||||
data[*pc].ins=MOMO_STACK_CMP_NE; \
|
data[*pc].ins=MOMO_STACK_CMP_NE; \
|
||||||
data[*pc].param=0; \
|
data[*pc].param=0; \
|
||||||
(*pc)++; \
|
(*pc)++; \
|
||||||
|
isCompare=1; \
|
||||||
} else { \
|
} else { \
|
||||||
printf("ERROR: invalid operation\n"); \
|
printf("ERROR: invalid operation\n"); \
|
||||||
return 4; \
|
return 4; \
|
||||||
} \
|
} \
|
||||||
|
if (state[curState].curBigOp[0] && isCompare) { \
|
||||||
|
printf("PENDING BIG OP...\n"); \
|
||||||
|
if (strcmp(state[curState].curBigOp,"&&")==0) { \
|
||||||
|
data[*pc].ins=MOMO_STACK_CMP_AND; \
|
||||||
|
data[*pc].param=0; \
|
||||||
|
(*pc)++; \
|
||||||
|
} else if (strcmp(state[curState].curBigOp,"||")==0) { \
|
||||||
|
data[*pc].ins=MOMO_STACK_CMP_OR; \
|
||||||
|
data[*pc].param=0; \
|
||||||
|
(*pc)++; \
|
||||||
|
} \
|
||||||
|
memset(state[curState].curBigOp,0,8); \
|
||||||
|
} \
|
||||||
|
if (strcmp(state[curState].curOp,"&&")==0 || strcmp(state[curState].curOp,"||")==0) { \
|
||||||
|
printf("PREPARING BIG OP...\n"); \
|
||||||
|
strncpy(state[curState].curBigOp,state[curState].curOp,8); \
|
||||||
|
} \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
memset(state[curState].curOp,0,8); \
|
memset(state[curState].curOp,0,8); \
|
||||||
|
@ -298,6 +320,7 @@ unsigned char compileExprSub(const char** ptr, struct StackData* data, size_t* p
|
||||||
unsigned char oldIsOp;
|
unsigned char oldIsOp;
|
||||||
char curIdent[32];
|
char curIdent[32];
|
||||||
char curOp[8];
|
char curOp[8];
|
||||||
|
char curBigOp[8];
|
||||||
unsigned char curIdentLen;
|
unsigned char curIdentLen;
|
||||||
unsigned char curOpLen;
|
unsigned char curOpLen;
|
||||||
unsigned char startBranch;
|
unsigned char startBranch;
|
||||||
|
@ -368,7 +391,21 @@ unsigned char compileExprSub(const char** ptr, struct StackData* data, size_t* p
|
||||||
break;
|
break;
|
||||||
case ')':
|
case ')':
|
||||||
// pop last state
|
// pop last state
|
||||||
|
FINISH_OP;
|
||||||
curState--;
|
curState--;
|
||||||
|
if (state[curState].curBigOp[0]) {
|
||||||
|
printf("PENDING BIG OP...\n");
|
||||||
|
if (strcmp(state[curState].curBigOp,"&&")==0) {
|
||||||
|
data[*pc].ins=MOMO_STACK_CMP_AND;
|
||||||
|
data[*pc].param=0;
|
||||||
|
(*pc)++;
|
||||||
|
} else if (strcmp(state[curState].curBigOp,"||")==0) {
|
||||||
|
data[*pc].ins=MOMO_STACK_CMP_OR;
|
||||||
|
data[*pc].param=0;
|
||||||
|
(*pc)++;
|
||||||
|
}
|
||||||
|
memset(state[curState].curBigOp,0,8);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
break;
|
break;
|
||||||
case ' ':
|
case ' ':
|
||||||
|
@ -407,7 +444,7 @@ unsigned char compileExprSub(const char** ptr, struct StackData* data, size_t* p
|
||||||
state[curState].pendingBranch=0;
|
state[curState].pendingBranch=0;
|
||||||
} else {
|
} else {
|
||||||
state[curState].pendingBranch=*pc;
|
state[curState].pendingBranch=*pc;
|
||||||
data[*pc].ins=MOMO_STACK_BEQ;
|
data[*pc].ins=MOMO_STACK_BNE;
|
||||||
data[*pc].param=0;
|
data[*pc].param=0;
|
||||||
}
|
}
|
||||||
state[curState].startBranch=0;
|
state[curState].startBranch=0;
|
||||||
|
@ -725,7 +762,11 @@ const char* momo_bindtextdomain(const char* domainName, const char* dirName) {
|
||||||
// dump program
|
// dump program
|
||||||
printf("compiled program:\n");
|
printf("compiled program:\n");
|
||||||
for (int i=0; i<256; i++) {
|
for (int i=0; i<256; i++) {
|
||||||
printf("%s %u\n",stackInsNames[newDomain->pluralProgram[i].ins],newDomain->pluralProgram[i].param);
|
if (stackTakesArg[newDomain->pluralProgram[i].ins]) {
|
||||||
|
printf("%s %u\n",stackInsNames[newDomain->pluralProgram[i].ins],newDomain->pluralProgram[i].param);
|
||||||
|
} else {
|
||||||
|
printf("%s\n",stackInsNames[newDomain->pluralProgram[i].ins]);
|
||||||
|
}
|
||||||
if (newDomain->pluralProgram[i].ins==0) break;
|
if (newDomain->pluralProgram[i].ins==0) break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue