Audited all 30 %macro in asm/. The format was already followed almost
everywhere -- the doc block sits above the %macro, or above the %synth block
that generates its body -- but four things were wrong:
raw_and.r8asm the two doc blocks still carried the old names (bit_and, and)
raw_xor.r8asm raw_xor's first two parameters were typed Unit, not Cell
lib.r8asm step / back / output had no type at all ("%1: step_count")
and two files still repeated the retracted claim that set_0 only converges on 0
and 1 (raw_add_nc.r8asm, raw_add_nc4.r8asm). Those now say what set_0 really
does -- it clears the low bit -- and stay on the point that matters for a bit
level slice: A, B and the carry must be single bits because the searched
program only tracks the low K bits.
Parameter types are now a closed set: Cell, CellBit, Unit, UnitBit, Count
(Count is for step/back's repeat count). It is written down in AGENTS.md
together with the format, so the next macro has something to copy.
New r8forge_macro_docs_test checks the format instead of trusting review: a doc
block must exist, its "; Macro <name>" line must match the macro, the number
of documented parameters must equal argc, and every type must be one of the
five. Negative tested by deleting a parameter line and by renaming a block --
it reports both and exits 1.
Also fixes a real annoyance found while running the checks: %synth writes its
generated macro to the system temp directory and imports it. That write fails
intermittently on this machine (same directory, cmd can write, r8forge cannot;
cause unknown), which turned every assembly into "cannot open ...synth.r8asm".
The body is now built in memory and written to the temp directory or, if that
open fails, to the working directory, with one stderr line saying so.
Full suite green: test.r8asm 1666 characters, full_add 8/8, verify_raw_add_nc,
byte_op_test 65536x4 + 512, macro_docs_test 30/30, fibonacci prints its ten
numbers.
906 lines
31 KiB
C++
906 lines
31 KiB
C++
#include "r8forge/r8asm.hh"
|
|
#include <cstdio>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <algorithm>
|
|
#include <cstddef>
|
|
#include <iostream>
|
|
#include <limits>
|
|
#include <list>
|
|
#include <map>
|
|
#include <sstream>
|
|
#include <stack>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#define EMPTY_CHARS (" \t\n\r\f\v")
|
|
#define OPERATORS ("+-*/^~\"()&|")
|
|
namespace r8asm_strtools {
|
|
|
|
bool is_integer(const std::string &str) {
|
|
return !str.empty() &&
|
|
std::ranges::all_of(str, [](char c) { return c >= '0' && c <= '9'; });
|
|
}
|
|
|
|
void trim(std::string &str) { // Clean empty chars in the string.
|
|
auto start = str.find_first_not_of(EMPTY_CHARS);
|
|
|
|
if (!(start ==
|
|
std::string::npos)) { // The string isn't filled with empty chars.
|
|
auto tmp_end = str.find_last_not_of(EMPTY_CHARS);
|
|
str = str.substr(start, tmp_end - start + 1);
|
|
} else
|
|
str = "";
|
|
}
|
|
|
|
std::string peel(std::string &str) { // Peel the first word from string.
|
|
for (size_t i = 1; i < str.size();
|
|
i++) { // insert space before and after operators appear
|
|
if (std::string(OPERATORS).find(str[i]) != std::string::npos) {
|
|
if (str[i - 1] != ' ') {
|
|
str = str.substr(0, i) + ' ' + str.substr(i, str.size());
|
|
i++;
|
|
}
|
|
if (str[i + 1] != ' ') {
|
|
str = str.substr(0, i + 1) + ' ' + str.substr(i + 1, str.size());
|
|
}
|
|
}
|
|
}
|
|
trim(str);
|
|
auto end = str.find_first_of(EMPTY_CHARS);
|
|
|
|
if (end != std::string::npos) {
|
|
std::string tmp_str = str.substr(0, end);
|
|
str = str.substr(end);
|
|
trim(str);
|
|
return (tmp_str);
|
|
} else {
|
|
std::string tmp_str = str;
|
|
str = "";
|
|
return (tmp_str);
|
|
}
|
|
}
|
|
} // namespace r8asm_strtools
|
|
#undef EMPTY_CHARS
|
|
#undef OPERATORS
|
|
|
|
void r8asm::ast::set_cell_size(size_t cell_size) {
|
|
CELL_SIZE = cell_size;
|
|
// the names a .r8asm program sees stay as they were: _CELL_SIZE is the
|
|
// machine word width in bits (CELL_SIZE on the C++ side)
|
|
scopes = {{{"_CELL_SIZE", static_cast<general_data>(cell_size)},
|
|
{"_CELL_MAX", static_cast<general_data>((1 << cell_size) - 1)},
|
|
{"_ROL_STEP", static_cast<general_data>(cell_size - 1)}}};
|
|
}
|
|
|
|
r8asm::general_data r8asm::ast::expand_node_to_data(const ast_node &node) {
|
|
general_data to_return = 0;
|
|
switch (node.type) {
|
|
case ast_node_t::CONSTANT:
|
|
to_return = std::stoi(node.value);
|
|
break;
|
|
case ast_node_t::PARAMETER:
|
|
to_return = scopes.back().at('%' + node.value);
|
|
break;
|
|
case ast_node_t::VARIABLE: {
|
|
bool found = false;
|
|
for (auto i = scopes.rbegin(); i != scopes.rend(); i++) {
|
|
auto tmp = i->find(node.value);
|
|
if (tmp != i->end()) {
|
|
to_return = tmp->second;
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!found && reported.insert(node.value).second)
|
|
fail("unknown name '" + node.value + "'");
|
|
} break;
|
|
case ast_node_t::OPERATOR: {
|
|
switch (operator_map.at(node.value)) {
|
|
case operator_t::ADD:
|
|
to_return = expand_node_to_data(node.children[0]) +
|
|
expand_node_to_data(node.children[1]);
|
|
break;
|
|
case operator_t::SUB:
|
|
to_return = expand_node_to_data(node.children[0]) -
|
|
expand_node_to_data(node.children[1]);
|
|
break;
|
|
case operator_t::MUL:
|
|
to_return = expand_node_to_data(node.children[0]) *
|
|
expand_node_to_data(node.children[1]);
|
|
break;
|
|
case operator_t::DIV:
|
|
to_return = expand_node_to_data(node.children[0]) /
|
|
expand_node_to_data(node.children[1]);
|
|
break;
|
|
case operator_t::XOR:
|
|
to_return = expand_node_to_data(node.children[0]) ^
|
|
expand_node_to_data(node.children[1]);
|
|
break;
|
|
case operator_t::AND:
|
|
to_return = expand_node_to_data(node.children[0]) &
|
|
expand_node_to_data(node.children[1]);
|
|
break;
|
|
case operator_t::OR:
|
|
to_return = expand_node_to_data(node.children[0]) |
|
|
expand_node_to_data(node.children[1]);
|
|
break;
|
|
case operator_t::NOT:
|
|
to_return = ~expand_node_to_data(node.children[0]);
|
|
break;
|
|
default:;
|
|
break;
|
|
}
|
|
break;
|
|
};
|
|
|
|
default:
|
|
std::cerr << "ERROR: THIS NODE SHOULD NOT BE EXPANDED TO DATA!!!"
|
|
<< std::endl;
|
|
break;
|
|
}
|
|
return to_return;
|
|
}
|
|
|
|
std::string r8asm::ast::expand_node_to_string(const ast_node &node) {
|
|
return node.value;
|
|
}
|
|
|
|
#define EMPTY_CODEBLOCK \
|
|
{ \
|
|
r8asm::ast_node_t::CODEBLOCK, "", {} \
|
|
}
|
|
|
|
// TODO: FILL THE BLANK
|
|
#define ROT8_NODE(INS) \
|
|
{ \
|
|
r8asm::ast_node_t::ROT8, std::string("rot8_") + INS, {} \
|
|
}
|
|
r8asm::ast_node r8asm::ast::expand_node_to_ins(const ast_node &node) {
|
|
ast_node to_return = EMPTY_CODEBLOCK;
|
|
switch (node.type) {
|
|
case ast_node_t::ROT8: {
|
|
/*
|
|
ROT8
|
|
value: rot8_ins
|
|
*/
|
|
to_return.children = {node};
|
|
switch (rot8_map.at(node.value)) {
|
|
case rot8_t::STP:
|
|
dataptr++;
|
|
break;
|
|
case rot8_t::BTP:
|
|
dataptr--;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
} break;
|
|
case ast_node_t::REPEAT: {
|
|
/*
|
|
REPEAT
|
|
children[0]: count
|
|
children[1]: codeblock
|
|
*/
|
|
for (data count = expand_node_to_data(node.children[0]); count > 0; count--)
|
|
for (const auto &i : expand_node_to_ins(node.children[1]).children)
|
|
to_return.children.push_back(i);
|
|
} break;
|
|
case ast_node_t::MACROCALL: {
|
|
/*
|
|
MACROCALL
|
|
value: macro_name
|
|
children: arguments
|
|
*/
|
|
|
|
std::map<std::string, general_data> tmp_scope = {};
|
|
for (auto i = 0; i < node.children.size(); i++)
|
|
tmp_scope.insert_or_assign('%' + std::to_string(i + 1),
|
|
// %1 in macro means children[0]
|
|
expand_node_to_data(node.children[i]));
|
|
scopes.push_back(tmp_scope);
|
|
if (macros.find(node.value) == macros.end())
|
|
fail("unknown macro '" + node.value + "'");
|
|
else
|
|
for (const auto &it : expand_node_to_ins(macros.at(node.value)).children)
|
|
to_return.children.push_back(it);
|
|
scopes.pop_back();
|
|
|
|
} break;
|
|
|
|
case ast_node_t::CODEBLOCK: {
|
|
/*
|
|
CODEBLOCK
|
|
children: ins
|
|
*/
|
|
for (const auto &i : node.children)
|
|
for (const auto &ins : expand_node_to_ins(i).children)
|
|
to_return.children.push_back(ins);
|
|
} break;
|
|
|
|
case ast_node_t::BUILTIN: {
|
|
/*
|
|
BUILTIN
|
|
value: type
|
|
children: args
|
|
*/
|
|
if (builtin_map.find(node.value) == builtin_map.end()) {
|
|
fail("unknown builtin '." +
|
|
node.value.substr(std::string("builtin_").size()) + "'");
|
|
return to_return;
|
|
}
|
|
switch (builtin_map.at(node.value)) {
|
|
// WARNING: node.value HAS BEEN USED!!! DO NOT CONSUME IT AGAIN!!!
|
|
case builtin_t::ANCHOR: {
|
|
/*
|
|
ANCHOR
|
|
children[0]: var_name
|
|
*/
|
|
scopes.back().insert_or_assign(expand_node_to_string(node.children[0]),
|
|
dataptr);
|
|
} break;
|
|
case builtin_t::TP: {
|
|
/*
|
|
TP
|
|
children[0]: destination
|
|
*/
|
|
addr destination = expand_node_to_data(node.children[0]);
|
|
if (destination > dataptr)
|
|
for (addr i = 0; i < destination - dataptr; i++)
|
|
to_return.children.push_back(ROT8_NODE("stp"));
|
|
else
|
|
for (addr i = 0; i < dataptr - destination; i++)
|
|
to_return.children.push_back(ROT8_NODE("btp"));
|
|
dataptr = destination;
|
|
// stp or btp might be created
|
|
// so dataptr must be updated at the same time.
|
|
} break;
|
|
case builtin_t::XOR: {
|
|
/*
|
|
XOR
|
|
children[0]: argument
|
|
WARNING: argument bigger than ((1<<CELL_SIZE)-1) will be cut off
|
|
e.g.
|
|
children[0] -> 5(0b101)
|
|
when define CELL_SIZE 2, only the last 2 bit will be noticed,
|
|
and 5 means 0b01(1) here.
|
|
|
|
then the node will be expanded like this:
|
|
|
|
flb
|
|
ror
|
|
ror
|
|
*/
|
|
data d = expand_node_to_data(node.children[0]);
|
|
for (auto i = 0; i < scopes[0].at("_CELL_SIZE"); i++) {
|
|
if (d & 1)
|
|
to_return.children.push_back(ROT8_NODE("flb"));
|
|
to_return.children.push_back(ROT8_NODE("ror"));
|
|
d >>= 1;
|
|
}
|
|
} break;
|
|
case builtin_t::LOOP: {
|
|
to_return.children.push_back(ROT8_NODE("stp"));
|
|
to_return.children.push_back(ROT8_NODE("biz"));
|
|
to_return.children.push_back(ROT8_NODE("btp"));
|
|
// >[<
|
|
} break;
|
|
case builtin_t::ENDLOOP: {
|
|
// Consume one '1' from the auxiliary cell and pull the next bit down.
|
|
// Without the ror, the rnz could only ever see the bit that flb just
|
|
// cleared, so a .loop over 0b111 ran once instead of three times
|
|
// (SPEC: "consecutive 1s starting from the LSB").
|
|
to_return.children.push_back(ROT8_NODE("stp"));
|
|
to_return.children.push_back(ROT8_NODE("flb"));
|
|
to_return.children.push_back(ROT8_NODE("ror"));
|
|
to_return.children.push_back(ROT8_NODE("rnz"));
|
|
to_return.children.push_back(ROT8_NODE("btp"));
|
|
// >-+]<
|
|
} break;
|
|
case builtin_t::VAR: {
|
|
/*
|
|
VAR
|
|
children[0]: var_name
|
|
children[1]: var_value
|
|
*/
|
|
scopes.back().insert_or_assign(expand_node_to_string(node.children[0]),
|
|
expand_node_to_data(node.children[1]));
|
|
} break;
|
|
default:
|
|
break;
|
|
};
|
|
} break;
|
|
default:;
|
|
break;
|
|
}
|
|
return to_return;
|
|
}
|
|
#undef ROT8_NODE
|
|
|
|
#define ROT8_BC_TO_CHAR(bc) ("><+-[].,"[static_cast<int>(bc)])
|
|
std::string r8asm::ast::expand_root() {
|
|
auto rot8_ins = expand_node_to_ins(root);
|
|
std::string rot8_tape = "";
|
|
for (const auto &i : rot8_ins.children)
|
|
rot8_tape += ROT8_BC_TO_CHAR(rot8_map.at(i.value));
|
|
return rot8_tape;
|
|
}
|
|
#undef ROT8_BC_TO_CHAR
|
|
|
|
std::string r8asm::export_rot8() { return main_tree.expand_root(); }
|
|
|
|
bool r8asm::ast::import_macro(const std::string ¯o_name,
|
|
const ast_node &node) {
|
|
return macros.insert_or_assign(macro_name, node).second;
|
|
}
|
|
|
|
#define POINTS_AT_DIRECTIVE(iter, TYPE) \
|
|
(iter->tokens[0].type == token_t::DIRECTIVE && \
|
|
directive_keywords.at(iter->tokens[0].value) == directive_t::TYPE)
|
|
|
|
r8asm::ast_node r8asm::parse(const token_stream &stream) {
|
|
|
|
ast_node to_return = EMPTY_CODEBLOCK;
|
|
token_stream tmp_codeblock_stream = {stream.filename};
|
|
/*
|
|
ast_node tmp_codeblock_node;
|
|
|
|
std::vector<ast_node> *destination = &to_return.children;
|
|
std::vector<ast_node> *last_destination = nullptr;*/
|
|
|
|
for (auto it = stream.lines.begin(); it < stream.lines.end(); it++) {
|
|
switch (it->tokens[0].type) {
|
|
case token_t::DIRECTIVE: {
|
|
/*
|
|
directive will be tokenized like this:
|
|
macro raw_add
|
|
0 1
|
|
|
|
or:
|
|
macro raw_add 2 + 4
|
|
0 1 2 3 4
|
|
(though [2] [3] [4] won't be used)
|
|
|
|
if it's a rep block:
|
|
rep 1 + 1
|
|
0 1 2 3
|
|
|
|
if it's a include line:
|
|
include filename
|
|
0 1
|
|
*/
|
|
if (directive_keywords.find(it->tokens[0].value) ==
|
|
directive_keywords.end()) {
|
|
main_tree.fail("unknown directive '%" + it->tokens[0].value + "'");
|
|
break;
|
|
}
|
|
switch (directive_keywords.at(it->tokens[0].value)) {
|
|
// match an entire operation (not a cut for arguments) first
|
|
case directive_t::MACRO: {
|
|
|
|
std::string macro_name = it->tokens[1].value;
|
|
|
|
tmp_codeblock_stream.lines = {};
|
|
|
|
depth_t layer = 0;
|
|
// now it points at %macro line
|
|
for (it += 1; !(POINTS_AT_DIRECTIVE(it, ENDMACRO) && (layer == 0));
|
|
it++) {
|
|
if (POINTS_AT_DIRECTIVE(it, MACRO))
|
|
layer++;
|
|
else if (POINTS_AT_DIRECTIVE(it, ENDMACRO))
|
|
layer--;
|
|
tmp_codeblock_stream.lines.push_back(*it);
|
|
}
|
|
// now it points at %endmacro line
|
|
main_tree.import_macro(macro_name, parse(tmp_codeblock_stream));
|
|
} break;
|
|
case directive_t::REP: {
|
|
|
|
// build subtree for argument of rep
|
|
token_line buffer = {it->number, {}};
|
|
for (auto token_it = it->tokens.begin() + 1; // where argument begins
|
|
token_it < it->tokens.end(); token_it++)
|
|
buffer.tokens.push_back(*token_it);
|
|
/*
|
|
REPEAT
|
|
children[0]: count
|
|
children[1]: codeblock
|
|
*/
|
|
|
|
to_return.children.push_back({r8asm::ast_node_t::REPEAT, "", {}});
|
|
to_return.children.back().children.push_back(
|
|
parse_single_arg(buffer)); // count will be pushed here.
|
|
tmp_codeblock_stream.lines = {};
|
|
|
|
depth_t layer = 0;
|
|
// now it points at %rep line
|
|
for (it += 1; !(POINTS_AT_DIRECTIVE(it, ENDREP) && (layer == 0));
|
|
it++) {
|
|
if (POINTS_AT_DIRECTIVE(it, REP))
|
|
layer++;
|
|
else if (POINTS_AT_DIRECTIVE(it, ENDREP))
|
|
layer--;
|
|
tmp_codeblock_stream.lines.push_back(*it);
|
|
}
|
|
// now it points at %endrep line
|
|
|
|
to_return.children.back().children.push_back(
|
|
parse(tmp_codeblock_stream)); // codeblock will be pushed here.
|
|
|
|
} break;
|
|
case directive_t::INCLUDE: {
|
|
std::string filename = "";
|
|
auto tp = it->tokens.begin() + 1;
|
|
// now tp points at the LQUOTE
|
|
|
|
for (tp += 1; tp->type != token_t::RQUOTE; tp++)
|
|
filename += tp->value;
|
|
// now tp points at RQUOTE
|
|
|
|
import_file(filename, main_tree.CELL_SIZE);
|
|
// import_file has already said why if it failed; the error flag is
|
|
// set, so this file will not be reported as assembled.
|
|
} break;
|
|
case directive_t::SYNTH: {
|
|
// %synth <name>
|
|
// params <n>
|
|
// in %1 %2 %3
|
|
// out %4 %5
|
|
// map <2^|in| numbers>
|
|
// %endsynth
|
|
const std::string name = it->tokens[1].value;
|
|
ast::synth_spec spec;
|
|
depth_t layer = 0;
|
|
for (it += 1; !(POINTS_AT_DIRECTIVE(it, ENDSYNTH) && layer == 0); it++) {
|
|
if (POINTS_AT_DIRECTIVE(it, SYNTH))
|
|
layer++;
|
|
else if (POINTS_AT_DIRECTIVE(it, ENDSYNTH))
|
|
layer--;
|
|
if (it->tokens.empty())
|
|
continue;
|
|
const std::string &kw = it->tokens[0].value;
|
|
if (kw == "params") {
|
|
spec.params = std::stoi(it->tokens[1].value);
|
|
} else if (kw == "inputs" || kw == "outputs") {
|
|
// implied by the table now: with map the inputs are %1..%K (K from
|
|
// the number of entries) and the outputs follow them in bit order
|
|
main_tree.fail("'" + kw +
|
|
"' is gone: map already says which parameter is "
|
|
"which, use stat for the raw table");
|
|
} else if (kw == "metric") {
|
|
const std::string &m = it->tokens[1].value;
|
|
if (m == "chars")
|
|
spec.metric = 0;
|
|
else if (m == "flat")
|
|
spec.metric = 1;
|
|
else if (m == "blocks")
|
|
spec.metric = 2;
|
|
else
|
|
main_tree.fail("unknown metric '" + m +
|
|
"' (chars, flat or blocks)");
|
|
} else if (kw == "mode") {
|
|
const std::string &m = it->tokens[1].value;
|
|
if (m == "layers")
|
|
spec.mode = 1;
|
|
else if (m == "tape")
|
|
spec.mode = 0;
|
|
else
|
|
main_tree.fail("unknown mode '" + m + "'");
|
|
} else if (kw == "layout") {
|
|
for (size_t i = 1; i < it->tokens.size(); i++)
|
|
spec.layout.push_back(std::stoi(it->tokens[i].value));
|
|
} else if (kw == "map") {
|
|
for (size_t i = 1; i < it->tokens.size(); i++)
|
|
spec.map.push_back(std::stoi(it->tokens[i].value));
|
|
} else if (kw == "stat") {
|
|
// the whole state table: one P-bit value per input combination
|
|
for (size_t i = 1; i < it->tokens.size(); i++)
|
|
spec.stat.push_back(std::stoi(it->tokens[i].value));
|
|
} else {
|
|
main_tree.fail("unknown line '" + kw + "' inside %synth");
|
|
}
|
|
}
|
|
// map alone says which parameter is which: 2^K entries mean %1..%K are
|
|
// the inputs and the value's bits land on the parameters after them
|
|
if (!spec.map.empty() && spec.in.empty()) {
|
|
size_t k = 0;
|
|
while ((size_t(1) << k) < spec.map.size())
|
|
k++;
|
|
if ((size_t(1) << k) != spec.map.size()) {
|
|
main_tree.fail("%synth '" + name + "': map needs 2^inputs entries");
|
|
} else {
|
|
for (size_t i = 0; i < k; i++)
|
|
spec.in.push_back(int(i));
|
|
int width = 0;
|
|
for (int val : spec.map)
|
|
for (int b = 0; b < 31; b++)
|
|
if ((val >> b) & 1)
|
|
width = std::max(width, b + 1);
|
|
for (int i = 0; i < width; i++)
|
|
spec.out.push_back(int(k) + i);
|
|
if (int(k) + width > spec.params)
|
|
main_tree.fail("%synth '" + name + "': map does not fit in " +
|
|
std::to_string(spec.params) + " parameters");
|
|
}
|
|
}
|
|
auto prog = r8asm_synth_program(spec.params, spec.in, spec.out,
|
|
spec.map, spec.stat, name, spec.layout,
|
|
spec.mode, spec.metric);
|
|
if (prog.empty()) {
|
|
main_tree.fail("%synth '" + name + "': nothing emitted");
|
|
} else {
|
|
// Write the synthesised body out as a macro and import it: the code
|
|
// then goes through the normal, well tested expansion path instead
|
|
// of being spliced by hand. set_1 is spelled out (set_0 + flb) so
|
|
// the generated file needs nothing from lib.r8asm.
|
|
//
|
|
// It goes to the system temp directory, and to the working directory
|
|
// if that one cannot be written: a sandbox may allow a process to
|
|
// write only inside its own tree, and then temp_directory_path()
|
|
// happily returns a path whose open() fails.
|
|
std::ostringstream body;
|
|
body << "%macro " << name << " " << spec.params << "\n";
|
|
for (const auto &i : prog) {
|
|
body << ".tp %" << i[0] + 1 << "\nbiz\n";
|
|
for (int b = 0; b < 8; b++)
|
|
if (i[1] & (1 << b)) {
|
|
body << ".tp %" << b + 1 << "\n";
|
|
if (i[2])
|
|
body << "biz\nflb\nrnz\n"; // set_0
|
|
body << "flb\n";
|
|
}
|
|
body << ".tp %" << i[0] + 1 << "\nflb\nrnz\n";
|
|
}
|
|
body << "%endmacro\n";
|
|
|
|
std::vector<std::filesystem::path> dirs;
|
|
std::error_code ec;
|
|
const std::filesystem::path temp_dir =
|
|
std::filesystem::temp_directory_path(ec);
|
|
if (!ec)
|
|
dirs.push_back(temp_dir);
|
|
const std::filesystem::path work_dir =
|
|
std::filesystem::current_path(ec);
|
|
if (!ec)
|
|
dirs.push_back(work_dir);
|
|
|
|
std::string path;
|
|
for (size_t d = 0; d < dirs.size(); d++) {
|
|
const std::filesystem::path candidate =
|
|
dirs[d] / (name + ".synth.r8asm");
|
|
std::ofstream file(candidate, std::ios::binary);
|
|
if (!file)
|
|
continue;
|
|
file << body.str();
|
|
file.close();
|
|
if (!file)
|
|
continue;
|
|
if (d > 0)
|
|
std::cerr << "r8asm: %synth " << name << ": "
|
|
<< dirs.front().string()
|
|
<< " is not writable, using " << dirs[d].string()
|
|
<< std::endl;
|
|
path = candidate.string();
|
|
break;
|
|
}
|
|
if (path.empty()) {
|
|
main_tree.fail("%synth '" + name +
|
|
"': cannot write the synthesised macro anywhere");
|
|
} else {
|
|
import_file(path, main_tree.CELL_SIZE);
|
|
std::remove(path.c_str());
|
|
}
|
|
}
|
|
} break;
|
|
default:
|
|
break;
|
|
}
|
|
} break;
|
|
case token_t::BUILTIN: {
|
|
/*
|
|
BUILTIN
|
|
value: type
|
|
children: args
|
|
*/
|
|
/* like this:
|
|
builtin_var pos1 15
|
|
0 1 2
|
|
*/
|
|
to_return.children.push_back(
|
|
{r8asm::ast_node_t::BUILTIN, it->tokens[0].value, {}});
|
|
// build subtree for argument(s) of builtin
|
|
token_line buffer = {it->number, {}};
|
|
for (auto token_it = it->tokens.begin() + 1; // where argument begins
|
|
token_it < it->tokens.end(); token_it++)
|
|
buffer.tokens.push_back(*token_it);
|
|
auto args = parse_args(buffer).children;
|
|
for (const auto &arg_it : args)
|
|
to_return.children.back().children.push_back(
|
|
arg_it); // though all string args are treated as VARIABLE, I guess
|
|
// builtin_var will read children[0].value successfully.
|
|
} break;
|
|
case token_t::MACROCALL: {
|
|
/* like this:
|
|
raw_add 2 4 4 + 2 ( 4 + 4 ) pos1 pos1 + 2
|
|
0 1 ...
|
|
*/
|
|
to_return.children.push_back(
|
|
{r8asm::ast_node_t::MACROCALL, it->tokens[0].value, {}});
|
|
// build subtree for argument(s) of macro
|
|
token_line buffer = {it->number, {}};
|
|
for (auto token_it = it->tokens.begin() + 1; // where argument begins
|
|
token_it < it->tokens.end(); token_it++)
|
|
buffer.tokens.push_back(*token_it);
|
|
/*
|
|
MACROCALL
|
|
value: macro_name
|
|
children: arguments
|
|
*/
|
|
auto args = parse_args(buffer).children;
|
|
for (const auto &arg_it : args)
|
|
to_return.children.back().children.push_back(arg_it);
|
|
} break;
|
|
case token_t::ROT8: {
|
|
to_return.children.push_back(
|
|
{r8asm::ast_node_t::ROT8, it->tokens[0].value, {}});
|
|
} break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
return to_return;
|
|
}
|
|
#undef POINTS_AT_DIRECTIVE
|
|
|
|
#define UNRELATED_TO_LEFT(present_token) \
|
|
(it != line.tokens.begin() && \
|
|
((present_token->type == token_t::LPAREN || \
|
|
present_token->type == token_t::STRING || \
|
|
present_token->type == token_t::PARAMETER || \
|
|
(present_token->type == token_t::OPERATOR && \
|
|
(present_token->value == "op_neg" || \
|
|
present_token->value == "op_not"))) && \
|
|
(!((present_token - 1)->type == token_t::OPERATOR || \
|
|
(present_token - 1)->type == token_t::LPAREN))))
|
|
r8asm::ast_node r8asm::parse_args(const token_line &line) {
|
|
ast_node to_return = EMPTY_CODEBLOCK;
|
|
// arguments will be storaged in to_return.children
|
|
std::vector<token_line> raw_args;
|
|
std::list<token> buffer;
|
|
|
|
for (auto it = line.tokens.begin(); it != line.tokens.end(); it++) {
|
|
|
|
if (UNRELATED_TO_LEFT(it)) {
|
|
raw_args.push_back({line.number, {}});
|
|
while (!buffer.empty()) {
|
|
raw_args.back().tokens.push_back(buffer.front());
|
|
buffer.pop_front();
|
|
}
|
|
}
|
|
buffer.push_back(*it);
|
|
}
|
|
if (!buffer.empty()) {
|
|
raw_args.push_back({line.number, {}});
|
|
while (!buffer.empty()) {
|
|
raw_args.back().tokens.push_back(buffer.front());
|
|
buffer.pop_front();
|
|
}
|
|
}
|
|
for (const auto &it : raw_args)
|
|
to_return.children.push_back(parse_single_arg(it));
|
|
return to_return;
|
|
}
|
|
|
|
#undef UNRELATED_TO_LEFT
|
|
|
|
r8asm::ast_node r8asm::parse_single_arg(const token_line &line) {
|
|
ast_node to_return;
|
|
std::stack<ast_node> data_stack;
|
|
std::stack<ast_node> symbol_stack;
|
|
|
|
depth_t right_assoc_depth = 0;
|
|
auto deal_with_assoc = [&data_stack, &symbol_stack, &right_assoc_depth] {
|
|
while (right_assoc_depth) {
|
|
auto unary_op = symbol_stack.top();
|
|
symbol_stack.pop();
|
|
unary_op.children.push_back(data_stack.top());
|
|
data_stack.pop();
|
|
data_stack.push(unary_op);
|
|
right_assoc_depth--;
|
|
}
|
|
};
|
|
|
|
for (auto it = line.tokens.begin(); it < line.tokens.end(); it++) {
|
|
switch (it->type) {
|
|
case token_t::LPAREN: {
|
|
depth_t layer = 0;
|
|
token_line buffer = {line.number, {}};
|
|
|
|
for (it += 1; !((it->type == token_t::RPAREN) && (layer == 0)); it++) {
|
|
if (it->type == token_t::LPAREN)
|
|
layer++;
|
|
else if (it->type == token_t::RPAREN)
|
|
layer--;
|
|
buffer.tokens.push_back(*it);
|
|
}
|
|
data_stack.push(parse_single_arg(buffer));
|
|
deal_with_assoc();
|
|
} break;
|
|
case token_t::STRING: {
|
|
{
|
|
if (r8asm_strtools::is_integer(it->value))
|
|
data_stack.push({r8asm::ast_node_t::CONSTANT, it->value, {}});
|
|
else
|
|
data_stack.push({r8asm::ast_node_t::VARIABLE, it->value, {}});
|
|
}
|
|
deal_with_assoc();
|
|
} break;
|
|
case token_t::PARAMETER: {
|
|
data_stack.push({r8asm::ast_node_t::PARAMETER, (it->value), {}});
|
|
deal_with_assoc();
|
|
} break;
|
|
case token_t::OPERATOR:
|
|
// precedence: NEG = NOT
|
|
// > MUL = DIV
|
|
// > ADD = SUB
|
|
// > AND
|
|
// > XOR
|
|
// > OR
|
|
{
|
|
#define GET_PRECEDENCE(op_key) \
|
|
(main_tree.op_precedence_map.at(main_tree.operator_map.at(op_key)))
|
|
switch (main_tree.operator_map.at(it->value)) {
|
|
case ast::operator_t::NOT: // unary operators
|
|
{
|
|
right_assoc_depth++;
|
|
symbol_stack.push({ast_node_t::OPERATOR, it->value, {}});
|
|
} break;
|
|
default: // now all of them are binary operators
|
|
{
|
|
while ((!symbol_stack.empty()) &&
|
|
GET_PRECEDENCE(it->value) >=
|
|
GET_PRECEDENCE(symbol_stack.top().value))
|
|
// e.g. pushing + while here's already a * in symbol_stack
|
|
{
|
|
auto tmp_op = symbol_stack.top();
|
|
symbol_stack.pop();
|
|
auto tmp_arg = data_stack.top(); // 2nd arg got
|
|
data_stack.pop();
|
|
tmp_op.children.push_back(data_stack.top()); // 1st arg pushed
|
|
data_stack.pop();
|
|
tmp_op.children.push_back(tmp_arg);
|
|
data_stack.push(tmp_op);
|
|
}
|
|
symbol_stack.push({ast_node_t::OPERATOR, it->value, {}});
|
|
} break;
|
|
}
|
|
}
|
|
#undef GET_PRECEDENCE
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
while (!symbol_stack.empty()) {
|
|
auto tmp_op = symbol_stack.top();
|
|
symbol_stack.pop();
|
|
auto tmp_arg = data_stack.top(); // 2nd arg got
|
|
data_stack.pop();
|
|
tmp_op.children.push_back(data_stack.top()); // 1st arg pushed
|
|
data_stack.pop();
|
|
tmp_op.children.push_back(tmp_arg);
|
|
data_stack.push(tmp_op);
|
|
}
|
|
to_return = data_stack.top();
|
|
return to_return;
|
|
}
|
|
|
|
#undef EMPTY_CODEBLOCK
|
|
|
|
#define OPERATORS ("+-*/^~&|")
|
|
std::vector<r8asm::token_line>
|
|
r8asm::token_stream::lex(const std::string &char_stream) {
|
|
std::vector<token_line> to_return;
|
|
|
|
std::istringstream iss(char_stream);
|
|
std::string str_buffer;
|
|
line_number num = 0;
|
|
bool next_will_be_remark = false;
|
|
|
|
const std::map<std::string, std::string> operator_map = {
|
|
// without "-"
|
|
{"+", "op_add"}, {"*", "op_mul"}, {"/", "op_div"}, {"^", "op_xor"},
|
|
{"&", "op_and"}, {"|", "op_or"}, {"~", "op_not"}, {"-", "op_sub"},
|
|
};
|
|
|
|
bool first_line = true;
|
|
while (std::getline(iss, str_buffer)) {
|
|
// a UTF-8 BOM would otherwise end up glued to the first token and every
|
|
// editor on Windows writes one
|
|
if (first_line && str_buffer.size() >= 3 &&
|
|
(unsigned char)str_buffer[0] == 0xEF &&
|
|
(unsigned char)str_buffer[1] == 0xBB &&
|
|
(unsigned char)str_buffer[2] == 0xBF)
|
|
str_buffer.erase(0, 3);
|
|
first_line = false;
|
|
std::vector<token> token_buffer = {};
|
|
bool start_of_line = true;
|
|
bool lquote_appeared = false;
|
|
|
|
for (std::string token_val = r8asm_strtools::peel(str_buffer);
|
|
token_val != ""; token_val = r8asm_strtools::peel(str_buffer)) {
|
|
if (lquote_appeared && token_val[0] != '\"')
|
|
token_buffer.push_back({r8asm::token_t::STRING, token_val});
|
|
else {
|
|
if (start_of_line) { // it's the first token in the line
|
|
start_of_line = false;
|
|
if (token_val[0] == '.') // builtin
|
|
token_buffer.push_back(
|
|
{r8asm::token_t::BUILTIN, "builtin_" + token_val.substr(1)});
|
|
// e.g. .var -> builtin_var
|
|
else if (token_val[0] == '%') // directive
|
|
token_buffer.push_back(
|
|
{r8asm::token_t::DIRECTIVE, token_val.substr(1)});
|
|
// e.g. %macro -> macro
|
|
else if (token_val[0] == ';') // remark
|
|
next_will_be_remark = true;
|
|
else if (raw_rot8_ins.find(token_val) != raw_rot8_ins.end()) // rot8
|
|
token_buffer.push_back({r8asm::token_t::ROT8, "rot8_" + token_val});
|
|
else // macro
|
|
token_buffer.push_back({r8asm::token_t::MACROCALL, token_val});
|
|
} else { // it's a part of argument
|
|
if (!next_will_be_remark) {
|
|
if (token_val[0] == ';') // remark
|
|
next_will_be_remark = true;
|
|
else if (token_val[0] == '%') // parameter
|
|
token_buffer.push_back(
|
|
{r8asm::token_t::PARAMETER, token_val.substr(1)});
|
|
else if (token_val[0] == '"') { // quote(filename)
|
|
if (!lquote_appeared) {
|
|
lquote_appeared = true; // only %include will use quotes, they
|
|
// won't appear again
|
|
token_buffer.push_back({r8asm::token_t::LQUOTE, token_val});
|
|
} else
|
|
token_buffer.push_back({r8asm::token_t::RQUOTE, token_val});
|
|
} else if (token_val[0] == '(')
|
|
token_buffer.push_back({r8asm::token_t::LPAREN, token_val});
|
|
else if (token_val[0] == ')')
|
|
token_buffer.push_back({r8asm::token_t::RPAREN, token_val});
|
|
else if (std::string(OPERATORS).find(token_val[0]) !=
|
|
std::string::npos) // operator
|
|
token_buffer.push_back(
|
|
{r8asm::token_t::OPERATOR, operator_map.at(token_val)});
|
|
else
|
|
token_buffer.push_back({r8asm::token_t::STRING, token_val});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
num++;
|
|
if (!token_buffer.empty())
|
|
to_return.push_back({num, token_buffer});
|
|
next_will_be_remark = false;
|
|
}
|
|
return to_return;
|
|
}
|
|
#undef OPERATORS
|
|
|
|
bool r8asm::import_file(const std::string &filename, size_t cell_size) {
|
|
token_stream stream(filename);
|
|
if (!stream.ok) {
|
|
main_tree.fail("cannot open '" + filename + "'");
|
|
return (false);
|
|
}
|
|
|
|
// Before parsing: %include asks main_tree.CELL_SIZE for the width to hand
|
|
// to the included file, and that used to answer 0 because the size was
|
|
// only set at the end of this function.
|
|
main_tree.set_cell_size(cell_size);
|
|
|
|
for (const auto &token_it : parse(stream).children) {
|
|
main_tree.root.children.push_back(token_it);
|
|
}
|
|
return (!main_tree.error);
|
|
} |