[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tyndur-devel] [PATCH] Endlich ist es soweit: tyndur kann nun einer der wichtigsten modernen Sprachen interpretieren: Malbolge! Ich hoffe, somit können wir den Großteil der weiteren Treiber und Module und Anwendungen in Malbolge entwickeln. Somit entfällt das Compillieren, denn wir interpretieren von heute an mit diesem ultra-performanten Interpreter.



From: Alexander Hartmut Kluth <derhartmut@xxxxxxxx>

---
 src/modules/c/malbolge/Makefile.all |    7 +
 src/modules/c/malbolge/malbolge.c   |  211 +++++++++++++++++++++++++++++++++++
 2 files changed, 218 insertions(+), 0 deletions(-)
 create mode 100644 src/modules/c/malbolge/Makefile.all
 create mode 100644 src/modules/c/malbolge/malbolge.c

diff --git a/src/modules/c/malbolge/Makefile.all b/src/modules/c/malbolge/Makefile.all
new file mode 100644
index 0000000..0ab349e
--- /dev/null
+++ b/src/modules/c/malbolge/Makefile.all
@@ -0,0 +1,7 @@
+shopt -s extglob
+source $LOST_BUILDMK_ROOT/config.sh
+
+echo "LD   $1/apps/malbolge"
+$LOST_TOOLS_LD -omalbolge -Ttext=0x40000000 *.o --start-group $2 --end-group
+
+mv malbolge $1/apps/
diff --git a/src/modules/c/malbolge/malbolge.c b/src/modules/c/malbolge/malbolge.c
new file mode 100644
index 0000000..0e5c4c2
--- /dev/null
+++ b/src/modules/c/malbolge/malbolge.c
@@ -0,0 +1,211 @@
+/*
+ * Copyright (c) 2009 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Alexander H. Kluth.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the tyndur Project
+ *     and its contributors.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+
+
+void exec(unsigned short *mem);
+unsigned short op(unsigned short x, unsigned short y);
+
+const char xlat1[] =
+  "+b(29e*j1VMEKLyC})8&m#~W>qxdRp0wkrUo[D7,XTcA\"lI"
+  ".v%{gJh4G\\-=O@5`_3i<?Z';FNQuY]szf$!BS/|t:Pn6^Ha";
+
+const char xlat2[] =
+  "5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72FhOA1C"
+  "B6v^=I_0/8|jsb9m<.TVac`uY*MK'X~xDl}REokN:#?G\"i@";
+
+int main(int argc, char **argv)
+{
+    FILE *f;
+    unsigned short i = 0;
+    int x;
+    unsigned short *mem;
+    int lines = 1;
+    
+    if (argc != 2) {
+        fprintf(stderr, "Benutzung: malbolge [DATEI]\n");
+        return 1;
+    }
+    
+    if ((f = fopen(argv[1], "r")) == NULL) {
+        fprintf(stderr, "Fehler beim Öffnen von %s", argv[1]);
+        return 1;
+    }
+
+    mem = (unsigned short *)malloc(sizeof(unsigned short)*65535);
+
+    if (mem == NULL) {
+        fclose(f);
+        fprintf(stderr, "Nicht genug Speicher!\n");
+        return 1;
+    }
+    
+    while ((x = getc(f)) != EOF) {
+        if (isspace(x)) {
+            continue;
+        }
+        
+        if (x == '\n') {
+            lines++;
+        }
+        
+        if (x < 127 && x > 32) {
+            if (strchr( "ji*p</vo", xlat1[(x - 33 + i) % 94] ) == NULL) {
+                fprintf(stderr, "Zeile %i: Invalider Character \n", lines);
+                free(mem);
+                fclose(f);
+                return 1;
+            }
+        }
+        
+        if (i == 65536) {
+            fprintf(stderr, "Fehler: Eingabedatei zu groß\n");
+            free(mem);
+            fclose(f);
+            return 1;
+        }
+        
+        mem[i++] = x;
+    }
+    
+    fclose(f);
+    
+    while (i < 59049) {
+        mem[i] = op( mem[i - 1], mem[i - 2]);
+        i++;
+    }
+        
+    exec(mem);
+    free(mem);
+    return (0);
+}
+
+
+void exec(unsigned short *mem)
+{
+    unsigned short a = 0, c = 0, d = 0;
+    int x = 0;
+  
+    for (;;) {
+        if (mem[c] < 33 || mem[c] > 126) {
+            continue;
+        }
+        
+        switch (xlat1[(mem[c] - 33 + c ) % 94]) {
+            case 'j': 
+                d = mem[d]; 
+                break;
+                
+            case 'i':
+                c = mem[d]; 
+                break;
+                
+            case '*': 
+                a = mem[d] = mem[d] / 3 + mem[d] % 3 * 19683; 
+                break;
+                
+            case 'p': 
+                a = mem[d] = op(a, mem[d]); 
+                break;
+                
+            case '<':
+                if (x == '\n') {
+                    printf("\n"); 
+                } 
+                
+                break;
+            case '/':
+                x = getc(stdin);
+                if (x == '\n') {
+                    a = 10;
+                }
+
+                if (x == EOF) {
+                    a = 65535; 
+                } else {
+                    a = x;
+                }
+                break;
+            case 'v': 
+                return;
+        }
+        
+        mem[c] = xlat2[mem[c] - 33];
+        
+        if (c == 65535) {
+            c = 0; 
+        } else {
+            c++;
+        }
+        
+        if (d == 65535) {
+            d = 0; 
+        } else {
+            d++;
+        }
+    }
+}
+
+
+unsigned short op(unsigned short x, unsigned short y)
+{
+    unsigned short i = 0, j;
+    static const unsigned short p9[5] = { 
+        1, 
+        9, 
+        81, 
+        729, 
+        6561 
+    };
+  
+    static const unsigned short o[9][9] = {
+      { 4, 3, 3, 1, 0, 0, 1, 0, 0 },
+      { 4, 3, 5, 1, 0, 2, 1, 0, 2 },
+      { 5, 5, 4, 2, 2, 1, 2, 2, 1 },
+      { 4, 3, 3, 1, 0, 0, 7, 6, 6 },
+      { 4, 3, 5, 1, 0, 2, 7, 6, 8 },
+      { 5, 5, 4, 2, 2, 1, 8, 8, 7 },
+      { 7, 6, 6, 7, 6, 6, 4, 3, 3 },
+      { 7, 6, 8, 7, 6, 8, 4, 3, 5 },
+      { 8, 8, 7, 8, 8, 7, 5, 5, 4 },
+    };
+  
+    for (j = 0; j < 5; j++) {
+        i += o[y / p9[j] % 9][x / p9[j] % 9] * p9[j];
+    }
+  
+    return i;
+}
+
-- 
1.6.0.4