OpenCores
URL https://opencores.org/ocsvn/marca/marca/trunk

Subversion Repositories marca

[/] [marca/] [tags/] [INITIAL/] [spar/] [exprs.l] - Blame information for rev 8

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jeunes2
/* This file is part of the assembler "spar" for marca.
2
   Copyright (C) 2007 Wolfgang Puffitsch
3
 
4
   This program is free software; you can redistribute it and/or modify it
5
   under the terms of the GNU Library General Public License as published
6
   by the Free Software Foundation; either version 2, or (at your option)
7
   any later version.
8
 
9
   This program is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
   Library General Public License for more details.
13
 
14
   You should have received a copy of the GNU Library General Public
15
   License along with this program; if not, write to the Free Software
16
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA */
17
 
18
%option nounput
19
 
20
%{
21
 
22
#include 
23
#include 
24
 
25
#include "exprs.h"
26
#include "exprs.tab.h"
27
#include "spar.h"
28
#include "symtab.h"
29
 
30
int exprsparse(int64_t *, int64_t *);
31
 
32
static int allow_syms = 1;
33
static int resolve_syms = 1;
34
static int localize_syms = 0;
35
static char *localized_expr = NULL;
36
 
37
#define YY_USER_ACTION \
38
do { \
39
  if (localize_syms) { \
40
    localized_expr = xrealloc(localized_expr, \
41
                              strlen(localized_expr) \
42
                              + yyleng + 1); \
43
    strcat(localized_expr, yytext); \
44
  } \
45
} while(0);
46
 
47
%}
48
 
49
BNUM (0b[0-1]+)
50
ONUM (0[0-7]*)
51
DNUM ([1-9][0-9]*)
52
XNUM (0x[0-9a-fA-F]+)
53
SYM  (\.?[_\.a-zA-Z0-9@]+)
54
 
55
%%
56
 
57
"hi"   return HI;
58
"lo"   return LO;
59
 
60
"("    return '(';
61
")"    return ')';
62
 
63
"+"    return '+';
64
"-"    return '-';
65
"~"    return '~';
66
"!"    return '!';
67
"*"    return '*';
68
"/"    return '/';
69
"|/|"  return CUDIV;
70
"%"    return '%';
71
"|%|"  return CUMOD;
72
">>"   return CSHR;
73
"<<"   return CSHL;
74
">>>"  return CSLR;
75
"<"    return '<';
76
">"    return '>';
77
"<="   return CLE;
78
">="   return CGE;
79
"|<|"  return CULT;
80
"|>|"  return CUGT;
81
"|<=|" return CULE;
82
"|>=|" return CUGE;
83
"=="   return CEQU;
84
"!="   return CNEQ;
85
"&"    return '&';
86
"^"    return '^';
87
"|"    return '|';
88
"&&"   return CLAND;
89
"||"   return CLOR;
90
"?"    return '?';
91
":"    return ':';
92
"
93
">?"   return CMAX;
94
"|
95
"|>?|" return CUMAX;
96
 
97
{BNUM} {
98
          exprslval.intval = strtol(yytext+2, NULL, 2);
99
          return NUM;
100
       }
101
 
102
{ONUM} {
103
          exprslval.intval = strtol(yytext, NULL, 8);
104
          return NUM;
105
       }
106
 
107
{DNUM} {
108
          exprslval.intval = strtol(yytext, NULL, 10);
109
          return NUM;
110
       }
111
 
112
{XNUM} {
113
          exprslval.intval = strtol(yytext+2, NULL, 16);
114
          return NUM;
115
       }
116
 
117
{SYM}  {
118
          char *s = yytext;
119
 
120
          if (localize_syms)
121
            {
122
              s = localize_string(yytext);
123
              localized_expr = xrealloc(localized_expr,
124
                                        strlen(localized_expr)
125
                                        + strlen(s)
126
                                        - yyleng + 1);
127
              strcpy(localized_expr
128
                     + strlen(localized_expr)
129
                     - yyleng, s);
130
            }
131
          else if (!allow_syms)
132
            {
133
              exprslval.intval = 0;
134
              fprintf(stderr, "no symbol allowed here: `%s'\n", s);
135
              error_count++;
136
            }
137
          else if (!resolve_syms)
138
            {
139
              exprslval.intval = 0;
140
            }
141
          else if (get_sym(s) == NULL)
142
            {
143
              exprslval.intval = 0;
144
              fprintf(stderr, "symbol in expression not found: `%s'\n", s);
145
              error_count++;
146
            }
147
          else
148
            {
149
              exprslval.intval = get_sym(s)->addr;
150
            }
151
 
152
          return IDENT;
153
       }
154
 
155
[ \t\r\n]+ { /* ignore whitespace */ }
156
 
157
%%
158
 
159
int yywrap()
160
{
161
  return 1;
162
}
163
 
164
char *expr_localize(const char *expr)
165
{
166
  int64_t value = 0;
167
  int64_t symcount = 0;
168
 
169
  allow_syms = 1;
170
  resolve_syms = 0;
171
  localize_syms = 1;
172
  localized_expr = xmalloc(1);
173
  localized_expr[0] = '\0';
174
 
175
  exprs_scan_string(expr);
176
  exprsparse(&value, &symcount);
177
 
178
  return localized_expr;
179
}
180
 
181
int64_t expr_symcount(const char *expr)
182
{
183
  int64_t value = 0;
184
  int64_t symcount = 0;
185
 
186
  allow_syms = 1;
187
  resolve_syms = 0;
188
  localize_syms = 0;
189
  localized_expr = NULL;
190
 
191
  exprs_scan_string(expr);
192
  exprsparse(&value, &symcount);
193
 
194
  return symcount;
195
}
196
 
197
int64_t expr_nevaluate(const char *expr)
198
{
199
  int64_t value = 0;
200
  int64_t symcount = 0;
201
 
202
  allow_syms = 0;
203
  resolve_syms = 0;
204
  localize_syms = 0;
205
  localized_expr = NULL;
206
 
207
  exprs_scan_string(expr);
208
  exprsparse(&value, &symcount);
209
 
210
  return value;
211
}
212
 
213
int64_t expr_evaluate(const char *expr)
214
{
215
  int64_t value = 0;
216
  int64_t symcount = 0;
217
 
218
  allow_syms = 1;
219
  resolve_syms = 1;
220
  localize_syms = 0;
221
  localized_expr = NULL;
222
 
223
  exprs_scan_string(expr);
224
  exprsparse(&value, &symcount);
225
 
226
  return value;
227
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.