blob: 26219c95acdada771f2ffbc8186fb74ace79e96d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
Ducky
=====
IRC: #ducky-lang at `irc.freenode.net`
## Introduction
Ducky is a BASIC-like programming language originally insipred by Duckyscript, the USB Rubber Ducky's scripting language.
## Examples
### Hello World
#!/bin/ducky
LOG Hello, world!
NEWLINE
Simple!
### Fibonacci Sequence
#!/bin/ducky
LET a = 0; LET b = 0; LET iter = 0
LABEL loop_start
LOGVAR a
NEWLINE
LET c = a+b
LET a = b
LET b = c
INC iter
IF iter < 20; GOTO loop_start
LOG Done!
NEWLINE
### 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.
### Unix/Linux
make
sudo make install
This installs the `/bin/ducky` binary.
## Usage
### Running Directly (no compilation)
ducky scriptname.ds
### Compiling to bytecode
ducky -c scriptname.ds
This will create `a.out`, which contains the bytecode.
### Executing bytecode
ducky a.out
### Full Compile (from ducky to machine code)
ducky -a scriptname.ds
./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.
## Benchmark Results
21 Nov 2015:
c - clang 26 27 27 27 27 27 27 27 27 27 - AVG 26.9
c - gcc 29 29 29 29 29 29 29 29 29 29 - AVG 29
c - tcc 28 28 28 28 28 28 28 28 28 28 - AVG 28
ducky - clang 15 15 15 15 15 15 15 15 15 15 - AVG 15
ducky - gcc 28 28 28 28 28 28 28 28 28 26 - AVG 27.8
26 Nov 2015:
<table>
<tr><th>Language</th><th>Compiler</th><th>Optimization Level</th><th>Scores</th><th>Mean score</th></tr>
<tr><td>Ducky</td><td>Clang</td><td>-O0</td><td>4 4 4 4 4 4 4 4 4 4</td> <td>4</td></tr>
<tr><td>Ducky</td><td>Clang</td><td>-O1</td><td>15 15 15 15 15 15 15 15 15 15</td><td>15</td></tr>
<tr><td>Ducky</td><td>Clang</td><td>-O2</td><td>21 21 21 21 21 21 21 21 21 21</td><td>21</td></tr>
<tr><td>Ducky</td><td>Clang</td><td>-O3</td><td>21 21 21 21 21 21 21 21 21 21</td><td>21</td></tr>
<tr><td>Ducky</td><td>GCC</td><td>-O0</td><td>5 5 4 5 5 5 5 5 5 5</td> <td>4.9</td></tr>
<tr><td>Ducky</td><td>GCC</td><td>-O1</td><td>22 23 23 23 23 23 23 23 23 23</td><td>22.9</td></tr>
<tr><td>Ducky</td><td>GCC</td><td>-O2</td><td>26 26 26 26 26 26 26 26 26 26</td><td>26</td></tr>
<tr><td>Ducky</td><td>GCC</td><td>-O3</td><td>26 26 26 26 26 26 26 26 26 26</td><td>26</td></tr>
<tr><td>C</td><td>Clang</td><td>-O0</td><td>28 28 28 28 28 28 28 28 28 28</td><td>28</td></tr>
<tr><td>C</td><td>Clang</td><td>-O1</td><td>28 29 29 28 28 28 28 28 29 29</td><td>28.4</td></tr>
<tr><td>C</td><td>Clang</td><td>-O2</td><td>28 29 29 29 28 29 29 28 28 28</td><td>28.4</td></tr>
<tr><td>C</td><td>Clang</td><td>-O3</td><td>28 28 28 29 28 29 28 29 28 28</td><td>28.3</td></tr>
<tr><td>C</td><td>GCC</td><td>-O0</td><td>28 28 28 28 28 28 28 28 28 28</td><td>28</td></tr>
<tr><td>C</td><td>GCC</td><td>-O1</td><td>30 30 30 30 29 30 29 30 30 29</td><td>29.7</td></tr>
<tr><td>C</td><td>GCC</td><td>-O2</td><td>30 30 30 30 30 30 30 30 30 30</td><td>30</td></tr>
<tr><td>C</td><td>GCC</td><td>-O3</td><td>30 30 30 30 30 30 30 30 30 30</td><td>30</td></tr>
</table>
## Future Directions
- Refactor code
- Input
- Add more builtins
- Arrays?
- Functions?
|