aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2015-11-08 12:42:29 -0500
committerFranklin Wei <git@fwei.tk>2015-11-08 12:42:29 -0500
commit89e35bddfb92c6c37177d9830bf0664e9abb29d9 (patch)
tree1fb0cbfe650c124dda549e5ef5cf8e571e563df4 /src
parentacfc6dd899088fef88c075a11557b37d56f78d5d (diff)
downloadducky-89e35bddfb92c6c37177d9830bf0664e9abb29d9.zip
ducky-89e35bddfb92c6c37177d9830bf0664e9abb29d9.tar.gz
ducky-89e35bddfb92c6c37177d9830bf0664e9abb29d9.tar.bz2
ducky-89e35bddfb92c6c37177d9830bf0664e9abb29d9.tar.xz
stuff
Diffstat (limited to 'src')
-rw-r--r--src/ducky.c354
1 files changed, 187 insertions, 167 deletions
diff --git a/src/ducky.c b/src/ducky.c
index 712dd2c..e3b00b6 100644
--- a/src/ducky.c
+++ b/src/ducky.c
@@ -19,8 +19,10 @@
*
* "LET X=<EXPR>;" - loads the value of <EXPR> into variable X. Greedy.
*
- * "LOG ..." - outputs any remaining text to the device's screen. Greedy.
+ * "LOG ..." - outputs any remaining text to the device's screen.
* "LOGVAR <EXPR>;" - outputs variable <EXPR> in decimal to the device's screen
+ * "NEWLINE" - outputs a newline
+ * "LOGCHAR <EXPR>;" - outputs <EXPR> as an ASCII character
******************************************************************************/
/*** Defines ***/
@@ -35,14 +37,14 @@
#define CALL_STACK_SZ 64
#define VARMAP_SIZE 256
-#define VARFORMAT "%d"
+#define VARFORMAT "%lld"
#define VARNAME_MAX 24
#define ARRAYLEN(x) (sizeof(x)/sizeof(x[0]))
#define MIN(x,y) ((x<y)?(x):(y))
-typedef int vartype;
+typedef long long int vartype;
/*** Globals ***/
@@ -88,7 +90,6 @@ uint32_t var_hash(const char *str)
struct varnode_t *lookup_var(const char *name)
{
- vid_writef("lookup up variable %s", name);
uint32_t hash = var_hash(name) % VARMAP_SIZE;
struct varnode_t *iter = var_map[hash];
@@ -181,7 +182,7 @@ void exit_handler(void)
void vid_write(const char *str)
{
- puts(str);
+ printf("%s", str);
}
void __attribute__((format(printf,1,2))) vid_writef(const char *fmt, ...)
@@ -204,7 +205,7 @@ void __attribute__((noreturn,format(printf,1,2))) error(const char *fmt, ...)
vsnprintf(fmtbuf, sizeof(fmtbuf), fmt, ap);
if(current_line)
vid_writef("Line %d: ", current_line);
- vid_writef("ERROR: %s", fmtbuf);
+ vid_writef("ERROR: %s\n", fmtbuf);
va_end(ap);
exit(EXIT_FAILURE);
@@ -217,7 +218,7 @@ void __attribute__((format(printf,1,2))) warning(const char *fmt, ...)
va_list ap;
va_start(ap, fmt);
vsnprintf(fmtbuf, sizeof(fmtbuf), fmt, ap);
- vid_writef("Line %d: WARNING: %s", current_line, fmtbuf);
+ vid_writef("Line %d: WARNING: %s\n", current_line, fmtbuf);
va_end(ap);
}
@@ -255,8 +256,6 @@ int read_line(int fd, char *buf, size_t sz)
off_t *index_lines(int fd, unsigned *numlines)
{
- vid_write("Indexing file...");
-
size_t sz = sizeof(off_t);
off_t *data = malloc(sz);
@@ -818,7 +817,6 @@ vartype eval_expr(char *str)
error("invalid expression");
}
- vid_writef("eval_expr %s = %d", str, numstack[0]);
return numstack[0];
}
@@ -968,12 +966,11 @@ int logvar_handler(char **save, int *repeats_left)
char *tok = strtok_r(NULL, ";", save);
if(tok)
{
- vid_writef("%d", eval_expr(tok));
+ vid_writef(VARFORMAT, eval_expr(tok));
return OK;
}
else
error("expected expression after LOGVAR");
-
}
int rem_handler(char **save, int *repeats_left)
@@ -988,168 +985,182 @@ int quit_handler(char **save, int *repeats_left)
return DONE;
}
+int newline_handler(char **save, int *repeats_left)
+{
+ (void) save; (void) repeats_left;
+ vid_write("\n");
+ return OK;
+}
+
+int logchar_handler(char **save, int *repeats_left)
+{
+ (void) repeats_left;
+ char *tok = strtok_r(NULL, ";", save);
+ if(tok)
+ {
+ vid_writef("%c", eval_expr(tok));
+ return OK;
+ }
+ else
+ error("expected expression after LOGCHAR");
+
+}
+
struct token_t {
const char *tok;
int (*func)(char **save, int *repeats_left);
- bool allow_trailing;
- size_t len;
} tokens[] = {
- { "LET", let_handler, },
- { "REPEAT", repeat_handler, },
- { "JUMP", goto_handler, },
- { "GOTO", goto_handler, },
- { "CALL", call_handler, },
- { "GOSUB", call_handler, },
- { "RET", ret_handler, },
- { "RETURN", ret_handler, },
- { "INC", inc_handler, },
- { "DEC", dec_handler, },
- { "IF", if_handler, },
- { "DELAY", delay_handler, },
- { "LOG", log_handler, },
- { "LOGVAR", logvar_handler, },
- { "REM", rem_handler, },
- { "//", rem_handler, },
- { "#", rem_handler, },
- { "LABEL", rem_handler, },
- { "LBL", rem_handler, },
- { "QUIT", quit_handler, },
- { "EXIT", quit_handler, },
+ { "LET", let_handler, },
+ { "REPEAT", repeat_handler, },
+ { "JUMP", goto_handler, },
+ { "GOTO", goto_handler, },
+ { "CALL", call_handler, },
+ { "GOSUB", call_handler, },
+ { "RET", ret_handler, },
+ { "RETURN", ret_handler, },
+ { "INC", inc_handler, },
+ { "DEC", dec_handler, },
+ { "IF", if_handler, },
+ { "DELAY", delay_handler, },
+ { "LOG", log_handler, },
+ { "LOGVAR", logvar_handler, },
+ { "LOGCHAR", logchar_handler },
+ { "NEWLINE", newline_handler, },
+ { "REM", rem_handler, },
+ { "//", rem_handler, },
+ { "LABEL", rem_handler, },
+ { "LBL", rem_handler, },
+ { "QUIT", quit_handler, },
+ { "EXIT", quit_handler, },
};
/* once again, this lookup table is implemented with a perfect hash map */
-#define TOKMAP_SIZE 21
+#define TOKMAP_SIZE ARRAYLEN(tokens)
struct token_t tokmap[TOKMAP_SIZE];
/* auto-generated with mph-1.2 */
/*
* d=3
- * n=26
- * m=21
+ * n=28
+ * m=22
* c=1.23
- * maxlen=6
- * minklen=1
- * maxklen=6
- * minchar=35
+ * maxlen=7
+ * minklen=2
+ * maxklen=7
+ * minchar=47
* maxchar=89
* loop=0
- * numiter=113
+ * numiter=200
* seed=
*/
static int g[] = {
- 5, 8, 20, 18, 19, 0, 7, 5, 14, 10,
- 13, 0, 20, 2, 18, 19, 10, 5, 15, 0,
- 4, 1, 0, 0, 15, 16,
+ 19, 12, -1, 15, 17, 1, 4, 4, 0, 13,
+ 10, 19, 18, -1, 14, 19, 19, 21, 19, 0,
+ 7, 0, 15, 18, 4, 0, 4, 3,
};
static int T0[] = {
- 0x05, 0x02, 0x10, 0x18, 0x10, 0x09, 0x0b, 0x09, 0x02, 0x00,
- 0x0f, 0x01, 0x04, 0x0c, 0x02, 0x16, 0x09, 0x09, 0x08, 0x03,
- 0x13, 0x06, 0x13, 0x00, 0x17, 0x16, 0x08, 0x11, 0x0b, 0x02,
- 0x03, 0x10, 0x06, 0x16, 0x10, 0x17, 0x05, 0x03, 0x08, 0x08,
- 0x04, 0x19, 0x0b, 0x0a, 0x0c, 0x0e, 0x06, 0x17, 0x19, 0x11,
- 0x02, 0x14, 0x19, 0x17, 0x14, 0x14, 0x08, 0x02, 0x03, 0x0d,
- 0x09, 0x16, 0x11, 0x05, 0x0c, 0x0e, 0x13, 0x08, 0x0b, 0x0c,
- 0x0a, 0x0a, 0x05, 0x17, 0x0b, 0x14, 0x16, 0x04, 0x03, 0x0f,
- 0x09, 0x07, 0x11, 0x05, 0x06, 0x0d, 0x19, 0x10, 0x11, 0x04,
- 0x06, 0x00, 0x00, 0x17, 0x06, 0x0c, 0x0d, 0x01, 0x17, 0x18,
- 0x10, 0x09, 0x0b, 0x15, 0x06, 0x16, 0x11, 0x02, 0x02, 0x14,
- 0x15, 0x04, 0x07, 0x02, 0x0f, 0x09, 0x14, 0x14, 0x06, 0x12,
- 0x02, 0x17, 0x18, 0x0a, 0x08, 0x07, 0x0f, 0x0e, 0x11, 0x04,
- 0x0b, 0x12, 0x0c, 0x08, 0x02, 0x08, 0x18, 0x15, 0x09, 0x13,
- 0x16, 0x04, 0x19, 0x05, 0x08, 0x0e, 0x11, 0x02, 0x08, 0x17,
- 0x15, 0x0c, 0x16, 0x13, 0x17, 0x07, 0x03, 0x0c, 0x17, 0x14,
- 0x10, 0x08, 0x0d, 0x05, 0x12, 0x03, 0x03, 0x11, 0x18, 0x0a,
- 0x14, 0x09, 0x09, 0x12, 0x05, 0x14, 0x0b, 0x01, 0x0e, 0x16,
- 0x15, 0x02, 0x0e, 0x01, 0x0a, 0x16, 0x18, 0x06, 0x00, 0x17,
- 0x18, 0x0d, 0x05, 0x12, 0x0f, 0x10, 0x15, 0x14, 0x07, 0x15,
- 0x04, 0x03, 0x05, 0x10, 0x16, 0x0a, 0x0a, 0x09, 0x0e, 0x18,
- 0x07, 0x09, 0x02, 0x15, 0x0a, 0x0d, 0x13, 0x08, 0x13, 0x13,
- 0x07, 0x08, 0x14, 0x19, 0x15, 0x0b, 0x16, 0x18, 0x0b, 0x14,
- 0x08, 0x12, 0x13, 0x0b, 0x11, 0x03, 0x11, 0x0b, 0x04, 0x12,
- 0x0b, 0x0a, 0x12, 0x0b, 0x07, 0x00, 0x10, 0x15, 0x06, 0x08,
- 0x12, 0x0d, 0x12, 0x0d, 0x0f, 0x0d, 0x18, 0x0b, 0x0d, 0x0b,
- 0x07, 0x15, 0x05, 0x02, 0x07, 0x17, 0x07, 0x18, 0x08, 0x0c,
- 0x12, 0x13, 0x16, 0x0c, 0x07, 0x13, 0x19, 0x13, 0x11, 0x14,
- 0x0e, 0x05, 0x16, 0x0f, 0x13, 0x03, 0x0b, 0x09, 0x16, 0x00,
- 0x11, 0x06, 0x15, 0x0a, 0x0f, 0x0d, 0x06, 0x04, 0x11, 0x19,
- 0x01, 0x03, 0x01, 0x0e, 0x10, 0x0d, 0x09, 0x11, 0x06, 0x01,
- 0x0d, 0x14, 0x06, 0x0b, 0x0c, 0x02, 0x0e, 0x19, 0x0d, 0x0d,
- 0x00, 0x04, 0x13, 0x17, 0x11, 0x08, 0x0d, 0x17, 0x0c, 0x04,
+ 0x15, 0x0c, 0x1a, 0x17, 0x16, 0x02, 0x16, 0x07, 0x17, 0x09,
+ 0x0f, 0x0f, 0x0f, 0x17, 0x07, 0x0c, 0x1a, 0x08, 0x17, 0x04,
+ 0x14, 0x07, 0x06, 0x07, 0x10, 0x0e, 0x0e, 0x18, 0x01, 0x08,
+ 0x1b, 0x17, 0x14, 0x19, 0x12, 0x1a, 0x0b, 0x0c, 0x12, 0x07,
+ 0x05, 0x05, 0x06, 0x13, 0x06, 0x19, 0x07, 0x0d, 0x01, 0x0c,
+ 0x1a, 0x09, 0x15, 0x1b, 0x16, 0x03, 0x15, 0x19, 0x12, 0x07,
+ 0x1b, 0x05, 0x08, 0x0a, 0x15, 0x05, 0x16, 0x0b, 0x0a, 0x00,
+ 0x0e, 0x0d, 0x08, 0x0a, 0x04, 0x1b, 0x07, 0x17, 0x0c, 0x15,
+ 0x07, 0x0a, 0x02, 0x0c, 0x15, 0x08, 0x0d, 0x12, 0x10, 0x00,
+ 0x07, 0x13, 0x15, 0x10, 0x17, 0x0a, 0x0e, 0x01, 0x14, 0x14,
+ 0x1a, 0x06, 0x15, 0x05, 0x0a, 0x14, 0x03, 0x12, 0x04, 0x0c,
+ 0x11, 0x1b, 0x0e, 0x1b, 0x17, 0x03, 0x08, 0x09, 0x15, 0x08,
+ 0x15, 0x0c, 0x1b, 0x0e, 0x00, 0x16, 0x08, 0x1b, 0x08, 0x10,
+ 0x0a, 0x00, 0x09, 0x01, 0x00, 0x09, 0x13, 0x03, 0x19, 0x03,
+ 0x05, 0x15, 0x10, 0x06, 0x13, 0x02, 0x19, 0x15, 0x13, 0x12,
+ 0x18, 0x16, 0x02, 0x0b, 0x0a, 0x1b, 0x0a, 0x15, 0x08, 0x06,
+ 0x09, 0x12, 0x06, 0x12, 0x14, 0x12, 0x0c, 0x17, 0x15, 0x09,
+ 0x1a, 0x0b, 0x05, 0x00, 0x1b, 0x0c, 0x10, 0x09, 0x02, 0x17,
+ 0x11, 0x0a, 0x15, 0x11, 0x18, 0x12, 0x16, 0x00, 0x0e, 0x10,
+ 0x18, 0x11, 0x0b, 0x07, 0x0b, 0x1a, 0x1b, 0x07, 0x07, 0x15,
+ 0x0a, 0x16, 0x1a, 0x0f, 0x06, 0x19, 0x0c, 0x17, 0x06, 0x0e,
+ 0x12, 0x08, 0x18, 0x17, 0x09, 0x0c, 0x03, 0x1b, 0x15, 0x0e,
+ 0x06, 0x01, 0x11, 0x0e, 0x04, 0x08, 0x05, 0x1b, 0x05, 0x03,
+ 0x15, 0x04, 0x17, 0x10, 0x0e, 0x07, 0x0e, 0x14, 0x13, 0x17,
+ 0x14, 0x1b, 0x0a, 0x07, 0x18, 0x04, 0x03, 0x00, 0x10, 0x19,
+ 0x1a, 0x16, 0x0a, 0x0f, 0x14, 0x1a, 0x18, 0x19, 0x0c, 0x1b,
+ 0x06, 0x0a, 0x03, 0x0b, 0x0e, 0x1b, 0x1b, 0x0d, 0x03, 0x0d,
+ 0x19, 0x0a, 0x0f, 0x14, 0x14, 0x06, 0x00, 0x01, 0x10, 0x0d,
+ 0x19, 0x18, 0x09, 0x1b, 0x17, 0x16, 0x14, 0x15, 0x0b, 0x10,
+ 0x14, 0x02, 0x1b, 0x08, 0x0d, 0x0d, 0x13, 0x0c, 0x0a, 0x16,
+ 0x09,
};
static int T1[] = {
- 0x16, 0x10, 0x05, 0x09, 0x08, 0x14, 0x02, 0x00, 0x01, 0x11,
- 0x05, 0x16, 0x0f, 0x06, 0x14, 0x0b, 0x05, 0x02, 0x05, 0x13,
- 0x15, 0x02, 0x11, 0x0c, 0x0a, 0x13, 0x05, 0x08, 0x0b, 0x06,
- 0x0d, 0x09, 0x18, 0x14, 0x15, 0x07, 0x0f, 0x17, 0x09, 0x12,
- 0x10, 0x11, 0x0f, 0x07, 0x19, 0x0b, 0x12, 0x04, 0x0f, 0x17,
- 0x19, 0x0c, 0x02, 0x11, 0x18, 0x0b, 0x19, 0x01, 0x15, 0x12,
- 0x04, 0x0c, 0x02, 0x10, 0x14, 0x03, 0x14, 0x0a, 0x19, 0x09,
- 0x07, 0x13, 0x0c, 0x03, 0x01, 0x11, 0x18, 0x0c, 0x06, 0x07,
- 0x01, 0x00, 0x12, 0x13, 0x08, 0x16, 0x04, 0x07, 0x17, 0x01,
- 0x01, 0x03, 0x0e, 0x05, 0x13, 0x08, 0x09, 0x10, 0x14, 0x0a,
- 0x01, 0x01, 0x05, 0x0f, 0x04, 0x08, 0x06, 0x04, 0x16, 0x0d,
- 0x0d, 0x02, 0x12, 0x01, 0x01, 0x0c, 0x17, 0x08, 0x19, 0x18,
- 0x15, 0x17, 0x0c, 0x11, 0x01, 0x00, 0x08, 0x00, 0x03, 0x02,
- 0x16, 0x19, 0x14, 0x11, 0x10, 0x16, 0x06, 0x07, 0x0d, 0x05,
- 0x0c, 0x00, 0x07, 0x04, 0x03, 0x0a, 0x12, 0x03, 0x12, 0x11,
- 0x01, 0x10, 0x11, 0x0f, 0x09, 0x14, 0x11, 0x11, 0x15, 0x14,
- 0x16, 0x13, 0x16, 0x10, 0x0a, 0x0f, 0x02, 0x0d, 0x06, 0x0b,
- 0x01, 0x06, 0x19, 0x05, 0x18, 0x04, 0x09, 0x0a, 0x03, 0x0d,
- 0x02, 0x0e, 0x14, 0x18, 0x0f, 0x0e, 0x07, 0x0f, 0x15, 0x0c,
- 0x15, 0x09, 0x12, 0x07, 0x0e, 0x0b, 0x16, 0x12, 0x00, 0x03,
- 0x04, 0x02, 0x0b, 0x05, 0x09, 0x0b, 0x0b, 0x14, 0x15, 0x0e,
- 0x08, 0x19, 0x05, 0x04, 0x00, 0x16, 0x15, 0x09, 0x0b, 0x12,
- 0x17, 0x18, 0x0c, 0x03, 0x0d, 0x14, 0x0e, 0x05, 0x09, 0x0f,
- 0x0a, 0x02, 0x12, 0x08, 0x0e, 0x14, 0x0c, 0x08, 0x07, 0x12,
- 0x17, 0x09, 0x01, 0x00, 0x07, 0x11, 0x00, 0x00, 0x04, 0x0c,
- 0x0d, 0x03, 0x0a, 0x01, 0x08, 0x00, 0x15, 0x16, 0x05, 0x04,
- 0x0d, 0x11, 0x07, 0x05, 0x19, 0x17, 0x00, 0x0d, 0x05, 0x09,
- 0x08, 0x04, 0x12, 0x0b, 0x05, 0x15, 0x0d, 0x0e, 0x19, 0x0a,
- 0x0a, 0x03, 0x10, 0x03, 0x02, 0x0d, 0x0c, 0x03, 0x02, 0x09,
- 0x09, 0x03, 0x01, 0x08, 0x03, 0x01, 0x07, 0x17, 0x00, 0x15,
- 0x00, 0x05, 0x03, 0x16, 0x01, 0x09, 0x11, 0x10, 0x19, 0x12,
- 0x01, 0x09, 0x15, 0x11, 0x0c, 0x18, 0x06, 0x01, 0x01, 0x08,
- 0x0a, 0x0c, 0x0e, 0x0c, 0x16, 0x11, 0x0f, 0x05, 0x0e, 0x11,
+ 0x13, 0x0e, 0x17, 0x12, 0x13, 0x12, 0x08, 0x19, 0x05, 0x0d,
+ 0x0b, 0x07, 0x06, 0x05, 0x0b, 0x0f, 0x06, 0x07, 0x12, 0x06,
+ 0x11, 0x0d, 0x08, 0x10, 0x18, 0x1b, 0x18, 0x12, 0x03, 0x00,
+ 0x00, 0x16, 0x1a, 0x07, 0x18, 0x11, 0x19, 0x10, 0x1a, 0x0e,
+ 0x0e, 0x16, 0x16, 0x02, 0x10, 0x0d, 0x11, 0x0a, 0x02, 0x14,
+ 0x00, 0x0c, 0x1a, 0x1a, 0x08, 0x02, 0x01, 0x06, 0x0b, 0x0b,
+ 0x06, 0x02, 0x13, 0x15, 0x0f, 0x11, 0x0d, 0x01, 0x04, 0x0c,
+ 0x0b, 0x13, 0x02, 0x11, 0x06, 0x12, 0x0e, 0x07, 0x01, 0x10,
+ 0x1b, 0x01, 0x01, 0x0a, 0x0b, 0x09, 0x0c, 0x13, 0x11, 0x0a,
+ 0x03, 0x11, 0x02, 0x13, 0x01, 0x08, 0x0c, 0x0a, 0x06, 0x0e,
+ 0x00, 0x13, 0x1b, 0x03, 0x12, 0x01, 0x01, 0x0e, 0x0d, 0x09,
+ 0x11, 0x1b, 0x08, 0x05, 0x0b, 0x0c, 0x02, 0x08, 0x10, 0x13,
+ 0x02, 0x13, 0x14, 0x04, 0x16, 0x15, 0x18, 0x06, 0x10, 0x13,
+ 0x1a, 0x01, 0x03, 0x16, 0x17, 0x12, 0x1b, 0x10, 0x0f, 0x0f,
+ 0x06, 0x0f, 0x0f, 0x1a, 0x10, 0x17, 0x18, 0x09, 0x06, 0x10,
+ 0x01, 0x03, 0x06, 0x0d, 0x03, 0x0d, 0x0d, 0x0f, 0x16, 0x09,
+ 0x13, 0x14, 0x0b, 0x16, 0x1a, 0x12, 0x0d, 0x1a, 0x06, 0x00,
+ 0x19, 0x18, 0x17, 0x18, 0x1b, 0x10, 0x0d, 0x14, 0x17, 0x16,
+ 0x0a, 0x04, 0x0e, 0x03, 0x10, 0x1a, 0x01, 0x10, 0x19, 0x04,
+ 0x09, 0x0f, 0x08, 0x0b, 0x1a, 0x0f, 0x0f, 0x09, 0x09, 0x1b,
+ 0x18, 0x08, 0x16, 0x03, 0x10, 0x05, 0x14, 0x02, 0x19, 0x0f,
+ 0x18, 0x13, 0x03, 0x16, 0x06, 0x1b, 0x01, 0x0f, 0x19, 0x0d,
+ 0x00, 0x0a, 0x11, 0x0f, 0x0d, 0x0e, 0x08, 0x10, 0x1b, 0x0c,
+ 0x1b, 0x19, 0x08, 0x17, 0x0c, 0x1b, 0x0a, 0x12, 0x0d, 0x0f,
+ 0x0a, 0x14, 0x04, 0x0f, 0x0b, 0x05, 0x0f, 0x18, 0x04, 0x18,
+ 0x09, 0x05, 0x06, 0x1b, 0x04, 0x13, 0x19, 0x0c, 0x1b, 0x0c,
+ 0x18, 0x19, 0x08, 0x0e, 0x11, 0x0b, 0x03, 0x16, 0x1b, 0x15,
+ 0x11, 0x14, 0x09, 0x09, 0x17, 0x0e, 0x12, 0x1a, 0x14, 0x12,
+ 0x19, 0x08, 0x16, 0x07, 0x12, 0x0a, 0x17, 0x14, 0x13, 0x06,
+ 0x10, 0x0f, 0x03, 0x18, 0x0d, 0x04, 0x13, 0x10, 0x1b, 0x03,
+ 0x09,
};
static int T2[] = {
- 0x19, 0x06, 0x18, 0x03, 0x02, 0x11, 0x12, 0x06, 0x0a, 0x0e,
- 0x0b, 0x10, 0x0e, 0x19, 0x09, 0x0f, 0x12, 0x0c, 0x19, 0x18,
- 0x08, 0x18, 0x07, 0x02, 0x11, 0x07, 0x15, 0x18, 0x19, 0x0f,
- 0x06, 0x00, 0x15, 0x05, 0x03, 0x00, 0x18, 0x17, 0x06, 0x0b,
- 0x0c, 0x11, 0x01, 0x00, 0x12, 0x0c, 0x11, 0x0c, 0x18, 0x10,
- 0x0a, 0x06, 0x11, 0x11, 0x0b, 0x19, 0x0a, 0x00, 0x08, 0x10,
- 0x19, 0x03, 0x05, 0x01, 0x15, 0x14, 0x06, 0x08, 0x03, 0x00,
- 0x18, 0x14, 0x03, 0x0b, 0x11, 0x11, 0x16, 0x14, 0x0d, 0x14,
- 0x13, 0x19, 0x01, 0x16, 0x0b, 0x07, 0x17, 0x18, 0x07, 0x06,
- 0x10, 0x08, 0x09, 0x18, 0x09, 0x06, 0x12, 0x12, 0x10, 0x15,
- 0x12, 0x10, 0x11, 0x15, 0x04, 0x08, 0x0d, 0x00, 0x05, 0x02,
- 0x05, 0x04, 0x04, 0x12, 0x17, 0x08, 0x0b, 0x06, 0x05, 0x08,
- 0x0e, 0x16, 0x04, 0x0b, 0x00, 0x14, 0x02, 0x0d, 0x10, 0x0e,
- 0x12, 0x17, 0x11, 0x09, 0x00, 0x14, 0x16, 0x01, 0x0d, 0x03,
- 0x18, 0x12, 0x07, 0x02, 0x0c, 0x06, 0x0c, 0x17, 0x0c, 0x12,
- 0x07, 0x03, 0x10, 0x0b, 0x10, 0x10, 0x06, 0x12, 0x05, 0x18,
- 0x07, 0x17, 0x15, 0x18, 0x08, 0x06, 0x04, 0x04, 0x02, 0x13,
- 0x18, 0x0a, 0x17, 0x01, 0x02, 0x0d, 0x12, 0x17, 0x0b, 0x0f,
- 0x0f, 0x12, 0x0e, 0x09, 0x05, 0x10, 0x12, 0x0e, 0x07, 0x13,
- 0x13, 0x03, 0x01, 0x18, 0x00, 0x0c, 0x04, 0x04, 0x13, 0x06,
- 0x17, 0x13, 0x10, 0x16, 0x15, 0x14, 0x0c, 0x0f, 0x11, 0x17,
- 0x04, 0x09, 0x12, 0x15, 0x14, 0x19, 0x0b, 0x0c, 0x0d, 0x14,
- 0x15, 0x05, 0x0d, 0x15, 0x0b, 0x11, 0x0f, 0x10, 0x04, 0x10,
- 0x02, 0x03, 0x02, 0x0c, 0x11, 0x00, 0x04, 0x06, 0x19, 0x10,
- 0x0e, 0x0b, 0x0c, 0x11, 0x07, 0x02, 0x02, 0x10, 0x13, 0x11,
- 0x08, 0x0e, 0x16, 0x18, 0x0b, 0x09, 0x11, 0x02, 0x19, 0x16,
- 0x12, 0x04, 0x01, 0x14, 0x10, 0x14, 0x16, 0x16, 0x00, 0x16,
- 0x0f, 0x10, 0x09, 0x01, 0x08, 0x08, 0x0a, 0x18, 0x11, 0x0f,
- 0x01, 0x12, 0x16, 0x0e, 0x15, 0x12, 0x10, 0x0c, 0x19, 0x01,
- 0x02, 0x06, 0x13, 0x16, 0x01, 0x0f, 0x17, 0x04, 0x12, 0x12,
- 0x17, 0x04, 0x09, 0x0f, 0x06, 0x19, 0x17, 0x10, 0x17, 0x10,
- 0x07, 0x18, 0x09, 0x06, 0x0f, 0x04, 0x18, 0x05, 0x12, 0x00,
- 0x08, 0x15, 0x06, 0x02, 0x11, 0x07, 0x13, 0x10, 0x0d, 0x0d,
+ 0x16, 0x04, 0x18, 0x10, 0x13, 0x0f, 0x08, 0x19, 0x19, 0x17,
+ 0x13, 0x0b, 0x0b, 0x08, 0x0a, 0x08, 0x01, 0x05, 0x15, 0x1a,
+ 0x11, 0x02, 0x16, 0x0f, 0x0d, 0x09, 0x16, 0x13, 0x17, 0x0d,
+ 0x05, 0x11, 0x11, 0x0d, 0x05, 0x14, 0x01, 0x19, 0x12, 0x0a,
+ 0x15, 0x15, 0x15, 0x17, 0x13, 0x15, 0x0e, 0x18, 0x11, 0x0f,
+ 0x03, 0x04, 0x03, 0x09, 0x1a, 0x13, 0x04, 0x08, 0x09, 0x07,
+ 0x15, 0x16, 0x05, 0x11, 0x02, 0x00, 0x15, 0x10, 0x1b, 0x0d,
+ 0x14, 0x18, 0x10, 0x0f, 0x13, 0x13, 0x14, 0x11, 0x10, 0x0a,
+ 0x05, 0x03, 0x1a, 0x14, 0x0d, 0x19, 0x05, 0x1a, 0x1a, 0x00,
+ 0x06, 0x0c, 0x07, 0x11, 0x01, 0x18, 0x12, 0x06, 0x02, 0x06,
+ 0x1a, 0x17, 0x03, 0x0d, 0x09, 0x02, 0x09, 0x19, 0x03, 0x01,
+ 0x16, 0x0d, 0x17, 0x10, 0x01, 0x06, 0x18, 0x06, 0x10, 0x16,
+ 0x07, 0x06, 0x12, 0x1a, 0x17, 0x04, 0x16, 0x19, 0x0a, 0x1b,
+ 0x15, 0x16, 0x13, 0x11, 0x09, 0x1b, 0x02, 0x05, 0x12, 0x0c,
+ 0x10, 0x11, 0x08, 0x0f, 0x0c, 0x05, 0x0d, 0x0f, 0x19, 0x01,
+ 0x1b, 0x17, 0x03, 0x08, 0x00, 0x1a, 0x0d, 0x0c, 0x07, 0x19,
+ 0x17, 0x01, 0x03, 0x0e, 0x02, 0x18, 0x19, 0x10, 0x02, 0x00,
+ 0x0c, 0x02, 0x0a, 0x0b, 0x1a, 0x11, 0x19, 0x01, 0x16, 0x02,
+ 0x09, 0x01, 0x10, 0x1a, 0x0c, 0x0c, 0x12, 0x10, 0x18, 0x1b,
+ 0x14, 0x17, 0x17, 0x12, 0x1b, 0x08, 0x0d, 0x0c, 0x07, 0x1a,
+ 0x04, 0x03, 0x0b, 0x0e, 0x1b, 0x16, 0x10, 0x08, 0x17, 0x16,
+ 0x16, 0x10, 0x17, 0x0a, 0x1a, 0x0c, 0x17, 0x0f, 0x0a, 0x19,
+ 0x00, 0x0c, 0x08, 0x11, 0x17, 0x09, 0x17, 0x0f, 0x00, 0x05,
+ 0x17, 0x05, 0x05, 0x17, 0x17, 0x15, 0x0f, 0x00, 0x00, 0x19,
+ 0x0c, 0x19, 0x0f, 0x11, 0x08, 0x18, 0x01, 0x10, 0x17, 0x17,
+ 0x0d, 0x18, 0x13, 0x15, 0x0d, 0x0e, 0x0e, 0x09, 0x05, 0x17,
+ 0x14, 0x17, 0x16, 0x16, 0x1b, 0x10, 0x10, 0x0c, 0x06, 0x02,
+ 0x17, 0x12, 0x05, 0x08, 0x12, 0x00, 0x0b, 0x17, 0x07, 0x12,
+ 0x08, 0x01, 0x18, 0x0a, 0x19, 0x16, 0x01, 0x0b, 0x12, 0x12,
+ 0x06, 0x0a, 0x0e, 0x0c, 0x10, 0x0d, 0x00, 0x04, 0x19, 0x12,
+ 0x06,
};
#define uchar unsigned char
@@ -1161,32 +1172,32 @@ tok_hash(const uchar *key)
unsigned f0, f1, f2;
const uchar *kp = key;
- for (i=-35, f0=f1=f2=0; *kp; ++kp) {
- if (*kp < 35 || *kp > 89)
+ for (i=-47, f0=f1=f2=0; *kp; ++kp) {
+ if (*kp < 47 || *kp > 89)
return -1;
- if (kp-key > 5)
+ if (kp-key > 6)
return -1;
f0 += T0[i + *kp];
f1 += T1[i + *kp];
f2 += T2[i + *kp];
- i += 55;
+ i += 43;
}
- if (kp-key < 1)
+ if (kp-key < 2)
return -1;
- f0 %= 26;
- f1 %= 26;
- f2 %= 26;
+ f0 %= 28;
+ f1 %= 28;
+ f2 %= 28;
- return (g[f0] + g[f1] + g[f2]) % 21;
+ return (g[f0] + g[f1] + g[f2]) % 22;
}
void tokmap_insert(struct token_t *tok)
{
uint32_t hash = tok_hash(tok->tok) % TOKMAP_SIZE;
- if(tokmap[hash].tok)
- error("hash map collision %lu: %s VS %s", hash, tok->tok, tokmap[hash].tok);
+ if(hash < 0 || tokmap[hash].tok)
+ error("FIXME: hash map collision");
memcpy(tokmap+hash, tok, sizeof(*tok));
}
@@ -1195,7 +1206,6 @@ void init_tokmap(void)
memset(tokmap, 0, sizeof(tokmap));
for(unsigned int i = 0; i < ARRAYLEN(tokens); ++i)
{
- tokens[i].len = strlen(tokens[i].tok);
tokmap_insert(tokens+i);
}
}
@@ -1211,13 +1221,16 @@ void init_globals(void)
memset(var_map, 0, sizeof(var_map));
}
-void ducky_main(int fd)
+void ducky_main(int fd, bool verbose)
{
init_globals();
- vid_write("*** DS-2 INIT ***");
- vid_write("QUACK AT YOUR OWN RISK!");
- vid_write("The author assumes no liability for any damages caused by this program.");
+ if(verbose)
+ {
+ vid_write("*** DS-2 INIT ***");
+ vid_write("QUACK AT YOUR OWN RISK!");
+ vid_write("The author assumes no liability for any damages caused by this program.");
+ }
file_des = fd;
@@ -1243,10 +1256,12 @@ void ducky_main(int fd)
setConst("false", true);
line_offset = index_lines(file_des, &num_lines);
- vid_writef("Indexing complete (%u lines).", num_lines);
-
- vid_write("Executing...");
+ if(verbose)
+ {
+ vid_writef("Indexing complete (%u lines).", num_lines);
+ vid_write("Executing...");
+ }
int repeats_left = 0;
while(1)
@@ -1255,7 +1270,8 @@ void ducky_main(int fd)
memset(instr_buf, 0, sizeof(instr_buf));
if(read_line(file_des, instr_buf, sizeof(instr_buf)) <= 0)
{
- vid_writef("end of file");
+ if(verbose)
+ vid_writef("end of file");
goto done;
}
char *tok = NULL, *save = NULL;
@@ -1288,11 +1304,11 @@ void ducky_main(int fd)
case NEXT:
goto next_line;
default:
- error("unknown return");
+ error("FIXME: invalid return value");
}
else if(tok[0] != '#')
{
- error("unknown token `%s` on line %d %d, %d", tok, current_line, hash, tok_hash("CTRL"));
+ error("unknown token `%s` on line %d %d", tok, current_line);
goto done;
}
} while(tok);
@@ -1304,7 +1320,11 @@ void ducky_main(int fd)
if(repeats_left)
jump_line(file_des, current_line);
else
+ {
+ if(current_line + 2 > num_lines)
+ goto done;
jump_line(file_des, current_line + 2);
+ }
}
next_line:
;