Skip to content
Permalink
Browse files

add sub commands

  • Loading branch information
samdoshi committed Jan 10, 2017
1 parent dc7cdda commit 09f95021c6a817e51199f0b5ad4c7f68761a7b01
Showing with 189 additions and 88 deletions.
  1. +2 −2 simulator/tt.c
  2. +3 −2 src/command.c
  3. +2 −2 src/command.h
  4. +16 −15 src/ops/controlflow.c
  5. +3 −3 src/ops/delay.c
  6. +1 −1 src/ops/op.h
  7. +7 −6 src/ops/stack.c
  8. +22 −12 src/scanner.rl
  9. +97 −40 src/teletype.c
  10. +5 −4 src/teletype.h
  11. +15 −0 tests/parser_tests.c
  12. +16 −1 tests/process_tests.c
@@ -58,7 +58,7 @@ void tele_ii_tx(uint8_t addr, uint8_t *data, uint8_t l) {
}

void tele_ii_tx_now(uint8_t addr, uint8_t *data, uint8_t l) {
printf("II_tx addr:%" PRIu8 " l:%" PRId16, addr, l);
printf("II_tx addr:%" PRIu8 " l:%" PRIu8, addr, l);
printf("\n");
}

@@ -144,7 +144,7 @@ int main() {
if (error_msg[0]) printf(": %s", error_msg);
printf("\n");
if (status == E_OK) {
process_result_t output = process(&es, &temp);
process_result_t output = process_command(&es, &temp);
if (output.has_value) { printf(">>> %i\n", output.value); }
}
}
@@ -10,7 +10,7 @@ void copy_command(tele_command_t *dst, const tele_command_t *src) {
memcpy(dst, src, sizeof(tele_command_t));
}

void copy_sub_command(tele_command_t *dst, const tele_command_t *src) {
void copy_post_command(tele_command_t *dst, const tele_command_t *src) {
dst->length = src->length - src->separator - 1;
dst->separator = -1;
memcpy(dst->data, &src->data[src->separator + 1],
@@ -29,7 +29,8 @@ void print_command(const tele_command_t *c, char *out) {
break;
}
case MOD: strcat(out, tele_mods[c->data[n].value]->name); break;
case SEP: strcat(out, ":"); break;
case PRE_SEP: strcat(out, ":"); break;
case SUB_SEP: strcat(out, ";"); break;
default: break;
}
strcat(out, " ");
@@ -5,7 +5,7 @@

#define COMMAND_MAX_LENGTH 12

typedef enum { NUMBER, MOD, SEP, OP } tele_word_t;
typedef enum { NUMBER, OP, MOD, PRE_SEP, SUB_SEP } tele_word_t;

typedef struct {
tele_word_t tag;
@@ -19,7 +19,7 @@ typedef struct {
} tele_command_t;

void copy_command(tele_command_t *dst, const tele_command_t *src);
void copy_sub_command(tele_command_t *dst, const tele_command_t *src);
void copy_post_command(tele_command_t *dst, const tele_command_t *src);
void print_command(const tele_command_t *c, char *out);

#endif
@@ -8,17 +8,18 @@

static void mod_PROB_func(scene_state_t *ss, exec_state_t *es,
command_state_t *cs,
const tele_command_t *sub_command);
const tele_command_t *post_command);
static void mod_IF_func(scene_state_t *ss, exec_state_t *es,
command_state_t *cs, const tele_command_t *sub_command);
command_state_t *cs,
const tele_command_t *post_command);
static void mod_ELIF_func(scene_state_t *ss, exec_state_t *es,
command_state_t *cs,
const tele_command_t *sub_command);
const tele_command_t *post_command);
static void mod_ELSE_func(scene_state_t *ss, exec_state_t *es,
command_state_t *cs,
const tele_command_t *sub_command);
const tele_command_t *post_command);
static void mod_L_func(scene_state_t *ss, exec_state_t *es, command_state_t *cs,
const tele_command_t *sub_command);
const tele_command_t *post_command);

static void op_SCENE_get(const void *data, scene_state_t *ss, exec_state_t *es,
command_state_t *cs);
@@ -44,51 +45,51 @@ const tele_op_t op_SCENE =

static void mod_PROB_func(scene_state_t *NOTUSED(ss), exec_state_t *es,
command_state_t *cs,
const tele_command_t *sub_command) {
const tele_command_t *post_command) {
int16_t a = cs_pop(cs);

if (rand() % 101 < a) { process(es, sub_command); }
if (rand() % 101 < a) { process_command(es, post_command); }
}

static void mod_IF_func(scene_state_t *NOTUSED(ss), exec_state_t *es,
command_state_t *cs,
const tele_command_t *sub_command) {
const tele_command_t *post_command) {
es->if_else_condition = false;
if (cs_pop(cs)) {
es->if_else_condition = true;
process(es, sub_command);
process_command(es, post_command);
}
}

static void mod_ELIF_func(scene_state_t *NOTUSED(ss), exec_state_t *es,
command_state_t *cs,
const tele_command_t *sub_command) {
const tele_command_t *post_command) {
if (!es->if_else_condition) {
if (cs_pop(cs)) {
es->if_else_condition = true;
process(es, sub_command);
process_command(es, post_command);
}
}
}

static void mod_ELSE_func(scene_state_t *NOTUSED(ss), exec_state_t *es,
command_state_t *NOTUSED(cs),
const tele_command_t *sub_command) {
const tele_command_t *post_command) {
if (!es->if_else_condition) {
es->if_else_condition = true;
process(es, sub_command);
process_command(es, post_command);
}
}

