File: util.c

package info (click to toggle)
gpasm 0.0.7-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 536 kB
  • ctags: 310
  • sloc: ansic: 2,011; perl: 1,330; yacc: 368; lex: 331; sh: 152; makefile: 34
file content (109 lines) | stat: -rw-r--r-- 2,613 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* Some helpful utility functions for gpasm
   Copyright (C) 1998 James Bowman

This file is part of gpasm.

gpasm is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

gpasm is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with gpasm; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

#include "stdhdr.h"

#include "gpasm.h"
#include "gperror.h"
#include "symbol.h"


/*
 * Parse a numeric constant
 */
int gpasm_number(char *s)
{
  char *endptr;
  int r;

  switch (tolower(s[0])) {
  case 'd':
    r = strtol(s + 2, &endptr, 10);
    break;
  case 'h':
    r = strtol(s + 2, &endptr, 16);
    break;
  case 'o':
    r = strtol(s + 2, &endptr, 8);
    break;
  case 'b':
    r = strtol(s + 2, &endptr, 2);
    break;
  default:
    assert(0); /* This should have been caught in the lexical stage */
  }

  /* Check that the number was OK */
  if (*endptr != '\'') {
    char complaint[80];

    sprintf(complaint,
	    isprint(*endptr) ?
	    "Illegal character '%c' in numeric constant" :
	    "Illegal character %#x in numeric constant",
	    *endptr);
    gperror(113, complaint);
  }

  return r;
}

void set_global(char *name, gpasmVal value)
{
  struct symbol *sym;
  struct variable *var;

  /* Search the entire stack (i.e. include macro's local symbol
     tables) for the symbol.  If not found, then add it to the global
     symbol table.  */

  sym = get_symbol(state.stTop, name);
  if (sym == NULL)
    sym = add_symbol(state.stGlobal, name);
  var = get_symbol_annotation(sym);
  if (var == NULL) {
    var = malloc(sizeof(*var));
    annotate_symbol(sym, var);
    var->value = value;
  } else {
    if ((state.pass == 2) &&
	(var->value != value)) {
      char message[BUFSIZ];

      sprintf(message,
	      "Value of symbol \"%s\" differs on second pass",
	      name);
      gperror(114, message);
    }
  }
}

void select_radix(char *radix_name)
{
  if (strcasecmp(radix_name, "hex") == 0)
    state.radix = 16;
  else if (strcasecmp(radix_name, "dec") == 0)
    state.radix = 10;
  else if (strcasecmp(radix_name, "oct") == 0)
    state.radix = 8;
  else {
    gperror(123, "Unrecognized radix");
  }
}