cfad47cfa3/t3compiler/tads3/test/data/wordsav.t

4b825dc642cb6eb9a060e54bf8d69288fbee4904cfad47cfa334b206c65f22086bcc5d63e6f70944
1
/*
2
 *   dictionary test 
3
 */
4
5
#include "tads.h"
6
#include "t3.h"
7
#include "dict.h"
8
9
obj1: object
10
    adjective = 'none'
11
    noun = 'none'
12
;
13
14
bluebook: object sdesc = "blue_book";
15
redbook: object sdesc = "red_book";
16
greenbook: object sdesc = "green_book";
17
blueball: object sdesc = "blue_ball";
18
redball: object sdesc = "red_ball";
19
greenball: object sdesc = "green_ball";
20
21
preinit()
22
{
23
}
24
25
main(args)
26
{
27
    local dict;
28
29
    /* create a dictionary */
30
    dict = new Dictionary(6);
31
32
    /* add some words */
33
    dict.addWord(bluebook, 'blue', &adjective);
34
    dict.addWord(redbook, 'red', &adjective);
35
    dict.addWord(greenbook, 'green', &adjective);
36
37
    dict.addWord(blueball, 'blue', &adjective);
38
    dict.addWord(redball, 'red', &adjective);
39
    dict.addWord(greenball, 'green', &adjective);
40
41
    dict.addWord(bluebook, 'book', &noun);
42
    dict.addWord(redbook, 'book', &noun);
43
    dict.addWord(greenbook, 'book', &noun);
44
45
    dict.addWord(blueball, 'ball', &noun);
46
    dict.addWord(redball, 'ball', &noun);
47
    dict.addWord(greenball, 'ball', &noun);
48
49
    /* start undo */
50
    savepoint();
51
52
    /* show instructions */
53
    "+word   - add a noun to bluebook\n
54
     -word   - remove a word\n
55
     word    - list objects matching word\n
56
     save    - save file\n
57
     restore - restore state\n
58
     undo    - undo state\n
59
     quit    - terminate\n";
60
61
    /* interactive command loop */
62
    for (;;)
63
    {
64
        local cmd;
65
        
66
        "\n>";
67
        cmd = inputLine();
68
        if (cmd == nil || cmd == 'quit')
69
            break;
70
71
        if (cmd.substr(1, 1) == '+')
72
        {
73
            savepoint();
74
            dict.addWord(bluebook, cmd.substr(2), &noun);
75
            "Added.\n";
76
        }
77
        else if (cmd.substr(1, 1) == '-')
78
        {
79
            savepoint();
80
            dict.removeWord(bluebook, cmd.substr(2), &noun);
81
            "Removed.\n";
82
        }
83
        else
84
        {
85
            local fname;
86
            
87
            switch(cmd)
88
            {
89
            case 'save':
90
                fname = inputFile('File to save', InFileSave,
91
                                  FileTypeT3Save, 0);
92
                if (fname[1] == InFileSuccess)
93
                {
94
                    if (saveGame(fname[2]) != nil)
95
                        "Restored.\n";
96
                    else
97
                        "Saved.\n";
98
                }
99
                break;
100
                
101
            case 'restore':
102
                fname = inputFile('File to restore', InFileOpen,
103
                                  FileTypeT3Save, 0);
104
                if (fname[1] == InFileSuccess)
105
                {
106
                    try
107
                    {
108
                        restoreGame(fname[2], 1);
109
                    }
110
                    catch (RuntimeError exc)
111
                    {
112
                        "Restore failed (<<exc.exceptionMessage>>).\n";
113
                    }
114
                }
115
                break;
116
                
117
            case 'undo':
118
                if (undo())
119
                    "Undid one command.\n";
120
                else
121
                    "No undo information available.\n";
122
                break;
123
124
            default:
125
                "noun '<<cmd>>': <<sayList(dict.findWord(cmd, &noun))>>\n";
126
                "adjective '<<cmd>>':
127
                    <<sayList(dict.findWord(cmd, &adjective))>>\n";
128
                break;
129
            }
130
        }
131
    }
132
133
    "Bye!\b";
134
}
135
136
sayList(lst)
137
{
138
    "[";
139
    for (local i = 1, local len = lst.length() ; i <= len ; ++i)
140
    {
141
        if (i > 1)
142
            ", ";
143
        lst[i].sdesc;
144
    }
145
    "]";
146
}