aboutsummaryrefslogtreecommitdiff
path: root/bf.lang
diff options
context:
space:
mode:
Diffstat (limited to 'bf.lang')
-rw-r--r--bf.lang104
1 files changed, 0 insertions, 104 deletions
diff --git a/bf.lang b/bf.lang
deleted file mode 100644
index 0ce6389..0000000
--- a/bf.lang
+++ /dev/null
@@ -1,104 +0,0 @@
-type byte := char;
-
-func putstr(char[] str) {
- int i := 0;
- while (str[i] != '\0') {
- putc(str[i]);
- i = i + 1;
- }
-}
-
-func int[] makejumpmap(char[] src, int srclen) {
- int[] jm := new int[srclen];
- int[] stack := new int[srclen];
- int stkp := 0;
- int i := 0;
- while (i < srclen) {
- // putint(i); putc(' '); putint(stkp); putc(' '); putc(src[i]); putc('\n');
- if (src[i] == '[') {
- stack[stkp] = i;
- stkp = stkp + 1;
- }
- if (src[i] == ']') {
- stkp = stkp - 1;
- int j := stack[stkp];
- jm[i] = j;
- jm[j] = i;
- }
- i = i + 1;
- }
- if (stkp != 0) {
- putstr("Unmatched opening brackets in BF source\n");
- exit(1);
- }
- /*i = 0;
- while (i < srclen) {
- putint(jm[i]);
- putc(' ');
- i = i + 1;
- }
- putc('\n');*/
- return jm;
-}
-
-func interpret(char[] src, int srclen) {
- int[] jm := makejumpmap(src, srclen);
- byte[] mem := new char[4088];
- int ip := 0;
- int memp := 0;
- while (src[ip] != '\0') {
- // putint(ip); putc('\n');
- char c := src[ip];
- if (c == '+') {mem[memp] = mem[memp] + byte(1);}
- if (c == '-') {mem[memp] = mem[memp] - byte(1);}
- if (c == '>') {memp = memp + 1;}
- if (c == '<') {memp = memp - 1;}
- if (c == '.') {putc(mem[memp]);}
- if (c == ',') {mem[memp] = byte(getc());}
- if (c == '[') {
- if (mem[memp] == '\0') {
- ip = jm[ip];
- }
- }
- if (c == ']') {
- if (mem[memp] != '\0') {
- ip = jm[ip];
- }
- }
- ip = ip + 1;
- }
-
- // debugger;
-
- putc('\n');
- memp = 0;
- while (memp < 10) {
- putint(int(mem[memp]) & 0xff); putc(' ');
- memp = memp + 1;
- }
- putc('\n');
-}
-
-func int main() {
- int bufsize := 12280;
- char[] source := new char[12280];
-
- int sourcelen := 0;
- while (1) {
- int c := getc();
- // putc(char(c));
- if (c < 0) {
- break;
- } else {
- source[sourcelen] = char(c);
- sourcelen = sourcelen + 1;
- if (sourcelen >= bufsize - 1) {
- break;
- }
- }
- }
- source[sourcelen] = '\0';
-
- interpret(source, sourcelen);
- return 0;
-}