| 1 | <h2>Comments</h2> |
| 2 | <pre><code># |
| 3 | # Foobar</code></pre> |
| 4 | |
| 5 | <h2>Strings and csets</h2> |
| 6 | <pre><code>"" |
| 7 | "Foo\"bar" |
| 8 | '' |
| 9 | 'a\'bcdefg'</code></pre> |
| 10 | |
| 11 | <h2>Numbers</h2> |
| 12 | <pre><code>42 |
| 13 | 3.14159 |
| 14 | 5.2E+8 |
| 15 | 16rface |
| 16 | 2r1101</code></pre> |
| 17 | |
| 18 | <h2>Full example</h2> |
| 19 | <pre><code># Author: Robert J. Alexander |
| 20 | global GameObject, Tree, Learn |
| 21 | record Question(question, yes, no) |
| 22 | procedure main() |
| 23 | GameObject := "animal" |
| 24 | Tree := Question("Does it live in water", "goldfish", "canary") |
| 25 | Get() # Recall prior knowledge |
| 26 | Game() # Play a game |
| 27 | return |
| 28 | end |
| 29 | # Game() -- Conducts a game. |
| 30 | # |
| 31 | procedure Game() |
| 32 | while Confirm("Are you thinking of ", Article(GameObject), " ", |
| 33 | GameObject) do Ask(Tree) |
| 34 | write("Thanks for a great game.") |
| 35 | if \Learn &Confirm("Want to save knowledge learned this session") |
| 36 | then Save() |
| 37 | return |
| 38 | end |
| 39 | # Confirm() -- Handles yes/no questions and answers. |
| 40 | # |
| 41 | procedure Confirm(q[]) |
| 42 | local answer, s |
| 43 | static ok |
| 44 | initial { |
| 45 | ok := table() |
| 46 | every ok["y" | "yes" | "yeah" | "uh huh"] := "yes" |
| 47 | every ok["n" | "no" | "nope" | "uh uh" ] := "no" |
| 48 | } |
| 49 | while /answer do { |
| 50 | every writes(!q) |
| 51 | write("?") |
| 52 | case s := read() | exit(1) of { |
| 53 | # Commands recognized at a yes/no prompt. |
| 54 | # |
| 55 | "save": Save() |
| 56 | "get": Get() |
| 57 | "list": List() |
| 58 | "dump": Output(Tree) |
| 59 | default: { |
| 60 | (answer := \ok[map(s, &ucase, &lcase)]) | |
| 61 | write("This is a \"yes\" or \"no\" question.") |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | return answer == "yes" |
| 66 | end |
| 67 | # Ask() -- Navigates through the barrage of questions leading to a |
| 68 | # guess. |
| 69 | # |
| 70 | procedure Ask(node) |
| 71 | local guess, question |
| 72 | case type(node) of { |
| 73 | "string": { |
| 74 | if not Confirm("It must be ", Article(node), " ", node, ", right") then { |
| 75 | Learn := "yes" |
| 76 | write("What were you thinking of?") |
| 77 | guess := read() | exit(1) |
| 78 | write("What question would distinguish ", Article(guess), " ", |
| 79 | guess, " from ", Article(node), " ", node, "?") |
| 80 | question := read() | exit(1) |
| 81 | if question[-1] == "?" then question[-1] := "" |
| 82 | question[1] := map(question[1], &lcase, &ucase) |
| 83 | if Confirm("For ", Article(guess), " ", guess, ", what would the answer be") |
| 84 | then return Question(question, guess, node) |
| 85 | else return Question(question, node, guess) |
| 86 | } |
| 87 | } |
| 88 | "Question": { |
| 89 | if Confirm(node.question) then node.yes := Ask(node.yes) |
| 90 | else node.no := Ask(node.no) |
| 91 | } |
| 92 | } |
| 93 | end |
| 94 | # Article() -- Come up with the appropriate indefinite article. |
| 95 | # |
| 96 | procedure Article(word) |
| 97 | return if any('aeiouAEIOU', word) then "an" else "a" |
| 98 | end |
| 99 | # Save() -- Store our acquired knowledge in a disk file name |
| 100 | # based on the GameObject. |
| 101 | # |
| 102 | procedure Save() |
| 103 | local f |
| 104 | f := open(GameObject || "s", "w") |
| 105 | Output(Tree, f) |
| 106 | close(f) |
| 107 | return |
| 108 | end |
| 109 | # Output() -- Recursive procedure used to output the knowledge tree. |
| 110 | # |
| 111 | procedure Output(node, f, sense) |
| 112 | static indent |
| 113 | initial indent := 0 |
| 114 | /f := &output |
| 115 | /sense := " " |
| 116 | case type(node) of { |
| 117 | "string": write(f, repl(" ", indent), sense, "A: ", node) |
| 118 | "Question": { |
| 119 | write(f, repl(" ", indent), sense, "Q: ", node.question) |
| 120 | indent +:= 1 |
| 121 | Output(node.yes, f, "y") |
| 122 | Output(node.no, f, "n") |
| 123 | indent -:= 1 |
| 124 | } |
| 125 | } |
| 126 | return |
| 127 | end |
| 128 | # Get() -- Read in a knowledge base from a file. |
| 129 | # |
| 130 | procedure Get() |
| 131 | local f |
| 132 | f := open(GameObject || "s", "r") | fail |
| 133 | Tree := Input(f) |
| 134 | close(f) |
| 135 | return |
| 136 | end |
| 137 | # Input() -- Recursive procedure used to input the knowledge tree. |
| 138 | # |
| 139 | procedure Input(f) |
| 140 | local nodetype, s |
| 141 | read(f) ? (tab(upto(~' \t')) & =("y" | "n" | "") & |
| 142 | nodetype := move(1) & move(2) & s := tab(0)) |
| 143 | return if nodetype == "Q" then Question(s, Input(f), Input(f)) else s |
| 144 | end |
| 145 | # List() -- Lists the objects in the knowledge base. |
| 146 | # |
| 147 | $define Length 78 |
| 148 | procedure List() |
| 149 | local lst, line, item |
| 150 | lst := Show(Tree, [ ]) |
| 151 | line := "" |
| 152 | every item := !sort(lst) do { |
| 153 | if *line + *item > Length then { |
| 154 | write(trim(line)) |
| 155 | line := "" |
| 156 | } |
| 157 | line ||:= item || ", " |
| 158 | } |
| 159 | write(line[1:-2]) |
| 160 | return |
| 161 | end |
| 162 | # |
| 163 | # Show() -- Recursive procedure used to navigate the knowledge tree. |
| 164 | # |
| 165 | procedure Show(node, lst) |
| 166 | if type(node) == "Question" then { |
| 167 | lst := Show(node.yes, lst) |
| 168 | lst := Show(node.no, lst) |
| 169 | } |
| 170 | else put(lst, node) |
| 171 | return lst |
| 172 | end</code></pre> |