* parzival The quest of =parzival= is to make it fun and easy to build parsers that consume streams and return common lisp values. E.g. A [[https://github.com/cbeo/parzival/blob/master/examples/Tutorial.org][JSON]] parser or can be written reasonbly succinctly in parzival. Check out the [[https://codeberg.org/hypergoof/parzival/src/branch/master/examples/Tutorial.org][Tutorial]] for a detailed introduction. If you use Emacs, load [[./parzival-indent.el]] to get custom indentation and keyword highlighting for parzival's special forms. ** A Neat / Dumb Example What follows is a quick example of using =parzival= to build a parser for simple arithmetic expressions. But first, you should be aware of two *completely optional* naming conventions that =parzival= adopts. 1. Names beginning and ending in a =<= are *parsers*. 2. Names that begin with =<<= are *higher-order functions* that accept or return parsers. #+begin_src lisp (defpackage :parzival-user (:use :cl :parzival)) (in-package :parzival-user) ;; we want to parse + - / or * and result in a function that can be ;; used do arithmetic on numbers (< (parse "33 * 2.5" PARZIVAL-USER> (parse "331 / 2.5" PARZIVAL-USER> (parse "foozball / 2.5" PARZIVAL-USER> T #+end_src In the last example the string =foozball= does not represent a real number, and hence, the parse fails. You can examine the third return value to see where in the input stream the parse failed. ** An Extended Silly / Dumber Example The code for this example is a little long winded, so for the impatient I present below the grand payoff. What we have here is a natural language calculator. It computes addition, subtraction, multipication, and division with all the convenience of typing out numbers without the cumbersome use of digits! Here is a REPL session #+BEGIN_SRC PARZIVAL-NUMBERS> (natural-language-calc) Hello! And Welcome To the Super Practical Natural Language Calculator! Type quit to quit > one hundred minus eight EQUALS ninety-two > twenty-nine plus four hundred seventy-seven EQUALS five hundred six > twenty over four EQUALS five > twenty-one times sixteen EQUALS three hundred thirty-six > four thousand nine hundred fifty-five times two hundred seventeen EQUALS one million seventy-five thousand two hundred thirty-five > quit "OK" PARZIVAL-NUMBERS> #+END_SRC (ta-dah) *** The Code #+BEGIN_SRC lisp (defpackage :parzival-numbers (:use :cl :parzival)) (in-package :parzival-numbers) (defun < ") (loop named goof-calc for line = (read-line) do (if (equal line "quit") (return-from goof-calc "OK") (let ((parsed (parse (string-downcase line) " parsed) (format t "No no no.. all wrong...~%> ")))))) #+END_SRC