]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
parse files
authorClyne Sullivan <clyne@bitgloo.com>
Thu, 9 Feb 2023 19:01:05 +0000 (14:01 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Thu, 9 Feb 2023 19:01:05 +0000 (14:01 -0500)
alee.cpp
core.fth [new file with mode: 0644]
parser.hpp

index 8ca209c819f1817d5451d8b6a7dcb9fa39a99bee..265982cdf2c64e377ca681c5c91fdb4b594095af 100644 (file)
--- a/alee.cpp
+++ b/alee.cpp
 #include "memdict.hpp"
 #include "parser.hpp"
 
+#include <fstream>
 #include <iostream>
+#include <vector>
 
-// : variable create 0 , ;
-// : create here constant ;
-// : constant
+static void parseLine(Parser&, State&, std::string_view);
+static void parseFile(Parser&, State&, std::istream&);
 
-int main()
+int main(int argc, char *argv[])
 {
     MemDict dict;
     State state (dict);
     Parser parser;
 
-    for (;;) {
-       std::string line;
-       std::cout << state.size() << ' ' << state.compiling << "> ";
-       std::getline(std::cin, line);
-
-       ParseStatus r;
-       std::string_view lv (line);
-       do {
-           r = parser.parse(state, lv);
-       } while (r == ParseStatus::Continue);
-
-       if (r != ParseStatus::Finished)
-           std::cout << "r " << to_string(r) << std::endl;
+    std::vector args (argv + 1, argv + argc);
+    for (const auto& a : args) {
+        std::ifstream file (a);
+       parseFile(parser, state, file);
     }
 
+    //std::cout << state.size() << ' ' << state.compiling << "> ";
+    parseFile(parser, state, std::cin);
+
     return 0;
 }
 
 int user_sys(State& state)
 {
-    const auto value = state.pop();
-    std::cout << value << std::endl;
+    switch (state.pop()) {
+    case 0:
+        std::cout << state.pop() << std::endl;
+       break;
+    }
+
     return 0;
 }
 
+void parseLine(Parser& parser, State& state, std::string_view line)
+{
+    ParseStatus r;
+    do {
+        r = parser.parse(state, line);
+    } while (r == ParseStatus::Continue);
+
+    if (r != ParseStatus::Finished)
+        std::cout << "r " << to_string(r) << std::endl;
+}
+
+void parseFile(Parser& parser, State& state, std::istream& file)
+{
+    while (file.good()) {
+        std::string line;
+        std::getline(file, line);
+        parseLine(parser, state, line);
+    }
+}
+
diff --git a/core.fth b/core.fth
new file mode 100644 (file)
index 0000000..ab53923
--- /dev/null
+++ b/core.fth
@@ -0,0 +1,15 @@
+( : variable create 0 , ; )
+( : create here const ; )
+
+: . 0 sys ;
+
+: over   1 pick ;
+: -rot   rot rot ;
+: nip    swap drop ;
+: tuck   swap over ;
+
+: 1+ 1 + ;
+: 1- 1 - ;
+
+: 0= 0 = ;
+: >= < 0= ;
index 2aee82af5e51e30652c0adb2830da7b5e07a7563..588315d917d4fce2d80af90a950fa7f64ce85082 100644 (file)
@@ -27,6 +27,8 @@ public:
     ParseStatus parse(State& state, std::string_view& str) {
         const auto end = str.find_first_of(" \t\n\r");
        const auto sub = str.substr(0, end);
+        if (sub.empty())
+            return ParseStatus::Finished;
 
        if (state.pass != Pass::None) {
            switch (state.pass) {