semantics.c

Sun, 30 Dec 2001 17:09:08 +0000

author
eric
date
Sun, 30 Dec 2001 17:09:08 +0000
changeset 18
3de372e4b230
parent 16
dbf5e39b1658
child 19
e9bf1ed4f331
permissions
-rw-r--r--

*** empty log message ***

eric@18 1 #include <stdlib.h>
eric@18 2 #include <string.h>
eric@16 3 #include <stdio.h>
eric@16 4
eric@16 5 #include "type.h"
eric@18 6 #include "semantics.h"
eric@16 7 #include "parser.tab.h"
eric@16 8
eric@16 9
eric@16 10 FILE *yyin;
eric@16 11 int line; /* line number in spec file */
eric@16 12
eric@16 13
eric@18 14 int input_page_count; /* total input pages in spec */
eric@18 15 int output_page_count; /* total output pages in spec */
eric@18 16
eric@18 17
eric@18 18 input_context_t *current_input_context;
eric@16 19
eric@16 20
eric@16 21 void input_push_context (input_context_type_t type)
eric@16 22 {
eric@18 23 input_context_t *new_input_context;
eric@18 24
eric@18 25 new_input_context = malloc (sizeof (input_context_t));
eric@18 26 if (! new_input_context)
eric@18 27 {
eric@18 28 fprintf (stderr, "failed to calloc an input context\n");
eric@18 29 return;
eric@18 30 }
eric@18 31
eric@18 32 if (current_input_context)
eric@18 33 {
eric@18 34 memcpy (new_input_context, current_input_context, sizeof (input_context_t));
eric@18 35 new_input_context->page_count = 0;
eric@18 36 }
eric@18 37 else
eric@18 38 memset (new_input_context, 0, sizeof (input_context_t));
eric@18 39
eric@18 40 new_input_context->parent_input_context = current_input_context;
eric@18 41 current_input_context = new_input_context;
eric@16 42 };
eric@16 43
eric@16 44 void input_pop_context (void)
eric@16 45 {
eric@18 46 if (! current_input_context)
eric@18 47 {
eric@18 48 fprintf (stderr, "failed to pop an input context\n");
eric@18 49 return;
eric@18 50 }
eric@18 51
eric@18 52 current_input_context = current_input_context->parent_input_context;
eric@16 53 };
eric@16 54
eric@16 55 void input_set_file (char *name)
eric@16 56 {
eric@16 57 };
eric@16 58
eric@16 59 void input_images (int first, int last)
eric@16 60 {
eric@18 61 input_page_count += ((last - first) + 1);
eric@16 62 if (first == last)
eric@16 63 printf ("image %d\n", first);
eric@16 64 else
eric@18 65 printf ("images %d..%d\n", first, last);
eric@16 66 }
eric@16 67
eric@18 68
eric@18 69 void output_push_context (void)
eric@18 70 {
eric@18 71 };
eric@18 72
eric@16 73 void output_set_file (char *name)
eric@16 74 {
eric@16 75 };
eric@16 76
eric@16 77 void output_pages (int first, int last)
eric@16 78 {
eric@18 79 output_page_count += ((last - first) + 1);
eric@16 80 if (first == last)
eric@16 81 printf ("page %d\n", first);
eric@16 82 else
eric@16 83 printf ("pages %d..%d\n", first, last);
eric@16 84 }
eric@16 85
eric@16 86
eric@16 87 void yyerror (char *s)
eric@16 88 {
eric@16 89 fprintf (stderr, "%d: %s\n", line, s);
eric@16 90 }
eric@16 91
eric@16 92
eric@16 93 boolean parse_spec_file (char *fn)
eric@16 94 {
eric@16 95 boolean result = 0;
eric@16 96
eric@16 97 yyin = fopen (fn, "r");
eric@16 98 if (! yyin)
eric@16 99 {
eric@16 100 fprintf (stderr, "can't open spec file '%s'\n", fn);
eric@16 101 goto fail;
eric@16 102 }
eric@16 103
eric@16 104 line = 1;
eric@16 105
eric@18 106 input_push_context (INPUT_CONTEXT_ALL); /* create initial input context */
eric@18 107 output_push_context (); /* create initial output context */
eric@18 108
eric@16 109 yyparse ();
eric@16 110
eric@18 111 if (input_page_count != output_page_count)
eric@18 112 {
eric@18 113 fprintf (stderr, "input page count %d != output page count %d\n",
eric@18 114 input_page_count, output_page_count);
eric@18 115 goto fail;
eric@18 116 }
eric@18 117
eric@18 118 fprintf (stderr, "%d pages specified\n", input_page_count);
eric@18 119
eric@16 120 result = 1;
eric@16 121
eric@16 122 fail:
eric@16 123 if (yyin)
eric@16 124 fclose (yyin);
eric@16 125
eric@16 126 return (result);
eric@16 127 }