Skip to content
Permalink
Browse files

move the stack to command_state_t, remove some global variables

  • Loading branch information
samdoshi committed Apr 30, 2016
1 parent 842d2ca commit 07e73c3df59d11c99890a2093b9b2c4377e44d01
Showing with 60 additions and 40 deletions.
  1. +2 −2 simulator/Makefile
  2. +1 −0 src/Makefile
  3. +1 −0 src/config.mk
  4. +9 −0 src/state.c
  5. +34 −1 src/state.h
  6. +11 −35 src/teletype.c
  7. +0 −1 src/teletype.h
  8. +2 −1 tests/Makefile
@@ -1,8 +1,8 @@
.PHONY: clean format
CFLAGS=-std=c99 -g -Wall -DSIM -I. -I../src -I../libavr32/src
DEPS =
OBJ = tt.o ../src/teletype.o ../src/table.o \
../src/euclidean/euclidean.o ../src/euclidean/data.o \
OBJ = tt.o ../src/teletype.o ../src/state.o \
../src/table.o ../src/euclidean/euclidean.o ../src/euclidean/data.o \
../libavr32/src/util.o

%.o: %.c $(DEPS)
@@ -69,6 +69,7 @@ include $(MAKEFILE_PATH)
format:
clang-format -style=file -i main.c
clang-format -style=file -i state.h
clang-format -style=file -i state.c
clang-format -style=file -i table.h
clang-format -style=file -i table.c
clang-format -style=file -i teletype.h
@@ -62,6 +62,7 @@ TARGET = $(THIS).elf
# List of C source files.
CSRCS = \
../src/main.c \
../src/state.c \
../src/table.c \
../src/teletype.c \
../src/euclidean/euclidean.c \
@@ -0,0 +1,9 @@
#include "state.h"

void cs_init(command_state_t *cs) {
cs->stack.top = 0;
}

int16_t cs_stack_size(command_state_t *cs) {
return cs->stack.top;
}
@@ -3,10 +3,15 @@

#include <stdint.h>

#define STACK_SIZE 8
#define CV_COUNT 4
#define Q_LENGTH 16
#define TR_COUNT 4

////////////////////////////////////////////////////////////////////////////////
// SCENE STATE /////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

typedef struct {
int16_t a;
int16_t b;
@@ -47,10 +52,38 @@ typedef struct {

typedef struct { scene_variables_t variables; } scene_state_t;

////////////////////////////////////////////////////////////////////////////////
// EXEC STATE //////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

typedef struct {
} exec_state_t;

////////////////////////////////////////////////////////////////////////////////
// COMMAND STATE ///////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

typedef struct {
} command_state_t;
int16_t values[STACK_SIZE];
int16_t top;
} command_state_stack_t;

typedef struct { command_state_stack_t stack; } command_state_t;

extern void cs_init(command_state_t *cs);
extern int16_t cs_stack_size(command_state_t *cs);

// by declaring the following static inline, each compilation unit (i.e. C
// file), gets its own copy of the function
static inline int16_t cs_pop(command_state_t *cs) {
cs->stack.top--;
return cs->stack.values[cs->stack.top];
}

static inline void cs_push(command_state_t *cs, int16_t data) {
cs->stack.values[cs->stack.top] = data;
cs->stack.top++;
}


#endif
@@ -107,35 +107,10 @@ static scene_state_t scene_state = {
.tr_time = { 100, 100, 100, 100 } }
};
static exec_state_t exec_state;
static command_state_t command_state;

/////////////////////////////////////////////////////////////////
// STACK ////////////////////////////////////////////////////////

static int16_t cs_pop(command_state_t *cs);
static void cs_push(command_state_t *cs, int16_t data);

static int16_t top;
static int16_t stack[STACK_SIZE];

int16_t cs_pop(command_state_t *NOTUSED(cs)) {
top--;
// sprintf(dbg,"\r\npop %d", stack[top]);
return stack[top];
}

void cs_push(command_state_t *NOTUSED(cs), int16_t data) {
stack[top] = data;
// sprintf(dbg,"\r\npush %d", stack[top]);
top++;
}


/////////////////////////////////////////////////////////////////
// VARS ARRAYS //////////////////////////////////////////////////

// ENUM IN HEADER

static void op_M_get(const void *data, scene_state_t *ss, exec_state_t *es,
command_state_t *cs);
static void op_M_set(const void *data, scene_state_t *ss, exec_state_t *es,
@@ -1878,7 +1853,8 @@ void copy_sub_command(tele_command_t *dst, tele_command_t *src) {
// PROCESS //////////////////////////////////////////////////////

process_result_t process(tele_command_t *c) {
top = 0;
command_state_t cs;
cs_init(&cs);

// if the command has a MOD, only process it
// allow the MOD to deal with processing the remainder
@@ -1888,28 +1864,28 @@ process_result_t process(tele_command_t *c) {
tele_word_t word_type = c->data[idx].t;
int16_t word_value = c->data[idx].v;

if (word_type == NUMBER) { cs_push(&command_state, word_value); }
if (word_type == NUMBER) { cs_push(&cs, word_value); }
else if (word_type == OP) {
tele_op_t op = tele_ops[word_value];

// if we're in the first command position, and there is a set fn
// pointer and we have enough params, then run set, else run get
if (idx == 0 && op.set != NULL && top >= op.params + 1)
op.set(op.data, &scene_state, &exec_state, &command_state);
if (idx == 0 && op.set != NULL &&
cs_stack_size(&cs) >= op.params + 1)
op.set(op.data, &scene_state, &exec_state, &cs);
else
op.get(op.data, &scene_state, &exec_state, &command_state);
op.get(op.data, &scene_state, &exec_state, &cs);
}
else if (word_type == MOD) {
tele_command_t sub_command;
copy_sub_command(&sub_command, c);
tele_mods[word_value].func(&scene_state, &exec_state,
&command_state, &sub_command);
tele_mods[word_value].func(&scene_state, &exec_state, &cs,
&sub_command);
}
}

if (top) {
process_result_t o = {.has_value = true,
.value = cs_pop(&command_state) };
if (cs_stack_size(&cs)) {
process_result_t o = {.has_value = true, .value = cs_pop(&cs) };
return o;
}
else {
@@ -9,7 +9,6 @@
#define SCRIPT_MAX_COMMANDS 6
#define SCRIPT_MAX_COMMANDS_ 5
#define COMMAND_MAX_LENGTH 12
#define STACK_SIZE 8
#define TELE_STACK_SIZE 8
#define TELE_D_SIZE 8

@@ -2,7 +2,8 @@
CFLAGS = -std=c99 -g -Wall -DSIM -I../src -I../libavr32/src

tests: main.o parser_tests.o process_tests.o \
../src/teletype.o ../src/table.o ../src/euclidean/data.o ../src/euclidean/euclidean.o \
../src/teletype.o ../src/state.o \
../src/table.o ../src/euclidean/data.o ../src/euclidean/euclidean.o \
../libavr32/src/util.o
$(CC) -o $@ $^ $(CFLAGS)

0 comments on commit 07e73c3

Please sign in to comment.