Imported Upstream version 0.2.0+beta
[debian/yforth] / yfvinit.c
1 /* yForth? - A Forth interpreter written in ANSI C
2  * Copyright (C) 2012 Luca Padovani
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  * ------------------------------------------------------------------------
17  * Module name:     yfvinit.c
18  * Abstract:        Initialize the vocabulary.
19  */
20
21 #include <string.h>
22 #include "yforth.h"
23 #include "core.h"
24 #include "ycore.h"
25 #if COREE_DEF
26 #       include "coree.h"
27 #endif
28 #if DOUBLE_DEF
29 #       include "double.h"
30 #endif
31 #if DOUBLEE_DEF
32 #       include "doublee.h"
33 #endif
34 #if FLOAT_DEF
35 #       if !COREE_DEF
36 #               include "coree.h"
37 #       endif
38 #       include "float.h"
39 #endif
40 #if FLOATE_DEF
41 #       include "floate.h"
42 #endif
43 #if MEMALL_DEF
44 #       include "memall.h"
45 #endif
46 #if SEARCH_DEF
47 #       include "search.h"
48 #endif
49 #if SEARCHE_DEF
50 #       include "searche.h"
51 #endif
52 #if TOOLS_DEF
53 #       include "tools.h"
54 #endif
55 #if TOOLSE_DEF
56 #       if !COREE_DEF
57 #               include "coree.h"
58 #       endif
59 #       include "toolse.h"
60 #endif
61 #if LOCALS_DEF
62 #       include "locals.h"
63 #endif
64 #if LOCALSE_DEF
65 #       include "localse.h"
66 #endif
67 #if FACILITY_DEF
68 #       include "facility.h"
69 #endif
70 #if FACILITYE_DEF
71 #       include "facilite.h"
72 #endif
73 #if STRING_DEF
74 #       include "string.h"
75 #endif
76 #if FILE_DEF
77 #       include "file.h"
78 #endif
79 #if     FILEE_DEF
80 #       include "filee.h"
81 #endif
82 #if BLOCK_DEF
83 #       include "block.h"
84 #endif
85 #if BLOCKE_DEF
86 #       include "blocke.h"
87 #endif
88 #if EXCEPTION_DEF
89 #       include "exceptio.h"
90 #endif
91 #if EXCEPTIONE_DEF
92 #       include "excepte.h"
93 #endif
94
95 static struct raw_voc iv[] = {
96 #define DECLARE_WORDS
97
98 #include "core.h"
99 #include "ycore.h"
100 #if COREE_DEF
101 #       include "coree.h"
102 #endif
103 #if DOUBLE_DEF
104 #       include "double.h"
105 #endif
106 #if DOUBLEE_DEF
107 #       include "doublee.h"
108 #endif
109 #if FLOAT_DEF
110 #       include "float.h"
111 #endif
112 #if FLOATE_DEF
113 #       include "floate.h"
114 #endif
115 #if MEMALL_DEF
116 #       include "memall.h"
117 #endif
118 #if SEARCH_DEF
119 #       include "search.h"
120 #endif
121 #if SEARCHE_DEF
122 #       include "searche.h"
123 #endif
124 #if TOOLS_DEF
125 #       include "tools.h"
126 #endif
127 #if TOOLSE_DEF
128 #       include "toolse.h"
129 #endif
130 #if LOCALS_DEF
131 #       include "locals.h"
132 #endif
133 #if LOCALSE_DEF
134 #       include "localse.h"
135 #endif
136 #if FACILITY_DEF
137 #       include "facility.h"
138 #endif
139 #if FACILITYE_DEF
140 #       include "facilite.h"
141 #endif
142 #ifdef STRING_DEF
143 #       include "string.h"
144 #endif
145 #if FILE_DEF
146 #       include "file.h"
147 #endif
148 #if     FILEE_DEF
149 #       include "filee.h"
150 #endif
151 #if BLOCK_DEF
152 #       include "block.h"
153 #endif
154 #if BLOCKE_DEF
155 #       include "blocke.h"
156 #endif
157 #if EXCEPTION_DEF
158 #       include "exceptio.h"
159 #endif
160 #if EXCEPTIONE_DEF
161 #       include "excepte.h"
162 #endif
163
164         { 0, 0, 0 },
165 };
166
167 #undef DECLARE_WORDS
168
169 /* init_vocabulary: loads words into the real dictionary from the table
170  * builded by including all the header files after the declaration of
171  * DECLARE_WORDS. See the header files such as "core.h" and the macro
172  * file "macro.h" for the implementation of this.
173  * This function returns the dictionary pointer after loading.
174  */
175 void init_vocabulary(Char **dp) {
176         struct word_def *w;
177         Char *name;
178         int i = 0;
179     while (iv[i].name) {                /* Last name is a NULL (see table above) */
180         name = *dp;                     /* "name" is a ptr to the name */
181         **dp = strlen(iv[i].name);      /* first copy length... */
182         strcpy(*dp + 1, iv[i].name);    /* ...and then the actual name */
183         *dp = (Char *) WORD_PTR(*dp);   /* advance "dp" */
184         w = (struct word_def *) *dp;    /* here begins the structure */
185         w->name = name;                 /* adjust pointer... */
186         w->class = iv[i].class;         /* ...and the class of the word */
187         ins_word(w);                    /* Finally adjust the link field... */
188         mark_word(w);                   /* ...accordingly with the hash function
189                                            and make the word visible */
190         *dp += sizeof(struct word_def); /* advance "dp" */
191         switch (iv[i].class & A_WORD) {     /* The last field must be adjusted here */
192                         case A_PRIMITIVE:
193                                 w->func[0] = iv[i].func;
194                                 break;
195                         case A_USER:
196                                 w->func[0] = (pfp) iv[i].func;
197                                 break;
198                 }
199                 i++;
200         }
201 }
202