aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2015-11-21 19:59:55 -0500
committerFranklin Wei <git@fwei.tk>2015-11-21 19:59:55 -0500
commitf4e82feb2492b0bf466c3921b3e9fc9d056f3ea3 (patch)
treeba27876ec89f652e007f36d40eeb645a798acbf4
parentafc3e4b88e193025e49736e545ccb7765b19d1b5 (diff)
downloadducky-f4e82feb2492b0bf466c3921b3e9fc9d056f3ea3.zip
ducky-f4e82feb2492b0bf466c3921b3e9fc9d056f3ea3.tar.gz
ducky-f4e82feb2492b0bf466c3921b3e9fc9d056f3ea3.tar.bz2
ducky-f4e82feb2492b0bf466c3921b3e9fc9d056f3ea3.tar.xz
fix C transcompiler REPEAT
-rw-r--r--README.md49
-rw-r--r--src/emitc.c3
2 files changed, 50 insertions, 2 deletions
diff --git a/README.md b/README.md
index eba809a..c2e67fa 100644
--- a/README.md
+++ b/README.md
@@ -30,6 +30,31 @@ Simple!
IF iter < 20; GOTO loop_start
LOG Done!
+### Prime Counter
+
+ #!/bin/ducky
+ LOG Counting...
+ NEWLINE
+ LET primes=0
+ LET MAX=100000
+ LET n = 2
+ LBL test_number
+ LET iter = 2
+ LET stop = sqrt n
+ LBL test_loop
+ IF !(n % iter); GOTO composite
+ INC iter
+ IF iter <= stop; GOTO test_loop
+ INC primes
+ LBL composite
+ INC n
+ IF n < MAX; GOTO test_number
+ LOG Number of primes below
+ LOGVAR MAX
+ LOG :
+ LOGVAR primes
+ NEWLINE
+
## Building
POSIX systems are supported, as are some Rockbox targets.
@@ -57,10 +82,32 @@ This will create `a.out`, which contains the bytecode.
ducky a.out
+## Technical Details
+
+The program consists of four parts: the interpreter, compiler, bytecode interpreter, and C transcompiler.
+
+### Interpreter
+
+Executes ducky directly.
+
+
+### Bytecode Compiler
+
+Compiles ducky to a stack-machine based bytecode.
+
+### Bytecode Interpreter
+
+Executes the bytecode generated by the bytecode compiler.
+
+### C Transcompiler
+
+Translates bytecode generated by the bytecode compiler into C.
+Depends on labels as values, which is a GCC extension.
+
## Future Directions
- - Compile to C?
- Refactor code
+ - Input
- Add more builtins
- Arrays?
- Functions?
diff --git a/src/emitc.c b/src/emitc.c
index e200ce5..4e356d6 100644
--- a/src/emitc.c
+++ b/src/emitc.c
@@ -218,7 +218,7 @@ static void repeat_handler(void)
write_src("--repeats_left;\n");
write_src("if(repeats_left > 0)\n");
write_src("{\n");
- write_src("{JUMP(repeat_line);\n");
+ write_src("JUMP(repeat_line);\n");
write_src("}\n");
write_src("}\n");
write_src("else\n");
@@ -782,6 +782,7 @@ void write_stub_code(int num_lines)
write_src("imm_t stack[STACK_SZ];\n");
write_src("imm_t callstack[CALLSTACK_SZ];\n");
write_src("imm_t stack_pointer, callstack_pointer;\n");
+ write_src("unsigned repeats_left = 0, repeat_line = 0;\n");
write_src("struct var_t { imm_t val; bool constant; };\n");
write_src("struct var_t vars[MAX_VARS];\n\n");