cosmetix
[k8jam.git] / src / parse.c
blobcb0df003e5ca121110727394cb8fff4bba78a9ad
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 #include "jamgram.c"
30 static int yyparse (void) {
31 int tk;
32 void *pp = ParseAlloc(malloc);
33 #ifndef NDEBUG
34 /*ParseTrace(stderr, "**");*/
35 #endif
36 do {
37 tk = yylex();
38 Parse(pp, tk, yylval);
39 } while (tk != 0);
40 ParseFree(pp, free);
41 return 0;
45 void parse_file (const char *f) {
46 /* suspend scan of current file and push this new file in the stream */
47 yyfparse(f);
48 /* now parse each block of rules and execute it */
49 /* execute it outside of the parser so that recursive */
50 /* calls to yyrun() work (no recursive yyparse's) */
51 for (;;) {
52 LOL l;
53 PARSE *p;
54 int jmp = 0; /* JMP_NONE */
55 /* $(<) and $(>) empty in outer scope */
56 lol_init(&l);
57 /* filled by yyparse() calling parse_save() */
58 yypsave = 0;
59 /* if parse error or empty parse, outta here */
60 if (yyparse() || !(p = yypsave)) break;
61 /* run the parse tree */
62 list_free((*(p->func))(p, &l, &jmp));
63 parse_free(p);
68 void parse_save (PARSE *p) {
69 yypsave = p;
73 PARSE *parse_make (
74 LIST *(*func) (PARSE *p, LOL *args, int *jmp),
75 PARSE *left,
76 PARSE *right,
77 PARSE *third,
78 const char *string,
79 const char *string1,
80 int num)
82 PARSE *p = (PARSE *)malloc(sizeof(PARSE));
83 p->func = func;
84 p->left = left;
85 p->right = right;
86 p->third = third;
87 p->string = string;
88 p->string1 = string1;
89 p->num = num;
90 p->refs = 1;
91 return p;
95 void parse_refer (PARSE *p) {
96 ++p->refs;
100 void parse_free (PARSE *p) {
101 if (--p->refs) return;
102 if (p->string) freestr(p->string);
103 if (p->string1) freestr(p->string1);
104 if (p->left) parse_free(p->left);
105 if (p->right) parse_free(p->right);
106 if (p->third) parse_free(p->third);
107 free((char *)p);
111 JAMFA_CONST const char *multiform_suffix (int cnt) {
112 return (cnt == 1 ? "" : "s");