Sigil Docs: Example Programs

Sigil / Documentation / Example Programs

A Command-line Greeting Program

module main
  import
    prelude
    sys.console.(get-line put-line)

greet  String  String
greet name  "Hello, ${name}"

main  IO Unit
main  do
  put-line "What is your name?"
  name ← get-line
  put-line (greet name)

Webpage

This code would compile to javascript and describes a webpage with

  • A header (h1) displaying 'Hello from Sigil'
  • a line break
  • an input element where the user can input text
  • a span which displays the text the user input, but reversed
module main
  import
    glint
    glint-dom

main : IO Unit
main  main-widget ⧺ do
  h1 (text "Hello from Sigil")
  br
  text ← input-element
  dyn-text ⧺ arr reversed <<< value_of text

Command-line Calculator

A simple calculator: it reads a line of input, e.g. "2 + 3 * 10" and performs the corresponding calculation (here, yielding 32).

module main
  import early

arith  N : 𝕌  Grammar N (Prod N String String )
arith  do
  x1 ← rule $ _+_ <$> x1 << token "+" <*> x2 <|> x2
  x2 ← rule $ _×_ <$> x2 << token "*" <*> x3 <|> x3
  x3 ← rule $ (read <*> satisfy isNum) <|> token "(" *> x1 <* token ")"
  pure x1

main  do
  input ← get-line
  expr ← parse (parser arith) (words input)
  φ expr.
    :ok val  print val
    :err message  print "Error: ${message}"))