#+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/