Select Git revision
Forked from
Toby Murray / swen90006-a2-2020
Source project has a limited visibility.
dc.c 18.76 KiB
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include "debug.h"
#if defined(DC_LIBFUZZER) || defined(DC_FUZZ)
#include <stdint.h>
#define LIBFUZZER_INPUT_FILE "libFuzzerInput.XXXXXX";
/* turn off tracing to make it run faster */
#define printf(...)
#define fprintf(...)
#endif
const char INSTRUCTION_ADD[] = "+";
const char INSTRUCTION_MULT[] = "*";
const char INSTRUCTION_SUB[] = "-";
const char INSTRUCTION_DIV[] = "/";
const char INSTRUCTION_PUSH[] = "push";
const char INSTRUCTION_POP[] = "pop";
const char INSTRUCTION_LOAD[] = "load";
const char INSTRUCTION_REM[] = "remove";
const char INSTRUCTION_STORE[] = "store";
const char INSTRUCTION_SAVE[] = "save";
const char INSTRUCTION_LIST[] = "list";
const char INSTRUCTION_PRINT[] = "print";
/* variables are signed 32-bit integers */
typedef int32_t value_t;
/* we store a mapping from variable names to values using a binary tree
to try to ensure log lookup performance */
typedef struct node {
char * varname;
value_t value;
struct node *left;
struct node *right;
} node_t;
static const node_t * lookup(const node_t *p, const char *varname){
while (p != NULL){
int ret = strcmp(varname,p->varname);
if (ret == 0){
return p;
}else if (ret < 0){
p = p->left;
}else{
p = p->right;
}
}
return p; // not found
}
static void node_print(const node_t *p){
printf("VARIABLE: %s, VALUE: %d\n",p->varname,p->value);
}