summaryrefslogtreecommitdiffhomepage
path: root/mixlib/mix_eval_scanner.l
blob: 8d0d3bd4a6be24127412058863640612e8b15efe (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* -*-c-*- ------------------ mix_eval_scanner.l :
 * scanner used by mix_eval_t
 * ------------------------------------------------------------------
 *  $Id: mix_eval_scanner.l,v 1.4 2005/09/20 19:43:13 jao Exp $
 * ------------------------------------------------------------------
 * Copyright (C) 2000, 2004 Free Software Foundation, Inc.
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

%{
#include <string.h>
#include "mix.h"
#include "xmix_eval.h"

#define YY_DECL							\
  mix_eval_result_t  mix_eval_expr (mix_eval_data_ *data)

/* keep track of current position in buffer */
#define YY_USER_ACTION yypos += yyleng;

#define RETURN_STATE(err)			\
  do {						\
    done = TRUE;                                \
    state = err;				\
    BEGIN (INITIAL);				\
  } while (FALSE)

#define CLEAN_UP()				\
  do {						\
    yy_delete_buffer (buffer);			\
    g_free (expr_cp);				\
 } while (FALSE)

static mix_word_t eval_binop_ (const gchar *op, mix_word_t x, mix_word_t y);

static int unput_word_ (mix_word_t word);

/*   { */
/*     gchar *value; */
/*     gint k, result; */
/*     value = g_strdup_printf ("%s%ld", */
/*                              mix_word_is_negative (word)? "-":"+", */
/*                              mix_word_magnitude (word)); */
/*     result = strlen (value); */
/*     for (k = result - 1; k >= 0; --k) */
/*       unput (value[k]); */
/*     g_free (value); */
/*     return result; */
/*   } */

%}

%option nomain
%option caseless
%option array
%option stack
%option noyywrap
%option noyy_top_state
%option noreject
%option outfile="lex.yy.c"

%s  EVAL
%s  WEVAL

ws	[ \t]+
digit	[0-9]
letter	[A-Z]
number	[+-]?{digit}+
mixchar [0-9A-Z .,'')(+*/=$<>@;:\-]
locsymbol   {digit}H
flocsymbol  {digit}F
blocsymbol  {digit}B
symbol	{digit}*{letter}+[A-Z0-9]*
binops	"+"|"-"|"/"|"//"|":"
binop	{binops}|"*"
atexpr	{digit}+|{symbol}|\*
expr	[+-]?{atexpr}({binop}{1}{atexpr})*
fpart   \({expr}\)
wexpr   {expr}({fpart})?(,{expr}({fpart})?)*


%%

%{
  YY_BUFFER_STATE buffer;
  mix_word_t expr_val = MIX_WORD_ZERO, wexpr_val = MIX_WORD_ZERO;
  mix_word_t wexpr_val_tmp = MIX_WORD_ZERO;
  mix_eval_result_t state = MIX_EVAL_OK;
  gchar *expr_cp;
  gint yypos = -22; /* to account for padding */
  gboolean is_fp = FALSE, done = FALSE;
  g_assert (data != NULL && data->expr != NULL);
  /* make room enough to unput computed values */
  expr_cp = g_strdup_printf ("%-20s%s"," ", data->expr);
  buffer = yy_scan_string (expr_cp);
  data->errpos = -1;
%}


<*><<EOF>> {
  CLEAN_UP ();
  return MIX_EVAL_INTERN;
}

<INITIAL>{
  {ws} /* eat whitespace */
  . {
    if (!done && state == MIX_EVAL_OK) {
       yypos -= yyleng; yyless (0);
      yy_push_state (WEVAL);
    } else {
      CLEAN_UP ();
      if (state == MIX_EVAL_OK) return (MIX_EVAL_SYNTAX);
      data->errpos = yypos;
      return state;
    }
  }
  "\n" {
    CLEAN_UP();
    if (state == MIX_EVAL_OK)
	data->value = wexpr_val;
    else
	data->errpos = yypos;
    return state;
  }
}

<WEVAL>{
  {number}"(" {
    is_fp = TRUE;
    wexpr_val_tmp = mix_word_new (atol (yytext));
  }
  {number}")"  {
    glong val  = atol (yytext);
    if ( !is_fp ) {
      RETURN_STATE (MIX_EVAL_MIS_PAREN);
    }  else if ( val < 0 || val > MIX_BYTE_MAX
         || !mix_fspec_is_valid (mix_byte_new (val)) ) {
      RETURN_STATE (MIX_EVAL_INV_FSPEC);
    } else {
      is_fp = FALSE;
      wexpr_val = mix_word_store_field (mix_byte_new (val),
					wexpr_val_tmp,
					wexpr_val);
    }
  }
  {number}/({ws}*\n)|[,()] {
    wexpr_val = mix_word_new (atol (yytext));
  }
  {expr}/({ws}*\n)|[,()] {
    if (yytext[0] != '*')
      {
	yypos -= yyleng;
	yyless (0);
      }
    else
      {
	yypos -= yyleng - 1;
	expr_val = mix_short_to_word_fast (data->loc);
	yyless (1);
      }
    yy_push_state (EVAL);
  }
  ,/{expr} /* eat comma if followed by expression */
  [\t\n ] { /* ok if not inside an f-part */
    if ( is_fp ) {
      RETURN_STATE (MIX_EVAL_MIS_PAREN);
    }
    unput (yytext[yyleng-1]); --yypos;
    done = TRUE;
    yy_pop_state ();
  }
  .  RETURN_STATE (MIX_EVAL_SYNTAX);
}

<EVAL>{
  {binop}{digit}+ {
    const gchar *s = ( yytext[1] == '/' ) ? yytext+2 : yytext+1;
    mix_word_t value = mix_word_new (atol (s));
    expr_val = eval_binop_ (yytext, expr_val, value);
  }
  {binop}{symbol} {
    const gchar *s = ( yytext[1] == '/' ) ? yytext+2 : yytext+1;
    if ( !mix_symbol_table_is_defined (data->table, s) ) {
      RETURN_STATE (MIX_EVAL_UNDEF_SYM);
    }
    expr_val = eval_binop_ (yytext, expr_val,
			    mix_symbol_table_value (data->table, s));
  }
  {binop}"*" {
    expr_val = eval_binop_ (yytext, expr_val,
			    mix_short_to_word_fast (data->loc));
  }
  "*"  yypos -= unput_word_ (mix_short_to_word_fast (data->loc));
  {number}    expr_val = mix_word_new (atol (yytext));
  {symbol} {
    if ( !mix_symbol_table_is_defined (data->table, yytext) ) {
      RETURN_STATE (MIX_EVAL_UNDEF_SYM);
    }
    expr_val = mix_symbol_table_value (data->table, yytext);
  }
  [,)(\n\t ]   {
    unput (yytext[0]); --yypos;
    yypos -= unput_word_ (expr_val);
    yy_pop_state ();
  }

  .   RETURN_STATE (MIX_EVAL_SYNTAX);
}


%%

static mix_word_t
eval_binop_ (const gchar *op, mix_word_t x, mix_word_t y)
{
  mix_word_t result = MIX_WORD_ZERO;
  switch (op[0])
    {
    case '+':
      result = mix_word_add (x,y);
      break;
    case '-':
      result = mix_word_sub (x,y);
      break;
    case '*':
      mix_word_mul (x, y, NULL, &result);
      break;
    case ':':
      {
	mix_word_t a;
	mix_word_mul (x, 8, NULL, &a);
	result = mix_word_add (a, y);
	break;
      }
    case '/':
      if ( strlen (op) > 1 && op[1] == '/' ) {
	mix_word_div (x,MIX_WORD_ZERO,y, &result, NULL);
      } else {
	mix_word_div (MIX_WORD_ZERO, x, y, &result, NULL);
      }
      break;
    default:
      g_assert_not_reached ();
    }
  return result;
}

static int
unput_word_ (mix_word_t word)
{
  gchar *value;
  gint k, result;
  value = g_strdup_printf ("%s%ld",
                           mix_word_is_negative (word)? "-":"+",
                           mix_word_magnitude (word));
  result = strlen (value);
  for (k = result - 1; k >= 0; --k)
    unput (value[k]);
  g_free (value);
  return result;
}