]> Nishi Git Mirror - serenade.git/commitdiff
parsing works, and formatting
authornishi <nishi@0f02c867-ac3d-714e-8a88-971ba1f6efcf>
Tue, 23 Apr 2024 11:17:41 +0000 (11:17 +0000)
committernishi <nishi@0f02c867-ac3d-714e-8a88-971ba1f6efcf>
Tue, 23 Apr 2024 11:17:41 +0000 (11:17 +0000)
git-svn-id: file:///raid/svn-main/nishi-serenade/trunk@18 0f02c867-ac3d-714e-8a88-971ba1f6efcf

.clang-format [new file with mode: 0644]
Makefile
Serenade/main.c
Serenade/parser.c
Serenade/parser.h
Serenade/util.c
Tool/configgen.c

diff --git a/.clang-format b/.clang-format
new file mode 100644 (file)
index 0000000..53f287c
--- /dev/null
@@ -0,0 +1,12 @@
+---
+# $Id$
+Language: Cpp
+UseTab: Always
+TabWidth: 8
+IndentWidth: 8
+PointerAlignment: Left
+ColumnLimit: 1024
+AllowShortIfStatementsOnASingleLine: Always
+AllowShortBlocksOnASingleLine: Never
+AllowShortLoopsOnASingleLine: true
+SpaceBeforeParens: Never
index f350eae84e460716353070bc1120f465a09c8ab2..a1b4d927ce11f7f858ac746b7ad078ae13a77674 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ SUFFIX =
 
 .include "platform.mk"
 
-.PHONY: all ./Serenade ./Tool replace
+.PHONY: all ./Serenade ./Tool replace format
 
 all: ./Tool ./config.h ./Serenade
 
@@ -35,3 +35,6 @@ replace:
                mv $$i.new $$i; \
                echo "done"; \
        done
+
+format:
+       clang-format -i $(FILES)
index ac211f1952fab6c7bd63f91002de0c8e0a2eeb04..b4f8d16553175ad52316c2bbed183f45e17d7a3e 100644 (file)
 /* -------------------------------------------------------------------------- */
 /* --- END LICENSE --- */
 
-#include "serenade.h"
-#include "parser.h"
 #include "../config.h"
+#include "parser.h"
+#include "serenade.h"
 
-#include <string.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/stat.h>
 
