esolang

Grocery List

A program is a shopping list. Only the first letter of each item matters, so "eggs" and "espresso" do the same thing.

PuzzleHunter84, 2009 · Turing complete: unknown · ●○○ easy · esolangs.org

Tested with 3 known programs · Test method

Run it

Expected: Hi

The idea

PuzzleHunter84 made Grocery List in 2009. A program starts with a store name, then a blank line, then one food item per line. Each letter of the alphabet is a command, and an item runs the command of its first letter.

Read the story

Some commands also look at the rest of the item. n pushes the number of letters in the item, so "nectarines" pushes 10. v pushes the first letter of the next item and skips it. The joke is that any real shopping list is a program, though most of them crash or do nothing useful.

The machine

A stack of integers. Popping an empty stack gives 0. "Top" is the top value, "next" the one below.

LetterDoes
a mAdd, multiply
s d rTop minus next, top divided by next (rounded down), top mod next
g z1 if top > next, else 0; logical not
c f x kDup, swap, drop, clear the stack
b uBring the bottom value to the top; send the top to the bottom
n wPush the item's letter count; push 100
vPush the first letter of the next item, and skip that item
yRemove the stack item that many places down (the item's letter count)
hPop a value, take it mod 26, and run that letter (0 is a)
jPop n and skip the next n items
l ... eLoop while the top is not 0 (checked at both ends, not popped)
iRead a character (0 at end of input)
o pPop and print as a number, as a character
q tNothing; stop

Dividing by zero stops the run.

Example

This is the cat program, which it compares to brainfuck's ,[.,].

This is the cat program, which it compares to brainfuck's ,[.,]. Ice cream reads a character, lettuce opens a loop, popcorn prints, imported bisque reads again, and eggs loop back while the character is not 0. Tunafish ends the shopping trip.

Patterns it teaches

Try this

  1. Print the number 100 with a two-item list.
  2. Print A (65): find food items whose letter counts multiply or add to 65.
  3. Hard: read a character and print it in upper case.

Further

Stack-based

Write your own in Run Convert a program from Grocery List

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: grocery-list.

Command line

shib run grocery-list -e 'Corner Shop

watermelon
oranges'
shib manual grocery-list

Source

Module
langs/stack/ports.js
Interpreter
langs/stack/grocery-list.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org