fix indentation

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

@ -35,7 +35,7 @@ int main(int argc, char *argv[])
std::vector args (argv + 1, argv + argc);
for (const auto& a : args) {
std::ifstream file (a);
parseFile(parser, state, file);
parseFile(parser, state, file);
}
//std::cout << state.size() << ' ' << state.compiling << "> ";
@ -49,7 +49,7 @@ int user_sys(State& state)
switch (state.pop()) {
case 0:
std::cout << state.pop() << std::endl;
break;
break;
}
return 0;

@ -193,13 +193,13 @@ int CoreWords::op_shr(State& state) {
}
int CoreWords::op_comment(State& state) {
state.pass = Pass::Comment;
state.pass = Pass::Comment;
return 0;
}
int CoreWords::op_colon(State& state) {
state.pass = Pass::Colon;
state.pushr(state.dict.here); // this is for EXIT
state.pushr(state.dict.here);
return 0;
}
@ -207,13 +207,13 @@ int CoreWords::op_semic(State& state) {
if (!state.compiling) {
state.ip = state.popr();
} else {
auto begin = state.popr();
auto begin = state.popr();
state.dict.write(begin,
(state.dict.read(begin) & 0x1F) |
((begin - state.dict.latest) << 6));
state.dict.write(begin,
(state.dict.read(begin) & 0x1F) |
((begin - state.dict.latest) << 6));
state.dict.latest = begin;
state.dict.latest = begin;
state.compiling = false;
}
@ -221,7 +221,7 @@ int CoreWords::op_semic(State& state) {
}
int CoreWords::op_here(State& state) {
state.push(state.dict.here);
state.push(state.dict.here);
return 0;
}
@ -266,7 +266,7 @@ int CoreWords::findi(std::string_view str)
std::string_view words (wordsarr, sizeof(wordsarr));
for (i = 0; i < words.size();) {
for (i = 0; i < words.size();) {
const auto end = words.find('\0', i);
if (words.compare(i, end - i, str) == 0)

@ -41,11 +41,11 @@ public:
private:
constexpr static char wordsarr[] =
"drop\0dup\0swap\0pick\0sys\0"
"+\0-\0*\0/\0%\0"
"@\0!\0rot\0>r\0r>\0"
"=\0<\0allot\0&\0|\0"
"^\0<<\0>>\0(\0:\0"
";\0here\0exit\0imm\0const\0";
"+\0-\0*\0/\0%\0"
"@\0!\0rot\0>r\0r>\0"
"=\0<\0allot\0&\0|\0"
"^\0<<\0>>\0(\0:\0"
";\0here\0exit\0imm\0const\0";
// lit, jmp, jmp0, ', lits
static Func get(int index);

@ -33,58 +33,57 @@ struct Dictionary
virtual int write(Addr, Cell) = 0;
Addr allot(Cell amount) {
Addr old = here;
here += amount;
return old;
Addr old = here;
here += amount;
return old;
}
void add(Cell value) {
write(here++, value);
write(here++, value);
}
void addDefinition(std::string_view str) {
add(str.size());
for (char c : str)
add(c);
for (char c : str)
add(c);
if (here & 1)
allot(1);
if (here & 1)
allot(1);
}
bool issame(Addr addr, std::string_view str, std::size_t n) {
if (str.size() != n)
return false;
if (str.size() != n)
return false;
for (char c : str) {
if (read(addr++) != c)
return false;
}
return true;
if (read(addr++) != c)
return false;
}
return true;
}
Addr find(std::string_view str) {
if (latest == 0)
return 0;
return 0;
auto lt = latest;
do {
auto lt = latest;
do {
const auto l = read(lt);
const auto len = l & 0x1F;
const auto len = l & 0x1F;
if (issame(lt + 1, str, len)) {
return lt;
} else {
lt -= l >> 6;
}
} while (lt);
if (issame(lt + 1, str, len))
return lt;
else
lt -= l >> 6;
} while (lt);
return 0;
return 0;
}
Addr getexec(Addr addr) {
const auto len = read(addr) & 0x1F;
return ((addr + 1 + len) + 1) & ~1;
const auto len = read(addr) & 0x1F;
return ((addr + 1 + len) + 1) & ~1;
}
};

@ -21,22 +21,19 @@
#include "corewords.hpp"
//#include <iostream>
class Executor
{
public:
static int fullexec(State& state, Addr addr) {
state.pushr(0);
state.ip = addr - 1;
do {
++state.ip;
//std::cout << "-- " << state.rsize() << "e " << state.ip << std::endl;
CoreWords::run(state.dict.read(state.ip), state);
} while (state.ip);
return 0;
state.ip = addr - 1;
do {
++state.ip;
CoreWords::run(state.dict.read(state.ip), state);
} while (state.ip);
return 0;
}
};

@ -26,83 +26,84 @@ class Parser
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);
const auto sub = str.substr(0, end);
if (sub.empty())
return ParseStatus::Finished;
if (state.pass != Pass::None) {
switch (state.pass) {
case Pass::Comment:
if (state.pass != Pass::None) {
switch (state.pass) {
case Pass::Comment:
if (str.front() == ')')
state.pass = Pass::None;
state.pass = Pass::None;
str = str.substr(1);
break;
case Pass::Colon:
state.pass = Pass::None;
state.compiling = true;
state.dict.addDefinition(sub);
break;
case Pass::Constant:
state.pass = Pass::None;
str = str.substr(1);
break;
case Pass::Colon:
state.pass = Pass::None;
state.compiling = true;
state.dict.addDefinition(sub);
break;
case Pass::Constant:
state.pass = Pass::None;
state.compiling = true;
state.dict.addDefinition(sub);
state.dict.add(CoreWords::HiddenWordLiteral);
state.dict.add(state.pop());
state.dict.addDefinition(sub);
state.dict.add(CoreWords::HiddenWordLiteral);
state.dict.add(state.pop());
state.dict.add(CoreWords::findi(";"));
CoreWords::run(CoreWords::findi(";"), state);
CoreWords::run(CoreWords::findi(";"), state);
break;
default:
break;
}
} else {
if (auto i = CoreWords::findi(sub); i >= 0) {
if (state.compiling)
default:
break;
}
} else {
if (auto i = CoreWords::findi(sub); i >= 0) {
if (state.compiling)
state.dict.add(i);
if (!state.compiling || sub.front() == ';')
CoreWords::run(i, state);
} else if (auto j = state.dict.find(sub); j > 0) {
auto e = state.dict.getexec(j);
if (state.compiling) {
if (state.dict.read(j) & CoreWords::Immediate) {
state.compiling = false;
Executor::fullexec(state, e);
state.compiling = true;
} else {
state.dict.add(CoreWords::HiddenWordJump);
state.dict.add(e);
}
} else {
Executor::fullexec(state, e);
}
} else {
char *p;
const auto l = static_cast<Cell>(std::strtol(sub.data(), &p, 10));
if (!state.compiling || sub.front() == ';')
CoreWords::run(i, state);
} else if (auto j = state.dict.find(sub); j > 0) {
auto e = state.dict.getexec(j);
if (state.compiling) {
if (state.dict.read(j) & CoreWords::Immediate) {
state.compiling = false;
Executor::fullexec(state, e);
state.compiling = true;
} else {
state.dict.add(CoreWords::HiddenWordJump);
state.dict.add(e);
}
} else {
Executor::fullexec(state, e);
}
} else {
char *p;
const auto l = static_cast<Cell>(std::strtol(sub.data(), &p, 10));
if (p != sub.data()) {
if (state.compiling) {
state.dict.add(CoreWords::HiddenWordLiteral);
state.dict.add(l);
} else {
state.push(l);
}
} else {
return ParseStatus::Error;
}
}
if (p != sub.data()) {
if (state.compiling) {
state.dict.add(CoreWords::HiddenWordLiteral);
state.dict.add(l);
} else {
state.push(l);
}
} else {
return ParseStatus::Error;
}
}
if (end == std::string_view::npos)
if (end == std::string_view::npos)
return ParseStatus::Finished;
}
const auto next = str.find_first_not_of(" \t\n\r", end);
const auto next = str.find_first_not_of(" \t\n\r", end);
if (next == std::string_view::npos) {
if (next == std::string_view::npos) {
return ParseStatus::Finished;
} else {
str = str.substr(next);
return ParseStatus::Continue;
}
} else {
str = str.substr(next);
return ParseStatus::Continue;
}
}
};

