make parser static; tested msp430 build

llvm
Clyne 2 years ago
parent 08a5696e60
commit f6e3fa4663
Signed by: clyne
GPG Key ID: 3267C8EBF3F9AFC7

@ -52,7 +52,6 @@ int main()
static SplitMemDict<alee_dat_len> dict (alee_dat); static SplitMemDict<alee_dat_len> dict (alee_dat);
State state (dict, readchar); State state (dict, readchar);
Parser parser;
serputs("alee forth\n\r"); serputs("alee forth\n\r");
@ -67,7 +66,7 @@ int main()
serputs("\n\r"); serputs("\n\r");
if (auto r = parser.parse(state, strbuf); r == 0) { if (auto r = Parser::parse(state, strbuf); r == 0) {
serputs(state.compiling() ? " compiled" : " ok"); serputs(state.compiling() ? " compiled" : " ok");
} else { } else {
switch (r) { switch (r) {
@ -148,6 +147,13 @@ void user_sys(State& state)
case 1: case 1:
serput(state.pop()); serput(state.pop());
break; break;
case 2:
{ auto addr = state.pop();
*((uint8_t *)addr) = state.pop(); }
break;
case 3:
state.push(*((uint8_t *)state.pop()));
break;
default: default:
break; break;
} }

@ -29,10 +29,8 @@
static bool okay = false; static bool okay = false;
static void readchar(State& state); static void readchar(State& state);
static void parseLine(Parser&, State&, const std::string&); static void parseLine(State&, const std::string&);
static void parseFile(Parser&, State&, std::istream&); static void parseFile(State&, std::istream&);
static Parser parser;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -42,11 +40,11 @@ int main(int argc, char *argv[])
std::vector args (argv + 1, argv + argc); std::vector args (argv + 1, argv + argc);
for (const auto& a : args) { for (const auto& a : args) {
std::ifstream file (a); std::ifstream file (a);
parseFile(parser, state, file); parseFile(state, file);
} }
okay = true; okay = true;
parseFile(parser, state, std::cin); parseFile(state, std::cin);
return 0; return 0;
} }
@ -115,7 +113,7 @@ void user_sys(State& state)
std::jmp_buf oldjb; std::jmp_buf oldjb;
memcpy(oldjb, state.jmpbuf, sizeof(std::jmp_buf)); memcpy(oldjb, state.jmpbuf, sizeof(std::jmp_buf));
state.ip = 0; state.ip = 0;
parser.parseSource(state); Parser::parseSource(state);
memcpy(state.jmpbuf, oldjb, sizeof(std::jmp_buf)); memcpy(state.jmpbuf, oldjb, sizeof(std::jmp_buf));
state.ip = oldip; state.ip = oldip;
} }
@ -123,9 +121,9 @@ void user_sys(State& state)
} }
} }
void parseLine(Parser& parser, State& state, const std::string& line) void parseLine(State& state, const std::string& line)
{ {
if (auto r = parser.parse(state, line.c_str()); r == 0) { if (auto r = Parser::parse(state, line.c_str()); r == 0) {
if (okay) if (okay)
std::cout << (state.compiling() ? "compiled" : "ok") << std::endl; std::cout << (state.compiling() ? "compiled" : "ok") << std::endl;
} else { } else {
@ -161,7 +159,7 @@ void parseLine(Parser& parser, State& state, const std::string& line)
} }
} }
void parseFile(Parser& parser, State& state, std::istream& file) void parseFile(State& state, std::istream& file)
{ {
while (file.good()) { while (file.good()) {
std::string line; std::string line;
@ -170,7 +168,7 @@ void parseFile(Parser& parser, State& state, std::istream& file)
if (line == "bye") if (line == "bye")
exit(0); exit(0);
parseLine(parser, state, line); parseLine(state, line);
} }
} }

@ -27,10 +27,8 @@
static bool okay = false; static bool okay = false;
static void readchar(State& state); static void readchar(State& state);
static void parseLine(Parser&, State&, const std::string&); static void parseLine(State&, const std::string&);
static void parseFile(Parser&, State&, std::istream&); static void parseFile(State&, std::istream&);
static Parser parser;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -43,12 +41,12 @@ int main(int argc, char *argv[])
std::vector args (argv + 1, argv + argc); std::vector args (argv + 1, argv + argc);
for (const auto& a : args) { for (const auto& a : args) {
std::ifstream file (a); std::ifstream file (a);
parseFile(parser, state, file); parseFile(state, file);
} }
} }
okay = true; okay = true;
parseFile(parser, state, std::cin); parseFile(state, std::cin);
return 0; return 0;
} }
@ -117,7 +115,7 @@ void user_sys(State& state)
std::jmp_buf oldjb; std::jmp_buf oldjb;
memcpy(oldjb, state.jmpbuf, sizeof(std::jmp_buf)); memcpy(oldjb, state.jmpbuf, sizeof(std::jmp_buf));
state.ip = 0; state.ip = 0;
parser.parseSource(state); Parser::parseSource(state);
memcpy(state.jmpbuf, oldjb, sizeof(std::jmp_buf)); memcpy(state.jmpbuf, oldjb, sizeof(std::jmp_buf));
state.ip = oldip; state.ip = oldip;
} }
@ -125,9 +123,9 @@ void user_sys(State& state)
} }
} }
void parseLine(Parser& parser, State& state, const std::string& line) void parseLine(State& state, const std::string& line)
{ {
if (auto r = parser.parse(state, line.c_str()); r == 0) { if (auto r = Parser::parse(state, line.c_str()); r == 0) {
if (okay) if (okay)
std::cout << (state.compiling() ? "compiled" : "ok") << std::endl; std::cout << (state.compiling() ? "compiled" : "ok") << std::endl;
} else { } else {
@ -163,7 +161,7 @@ void parseLine(Parser& parser, State& state, const std::string& line)
} }
} }
void parseFile(Parser& parser, State& state, std::istream& file) void parseFile(State& state, std::istream& file)
{ {
while (file.good()) { while (file.good()) {
std::string line; std::string line;
@ -172,7 +170,7 @@ void parseFile(Parser& parser, State& state, std::istream& file)
if (line == "bye") if (line == "bye")
exit(0); exit(0);
parseLine(parser, state, line); parseLine(state, line);
} }
} }

@ -28,12 +28,12 @@ class Parser
public: public:
constexpr static int UnknownWord = -1; constexpr static int UnknownWord = -1;
int parse(State&, const char *); static int parse(State&, const char *);
int parseSource(State&); static int parseSource(State&);
private: private:
int parseWord(State&, Word); static int parseWord(State&, Word);
int parseNumber(State&, Word); static int parseNumber(State&, Word);
}; };
#endif // ALEEFORTH_PARSER_HPP #endif // ALEEFORTH_PARSER_HPP

Loading…
Cancel
Save