File: ident.c

package info (click to toggle)
minc 2.1.10-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 8,160 kB
  • sloc: ansic: 82,507; sh: 10,666; yacc: 1,187; perl: 612; makefile: 586; lex: 319
file content (68 lines) | stat: -rw-r--r-- 1,279 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* Copyright David Leonard & Andrew Janke, 2000. All rights reserved. */

#define _GNU_SOURCE 1
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "node.h"

struct intern {
   const char *s;
   int id;
   struct intern *next;
   int is_scalar;
};

static struct intern *head = NULL;
static ident_t nextid = 0;

ident_t new_ident(const char *s){
   struct intern *i;

   for (i = head; i; i = i->next)
      if (strcmp(i->s, s) == 0)
         return i->id;

   i = malloc(sizeof *i);
   i->id = nextid++;
   i->s = strdup(s);
   i->next = head;
   i->is_scalar = islower(s[0]);
   head = i;
   return i->id;
}

const char *ident_str(ident_t id){
   struct intern *i;

   for (i = head; i; i = i->next)
      if (i->id == id)
         return i->s;
   return "?";
}

int ident_is_scalar(ident_t id){
   struct intern *i;

   for (i = head; i; i = i->next)
      if (i->id == id)
         return i->is_scalar;
   /* errx(1, "ident_is_scalar: no such ident %d", id); */
   fprintf(stderr, "ident_is_scalar: no such ident %d\n", id);
   exit(1);
   return 0;
}

ident_t ident_lookup(char *string)
{
   struct intern *i;

   for (i = head; i; i = i->next) {
      if (strcmp(i->s, string) == 0) {
         return i->id;
      }
   }
   return -1;

}