-int main(int argc, char** argv){
+int main(int argc, char** argv) {
        int i;
        bool loaded = false;
-       for(i = 1; i < argc; i++){
-               if(argv[i][0] == '-'){
-                       if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0){
+       for(i = 1; i < argc; i++) {
+               if(argv[i][0] == '-') {
+                       if(strcmp(argv[i], "--version") == 0 || strcmp(argv[i], "-V") == 0) {
                                printf("Serenade LISP %s\n", SERENADE_VERSION);
                                printf("Support: %s\n", SUPPORT);
                                printf("Stack size: %d\n", STACK_SIZE);
                                return 1;
-                       }else{
+                       } else {
                                fprintf(stderr, "%s: %s: invalid option\n", argv[0], argv[i]);
                                return 1;
                        }
-               }else{
+               } else {
                        /* file input */
                        loaded = true;
                        struct stat s;
-                       if(stat(argv[i], &s) == 0){
+                       if(stat(argv[i], &s) == 0) {
                                char* str = malloc(s.st_size);
                                FILE* f = fopen(argv[i], "rb");
-                               if(f == NULL){
+                               if(f == NULL) {
                                        fprintf(stderr, "%s: %s: fopen fail\n", argv[0], argv[i]);
                                        free(str);
                                        return 1;
                                }
                                fread(str, 1, s.st_size, f);
                                struct sn_generic** t = sn_parse(str, s.st_size);
-                               if(t != NULL){
+                               if(t != NULL) {
                                        int j;
-                                       for(j = 0; t[j] != NULL; j++){
+                                       for(j = 0; t[j] != NULL; j++) {
                                                sn_generic_free(t[j]);
                                        }
                                        free(t);
                                }
                                free(str);
-                       }else{
+                       } else {
                                fprintf(stderr, "%s: %s: stat fail\n", argv[0], argv[i]);
                                return 1;
                        }
                }
        }
 #ifdef HAS_REPL_SUPPORT
-       if(!loaded){
+       if(!loaded) {
                printf("Welcome to Serenade LISP %s\n", SERENADE_VERSION);
                printf("Support: %s\n", SUPPORT);
                printf("Stack size: %d\n", STACK_SIZE);
        }
        return 0;
 #else
-       if(!loaded){
+       if(!loaded) {
                fprintf(stderr, "usage: %s [options] input\n", argv[0]);
                return 1;
        }
index e7c72facb24a0475cb5a8141dbd30d99c363901b..2638bc23b7c294f8284720bed441efeac8615b2d 100644 (file)
@@ -29,8 +29,8 @@
 /* --- END LICENSE --- */
 
 #include "parser.h"
-#include "util.h"
 #include "../config.h"
+#include "util.h"
 
 #include <stdbool.h>
 #include <stddef.h>
@@ -48,140 +48,137 @@ struct sn_generic {
 };
 */
 
-#define PUSH_STACK(x)          if(strlen(argbuf) > 0 && br_stack[x] >= 0){ \
-                                       struct sn_generic* new_gn = malloc(sizeof(*new_gn)); \
-                                       new_gn->type = argbufmode; \
-                                       if(argbufmode == SN_TYPE_DOUBLE){ \
-                                               new_gn->number = atof(argbuf); \
-                                       }else if(argbufmode == SN_TYPE_STRING){ \
-                                               new_gn->string = sn_strdup(argbuf); \
-                                               new_gn->string_length = strlen(argbuf); \
-                                       } \
-                                       int j; \
-                                       struct sn_generic** old_args = gn_stack[x]->tree->args; \
-                                       for(j = 0; old_args[j] != NULL; j++); \
-                                       gn_stack[x]->tree->args = malloc(sizeof(*gn_stack[x]->tree->args) * (j + 2)); \
-                                       for(j = 0; old_args[j] != NULL; j++){ \
-                                               gn_stack[x]->tree->args[j] = old_args[j]; \
-                                       } \
-                                       gn_stack[x]->tree->args[j] = new_gn; \
-                                       gn_stack[x]->tree->args[j + 1] = NULL; \
-                                       free(old_args); \
-                               } \
-                               free(argbuf); \
-                               argbuf = malloc(1); \
-                               argbuf[0] = 0;
+void push_stack_generic(struct sn_generic* gen, struct sn_generic* pushthis) {
+       if(gen->type == SN_TYPE_TREE) {
+               struct sn_generic** old_gen = gen->tree->args;
+               int i;
+               for(i = 0; old_gen[i] != NULL; i++)
+                       ;
+               gen->tree->args = malloc(sizeof(struct sn_generic*) * (i + 2));
+               for(i = 0; old_gen[i] != NULL; i++) {
+                       gen->tree->args[i] = old_gen[i];
+               }
+
+               gen->tree->args[i] = pushthis;
+               gen->tree->args[i + 1] = NULL;
+
+               free(old_gen);
+       }
+}
 
-struct sn_generic* sn_expr_parse(char* data, unsigned long long size){
+void push_stack(struct sn_generic* gen, char* buf, int mode) {
+       struct sn_generic* newgen = malloc(sizeof(struct sn_generic));
+       newgen->type = mode;
+       if(mode == SN_TYPE_STRING || mode == SN_TYPE_FUNCTION) {
+               newgen->string = sn_strdup(buf);
+               newgen->string_length = strlen(buf);
+       } else if(mode == SN_TYPE_DOUBLE) {
+               newgen->number = atof(buf);
+       }
+       push_stack_generic(gen, newgen);
+}
+
+struct sn_generic* sn_expr_parse(char* data, unsigned long long size) {
        int i;
        int br = 0;
        bool dq = false;
-       int* br_stack = malloc(sizeof(*br_stack) * STACK_SIZE);
-       char** op_stack = malloc(sizeof(*op_stack) * STACK_SIZE);
        struct sn_generic** gn_stack = malloc(sizeof(*gn_stack) * STACK_SIZE);
-       for(i = 0; i < STACK_SIZE; i++){
-               br_stack[i] = 0;
-               op_stack[i] = NULL;
-               gn_stack[i] = NULL;
-       }
-       int argbufmode = 0;
+       int* index_stack = malloc(sizeof(int) * STACK_SIZE);
        char* argbuf = malloc(1);
        argbuf[0] = 0;
-       for(i = 0; i < size; i++){
+       int argbufmode = SN_TYPE_VOID;
+       for(i = 0; i < size; i++) {
                char c = data[i];
-               if(c == '"'){
+               if(c == '"') {
                        dq = !dq;
-               }else if(dq){
-                       argbufmode = SN_TYPE_STRING;
-                       char cbuf[2];
-                       cbuf[0] = c;
-                       cbuf[1] = 0;
+               } else if(dq) {
+                       char cbuf[2] = {c, 0};
+
                        char* tmp = argbuf;
                        argbuf = sn_strcat(tmp, cbuf);
                        free(tmp);
-               }else if(c == '\n'){
-               }else if(c == '('){
+                       argbufmode = SN_TYPE_STRING;
+               } else if(c == '(') {
+                       gn_stack[br] = malloc(sizeof(struct sn_generic));
+                       gn_stack[br]->type = SN_TYPE_TREE;
+                       gn_stack[br]->tree = malloc(sizeof(struct sn_tree));
+                       gn_stack[br]->tree->args = malloc(sizeof(struct sn_generic*));
+                       gn_stack[br]->tree->args[0] = NULL;
+                       index_stack[br] = 0;
                        br++;
-                       gn_stack[br - 1] = malloc(sizeof(*gn_stack));
-                       gn_stack[br - 1]->type = SN_TYPE_TREE;
-                       gn_stack[br - 1]->tree = malloc(sizeof(*gn_stack[br - 1]->tree));
-                       gn_stack[br - 1]->tree->op = malloc(sizeof(*gn_stack[br - 1]->tree->op));
-                       gn_stack[br - 1]->tree->op->type = SN_TYPE_FUNCTION;
-                       gn_stack[br - 1]->tree->args = malloc(sizeof(*gn_stack[br - 1]->tree->args));
-                       gn_stack[br - 1]->tree->args[0] = NULL;
-                       op_stack[br - 1] = malloc(1);
-                       op_stack[br - 1][0] = 0;
-               }else if(c == ')'){
-                       if(br > 0){
-                               gn_stack[br - 1]->tree->op->name = sn_strdup(op_stack[br - 1]);
+               } else if(c == ')') {
+                       if(strlen(argbuf) > 0) {
+                               push_stack(gn_stack[br - 1], argbuf, argbufmode);
+                               index_stack[br - 1]++;
                        }
-                       PUSH_STACK(br - 1);
-                       br_stack[br - 1] = 0;
-                       if(br_stack[br - 2] > 0){
-                               int j;
-                               struct sn_generic** old_args = gn_stack[br - 2]->tree->args;
-                               for(j = 0; old_args[j] != NULL; j++);
-                               gn_stack[br - 2]->tree->args = malloc(sizeof(*gn_stack[br - 2]->tree->args) * (j + 2));
-                               for(j = 0; old_args[j] != NULL; j++){
-                                       gn_stack[br - 2]->tree->args[j] = old_args[j];
-                               }
-                               gn_stack[br - 2]->tree->args[j] = gn_stack[br - 1];
-                               gn_stack[br - 2]->tree->args[j + 1] = NULL;
-                               free(old_args);
+                       free(argbuf);
+                       argbuf = malloc(1);
+                       argbuf[0] = 0;
+                       argbufmode = SN_TYPE_VOID;
+                       if(br > 1) {
+                               push_stack_generic(gn_stack[br - 2], gn_stack[br - 1]);
+                               index_stack[br - 2]++;
                        }
                        br--;
-               }else if(br > 0){
-                       if(c == ' '){
-                               PUSH_STACK(br - 1);
-                               br_stack[br - 1]++;
-                       }else if(br_stack[br - 1] == 0){
-                               char cbuf[2];
-                               cbuf[0] = c;
-                               cbuf[1] = 0;
-                               char* tmp = op_stack[br - 1];
-                               op_stack[br - 1] = sn_strcat(tmp, cbuf);
-                               free(tmp);
-                       }else{
-                               argbufmode = SN_TYPE_DOUBLE;
-                               char cbuf[2];
-                               cbuf[0] = c;
-                               cbuf[1] = 0;
+               } else {
+                       if(c == ' ') {
+                               if(strlen(argbuf) > 0) {
+                                       push_stack(gn_stack[br - 1], argbuf, argbufmode);
+                                       index_stack[br - 1]++;
+                               }
+                               free(argbuf);
+                               argbuf = malloc(1);
+                               argbuf[0] = 0;
+                               argbufmode = SN_TYPE_VOID;
+                       } else {
+                               char cbuf[2] = {c, 0};
+
                                char* tmp = argbuf;
                                argbuf = sn_strcat(tmp, cbuf);
                                free(tmp);
+
+                               if(argbufmode == SN_TYPE_VOID) {
+                                       if(c == '.' || (c >= '0' && c <= '9')) {
+                                               argbufmode = SN_TYPE_DOUBLE;
+                                       } else {
+                                               argbufmode = SN_TYPE_FUNCTION;
+                                       }
+                               }
                        }
                }
        }
+
        free(argbuf);
+
        struct sn_generic* gen = gn_stack[0];
        free(gn_stack);
-       free(br_stack);
-       free(op_stack);
+       free(index_stack);
+
        return gen;
 }
 
-struct sn_generic** sn_parse(char* data, unsigned long long size){
+struct sn_generic** sn_parse(char* data, unsigned long long size) {
        int br = 0;
        int i;
        int start = 0;
        bool dq = false;
-       for(i = 0; i < size; i++){
+       for(i = 0; i < size; i++) {
                char c = data[i];
-               if(c == '"'){
+               if(c == '"') {
                        dq = !dq;
-               }else if(dq){
-               }else if(c == '('){
-                       if(br == 0){
+               } else if(dq) {
+               } else if(c == '(') {
+                       if(br == 0) {
                                start = i;
                        }
                        br++;
-               }else if(c == ')'){
+               } else if(c == ')') {
                        br--;
-                       if(br == 0){
+                       if(br == 0) {
                                char* d = malloc(i - start + 1);
                                memcpy(d, data + start, i - start + 1);
                                struct sn_generic* gen = sn_expr_parse(d, i - start + 1);
-                               if(gen != NULL){
+                               if(gen != NULL) {
                                        sn_print_generic(gen);
                                        sn_generic_free(gen);
                                }
@@ -192,22 +189,19 @@ struct sn_generic** sn_parse(char* data, unsigned long long size){
        return NULL;
 }
 
-void sn_generic_free(struct sn_generic* g){
-       if(g->type == SN_TYPE_STRING){
+void sn_generic_free(struct sn_generic* g) {
+       if(g->type == SN_TYPE_STRING) {
                free(g->string);
-       }else if(g->type == SN_TYPE_TREE){
+       } else if(g->type == SN_TYPE_TREE) {
                sn_tree_free(g->tree);
        }
        free(g);
 }
 
-void sn_tree_free(struct sn_tree* t){
-       if(t->op != NULL){
-               sn_generic_free(t->op);
-       }
-       if(t->args != NULL){
+void sn_tree_free(struct sn_tree* t) {
+       if(t->args != NULL) {
                int i;
-               for(i = 0; t->args[i] != NULL; i++){
+               for(i = 0; t->args[i] != NULL; i++) {
                        sn_generic_free(t->args[i]);
                }
                free(t->args);
index b534e5bd6c2a5743f7be145f24eed3e385ec3467..030e18dc6f1400c77974bec75a3cfb0c5fe1c14f 100644 (file)
 #ifndef __SERENADE_PARSER_H__
 #define __SERENADE_PARSER_H__
 
-enum types {
-       SN_TYPE_DOUBLE = 0,
-       SN_TYPE_STRING,
-       SN_TYPE_TREE,
-       SN_TYPE_PTR,
-       SN_TYPE_FUNCTION,
-       SN_TYPE_VOID
-};
+enum types { SN_TYPE_DOUBLE = 0, SN_TYPE_STRING, SN_TYPE_TREE, SN_TYPE_PTR, SN_TYPE_FUNCTION, SN_TYPE_VOID };
 
 struct sn_generic {
        int type;
@@ -51,7 +44,6 @@ struct sn_generic {
 };
 
 struct sn_tree {
-       struct sn_generic* op;
        struct sn_generic** args;
 };
 
index db96a42177f91723d2dfef2d928922912daa8458..29b7f26dd0d3801d2f7c3bccf06507b20c1d776c 100644 (file)
 
 #include "util.h"
 
-#include <string.h>
-#include <stdlib.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
-char* sn_strcat(const char* str1, const char* str2){
+char* sn_strcat(const char* str1, const char* str2) {
        char* str = malloc(strlen(str1) + strlen(str2) + 1);
        memcpy(str, str1, strlen(str1));
        memcpy(str + strlen(str1), str2, strlen(str2));
@@ -42,31 +42,29 @@ char* sn_strcat(const char* str1, const char* str2){
        return str;
 }
 
-char* sn_strdup(const char* str1){
+char* sn_strdup(const char* str1) {
        char* str = malloc(strlen(str1) + 1);
        memcpy(str, str1, strlen(str1));
        str[strlen(str1)] = 0;
        return str;
 }
 
-void _sn_print_generic(struct sn_generic* gen, int n){
+void _sn_print_generic(struct sn_generic* gen, int n) {
        int i;
+       fprintf(stderr, "[%2d]", gen->type);
        for(i = 0; i < n; i++) fprintf(stderr, "    ");
-       if(gen->type == SN_TYPE_TREE){
-               fprintf(stderr, "%s\n", gen->tree->op->name);
-               if(gen->tree->args != NULL){
-                       for(i = 0; gen->tree->args[i] != NULL; i++){
+       if(gen->type == SN_TYPE_TREE) {
+               if(gen->tree->args != NULL) {
+                       for(i = 0; gen->tree->args[i] != NULL; i++) {
                                _sn_print_generic(gen->tree->args[i], n + 1);
                        }
                }
-       }else if(gen->type == SN_TYPE_DOUBLE){
-               fprintf(stderr, "%f\n", gen->number);
-       }else if(gen->type == SN_TYPE_STRING){
+       } else if(gen->type == SN_TYPE_DOUBLE) {
+               fprintf(stderr, "%f", gen->number);
+       } else if(gen->type == SN_TYPE_STRING || gen->type == SN_TYPE_FUNCTION) {
                fwrite(gen->string, 1, gen->string_length, stderr);
-               fprintf(stderr, "\n");
        }
+       fprintf(stderr, "\n");
 }
 
-void sn_print_generic(struct sn_generic* gen){
-       _sn_print_generic(gen, 0);
-}
+void sn_print_generic(struct sn_generic* gen) { _sn_print_generic(gen, 0); }
index 87082c4b685ab81ff01e714405f02c51472e1c21..5ad269872f317042c50cc7a7c0011efc89d8fff3 100644 (file)
 /* -------------------------------------------------------------------------- */
 /* --- END LICENSE --- */
 
-#include <stdio.h>
-#include <stdbool.h>
 #include <ctype.h>
+#include <stdbool.h>
+#include <stdio.h>
 
 char choice[256];
 
-char* asks[] = {
-       "repl",
-       "y",
-       "HAS_REPL_SUPPORT",
-       "Do you want the REPL support?",
+char* asks[] = {"repl", "y", "HAS_REPL_SUPPORT", "Do you want the REPL support?",
 
-       "ffi",
-       "n",
-       "HAS_FFI_SUPPORT",
-       "Do you want the FFI support?",
+               "ffi",  "n", "HAS_FFI_SUPPORT",  "Do you want the FFI support?",
 
-       NULL
-};
+               NULL};
 
-void show_dialog(int n){
+void show_dialog(int n) {
        fprintf(stderr, "[default is %c] %s ", asks[n * 4 + 1][0], asks[n * 4 + 3]);
        fflush(stderr);
 }
 
-int main(int argc, char** argv){
+int main(int argc, char** argv) {
        FILE* out = stdout;
        bool load = false;
-       if(argv[1] != NULL){
+       if(argv[1] != NULL) {
                out = fopen(argv[1], "w");
-               if(out != NULL){
+               if(out != NULL) {
                        load = true;
-               }else{
+               } else {
                        fprintf(stderr, "%s: %s: couldn't open the file\n", argv[0], argv[1]);
                        return 1;
                }
@@ -71,12 +63,12 @@ int main(int argc, char** argv){
        bool nl = false;
        int n = 0;
        show_dialog(n);
-       while(true){
+       while(true) {
                oldc = c;
                if(fread(&c, 1, 1, stdin) <= 0) break;
-               if(c == '\n'){
+               if(c == '\n') {
                        char ch = asks[n * 4 + 1][0];
-                       if(nl){
+                       if(nl) {
                                ch = tolower(oldc);
                        }
                        choice[n] = ch;
@@ -85,7 +77,7 @@ int main(int argc, char** argv){
                        nl = false;
                        if(asks[n * 4 + 1] == NULL) break;
                        show_dialog(n);
-               }else{
+               } else {
                        nl = true;
                }
        }
@@ -95,7 +87,7 @@ int main(int argc, char** argv){
        scanf("%d", &stack_size);
        fprintf(out, "#define STACK_SIZE %d\n", stack_size);
        fprintf(out, "#define SUPPORT \"");
-       for(n = 0; asks[n * 4] != NULL; n++){
+       for(n = 0; asks[n * 4] != NULL; n++) {
                if(n > 0) fprintf(out, " ");
                fprintf(out, "%c%s", choice[n] == 'y' ? '+' : '-', asks[n * 4]);
        }