acl4のプログラムのページ0002
(1) 「式の評価」プログラム
- a4_0008までの関数があれば、そこそこ面白いことができるはずだと思ったので、適当に書いてみました。
(2) p0002a の実行例
>p0002a "1+2*3"
7.000000
>p0002a "2*(3+4)"
14.000000
- ちゃんと演算子の優先順が正しく働いていることがわかります。
- もとはといえば、プリプロセッサを作るには #if とかをやる必要があって、それなら式の評価は必要そうだなと思ったのです。
- プリプロセッサでは整数演算しかやらないので、 double でやる理由はないのですが、まあ何となく今回は double でやってみました。
- 45行でできたっていうのは、なかなか良いと思います。
(3) p0002a.c [45行]
#define a_Version 1
#include <acl4.c>
double eval(Token0 *t0, int pri)
{
const char *t = Token1_get(t0), *s0; double d = 0.0; uint32_t c = t0->c;
if (c == '(') { d = eval(t0, 99); Token1_get(t0); goto op2; }
if (c == '+' && pri >= 2) { d = + eval(t0, 2); goto op2; }
if (c == '-' && pri >= 2) { d = - eval(t0, 2); goto op2; }
if (c == '!' && pri >= 2) { d = ! (int) eval(t0, 2); goto op2; }
if (c == '~' && pri >= 2) { d = ~ (int) eval(t0, 2); goto op2; }
d = strtod(t, NULL);
op2:
s0 = t0->s; t = Token1_get(t0); c = t0->c; // トークンを取ってくる前に、押し戻しのためにt0->sを控えておく.
if (c == '*' && pri >= 4) { d = d * eval(t0, 3); goto op2; }
if (c == '/' && pri >= 4) { d = d / eval(t0, 3); goto op2; }
if (c == '%' && pri >= 4) { d = (int) d % (int) eval(t0, 3); goto op2; }
if (c == '+' && pri >= 5) { d = d + eval(t0, 4); goto op2; }
if (c == '-' && pri >= 5) { d = d - eval(t0, 4); goto op2; }
if (c == ('<' | '<' << 8) && pri >= 6) { d = (int) d << (int) eval(t0, 5); goto op2; }
if (c == ('>' | '>' << 8) && pri >= 6) { d = (int) d >> (int) eval(t0, 5); goto op2; }
if (c == ('<' | '=' << 8) && pri >= 7) { d = d <= eval(t0, 6); goto op2; }
if (c == '<' && pri >= 7) { d = d < eval(t0, 6); goto op2; }
if (c == ('>' | '=' << 8) && pri >= 7) { d = d >= eval(t0, 6); goto op2; }
if (c == '>' && pri >= 7) { d = d > eval(t0, 6); goto op2; }
if (c == ('=' | '=' << 8) && pri >= 8) { d = d == eval(t0, 7); goto op2; }
if (c == ('!' | '=' << 8) && pri >= 8) { d = d != eval(t0, 7); goto op2; }
if (c == '&' && pri >= 9) { d = (int) d & (int) eval(t0, 8); goto op2; }
if (c == '^' && pri >= 10) { d = (int) d ^ (int) eval(t0, 9); goto op2; }
if (c == '|' && pri >= 11) { d = (int) d | (int) eval(t0, 10); goto op2; }
if (c == ('&' | '&' << 8) && pri >= 12) { d = (int) d && (int) eval(t0, 11); goto op2; }
if (c == ('|' | '|' << 8) && pri >= 13) { d = (int) d || (int) eval(t0, 12); goto op2; }
t0->s = s0; return d; // 一度読み込んだ未解釈の演算子をt0に押し戻してからreturn.
}
int main(int argc, const char **argv)
{
VecChr vc[1]; VecChr_iniArg(vc, argc, argv, 1, 3);
Token0 t0[1]; Token0_ini1(t0);
t0->s = vc->p; t0->s1 = vc->p + vc->n;
printf("%f\n", eval(t0, 99));
VecChr_din(vc);
a_malloc_debugList(_arg);
return 0;
}
(4) p0002b.c
(99) 更新履歴