iyi

Playground

samples/iyi/config.iyi

The first program that reads text - a key = value file, with the line.

samples/iyi/config.iyi · iyi-config.wasm · 172 KB
# The first program that reads text - a `key = value` file, with the line
# number in its error.
#
#   ./bin/iyi run samples/iyi/config.iyi
#
# Text comes apart into lines, a line into its two halves around a `=`, and
# each half loses its whitespace; a line that does not parse is an error
# value carrying where it was (SPEC.md III.1), not a printed message. Like
# `basics` and `inventory`, this documents no rule: it is here because the
# prelude grows only when a program in this repository needs something
# (SPEC.md 0.1.0 item 3), and cutting text is what the third program does.
module samples/config

struct ParseError
  property line : Int32
  property message : String

  def initialize(@line : Int32, @message : String)
  end
end

impl Error for ParseError
  def message : String
    "line #{@line}: #{@message}"
  end
end

def parse(text : String) : Hash(String, String) | ParseError
  config = {} of String => String
  text.lines.each_with_index do |raw, index|
    line = raw.strip
    next if line.empty? || line.starts_with?('#')
    eq = line.index('=')
    return ParseError.new(index + 1, "no '=' in #{line.inspect}") unless eq
    key = line[0, eq].strip
    return ParseError.new(index + 1, "empty key") if key.empty?
    config[key] = line[eq + 1, line.size - eq - 1].strip
  end
  config
end

good = "# the service\nname = iyi\nport= 8080\n\nmode =fast\n"
case parse(good)
in Hash(String, String)
  puts it                  # => {"name" => "iyi", "port" => "8080", "mode" => "fast"}
  puts it["port"].to_i + 1 # => 8081
  puts it.keys.join(", ")  # => name, port, mode
in ParseError then puts "error: #{it.message}"
end

bad = "name = iyi\nbroken line\n"
case parse(bad)
in Hash(String, String) then puts "parsed"
in ParseError           then puts "error: #{it.message}" # => error: line 2: no '=' in "broken line"
end

# The pieces a parser cuts, on their own.
puts "a,,b".split(',')               # => ["a", "", "b"]
puts "one\ntwo\n".lines              # => ["one", "two"]
puts "  padded\t".strip.inspect      # => "padded"
puts "config.iyi".ends_with?(".iyi") # => true
output
Press Run.

recorded with iyi 0.18.0 (built on Crystal 1.22.0-dev [2a9653052] (2026-10-18)) · Linux 7.0.0-38-generic x86_64, QEMU Virtual CPU version 2.5+ · 2026-10-19

You can type in the pane: nothing here compiles, so what you type cannot run, and the page says which bytes it is running the moment the text stops being the recorded file. There is no standard input, no filesystem and no threads on this target, and one sample fails on purpose for exactly that reason.

Run executes iyi-config.wasm, the module the compiler built from this file, in your browser. How this works

Try it on your machine

Copy the program into config.iyi, change something, and run it. Only a compiler on your own machine can run what you wrote.

sh, after installing
$ iyi run config.iyi