comparison examples/dsl/symbol.cpp @ 0:13d2b8934445

Import AnaGram (near-)release tree into Mercurial.
author David A. Holland
date Sat, 22 Dec 2007 17:52:45 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:13d2b8934445
1 /*****
2
3 AnaGram Programming Examples
4
5 A Dos Script Language
6 Symbol Table Definitions
7
8 Copyright 1993 Parsifal Software. All Rights Reserved.
9
10 This software is provided 'as-is', without any express or implied
11 warranty. In no event will the authors be held liable for any damages
12 arising from the use of this software.
13
14 Permission is granted to anyone to use this software for any purpose,
15 including commercial applications, and to alter it and redistribute it
16 freely, subject to the following restrictions:
17
18 1. The origin of this software must not be misrepresented; you must not
19 claim that you wrote the original software. If you use this software
20 in a product, an acknowledgment in the product documentation would be
21 appreciated but is not required.
22 2. Altered source versions must be plainly marked as such, and must not be
23 misrepresented as being the original software.
24 3. This notice may not be removed or altered from any source distribution.
25
26 *****/
27
28 #include "symbol.h"
29 #include <strdict.h>
30 #include <charsink.h>
31 #include <ctype.h>
32 #include "util.h"
33
34 extern string_accumulator sa;
35 extern string_dictionary sd;
36 extern unsigned stderr_index;
37
38 action_pointer copy_action(void);
39
40 void release(action_descriptor *d) {
41 delete [] d->args;
42 delete [] d->ap.pointer;
43 delete d;
44 }
45
46 void release(symbol_table_entry &s) {
47 switch (s.type) {
48 case string_type:
49 case value_type: {
50 delete [] s.data.text;
51 break;
52 }
53 case action_type: {
54 release(s.data.action);
55 break;
56 }
57 default:
58 /* not supposed to happen? */
59 break;
60 }
61 s.data.integer = 0;
62 }
63
64 void define_action(int n) {
65 unsigned index;
66 action_descriptor *d = new action_descriptor;
67
68 d->args = new unsigned[n];
69 d->n_args = n;
70 d->ap = copy_action();
71 while (n--) {
72 d->args[n] = sd << (sa--).top();
73 }
74 index = sd << (sa--).top();
75 release(st[index]);
76 st[index].type = action_type;
77 st[index].data.action = d;
78 }
79
80 void define_integer(const char *name, long value) {
81 unsigned index = sd << name;
82 release(st[index]);
83 st[index].type = integer_type;
84 st[index].data.integer = value;
85 }
86
87 void define_string(void) {
88 char *text = copy(sa--);
89 unsigned index = sd << (sa--).top();
90 release(st[index]);
91 st[index].type = string_type;
92 st[index].data.text = text;
93 }
94
95 const char *dos_internals[] = {
96 "CD",
97 "CHCP",
98 "CHDIR",
99 "CLS",
100 "COPY",
101 "CTTY",
102 "DATE",
103 "DEL",
104 "DIR",
105 "ERASE",
106 "MD",
107 "MKDIR",
108 "PATH",
109 "PROMPT",
110 "RD",
111 "REN",
112 "RENAME",
113 "RMDIR",
114 "SET",
115 "TIME",
116 "TYPE",
117 "VER",
118 "VERIFY",
119 "VOL",
120 "CHKDSK",
121 "COMP",
122 "DISKCOMP",
123 "DISKCOPY",
124 "KEYB",
125 "LABEL",
126 "MODE",
127 "PRINT",
128 "RECOVER",
129 NULL,
130 };
131
132 extern internal_commands_descriptor internal_commands[];
133 extern struct built_ins_descriptor built_ins[];
134 extern unsigned errorlevel_index;
135
136 void set_arg(int n, char *arg) {
137 unsigned index;
138 (++sa).printf("argv[%d]", n);
139 index = sd << (sa--).top();
140 st[index].type = string_type;
141 st[index].data.text = arg;
142 }
143
144
145 void init_dos_internals(void) {
146 int i;
147 unsigned index;
148 for (i = 0; dos_internals[i]; i++) {
149 st[sd << dos_internals[i]].type = dos_type;
150 }
151 for (i = 0; internal_commands[i].name; i++) {
152 index = sd << internal_commands[i].name;
153 st[index].type = internal_type;
154 st[index].data.proc = internal_commands[i].proc;
155 }
156 for (i = 0; built_ins[i].name; i++) {
157 index = sd << built_ins[i].name;
158 st[index].type = built_in_function_type;
159 st[index].data.func = built_ins[i].proc;
160 }
161 define_integer("errorlevel", 0);
162 errorlevel_index = sd["errorlevel"];
163 index = stderr_index = sd << "stderr";
164 st[index].type = string_type;
165 st[index].data.text = NULL;
166 }
167