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

[tyndur-devel] [PATCH] Interpreter für HQ9+



+ c: Ein Interpreter für die multifunktionelle Sprache HQ9+

Signed-off-by: Alexander Hartmut Kluth <hartmut@xxxxxxxxxx>
---
 src/modules/c/hq9+/Makefile.all |    7 ++
 src/modules/c/hq9+/main.c       |  164 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 171 insertions(+), 0 deletions(-)
 create mode 100644 src/modules/c/hq9+/Makefile.all
 create mode 100644 src/modules/c/hq9+/main.c

diff --git a/src/modules/c/hq9+/Makefile.all b/src/modules/c/hq9+/Makefile.all
new file mode 100644
index 0000000..ea43dc1
--- /dev/null
+++ b/src/modules/c/hq9+/Makefile.all
@@ -0,0 +1,7 @@
+shopt -s extglob
+source $LOST_BUILDMK_ROOT/config.sh
+
+echo "LD   $1/apps/hq9+"
+$LOST_TOOLS_LD -ohq9+ -Ttext=0x40000000 *.o --start-group $2 --end-group
+
+mv hq9+ $1/apps/
diff --git a/src/modules/c/hq9+/main.c b/src/modules/c/hq9+/main.c
new file mode 100644
index 0000000..1b1df33
--- /dev/null
+++ b/src/modules/c/hq9+/main.c
@@ -0,0 +1,164 @@
+/*
+ * Copyright (c) 2009 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Alexander Hartmut 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.
+ * 4. Neither the name of the tyndur Project nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * 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>
+
+
+void usage(void);
+void print_99_bottles_of_beer(void);
+
+int main(int argc, char* argv[])
+{
+    FILE* srcfp;
+    char  instruction;
+    char  inst_buffer[2048];
+    int   line = 0, cur_inst_num, all_inst_num;
+    int   accumulator = 0;
+
+    if (argc < 2) {
+        usage();
+
+        return -1;
+    }
+
+    srcfp = fopen(argv[1], "r");
+
+    if (srcfp == NULL) {
+        fprintf(stderr, "Fehler: Konnte %s nicht öffnen!\n", argv[1]);
+
+        return EXIT_FAILURE;
+    }
+
+    cur_inst_num = 0;
+
+    // Erstmal komplette Datei einlesen
+    while ((instruction = fgetc(srcfp))) {
+        inst_buffer[cur_inst_num++] = instruction;
+
+        // Bei Befehl 2048 angekommen? Buffer voll, Speicher abbrechen
+        if (cur_inst_num == 2048) {
+            break;
+        }
+    }
+
+    fclose(srcfp);
+
+    // Speichere die Nummer des letzten Befehls
+    all_inst_num = cur_inst_num;
+
+    for (cur_inst_num = 0; cur_inst_num < all_inst_num; cur_inst_num++) {
+        if (inst_buffer[cur_inst_num] == 'H' ||
+            inst_buffer[cur_inst_num] == 'h') {
+            printf("Hallo Welt!\n");
+        } else if (inst_buffer[cur_inst_num] == 'Q' ||
+                   inst_buffer[cur_inst_num] == 'q') {
+            int i;
+
+            for (i = 0; i <= all_inst_num; i++) {
+                printf("%c", inst_buffer[i]);
+            }
+        } else if (inst_buffer[cur_inst_num] == '9') {
+            print_99_bottles_of_beer();
+        } else if (inst_buffer[cur_inst_num] == '+') {
+            accumulator++;
+        } else if (inst_buffer[cur_inst_num] == '\n') {
+            line++;
+        } else if (inst_buffer[cur_inst_num] == ' ') {
+
+        } else {
+            if (inst_buffer[cur_inst_num] == '\0') {
+                break;
+            }
+
+            fprintf(stderr, "Unknown instruction %c, number %i at line %i\n",
+            inst_buffer[cur_inst_num], cur_inst_num, line);
+            break;
+        }
+     }
+
+    return EXIT_SUCCESS;
+}
+
+
+void usage()
+{
+    printf("Benutzung: hq9+ DATEINAME\n");
+}
+
+
+void print_99_bottles_of_beer()
+{
+    int i;
+
+    for (i = 99; i >= 0; i--) {
+        printf("%i Bottle", i);
+
+        if (i == 1) {
+            printf(" ");
+        } else {
+            printf("s ");
+        }
+
+        printf("of beer on the wall, %i Bottle", i);
+
+        if (i == 1) {
+            printf(" ");
+        } else {
+            printf("s ");
+        }
+
+        printf("of beer.\n");
+
+        if (i == 0) {
+            printf(
+            "Go to the store and buy some more, 99 bottles of beer on the wall."
+            );
+        } else {
+            int temp = i - 1;
+
+            printf("Take one down and pass it aroud, %i bottle", temp);
+
+            if (temp == 1) {
+                printf(" ");
+            } else {
+                printf("s ");
+            }
+
+            printf("of beer on the wall.\n");
+        }
+    }
+}
+
+
-- 
1.6.0.4