big fix in if-else compilation

This commit is contained in:
cozis
2021-11-01 02:35:33 +00:00
parent 91765b2ca0
commit e67231f321
6 changed files with 85 additions and 38 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
if false 1; if true 3; else none;
+76 -36
View File
@@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#include "../utils/defs.h" #include "../utils/defs.h"
#include "../utils/bucketlist.h" #include "../utils/bucketlist.h"
#include "executable.h" #include "executable.h"
@@ -31,53 +32,34 @@ struct xExeBuilder {
typedef struct { typedef struct {
const char *name; const char *name;
int stacksz;
int opcount; int opcount;
OperandType *optypes; OperandType *optypes;
} InstrInfo; } InstrInfo;
static const InstrInfo instr_table[] = { static const InstrInfo instr_table[] = {
[OPCODE_NOPE] = {"NOPE", 0, 0, NULL}, [OPCODE_NOPE] = {"NOPE", 0, NULL},
[OPCODE_POS] = {"POS", 1, 0, NULL}, [OPCODE_POS] = {"POS", 0, NULL},
[OPCODE_NEG] = {"NEG", 1, 0, NULL}, [OPCODE_NEG] = {"NEG", 0, NULL},
[OPCODE_ADD] = {"ADD", 2, 0, NULL}, [OPCODE_ADD] = {"ADD", 0, NULL},
[OPCODE_SUB] = {"SUB", 2, 0, NULL}, [OPCODE_SUB] = {"SUB", 0, NULL},
[OPCODE_MUL] = {"MUL", 2, 0, NULL}, [OPCODE_MUL] = {"MUL", 0, NULL},
[OPCODE_DIV] = {"DIV", 2, 0, NULL}, [OPCODE_DIV] = {"DIV", 0, NULL},
[OPCODE_PUSHINT] = {"PUSHINT", 0, 1, (OperandType[]) {OPTP_INT}}, [OPCODE_PUSHINT] = {"PUSHINT", 1, (OperandType[]) {OPTP_INT}},
[OPCODE_PUSHFLT] = {"PUSHFLT", 0, 1, (OperandType[]) {OPTP_FLOAT}}, [OPCODE_PUSHFLT] = {"PUSHFLT", 1, (OperandType[]) {OPTP_FLOAT}},
[OPCODE_PUSHSTR] = {"PUSHSTR", 0, 1, (OperandType[]) {OPTP_STRING}}, [OPCODE_PUSHSTR] = {"PUSHSTR", 1, (OperandType[]) {OPTP_STRING}},
[OPCODE_PUSHVAR] = {"PUSHVAR", 0, 1, (OperandType[]) {OPTP_STRING}}, [OPCODE_PUSHVAR] = {"PUSHVAR", 1, (OperandType[]) {OPTP_STRING}},
[OPCODE_PUSHTRU] = {"PUSHTRU", 0, 0, NULL}, [OPCODE_PUSHTRU] = {"PUSHTRU", 0, NULL},
[OPCODE_PUSHFLS] = {"PUSHFLS", 0, 0, NULL}, [OPCODE_PUSHFLS] = {"PUSHFLS", 0, NULL},
[OPCODE_PUSHNNE] = {"PUSHNNE", 0, 0, NULL}, [OPCODE_PUSHNNE] = {"PUSHNNE", 0, NULL},
[OPCODE_RETURN] = {"RETURN", 0, 0, NULL}, [OPCODE_RETURN] = {"RETURN", 0, NULL},
[OPCODE_JUMPIFNOTANDPOP] = {"JUMPIFNOTANDPOP", 1, 1, (OperandType[]) {OPTP_INT}}, [OPCODE_JUMPIFNOTANDPOP] = {"JUMPIFNOTANDPOP", 1, (OperandType[]) {OPTP_INT}},
[OPCODE_JUMP] = {"JUMP", 0, 1, (OperandType[]) {OPTP_INT}}, [OPCODE_JUMP] = {"JUMP", 1, (OperandType[]) {OPTP_INT}},
};
static const char *operand_type_names[] = {
[OPTP_INT] = "int",
[OPTP_FLOAT] = "float",
[OPTP_STRING] = "string",
};
static const char *operand_type_arts[] = {
[OPTP_INT] = "an",
[OPTP_FLOAT] = "a",
[OPTP_STRING] = "a",
};
static unsigned int operand_type_sizes[] = {
[OPTP_INT] = membersizeof(Operand, as_int),
[OPTP_FLOAT] = membersizeof(Operand, as_float),
[OPTP_STRING] = membersizeof(Operand, as_string),
}; };
Executable *Executable_Copy(Executable *exe) Executable *Executable_Copy(Executable *exe)
@@ -101,6 +83,46 @@ void Executable_Free(Executable *exe)
} }
} }
void Executable_Dump(Executable *exe)
{
for(int i = 0; i < exe->bodyl; i += 1)
{
Opcode opcode;
Operand ops[MAX_OPS];
int opc = MAX_OPS;
(void) Executable_Fetch(exe, i, &opcode, ops, &opc);
const InstrInfo *info = instr_table + exe->body[i].opcode;
fprintf(stderr, "%d: %s ", i, info->name);
for(int j = 0; j < opc; j += 1)
{
switch(ops[j].type)
{
case OPTP_INT:
fprintf(stderr, "%lld ", ops[j].as_int);
break;
case OPTP_FLOAT:
fprintf(stderr, "%f ", ops[j].as_float);
break;
case OPTP_STRING:
fprintf(stderr, "[%s] ", ops[j].as_string);
break;
case OPTP_PROMISE:
UNREACHABLE;
break;
}
}
fprintf(stderr, "\n");
}
}
_Bool Executable_SetSource(Executable *exe, Source *src) _Bool Executable_SetSource(Executable *exe, Source *src)
{ {
src = Source_Copy(src); src = Source_Copy(src);
@@ -248,6 +270,24 @@ _Bool ExeBuilder_Append(ExeBuilder *exeb, Error *error, Opcode opcode, Operand *
assert(exeb != NULL); assert(exeb != NULL);
assert(opc >= 0); assert(opc >= 0);
static const char *operand_type_names[] = {
[OPTP_INT] = "int",
[OPTP_FLOAT] = "float",
[OPTP_STRING] = "string",
};
static const char *operand_type_arts[] = {
[OPTP_INT] = "an",
[OPTP_FLOAT] = "a",
[OPTP_STRING] = "a",
};
static const unsigned int operand_type_sizes[] = {
[OPTP_INT] = membersizeof(Operand, as_int),
[OPTP_FLOAT] = membersizeof(Operand, as_float),
[OPTP_STRING] = membersizeof(Operand, as_string),
};
const InstrInfo *info = instr_table + opcode; const InstrInfo *info = instr_table + opcode;
if(opc != info->opcount) if(opc != info->opcount)
+1
View File
@@ -45,6 +45,7 @@ typedef struct xExeBuilder ExeBuilder;
Executable *Executable_Copy(Executable *exe); Executable *Executable_Copy(Executable *exe);
void Executable_Free(Executable *exe); void Executable_Free(Executable *exe);
void Executable_Dump(Executable *exe);
_Bool Executable_Fetch(Executable *exe, int index, Opcode *opcode, Operand *ops, int *opc); _Bool Executable_Fetch(Executable *exe, int index, Opcode *opcode, Operand *ops, int *opc);
_Bool Executable_SetSource(Executable *exe, Source *src); _Bool Executable_SetSource(Executable *exe, Source *src);
Source *Executable_GetSource(Executable *exe); Source *Executable_GetSource(Executable *exe);
+2
View File
@@ -181,6 +181,8 @@ int main(int argc, char **argv)
BPAlloc_Free(alloc); BPAlloc_Free(alloc);
} }
Executable_Dump(exe);
// Execute // Execute
{ {
Runtime *runtime = Runtime_New(-1, -1); Runtime *runtime = Runtime_New(-1, -1);
+5 -1
View File
@@ -495,6 +495,10 @@ static _Bool step(Runtime *runtime, Error *error)
} }
Object *top = Stack_Top(runtime->stack, 0); Object *top = Stack_Top(runtime->stack, 0);
if(!Runtime_Pop(runtime, error, 1))
return 0;
assert(top != NULL); assert(top != NULL);
if(!Object_IsBool(top)) if(!Object_IsBool(top))
@@ -503,7 +507,7 @@ static _Bool step(Runtime *runtime, Error *error)
return 0; return 0;
} }
if(Object_ToBool(top, error)) // This can't fail because we know it's a bool. if(!Object_ToBool(top, error)) // This can't fail because we know it's a bool.
runtime->frame->index = target; runtime->frame->index = target;
return 1; return 1;
BIN
View File
Binary file not shown.