@ -44,51 +44,51 @@ public:
constexpr State(Dictionary& d): dict(d) {}
Cell beyondip() const {
return dict.read(ip + 1);
return dict.read(ip + 1);
}
void pushr(Cell value) {
if (rsize() == ReturnStackSize)
if (rsize() == ReturnStackSize)
throw;
*++rsp = value;
}
Cell popr() {
if (rsize() == 0)
if (rsize() == 0)
throw;
return *rsp--;
return *rsp--;
}
void push(Cell value) {
if (size() == DataStackSize)
if (size() == DataStackSize)
throw;
*++dsp = value;
}
Cell pop() {
if (size() == 0)
if (size() == 0)
throw;
return *dsp--;
return *dsp--;
}
Cell& top() {
if (size() == 0)
if (size() == 0)
throw;
return *dsp;
return *dsp;
}
Cell& pick(std::size_t i) {
if (i >= size())
throw;
return *(dsp - i);
if (i >= size())
throw;
return *(dsp - i);
}
std::size_t size() const noexcept {
return std::distance(dstack, static_cast<const Cell *>(dsp)) + 1;
return std::distance(dstack, static_cast<const Cell *>(dsp)) + 1;
}
std::size_t rsize() const noexcept {
return std::distance(rstack, static_cast<const Cell *>(rsp)) + 1;
return std::distance(rstack, static_cast<const Cell *>(rsp)) + 1;
}
};

Loading…
Cancel
Save