git » unicorn-sparkle-basic.git » commit 4f9fdd0

parse from strings in addition to stdin, to facilitate testing

author Alan Dipert
2024-01-08 05:17:02 UTC
committer Alan Dipert
2024-01-08 05:17:02 UTC
parent 9055735a042beea3ccb33cd5f78849f87b32fd52

parse from strings in addition to stdin, to facilitate testing

main.c +19 -4

diff --git a/main.c b/main.c
index 519bdbe..b881bdd 100644
--- a/main.c
+++ b/main.c
@@ -3,16 +3,31 @@
 #include "ast.h"
 
 extern int yyparse();
+extern void yy_scan_string(char*);
+extern void yylex_destroy();
 
-struct node_tag* read_line() {
-  if (yyparse()) return NULL;
-  return ast_last_numbered_line;
+struct node_tag* read_line(char* line) {
+  int yyparse_failed;
+  yy_scan_string(line);
+  yyparse_failed = yyparse();
+  yylex_destroy();
+  return yyparse_failed ? NULL : ast_last_numbered_line;
+}
+
+struct node_tag* read_line_stdin() {
+  char* line = NULL;
+  size_t len = 0;
+  struct node_tag* read_node;
+  getline(&line, &len, stdin);
+  read_node = read_line(line);
+  free(line);
+  return read_node;
 }
 
 void read_loop() {
   struct node_tag* line;
   while (1) {
-    if ((line = read_line())) {
+    if ((line = read_line_stdin())) {
       printf("It parsed :-)\n");
       printf("linum: %d\n", line->data.line.linum);      
     }