home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- *
- * G E T _ S T M T . C
- * -------------------
- *
- * Description:
- * Compiles a C-statement block into an allocated memory chain.
- *
- * Included functions:
- * get_stmt - Does the job
- *
- * Revision:
- * Ver Date By Reason
- * --- ---- -- ------
- * 1.00 900625 Lars Berntzon Created
- *
- ******************************************************************************/
-
- #include "config.h"
-
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
-
- #ifdef STDLIB_H
- #include <stdlib.h>
- #endif
- #ifdef MALLOC_H
- #include <malloc.h>
- #endif
-
- #include "token.h"
- #include "comp.h"
-
- #define isnormal(ch) (isalnum(ch) || (ch) == '_')
-
- struct stmt *get_stmt()
- {
- char input[500];
- char token[TOKENSIZE];
- struct stmt *first = NULL, *sp = NULL, *tail = NULL;
- int pos = 0;
- int nesting = 1;
-
- if (GetTokNC(token) == NULL) {
- error("premature end of file fot C-statement");
- return NULL;
- }
-
- if (strcmp(token, "{") != 0) {
- error("expected '{' for C-statement");
- return NULL;
- }
-
- for(;;) {
- if (GetTokNC(token) == NULL) {
- error("premature end of file for C-statement");
- return NULL;
- }
-
- if (strcmp(token, "{") == 0) nesting++;
- else if (strcmp(token, "}") == 0) nesting--;
-
- if (newline || nesting == 0) {
- if (pos != 0) {
- sp = memalloc(offsetof(struct stmt, txt[strlen(input) + 1]));
- sp->line = line - newline;
- strcpy(sp->txt, input);
- sp->next = NULL;
- if (first == NULL) {
- first = sp;
- }
- else {
- tail->next = sp;
- }
- tail = sp;
- }
- if (nesting == 0) break;
-
- pos = (nesting - 1) * 4; /* indentation */
- memset(input, ' ', pos);
- input[pos] = 0;
- }
-
- if (pos != 0 && isnormal(token[0]) && isnormal(input[pos - 1])) {
- strcpy(input + pos, " ");
- pos += 1;
- }
- strcpy(input + pos, token);
- pos += strlen(token);
- }
- return first;
- }
-
- void unget_stmt(struct stmt *sp)
- {
- struct stmt *next;
-
- for(; sp != NULL; sp = next) {
- next = sp->next;
- free(sp);
- }
- }
-
-
-