First commit.
This commit is contained in:
commit
14d4bc5517
7 files changed
+149
No files matched your search
@@ -0,0 +1,3 @@
|
||||
BasedOnStyle: LLVM
|
||||
IndentWidth: 4
|
||||
UseTab: Always
|
||||
@@ -0,0 +1,5 @@
|
||||
build/
|
||||
.cache/
|
||||
.continue/
|
||||
.vscode/
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
project(R8ASM)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
include_directories(include)
|
||||
|
||||
file(GLOB_RECURSE SOURCES "src/*.cc")
|
||||
|
||||
add_library(r8asm_core STATIC ${SOURCES})
|
||||
|
||||
add_executable(r8asm_test tests/r8_test.cc)
|
||||
target_link_libraries(r8asm_test r8asm_core)
|
||||
@@ -0,0 +1,31 @@
|
||||
# R8ASM
|
||||
|
||||
---
|
||||
|
||||
```nasm
|
||||
|
||||
section .data
|
||||
UNIT_MAX 255
|
||||
|
||||
section .text
|
||||
|
||||
%macro erase 1
|
||||
.tp %1
|
||||
stp
|
||||
.xor UNIT_MAX
|
||||
btp
|
||||
.loop
|
||||
biz
|
||||
flb
|
||||
ror
|
||||
rnz
|
||||
.endloop
|
||||
%endmacro
|
||||
|
||||
_start:
|
||||
.xor 15
|
||||
erase $
|
||||
|
||||
```
|
||||
|
||||
##
|
||||
@@ -0,0 +1,42 @@
|
||||
#ifndef R8ASM
|
||||
#define R8ASM
|
||||
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#define ROT8_BC_TO_CHAR(bc) "><+-[].,"[static_cast<int>(bc)]
|
||||
|
||||
enum class rot8_bytecode { STP, BTP, ROR, FLB, BIZ, RNZ, OUT, INP };
|
||||
enum class r8asm_macro {
|
||||
LOOP,
|
||||
ENDLOOP,
|
||||
TP,
|
||||
XOR,
|
||||
};
|
||||
|
||||
using R8Operand = std::variant<std::monostate, int, std::string>;
|
||||
using R8MetaOp = std::variant<r8asm_macro, rot8_bytecode>;
|
||||
|
||||
struct R8Instruction {
|
||||
R8MetaOp op;
|
||||
R8Operand arg = std::monostate{};
|
||||
};
|
||||
|
||||
class R8Macro {
|
||||
std::vector<R8Operand> args = {std::monostate{}};
|
||||
std::vector<R8Instruction> ins;
|
||||
};
|
||||
|
||||
std::vector<rot8_bytecode> r8asm_tape_out(std::span<const R8Instruction> ops);
|
||||
|
||||
extern std::map<std::string, int> labels;
|
||||
extern std::map<std::string, int> datas;
|
||||
extern std::map<std::string, std::vector<R8Macro>> macros;
|
||||
|
||||
extern uint8_t UNIT_SIZE;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "r8asm/r8asm.hh"
|
||||
#include <variant>
|
||||
|
||||
uint8_t UNIT_SIZE = 8;
|
||||
|
||||
std::map<std::string, int> labels = {{std::string("main"), 0}};
|
||||
std::map<std::string, int> datas = {{std::string("_UNIT_SIZE"), UNIT_SIZE}};
|
||||
|
||||
void expand_xor(std::vector<rot8_bytecode> &tape, R8Operand arg) {
|
||||
if (const std::string *operand = std::get_if<std::string>(&arg)) {
|
||||
} // TODO
|
||||
}
|
||||
|
||||
void expand_loop(std::vector<rot8_bytecode> &tape, R8Operand arg) {
|
||||
tape.insert(tape.end(), {
|
||||
rot8_bytecode::STP,
|
||||
});
|
||||
}
|
||||
|
||||
std::vector<rot8_bytecode> r8asm_tape_out(std::span<const R8Instruction> ops) {
|
||||
std::vector<rot8_bytecode> tape;
|
||||
for (const auto it : ops) {
|
||||
int repeat = 1;
|
||||
if (const rot8_bytecode *bc = std::get_if<rot8_bytecode>(&it.op)) {
|
||||
if (const std::string *operand = std::get_if<std::string>(&it.arg))
|
||||
repeat = datas[*operand];
|
||||
else if (const int *operand = std::get_if<int>(&it.arg))
|
||||
repeat = *operand;
|
||||
for (int i = 0; i < repeat; i++)
|
||||
tape.push_back(*bc);
|
||||
} else if (const r8asm_macro *macro = std::get_if<r8asm_macro>(&it.op))
|
||||
switch (*macro) {
|
||||
case r8asm_macro::LOOP:
|
||||
expand_loop(tape, it.arg);
|
||||
break;
|
||||
case r8asm_macro::ENDLOOP:;
|
||||
break;
|
||||
case r8asm_macro::TP:;
|
||||
break;
|
||||
case r8asm_macro::XOR:;
|
||||
break;
|
||||
default:;
|
||||
}
|
||||
}
|
||||
return (tape);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "r8asm/r8asm.hh"
|
||||
#include <iostream>
|
||||
int main() {
|
||||
std::vector<R8Instruction> ins = {{rot8_bytecode::STP, 3}};
|
||||
std::vector<rot8_bytecode> tape = r8asm_tape_out(ins);
|
||||
for (const auto i : tape)
|
||||
std::cout << ROT8_BC_TO_CHAR(i);
|
||||
return (0);
|
||||
}
|
||||
Reference in new issue
Block a user