aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authors-over-42023-06-21 18:46:17 -0400
committers-over-42023-06-21 18:46:17 -0400
commit59a00398913b6dc8c669ff73720eaac3757a9d57 (patch)
tree5b1c68dc6aeed664a6b536f5ffc129f1effd7ddc /src
parent8d85c8442ff15100eeb7262bf7ea37472fd78095 (diff)
added basic cli
Diffstat (limited to 'src')
-rw-r--r--src/include/source.h12
-rw-r--r--src/include/tree.h1
-rw-r--r--src/main.c12
-rw-r--r--src/source.c60
4 files changed, 80 insertions, 5 deletions
diff --git a/src/include/source.h b/src/include/source.h
new file mode 100644
index 0000000..ff9f150
--- /dev/null
+++ b/src/include/source.h
@@ -0,0 +1,12 @@
+#ifndef SOURCE_H
+#define SOURCE_H
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+char* source_get(char* arg);
+char* source_get_from_stdin();
+char* source_get_from_fpath(char* path);
+
+#endif
diff --git a/src/include/tree.h b/src/include/tree.h
index 20c6714..c35c694 100644
--- a/src/include/tree.h
+++ b/src/include/tree.h
@@ -1,7 +1,6 @@
#ifndef TREE_H
#define TREE_H
-
#include <stdlib.h>
diff --git a/src/main.c b/src/main.c
index 4a6a370..d756f0b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -4,15 +4,16 @@
#include "include/util.h"
#include "include/token.h"
#include "include/lexer.h"
+#include "include/source.h"
int main(int argc, char* argv[]) {
- FILE* fsource;
- long fsource_size;
+ //FILE* fsource;
+ //long fsource_size;
char* source;
lexer_t* lexer;
int in_file;
-
+ /*
fsource = fopen(argv[1], "rb");
if (!fsource) { free(fsource); die("source file not found"); };
fseek(fsource, 0L, SEEK_END);
@@ -22,6 +23,9 @@ int main(int argc, char* argv[]) {
if (!source) { fclose(fsource); free(source); die("calloc failed"); }
if (1 != fread(source, fsource_size, 1, fsource)) { fclose(fsource); free(source); die("could not read source"); }
log_inf("source file loaded");
+ */
+
+ source = source_get(argv[1]);
lexer = lexer_init(source);
log_inf("lexer created");
@@ -47,7 +51,7 @@ int main(int argc, char* argv[]) {
// clean up
lexer_destroy(lexer);
- fclose(fsource);
+ //fclose(fsource);
free(source);
log_inf("source file closed");
diff --git a/src/source.c b/src/source.c
new file mode 100644
index 0000000..b098751
--- /dev/null
+++ b/src/source.c
@@ -0,0 +1,60 @@
+#include "include/source.h"
+#include "include/util.h"
+#include <stdio.h>
+
+char* source_get(char* arg) {
+ return arg?
+ source_get_from_fpath(arg):
+ source_get_from_stdin();
+
+}
+
+char* source_get_from_fpath(char* path) {
+ FILE* f; // the file to read from
+ long f_size; // the size of the file
+ char* src; // the source code to return
+
+ f = fopen(path, "rb");
+ if (!f) { die("source file not found: %s", path); }
+
+ fseek(f, 0L, SEEK_END);
+ f_size = ftell(f);
+ rewind(f);
+
+ src = calloc(1, f_size + 1);
+
+ if ((fread(src, f_size, 1, f) != 1) || !src) {
+ fclose(f);
+ free(src);
+ die("could not read source file: %s", path);
+ }
+
+ return src;
+}
+
+char* source_get_from_stdin() {
+ size_t len; // the length of the given source
+ char* src; // the source code to return
+
+ len = 0;
+ src = calloc(len, sizeof(char));
+
+ printf("> ");
+
+ while (src[len - 1] != EOF) {
+ char c; // the character being read
+
+ if (src[len - 1] == '\n') { printf("> "); }
+
+ c = getchar();
+ src = realloc(src, (len + sizeof(char)));
+ memcpy(src + len, &c, sizeof(char));
+ len += sizeof(char);
+ }
+
+ src[len - 1] = '\0'; // null terminate
+
+ putchar('\n');
+
+ return src;
+}