2018-08-07 02:14:36 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2018-08-07 11:03:21 +00:00
|
|
|
#include "xapp.h"
|
|
|
|
#include "xapp.yy.h"
|
2018-08-07 02:14:36 +00:00
|
|
|
|
|
|
|
long long result;
|
|
|
|
|
2019-07-07 18:08:49 +00:00
|
|
|
/* Check if EXP_FRAC is compatible */
|
|
|
|
#if (1 << EXP_FRAC) != (1 << CHAR_BIT) * ((EXP_FULL + CHAR_BIT - 1) / CHAR_BIT)
|
|
|
|
#error "Code needs to be modified to handle current EXP_FRAC!"
|
|
|
|
#endif
|
|
|
|
|
2018-08-07 02:14:36 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
TOKEN tokens[TOKENS];
|
|
|
|
char buffer[IO_BUFFER] = {'\0'};
|
|
|
|
|
|
|
|
/* Process input */
|
|
|
|
while (fgets(buffer, IO_BUFFER, stdin))
|
|
|
|
{
|
|
|
|
/* Tokenize the line */
|
|
|
|
int count;
|
|
|
|
if (count = tokenizeInput(buffer, tokens))
|
|
|
|
{
|
|
|
|
/* Iterate through the tokens */
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < count; ++i)
|
|
|
|
{
|
2018-08-13 12:49:48 +00:00
|
|
|
int j, sign;
|
2018-08-07 02:14:36 +00:00
|
|
|
const char *s = "", *p, *q;
|
|
|
|
switch (tokens[i].type)
|
|
|
|
{
|
2019-07-07 17:25:47 +00:00
|
|
|
/* Process each _SET_V("<label>") command */
|
|
|
|
case TT_LABEL:
|
|
|
|
/* Extract the start and ending indices */
|
|
|
|
p = strchr(tokens[i].text, '"') + 1;
|
|
|
|
q = strrchr(tokens[i].text, '"');
|
|
|
|
j = (int)(q - p);
|
2019-07-07 18:08:49 +00:00
|
|
|
/* Output in .BYTE format - this code is actually dependent on EXP_FRAC */
|
2021-07-08 01:19:07 +00:00
|
|
|
printf("0, <(%.*s - _rdt), >(%.*s - _rdt), 0", j, p, j, p);
|
2019-07-07 17:25:47 +00:00
|
|
|
break;
|
|
|
|
/* Process each _SET_V("<expression>") command */
|
|
|
|
case TT_EXPRESSION:
|
2018-08-07 02:14:36 +00:00
|
|
|
/* Extract the start and ending indices */
|
|
|
|
p = strchr(tokens[i].text, '"') + 1;
|
|
|
|
q = strrchr(tokens[i].text, '"');
|
|
|
|
/* Parse the input */
|
|
|
|
yyin = fmemopen((void *)p, q - p, "r");
|
|
|
|
yyparse();
|
|
|
|
fclose(yyin);
|
2018-08-13 12:49:48 +00:00
|
|
|
/* Round towards ± infinity */
|
|
|
|
sign = result < 0? -1: +1;
|
|
|
|
if (sign < 0) result = -result;
|
|
|
|
result += 1 << (INT_FRAC - EXP_FRAC - 1);
|
|
|
|
/* Normalize */
|
2018-08-14 01:01:18 +00:00
|
|
|
result >>= (INT_FRAC - EXP_FRAC);
|
|
|
|
if (sign < 0) result = -result;
|
2018-08-13 12:49:48 +00:00
|
|
|
/* Output in .BYTE format */
|
2018-08-07 02:14:36 +00:00
|
|
|
for (j = 0; j < EXP_FULL; j += CHAR_BIT)
|
|
|
|
{
|
2018-08-14 01:01:18 +00:00
|
|
|
printf("%s$%02llX", s, result & 0xff);
|
|
|
|
result >>= CHAR_BIT;
|
2018-08-07 02:14:36 +00:00
|
|
|
s = ", ";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Default, just print it */
|
|
|
|
printf("%s", tokens[i].text);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|