static void mod_L_func(scene_state_t *ss, exec_state_t *es, command_state_t *cs,
const tele_command_t *sub_command) {
const tele_command_t *post_command) {
int16_t a = cs_pop(cs);
int16_t b = cs_pop(cs);
int16_t loop_size = a < b ? b - a : a - b;

for (int16_t i = 0; i <= loop_size; i++) {
ss->variables.i = a < b ? a + i : a - i;
process(es, sub_command);
process_command(es, post_command);
}
}

@@ -6,7 +6,7 @@

static void mod_DEL_func(scene_state_t *ss, exec_state_t *es,
command_state_t *cs,
const tele_command_t *sub_command);
const tele_command_t *post_command);

static void op_DEL_CLR_get(const void *data, scene_state_t *ss,
exec_state_t *es, command_state_t *cs);
@@ -17,7 +17,7 @@ const tele_op_t op_DEL_CLR = MAKE_GET_OP(DEL.CLR, op_DEL_CLR_get, 0, false);

static void mod_DEL_func(scene_state_t *ss, exec_state_t *NOTUSED(es),
command_state_t *cs,
const tele_command_t *sub_command) {
const tele_command_t *post_command) {
int16_t i = 0;
int16_t a = cs_pop(cs);

@@ -30,7 +30,7 @@ static void mod_DEL_func(scene_state_t *ss, exec_state_t *NOTUSED(es),
if (ss->delay.count == 1) tele_delay(1);
ss->delay.time[i] = a;

copy_command(&ss->delay.commands[i], sub_command);
copy_command(&ss->delay.commands[i], post_command);
}
}

@@ -22,7 +22,7 @@ typedef struct {
typedef struct {
const char *name;
void (*const func)(scene_state_t *ss, exec_state_t *es, command_state_t *cs,
const tele_command_t *sub_command);
const tele_command_t *post_command);
const uint8_t params;
} tele_mod_t;

@@ -5,7 +5,7 @@
#include "teletype_io.h"

static void mod_S_func(scene_state_t *ss, exec_state_t *es, command_state_t *cs,
const tele_command_t *sub_command);
const tele_command_t *post_command);
static void op_S_ALL_get(const void *data, scene_state_t *ss, exec_state_t *es,
command_state_t *cs);
static void op_S_POP_get(const void *data, scene_state_t *ss, exec_state_t *es,
@@ -24,18 +24,19 @@ const tele_op_t op_S_L = MAKE_GET_OP(S.L, op_S_L_get, 0, true);

static void mod_S_func(scene_state_t *ss, exec_state_t *NOTUSED(es),
command_state_t *NOTUSED(cs),
const tele_command_t *sub_command) {
const tele_command_t *post_command) {
if (ss->stack_op.top < STACK_OP_SIZE) {
copy_command(&ss->stack_op.commands[ss->stack_op.top], sub_command);
copy_command(&ss->stack_op.commands[ss->stack_op.top], post_command);
ss->stack_op.top++;
if (ss->stack_op.top == 1) tele_s(1);
}
}

static void op_S_ALL_get(const void *NOTUSED(data), scene_state_t *ss,
exec_state_t *es, command_state_t *NOTUSED(cs)) {
for (int16_t i = 0; i < ss->stack_op.top; i++)
process(es, &ss->stack_op.commands[ss->stack_op.top - i - 1]);
for (int16_t i = 0; i < ss->stack_op.top; i++) {
process_command(es, &ss->stack_op.commands[ss->stack_op.top - i - 1]);
}
ss->stack_op.top = 0;
tele_s(0);
}
@@ -44,7 +45,7 @@ static void op_S_POP_get(const void *NOTUSED(data), scene_state_t *ss,
exec_state_t *es, command_state_t *NOTUSED(cs)) {
if (ss->stack_op.top) {
ss->stack_op.top--;
process(es, &ss->stack_op.commands[ss->stack_op.top]);
process_command(es, &ss->stack_op.commands[ss->stack_op.top]);
if (ss->stack_op.top == 0) tele_s(0);
}
}
@@ -37,9 +37,10 @@ error_t scanner(const char *data, tele_command_t *out,

%%{
separator = [ \n\t];
mod_separator = ' : ';
pre_separator = ' : ';
sub_seperator = ' ; ';

token = any+ -- (separator | mod_separator);
token = any+ -- (separator | pre_separator | sub_seperator);

action token {
// token matched
@@ -56,10 +57,6 @@ error_t scanner(const char *data, tele_command_t *out,
// if we have a match, copy data to the the command
out->data[out->length] = tele_data;

// if it's a SEP, we need to record it's position
// (validate checks for too many SEP tokens)
if (tele_data.tag == SEP) out->separator = out->length;

// increase the command length
out->length++;

@@ -73,12 +70,12 @@ error_t scanner(const char *data, tele_command_t *out,
}
}

action mod_separator {
// ':' mod separator matched
action pre_separator {
// ';' pre separator matched

// it's a SEP, we need to record it's position
// (validate checks for too many SEP tokens)
out->data[out->length].tag = SEP;
// it's a PRE_SEP, we need to record it's position
// (validate checks for too many PRE_SEP tokens)
out->data[out->length].tag = PRE_SEP;
out->data[out->length].value = 0;
out->separator = out->length;

@@ -89,9 +86,22 @@ error_t scanner(const char *data, tele_command_t *out,
if (out->length >= COMMAND_MAX_LENGTH) return E_LENGTH;
}

action sub_separator {
// ':' mod separator matched
out->data[out->length].tag = SUB_SEP;
out->data[out->length].value = 0;

// increase the command length
out->length++;

// if the command length is now too long, abort
if (out->length >= COMMAND_MAX_LENGTH) return E_LENGTH;
}

main := |*
separator;
mod_separator => mod_separator;
pre_separator => pre_separator;
sub_seperator => sub_separator;
token => token;
*|;

0 comments on commit 09f9502

Please sign in to comment.