parse files

llvm
Clyne 2 years ago
parent 4367821606
commit 59ecb61463
Signed by: clyne
GPG Key ID: 4C835D6F5F30690A

@ -19,40 +19,59 @@
#include "memdict.hpp" #include "memdict.hpp"
#include "parser.hpp" #include "parser.hpp"
#include <fstream>
#include <iostream> #include <iostream>
#include <vector>
// : variable create 0 , ; static void parseLine(Parser&, State&, std::string_view);
// : create here constant ; static void parseFile(Parser&, State&, std::istream&);
// : constant
int main() int main(int argc, char *argv[])
{ {
MemDict dict; MemDict dict;
State state (dict); State state (dict);
Parser parser; Parser parser;
for (;;) { std::vector args (argv + 1, argv + argc);
std::string line; for (const auto& a : args) {
std::cout << state.size() << ' ' << state.compiling << "> "; std::ifstream file (a);
std::getline(std::cin, line); parseFile(parser, state, file);
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::cout << state.size() << ' ' << state.compiling << "> ";
parseFile(parser, state, std::cin);
return 0; return 0;
} }
int user_sys(State& state) int user_sys(State& state)
{ {
const auto value = state.pop(); switch (state.pop()) {
std::cout << value << std::endl; case 0:
std::cout << state.pop() << std::endl;
break;
}
return 0; 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);
}
}

@ -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= ;

@ -27,6 +27,8 @@ public:
ParseStatus parse(State& state, std::string_view& str) { ParseStatus parse(State& state, std::string_view& str) {
const auto end = str.find_first_of(" \t\n\r"); const auto end = str.find_first_of(" \t\n\r");
const auto sub = str.substr(0, end); const auto sub = str.substr(0, end);
if (sub.empty())
return ParseStatus::Finished;
if (state.pass != Pass::None) { if (state.pass != Pass::None) {
switch (state.pass) { switch (state.pass) {

Loading…
Cancel
Save