This repository has been archived by the owner on Jun 12, 2021. It is now read-only.
/
brute_main.c
53 lines (44 loc) · 1.74 KB
/
brute_main.c
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
#include "forth.h"
#include <assert.h> // assert
#include <ctype.h> // isspace
#include <stdio.h> // EOF, getchar, printf
#include <string.h> // bzero
#define UNUSED __attribute__((unused))
static void push(struct forth* f, double v) { forth_push(f, v); }
static double pop(struct forth* f) { return forth_pop(f); }
#define WORD(word) static void word(struct forth* f, void* arg UNUSED)
WORD(add) { double b=pop(f), a=pop(f); push(f, a+b); }
WORD(sub) { double b=pop(f), a=pop(f); push(f, a-b); }
WORD(mul) { double b=pop(f), a=pop(f); push(f, a*b); }
WORD(div) { double b=pop(f), a=pop(f); push(f, a/b); }
WORD(drop) { pop(f); }
WORD(dup) { double v=pop(f); push(f,v); push(f,v); }
WORD(rot) { double a=pop(f), b=pop(f), c=pop(f); push(f,b); push(f,a); push(f,c); }
WORD(swap) { double a=pop(f), b=pop(f); push(f,a); push(f,b); }
int main(int argc UNUSED, char** argv UNUSED) {
char buf[128] = {0};
size_t len = 0;
struct forth f = {0, 0, {0}};
forth_add_normal(&f, "+", add, NULL);
forth_add_normal(&f, "-", sub, NULL);
forth_add_normal(&f, "*", mul, NULL);
forth_add_normal(&f, "/", div, NULL);
forth_add_normal(&f, "drop", drop, NULL);
forth_add_normal(&f, "dup", dup, NULL);
forth_add_normal(&f, "rot", rot, NULL);
forth_add_normal(&f, "swap", swap, NULL);
for (int c = getchar(); c != EOF; c = getchar()) {
if (len > 0 && isspace(c)) {
forth_eval(&f, buf, len);
bzero(buf, len);
len = 0;
for (size_t i = 0; '\n' == c && i < f.count; i++) {
printf("%g%c", f.stack[i], i == f.count - 1 ? '\n' : ' ');
}
continue;
}
assert (len < sizeof(buf));
buf[len++] = (char)c;
}
return 0;
}