added "jammod" command and "genman" module
[k8jam.git] / src / parse.c
blobe95c3bac6c237503db9ec24a4a089066288eae73
1 /*
2 * Copyright 1993, 2000 Christopher Seiwald.
3 * This file is part of Jam - see jam.c for Copyright information.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * parse.c - make and destroy parse trees as driven by the parser
21 #include "jam.h"
22 #include "lists.h"
23 #include "parse.h"
24 #include "scan.h"
25 #include "newstr.h"
28 static PARSE *yypsave;
31 #include "jamgram.c"
34 static int yyparse (void) {
35 int tk;
36 void *pp = ParseAlloc(malloc);
37 #ifndef NDEBUG
38 /*ParseTrace(stderr, "**");*/
39 #endif
40 do {
41 tk = yylex();
42 Parse(pp, tk, yylval);
43 } while (tk != 0);
44 ParseFree(pp, free);
45 return 0;
49 void parse_file (const char *f) {
50 /* suspend scan of current file and push this new file in the stream */
51 yyfparse(f);
52 /* now parse each block of rules and execute it */
53 /* execute it outside of the parser so that recursive */
54 /* calls to yyrun() work (no recursive yyparse's) */
55 for (;;) {
56 LOL l;
57 PARSE *p;
58 int jmp = 0; /* JMP_NONE */
59 /* $(<) and $(>) empty in outer scope */
60 lol_init(&l);
61 /* filled by yyparse() calling parse_save() */
62 yypsave = 0;
63 /* if parse error or empty parse, outta here */
64 if (yyparse() || !(p = yypsave)) break;
65 /* run the parse tree */
66 list_free((*(p->func))(p, &l, &jmp));
67 parse_free(p);
72 void parse_save (PARSE *p) {
73 yypsave = p;
77 PARSE *parse_make (
78 LIST *(*func) (PARSE *p, LOL *args, int *jmp),
79 PARSE *left,
80 PARSE *right,
81 PARSE *third,
82 const char *string,
83 const char *string1,
84 int num)
86 PARSE *p = (PARSE *)malloc(sizeof(PARSE));
87 p->func = func;
88 p->left = left;
89 p->right = right;
90 p->third = third;
91 p->string = string;
92 p->string1 = string1;
93 p->num = num;
94 p->refs = 1;
95 return p;
99 void parse_refer (PARSE *p) {
100 ++p->refs;
104 void parse_free (PARSE *p) {
105 if (--p->refs) return;
106 if (p->string) freestr(p->string);
107 if (p->string1) freestr(p->string1);
108 if (p->left) parse_free(p->left);
109 if (p->right) parse_free(p->right);
110 if (p->third) parse_free(p->third);
111 free((char *)p);
115 JAMFA_CONST const char *multiform_suffix (int cnt) {
116 return (cnt == 1 ? "" : "s");