-
Notifications
You must be signed in to change notification settings - Fork 1
/
symbol.c
134 lines (117 loc) · 3.56 KB
/
symbol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* symbol.c (acdc) - copyright Mike Arnautov 1990-2018.
* Licensed under GPL, version 3 or later (see the supplied LICENCE file).
*
* 03 Mar 16 MLA Removed non-ANSI C support.
* 29 Dec 14 MLA Fixed symbol arithmetic in STYLE 1.
* 06 Apr 10 MLA Resticted identifier names.
* 15 Mar 08 MLA Version 12 changes.
* 07 May 07 Stuart Munro bug: declare chksymb before fndsymb.
* 15 Jan 05 MLA Added AUTOSYMBOL handling.
* 07 Jan 03 MLA Use btree instead of tsearch.
* 17 Oct 01 MLA Improved compatibility with Platt's A-code.
* 24 Jul 01 MLA Added ANSI C declaration.
* 28 Jul 99 MLA Removed superfluous setjmp declaration.
* 24 Jul 99 MLA Fixed complier warnings.
* 16 Mar 91 MLA Unified symbol and vocab searches.
* 22 Nov 90 MLA Own TSEARCH -some systems don't have it.
* 15 Sep 90 MLA Initial coding.
*
*/
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include "acdc.h"
#include "game.h"
#include "symbol.h"
#include "const.h"
#include "btree.h"
#include "major.h"
struct node *addsymb (int btroot, char *name, int type, int refno)
{
struct node *np;
int len;
int auto_flag = btroot & 64;
btroot &= ROOT_MASK;
if (style > 1)
{
int bad_char = 1;
if (isalpha (*name) || strchr (".?!", *name))
{
char *c = name + 1;
bad_char = 0;
while (*c)
{
if (isalnum (*c) || strchr (".?!-_'/&", *c))
c++;
else
{
bad_char = 1;
break;
}
}
}
if (bad_char)
gripe (name, "Not a legal name.");
}
if ((np = (struct node *) malloc (sizeof (struct node))) == NULL)
gripe (name, "Unable to allocate memory.");
len = strlen (name);
if (style < 10 && len > 12 && strpbrk (name, "+-") == NULL)
{
len = 12;
*(name + 12) = '\0';
}
if ((np -> name = (char *) malloc (len + 1)) == NULL)
gripe (name, "Unable to allocate symbol name storage.");
strcpy (np -> name, name);
np -> type = type;
np -> state_count = (type == VAR) ? 1 : 0;
np -> used_count = 0;
np -> auto_flag = auto_flag;
np -> text_addr[0] = -1;
np -> text_addr[1] = -1;
np -> text_addr[2] = -1;
if (type == TEXT)
np -> inline_text = 0;
np -> name_addr = -1;
np -> proc_count = 0;
np -> proc_done = 0;
np -> arg_count = -1;
np -> symbol = NULL;
np -> refno = refno;
if (btadd (btroot, np) == 0)
gripe (name, "Symbol already defined.");
return (np);
}
/*======================================================================*/
struct node *fndsymb (int btroot, char *fname)
{
struct node *np;
char nbuf [160];
char *name = nbuf;
int maxlen = sizeof (nbuf) - 1;
int testing = (btroot & 32);
if (style < 10 && strlen (fname) > 12 && strpbrk (fname, "+-") == NULL)
maxlen = 12;
strncpy (nbuf, fname, maxlen);
*(name + maxlen) = '\0';
btroot &= ROOT_MASK;
if (*name == '-' || *name == '+' || *name == '!' || *name == '=')
name++;
np = btfind (btroot, name);
if (np == NULL && btroot == SYMBOL)
{
np = btfind (VOCAB, name);
if (np == NULL)
{
if (testing) return (NULL);
gripe (name, "Symbol not found.");
}
np = np -> symbol;
}
/* if (np && stage && btroot == SYMBOL)
(np -> used_count)++;
*/
return (np);
}
/************************************************************************/