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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
#+TITLE: First Impressions of the Myrddin Programming Language
#+TAGS: opinion, programming, myrddin
# Man the memory management model of this lang is so wierd I'm constantly
# dealing with things being freed from under my feet.
# Also, no shadowing, which sucks.
# It's been a long time since I've made a post like this.
I'm not sure there are many applications writen in Myrddin. The [[https://eigenstate.org/software/ircmyr][irc.myr]] IRC
client comes to mind, but not much else. It seems most of the work has gone
towards developing libraries.
It took a restless night for me to come up with a good project to use Myrddin
for. The limited architecture support restricted my choices somewhat. I had
considered writing a very lightweight [[https://en.wikipedia.org/wiki/Imageboard#Danbooru-style_boards][Booru]] engine to use as a self-hosted,
tagged image gallery, but my "server" [fn:1] is ARMv6, so I wouldn't be getting
much use out of it. Ah, well. The next language I plan to write about is built
upon LLVM, so I'll be able to target my server then. I also toyed with the idea
of a text editor and a Lisp interpreter, but settled on a CHIP-8 emulator.
First, it would be a good opportunity to demonstrate the C binding support as
I'll be using SDL. There's plenty of room to go above the bare-minimum for
features, such as additionally implementing an assembler, debugger, or JIT.
Furthermore, it would be (to my knowledge) the first graphical application
writen in Myrddin.
# Also, I figure having a CHIP-8 emulator of my own would be good, since it
# would be a fun target for a "toy" compiler if I decide to make one.
# Mention the [[https://myrlang.org/wishlist][wishlist]]
* Tooling
# There's a syntax highlighting implementation for vim, but I don't use vim
So I've written a [[https://git.sr.ht/~jakob/myrddin-mode][major mode]] for GNU Emacs.
# It's packaged in melpa!
* Build System
The build system for Myrddin is interesting, and very reminiscent of
- =mbld=
- =6m= is the actual compiler.
- =muse=???
# Error messages are AWFUL
#+BEGIN_SRC prog
jakob@Epsilon ~/Code/chip $ mbld -b asm asm.myr && ./asm
6m asm.myr
asm.myr:52: type "char" incompatible with "byte" near Otup:(union
`std.None
`std.Some byte[:]
;;,byte[:][:])
char from asm.myr:24
byte from asm.myr:8
FAIL: 6m asm.myr
#+END_SRC
# This one was in response to trying to do .len on an array rather than a slice
#+BEGIN_SRC prog
jakob@Epsilon ~/Code/chip $ mbld -b asm asm.myr
6m asm.myr
6m: typeinfo.c:363: tyoffset: Assertion `ty->type == Tystruct' failed.
CRASH: 6m asm.myr
#+END_SRC
One interesting difference between Myrddin and Rust is that Myrddin does not
require that arguments have a type specifier.
#+BEGIN_SRC myrddin
use std
const factorial = {n
if n < 1
-> 1
;;
-> n * factorial(n - 1)
}
const main = {
std.put("Hello, world!\n")
}
#+END_SRC
But as soon as you have more complicaed functions that aren't used...
[fn:1] At the moment, a Raspberry Pi B+. It does the job, but I'm afraid of blowing the 512MB RAM trying to run a full MediaGoblin instance on it.
# ---
- Entry point is the function named =main=.
- Simpler, but similar to Rust's format specifies for output (=std.put("{} + {} = {}\n", 2, 2, 5)=)
- Function prototypes not necessary (!)
- Drew Devault's recent mockup requires function prototypes.
- Lines aren't terminated with semicolons (feels a bit like Go?)
- Declarations begin with =var=, =const=, or =generic=.
- Specify type like in rust (=var acc: int=)
- There's type inference (cool!)
- There's no special syntax for function declarations; functions are declared by
assigning a function literal to a constant.
- A bit like Scheme, I guess?
- Function literal syntax is a bit weird, though.
#+BEGIN_SRC myrddin
{arg, list
function
body
}
#+END_SRC
- Argument types aren't actually needed (unlike in Rust).
- Fixed-width integer types (unlike C, more like Rust, which I really appreciate)
- Multiple exit points for a function, i.e.
#+BEGIN_SRC myrddin
const factorial = {n
if n < 1
-> 1
;;
-> n * factorial(n - 1)
}
#+END_SRC
- Minor point, but returning early from a function in Common Lisp is a pain in
the rear. (Of course =if= being an expression makes =factorial= trivial to
implement without it).
- Only control flow is =if/elif/else=, =while=, =for=.
- I like =elif=, like Python.
- Pattern matching!!!
- "Pattern matches can descend into the structure of almost any type.
Structures, arrays, strings, unions, and even values on the other end of
pointers are fair game."
- There's also algebraic data types, which I'm diggin'.
- The backtick syntax is nice for readability imo.
- =std.result=
- Generics. Dunno how to feel about that, yet.
- There are traits, which I'm not comfortable enough with in Rust to make a
comparison.
- Lends itself to allowing for iterators to be implemented.
- Libraries are so simple. Just a =pkg= declaration.
- Array literals are pretty cool.
- =x = [0: 1, 73: 2]=.
- Structs are literals, assigned to a =type= declaration.
- Operators are pretty much the same as C, which makes the learning curve quite nice.
- Only meaningful addition is =->=.
- Tuples!
- I especially enjoy the section on "Style" in the tutorial.
- snake_case is "acceptable", constants are Initialuppercase.
- 60 characters to line is a big opinion.
* Building
** On a Simple Level
- =mbld -b [binary-name] [source.myr ...]=
** Build System
I love this. =[name].proj= looks like
#+BEGIN_SRC myrddin
lib stack =
stk.myr
;;
#+END_SRC
then you just run =mbld=. For binaries, replace =lib= with =bin=.
* Standard Library
- std.optparse
* CFFI
Horribly documented. See [[https://eigenstate.org/archive/myrddin-dev/2015-Oct/0000002.html]].
# ---
- https://github.com/glouw/c8c
- https://github.com/dmatlack/chip8/tree/master/roms
- http://www.multigesture.net/articles/how-to-write-an-emulator-chip-8-interpreter/
|