about summary refs log tree commit diff
path: root/parser.c
diff options
context:
space:
mode:
authorNakidai <nakidai@disroot.org>2025-03-24 22:53:57 +0300
committerNakidai <nakidai@disroot.org>2025-03-24 22:53:57 +0300
commite8703c985af8cbe2de5eb235c7ec295ce1811c7f (patch)
treea6f82af9b65624d46ef19160f24ac0d7f2e03211 /parser.c
parentf312b357ab2ec3cf83a67945f3641b964a59e8d2 (diff)
download3cl-e8703c985af8cbe2de5eb235c7ec295ce1811c7f.tar.gz
3cl-e8703c985af8cbe2de5eb235c7ec295ce1811c7f.zip
Some improvements
- Fix paser
- Add verbose mode
Diffstat (limited to 'parser.c')
-rw-r--r--parser.c11
1 files changed, 10 insertions, 1 deletions
diff --git a/parser.c b/parser.c
index 87eb498..267bcb5 100644
--- a/parser.c
+++ b/parser.c
@@ -3,6 +3,7 @@
 #include <assert.h>
 #include <err.h>
 #include <stddef.h>
+#include <stdio.h>
 #include <stdlib.h>
 
 
@@ -53,6 +54,8 @@ struct cccl_Node *cccl_parse(struct cccl_Token tokens[], size_t tokens_length, e
 
     for (size_t i = 0; i < tokens_length; ++i)
     {
+        if (verbose)
+            fprintf(stderr, "T:[%c:%d] ", tokens[i].value, tokens[i].type);
         switch (tokens[i].type)
         {
         case cccl_Token_COMMAND: case cccl_Token_COMMANDWITHARG: case cccl_Token_BLOCKSTART:
@@ -82,6 +85,7 @@ struct cccl_Node *cccl_parse(struct cccl_Token tokens[], size_t tokens_length, e
         case cccl_Token_COMMANDWITHARG:
         {
             assert(i + 1 != tokens_length);
+            assert(tokens[i + 1].type == cccl_Token_IDENTIFIER);
             res->in[res->in_length - 1] = malloc(sizeof(struct cccl_Node));
             *res->in[res->in_length - 1] = (struct cccl_Node)
             {
@@ -92,6 +96,7 @@ struct cccl_Node *cccl_parse(struct cccl_Token tokens[], size_t tokens_length, e
         case cccl_Token_BLOCKSTART:
         {
             assert(i > 0);
+            assert(tokens[i - 1].type == cccl_Token_IDENTIFIER);
             char opening = tokens[i].value, closing;
             switch (tokens[i].value)
             {
@@ -101,10 +106,12 @@ struct cccl_Node *cccl_parse(struct cccl_Token tokens[], size_t tokens_length, e
             break; case '?': closing = ';';
             }
 
-            size_t oldi = i;
+            size_t oldi = i++;
             int depth = 1;
             for (; i < tokens_length; ++i)
             {
+                if (verbose)
+                    fprintf(stderr, "S:[%c %c %d] ", tokens[i].value, closing, depth);
                 if (tokens[i].value == opening)
                     ++depth;
                 else if (tokens[i].value == closing)
@@ -116,6 +123,8 @@ struct cccl_Node *cccl_parse(struct cccl_Token tokens[], size_t tokens_length, e
             errx(1, "No matching bracket for %c", opening);
 
 end:
+            putchar('\n');
+            puts("Exploring inner...");
             res->in[res->in_length - 1] = cccl_parse(
                 tokens + oldi + 1,
                 i - oldi - 1,