'[' and ']' are delimiters now
[k8jam.git] / src / parse.c
blobf01e8e7a51f501de3a2769d175cc6ed250c58b3d
1 /*
2 * Copyright 1993, 2000 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
7 /*
8 * parse.c - make and destroy parse trees as driven by the parser
10 * 09/07/00 (seiwald) - ref count on PARSE to avoid freeing when used,
11 * as per Matt Armstrong.
12 * 09/11/00 (seiwald) - structure reworked to reflect that (*func)()
13 * returns a LIST *.
14 * 10/22/02 (seiwald) - working return/break/continue statements
15 * 11/04/02 (seiwald) - const-ing for string literals
17 #include "jam.h"
18 #include "lists.h"
19 #include "parse.h"
20 #include "scan.h"
21 #include "newstr.h"
24 static PARSE *yypsave;
27 void parse_file (const char *f) {
28 /* suspend scan of current file and push this new file in the stream */
29 yyfparse(f);
30 /* now parse each block of rules and execute it */
31 /* execute it outside of the parser so that recursive */
32 /* calls to yyrun() work (no recursive yyparse's) */
33 for (;;) {
34 LOL l;
35 PARSE *p;
36 int jmp = 0; /* JMP_NONE */
37 /* $(<) and $(>) empty in outer scope */
38 lol_init(&l);
39 /* filled by yyparse() calling parse_save() */
40 yypsave = 0;
41 /* if parse error or empty parse, outta here */
42 if (yyparse() || !(p = yypsave)) break;
43 /* run the parse tree */
44 list_free((*(p->func))(p, &l, &jmp));
45 parse_free(p);
50 void parse_save (PARSE *p) {
51 yypsave = p;
55 PARSE *parse_make (
56 LIST *(*func) (PARSE *p, LOL *args, int *jmp),
57 PARSE *left,
58 PARSE *right,
59 PARSE *third,
60 const char *string,
61 const char *string1,
62 int num)
64 PARSE *p = (PARSE *)malloc(sizeof(PARSE));
65 p->func = func;
66 p->left = left;
67 p->right = right;
68 p->third = third;
69 p->string = string;
70 p->string1 = string1;
71 p->num = num;
72 p->refs = 1;
73 return p;
77 void parse_refer (PARSE *p) {
78 ++p->refs;
82 void parse_free (PARSE *p) {
83 if (--p->refs) return;
84 if (p->string) freestr(p->string);
85 if (p->string1) freestr(p->string1);
86 if (p->left) parse_free(p->left);
87 if (p->right) parse_free(p->right);
88 if (p->third) parse_free(p->third);
89 free((char *)p);
93 const char *multiFormSfx (int cnt) {
94 return cnt==1?"":"s";