cosmetic fixes
[k8jam.git] / parse.c
blobfe380348976a1a306899eefb1497f946a451e6ad
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
18 # include "jam.h"
19 # include "lists.h"
20 # include "parse.h"
21 # include "scan.h"
22 # include "newstr.h"
24 static PARSE *yypsave;
26 void
27 parse_file( const char *f )
29 /* Suspend scan of current file */
30 /* and push this new file in the stream */
32 yyfparse(f);
34 /* Now parse each block of rules and execute it. */
35 /* Execute it outside of the parser so that recursive */
36 /* calls to yyrun() work (no recursive yyparse's). */
38 for(;;)
40 LOL l;
41 PARSE *p;
42 int jmp = 0; /* JMP_NONE */
44 /* $(<) and $(>) empty in outer scope. */
46 lol_init( &l );
48 /* Filled by yyparse() calling parse_save() */
50 yypsave = 0;
52 /* If parse error or empty parse, outta here */
54 if( yyparse() || !( p = yypsave ) )
55 break;
57 /* Run the parse tree. */
59 list_free( (*(p->func))( p, &l, &jmp ) );
61 parse_free( p );
65 void
66 parse_save( PARSE *p )
68 yypsave = p;
71 PARSE *
72 parse_make(
73 LIST *(*func)( PARSE *p, LOL *args, int *jmp ),
74 PARSE *left,
75 PARSE *right,
76 PARSE *third,
77 const char *string,
78 const char *string1,
79 int num )
81 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;
92 return p;
95 void
96 parse_refer( PARSE *p )
98 ++p->refs;
101 void
102 parse_free( PARSE *p )
104 if( --p->refs )
105 return;
107 if( p->string )
108 freestr( p->string );
109 if( p->string1 )
110 freestr( p->string1 );
111 if( p->left )
112 parse_free( p->left );
113 if( p->right )
114 parse_free( p->right );
115 if( p->third )
116 parse_free( p->third );
118 free( (char *)p );