git » unicorn-sparkle-basic.git » commit 1ae8b34

handle empty lines better, add make format

author Alan Dipert
2024-01-09 20:30:47 UTC
committer Alan Dipert
2024-01-09 20:30:47 UTC
parent 5a11845536169dde464728418ccd94a02880ef50

handle empty lines better, add make format

Makefile +4 -1
main.c +2 -2
parse.c +18 -18
parse.h +16 -16

diff --git a/Makefile b/Makefile
index dd49b8c..892f832 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-.PHONY: push
+.PHONY: push format
 
 CC=gcc
 CFLAGS=-Wall -g
@@ -32,6 +32,9 @@ usbasic.tab.c usbasic.tab.h: usbasic.y
 clean:
 	rm -f usbasic lex.yy.c usbasic.tab.c usbasic.tab.h *.o
 
+format: main.c parse.h parse.c
+	clang-format -i $^
+
 push:
 	git push
 	ssh dreamhost ./git-arr-0.30/git-arr --config ./git-arr-0.30/tailrecursion.conf generate --output ./tailrecursion.com/git-arr
diff --git a/main.c b/main.c
index 74458ff..b90c9c1 100644
--- a/main.c
+++ b/main.c
@@ -1,8 +1,8 @@
-#include <stdio.h>
 #include "parse.h"
+#include <stdio.h>
 
 void read_loop() {
-  struct node_tag* line;
+  struct node_tag *line;
   while (1) {
     if ((line = read_line_stdin("🦄 "))) {
       printf("It parsed 👍\n");
diff --git a/parse.c b/parse.c
index c923115..762df91 100644
--- a/parse.c
+++ b/parse.c
@@ -1,9 +1,9 @@
-#include <stdio.h>
 #include "parse.h"
+#include <stdio.h>
 
-struct node_tag* ast_last_numbered_line;
+struct node_tag *ast_last_numbered_line;
 
-static struct node_tag* ast_alloc(enum NODE_TYPE node_type) {
+static struct node_tag *ast_alloc(enum NODE_TYPE node_type) {
   struct node_tag *new_node = malloc(sizeof(struct node_tag));
   if (new_node == NULL) {
     fprintf(stderr, "Memory allocation failed at %s:%d\n", __FILE__, __LINE__);
@@ -13,13 +13,13 @@ static struct node_tag* ast_alloc(enum NODE_TYPE node_type) {
   return new_node;
 }
 
-struct node_tag* ast_make_string(char *s) {
+struct node_tag *ast_make_string(char *s) {
   struct node_tag *new_node = ast_alloc(NODE_STRING);
   new_node->data.string.str = s;
   return new_node;
 }
 
-struct node_tag* ast_make_numbered_line(int line, struct node_tag* stmt) {
+struct node_tag *ast_make_numbered_line(int line, struct node_tag *stmt) {
   struct node_tag *new_node = ast_alloc(NODE_NUMBERED_LINE);
   new_node->data.line.linum = line;
   new_node->data.line.stmt = stmt;
@@ -27,32 +27,32 @@ struct node_tag* ast_make_numbered_line(int line, struct node_tag* stmt) {
   return new_node;
 }
 
-struct node_tag* ast_make_print(struct node_tag* expr) {
+struct node_tag *ast_make_print(struct node_tag *expr) {
   struct node_tag *new_node = ast_alloc(NODE_PRINT);
   new_node->data.print.expr = expr;
   return new_node;
 }
 
-struct node_tag* ast_make_if(struct node_tag* pred, struct node_tag* stmt) {
+struct node_tag *ast_make_if(struct node_tag *pred, struct node_tag *stmt) {
   struct node_tag *new_node = ast_alloc(NODE_IF);
   new_node->data.iff.pred = pred;
   new_node->data.iff.stmt = stmt;
   return new_node;
 }
 
-struct node_tag* ast_make_id(char *name) {
+struct node_tag *ast_make_id(char *name) {
   struct node_tag *new_node = ast_alloc(NODE_ID);
   new_node->data.id.name = name;
   return new_node;
 }
 
-struct node_tag* ast_make_number_integer(int val) {
+struct node_tag *ast_make_number_integer(int val) {
   struct node_tag *new_node = ast_alloc(NODE_NUMBER_INTEGER);
   new_node->data.number_integer.val = val;
   return new_node;
 }
 
-struct node_tag* read_line(char* line) {
+struct node_tag *read_line(char *line) {
   int yyparse_failed;
   yy_scan_string(line);
   yyparse_failed = yyparse();
@@ -60,13 +60,13 @@ struct node_tag* read_line(char* line) {
   return yyparse_failed ? NULL : ast_last_numbered_line;
 }
 
-struct node_tag* read_line_stdin(char* prompt) {
-  char* line = NULL;
-  struct node_tag* read_node = NULL;
-  if ((line = readline(prompt)) != NULL) {
-    add_history(line);
-    read_node = read_line(line);
-    free(line);
-  }
+struct node_tag *read_line_stdin(char *prompt) {
+  char *line = NULL;
+  struct node_tag *read_node;
+  line = readline(prompt);
+  if (strlen(line) == 0) return NULL;
+  read_node = read_line(line);
+  if (read_node != NULL) add_history(line);
+  free(line);
   return read_node;
 }
diff --git a/parse.h b/parse.h
index e926b40..878ffc1 100644
--- a/parse.h
+++ b/parse.h
@@ -1,8 +1,8 @@
+#include "usbasic.tab.h"
+#include <readline/history.h>
+#include <readline/readline.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <readline/readline.h>
-#include <readline/history.h>
-#include "usbasic.tab.h"
 
 enum NODE_TYPE {
   NODE_NUMBERED_LINE,
@@ -24,7 +24,7 @@ enum NODE_TYPE {
 
 struct node_line_data {
   int linum;
-  struct node_tag* stmt;
+  struct node_tag *stmt;
 };
 
 struct node_string_data {
@@ -32,12 +32,12 @@ struct node_string_data {
 };
 
 struct node_print_data {
-  struct node_tag* expr;
+  struct node_tag *expr;
 };
 
 struct node_iff_data {
-  struct node_tag* pred;
-  struct node_tag* stmt;
+  struct node_tag *pred;
+  struct node_tag *stmt;
 };
 
 struct node_id_data {
@@ -60,16 +60,16 @@ struct node_tag {
   } data;
 };
 
-struct node_tag* ast_make_string(char *s);
-struct node_tag* ast_make_numbered_line(int linum, struct node_tag* stmt);
-struct node_tag* ast_make_print(struct node_tag* expr);
-struct node_tag* ast_make_if(struct node_tag* pred, struct node_tag* stmt);
-struct node_tag* ast_make_id(char *name);
-struct node_tag* ast_make_number_integer(int val);
+struct node_tag *ast_make_string(char *s);
+struct node_tag *ast_make_numbered_line(int linum, struct node_tag *stmt);
+struct node_tag *ast_make_print(struct node_tag *expr);
+struct node_tag *ast_make_if(struct node_tag *pred, struct node_tag *stmt);
+struct node_tag *ast_make_id(char *name);
+struct node_tag *ast_make_number_integer(int val);
 
 extern int yyparse();
-extern void yy_scan_string(char*);
+extern void yy_scan_string(char *);
 extern void yylex_destroy();
 
-struct node_tag* read_line(char* line);
-struct node_tag* read_line_stdin();
+struct node_tag *read_line(char *line);
+struct node_tag *read_line_stdin();