Ohm v2
A golfing stack language in Ruby where every number is a BigDecimal and lists are mapped by how deep they are.
nickbclifford · Turing complete: yes · ●●○ moderate
Tested with 8 known programs · Test method
Run it
The idea
Ohm was made by nickbclifford for code golf, after 05AB1E and Jelly. Version 2 replaced true and false with 1 and 0. The interpreter is written in Ruby, in the repository nickbclifford/Ohm, and this implementation follows its source.
Read the story
A program is a row of components on one stack. When a component needs more values than the stack holds, the missing ones are inputs, read one line each. When the program ends without printing, the top of the stack is printed.
The machine
A number literal is pushed as text. Arithmetic reads its arguments as Ruby BigDecimals, so 7 2/ gives 3.5 exactly. Lengths and ranges stay Ruby integers.
Each component says how deep its arguments should be. A number where a number is wanted is used as it is. A list where a number is wanted is mapped, so 5@² squares each of 1 to 5. Two lists are zipped.
| Component | Does |
|---|---|
"…" .c | A string, one character |
+ - * / % | BigDecimal arithmetic |
² ‹ › | Square, take one, add one |
@ # | The list 1 to n, 0 to n |
l R S J Σ | Length, reverse, sort, join, sum |
D s | Duplicate, swap |
, L | Print with and without a newline |
?…¿…; | If, else |
:…; | For each, with the value in _ and the index in ^ |
M…; €…; | Repeat n times, map |
A list is printed in Ruby's own form. Integers show as digits, but a BigDecimal shows as 0.4e1, so a list of squares from 5@² prints [0.1e1, 0.4e1, 0.9e1, 0.16e2, 0.25e2].
Example
From the README.
From the README. The space keeps 13 and 2 apart.
² finds the stack empty and squares the first line of input. : runs its block once for each of 1 to 5 and _, prints the element.
This implementation is a subset. It has 86 of the components and five kinds of block. The rest, the extra wires on further lines, compressed strings and divisions that do not end stop the run with a reason.
Patterns it teaches
- Code golf: input fills in for missing arguments, so most programs never read it.
- Stack machines: one stack, with depth rules deciding when to map.
Try this
- Print the squares of 1 to 10, one per line.
- Read a line and print it reversed.
- Hard: print the sum of the even numbers up to the input.
Further
- The reference interpreter, in Ruby, with components.md listing every component
- Related: Ohm, 05AB1E
See also
Also in Golfing
Write your own in Run Convert a program from Ohm v2
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: ohm2.
Command line
shib run ohm2 -e '²' --input '12'shib manual ohm2Source
- Module
langs/tio/ports.js- Interpreter
langs/tio/ohm2.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language