| | 1 | /* |
| | 2 | * dictionary test |
| | 3 | */ |
| | 4 | |
| | 5 | #include "tads.h" |
| | 6 | #include "t3.h" |
| | 7 | #include "dict.h" |
| | 8 | |
| | 9 | class RuntimeError: object |
| | 10 | construct(errno, ...) { errno_ = errno; } |
| | 11 | display = "Runtime error: <<exceptionMessage>>" |
| | 12 | errno_ = 0 |
| | 13 | exceptionMessage = '' |
| | 14 | ; |
| | 15 | |
| | 16 | _say_embed(str) { tadsSay(str); } |
| | 17 | |
| | 18 | global: object |
| | 19 | preinited_ = nil |
| | 20 | dictionary_ = nil |
| | 21 | ; |
| | 22 | |
| | 23 | _main(args) |
| | 24 | { |
| | 25 | try |
| | 26 | { |
| | 27 | t3SetSay(&_say_embed); |
| | 28 | if (!global.preinited_) |
| | 29 | { |
| | 30 | preinit(); |
| | 31 | global.preinited_ = true; |
| | 32 | } |
| | 33 | if (!t3GetVMPreinitMode()) |
| | 34 | main(args); |
| | 35 | } |
| | 36 | catch (RuntimeError rte) |
| | 37 | { |
| | 38 | "\n<<rte.display>>\n"; |
| | 39 | } |
| | 40 | } |
| | 41 | |
| | 42 | obj1: object |
| | 43 | adjective = 'none' |
| | 44 | noun = 'none' |
| | 45 | ; |
| | 46 | |
| | 47 | bluebook: object sdesc = "blue_book"; |
| | 48 | redbook: object sdesc = "red_book"; |
| | 49 | greenbook: object sdesc = "green_book"; |
| | 50 | blueball: object sdesc = "blue_ball"; |
| | 51 | redball: object sdesc = "red_ball"; |
| | 52 | greenball: object sdesc = "green_ball"; |
| | 53 | |
| | 54 | preinit() |
| | 55 | { |
| | 56 | local dict; |
| | 57 | |
| | 58 | /* create a dictionary */ |
| | 59 | dict = global.dictionary_ = new Dictionary(6); |
| | 60 | |
| | 61 | /* add some words */ |
| | 62 | dict.addWord(bluebook, 'blue', &adjective); |
| | 63 | dict.addWord(redbook, 'red', &adjective); |
| | 64 | dict.addWord(greenbook, 'green', &adjective); |
| | 65 | |
| | 66 | dict.addWord(blueball, 'blue', &adjective); |
| | 67 | dict.addWord(redball, 'red', &adjective); |
| | 68 | dict.addWord(greenball, 'green', &adjective); |
| | 69 | |
| | 70 | dict.addWord(bluebook, 'book', &noun); |
| | 71 | dict.addWord(redbook, 'book', &noun); |
| | 72 | dict.addWord(greenbook, 'book', &noun); |
| | 73 | |
| | 74 | dict.addWord(blueball, 'ball', &noun); |
| | 75 | dict.addWord(redball, 'ball', &noun); |
| | 76 | dict.addWord(greenball, 'ball', &noun); |
| | 77 | } |
| | 78 | |
| | 79 | main(args) |
| | 80 | { |
| | 81 | local dict = global.dictionary_; |
| | 82 | local lst; |
| | 83 | |
| | 84 | /* look up some words */ |
| | 85 | "noun 'book': <<sayList(dict.findWord('book', &noun))>>\n"; |
| | 86 | "noun: 'ball': <<sayList(dict.findWord('ball', &noun))>>\n"; |
| | 87 | "adjective: 'red': <<sayList(dict.findWord('red', &adjective))>>\n"; |
| | 88 | "adjective: 'blue': <<sayList(dict.findWord('blue', &adjective))>>\n"; |
| | 89 | "adjective: 'green': <<sayList(dict.findWord('green', &adjective))>>\n"; |
| | 90 | |
| | 91 | /* shorten the truncation length */ |
| | 92 | dict.setTruncLen(3); |
| | 93 | |
| | 94 | "\bAfter setting truncation length to 3:\n"; |
| | 95 | "noun 'book': <<sayList(dict.findWord('book', &noun))>>\n"; |
| | 96 | "noun: 'ball': <<sayList(dict.findWord('ball', &noun))>>\n"; |
| | 97 | "adjective: 'red': <<sayList(dict.findWord('red', &adjective))>>\n"; |
| | 98 | "adjective: 'blue': <<sayList(dict.findWord('blue', &adjective))>>\n"; |
| | 99 | "adjective: 'green': <<sayList(dict.findWord('green', &adjective))>>\n"; |
| | 100 | |
| | 101 | "noun: 'boo': <<sayList(dict.findWord('boo', &noun))>>\n"; |
| | 102 | "noun: 'boop': <<sayList(dict.findWord('boop', &noun))>>\n"; |
| | 103 | "adjective: 'green': <<sayList(dict.findWord('green', &adjective))>>\n"; |
| | 104 | "adjective: 'gree': <<sayList(dict.findWord('gree', &adjective))>>\n"; |
| | 105 | "adjective: 'gre': <<sayList(dict.findWord('gre', &adjective))>>\n"; |
| | 106 | "adjective: 'gr': <<sayList(dict.findWord('gr', &adjective))>>\n"; |
| | 107 | "adjective: 'greek': <<sayList(dict.findWord('greek', &adjective))>>\n"; |
| | 108 | "adjective: 'greeny': <<sayList(dict.findWord('greeny', &adjective))>>\n"; |
| | 109 | |
| | 110 | "\bFinding with truncation:\n"; |
| | 111 | "noun: 'boo': <<sayList(dict.findWordTrunc('boo', &noun))>>\n"; |
| | 112 | "noun: 'boop': <<sayList(dict.findWordTrunc('boop', &noun))>>\n"; |
| | 113 | "adjective: 'green': |
| | 114 | <<sayList(dict.findWordTrunc('green', &adjective))>>\n"; |
| | 115 | "adjective: 'gree': |
| | 116 | <<sayList(dict.findWordTrunc('gree', &adjective))>>\n"; |
| | 117 | "adjective: 'gre': <<sayList(dict.findWordTrunc('gre', &adjective))>>\n"; |
| | 118 | "adjective: 'gr': <<sayList(dict.findWordTrunc('gr', &adjective))>>\n"; |
| | 119 | "adjective: 'greek': |
| | 120 | <<sayList(dict.findWordTrunc('greek', &adjective))>>\n"; |
| | 121 | "adjective: 'greeny': |
| | 122 | <<sayList(dict.findWordTrunc('greeny', &adjective))>>\n"; |
| | 123 | } |
| | 124 | |
| | 125 | sayList(lst) |
| | 126 | { |
| | 127 | "["; |
| | 128 | for (local i = 1, local len = lst.length() ; i <= len ; ++i) |
| | 129 | { |
| | 130 | if (i > 1) |
| | 131 | ", "; |
| | 132 | lst[i].sdesc; |
| | 133 | } |
| | 134 | "]"; |
| | 135 | } |