fixes, command for sm parser, basic sm printer

This commit is contained in:
ProgramSnail 2025-05-24 17:53:32 +03:00
parent 6c19722d9e
commit 66ccf639c8
4 changed files with 123 additions and 3 deletions

View file

@ -240,3 +240,5 @@ struct SMInstr {
std::vector<SMInstr> parse_sm(std::istream &in); std::vector<SMInstr> parse_sm(std::istream &in);
std::optional<SMInstr> parse_sm(const std::string &line); std::optional<SMInstr> parse_sm(const std::string &line);
std::string print_sm(const SMInstr &instr);

View file

@ -1,7 +1,9 @@
#include <fstream>
#include <iostream> #include <iostream>
#include "analyzer.hpp" #include "analyzer.hpp"
#include "parser.hpp" #include "parser.hpp"
#include "sm_parser.hpp"
extern "C" { extern "C" {
#include "../../runtime/runtime.h" #include "../../runtime/runtime.h"
@ -22,6 +24,13 @@ int main(int argc, char **argv) {
bool do_print = false; bool do_print = false;
if (strcmp(argv[1], "-i") == 0) { if (strcmp(argv[1], "-i") == 0) {
do_interpretation = true; do_interpretation = true;
} else if (strcmp(argv[1], "-sm") == 0) { // TODO: TMP, FOR CHECKS
std::ifstream file(argv[2]);
auto instrs = parse_sm(file);
std::cout << "instructions form file\n";
for (auto &instr : instrs) {
std::cout << print_sm(instr) << "\n";
}
} }
#ifdef WITH_CHECK #ifdef WITH_CHECK
else if (strcmp(argv[1], "-vi") == 0) { else if (strcmp(argv[1], "-vi") == 0) {

View file

@ -543,3 +543,112 @@ std::optional<SMInstr> parse_sm(const std::string &line) {
return instr.build(); return instr.build();
} }
// TODO: TMP: not efficient, for test purposes only
// TODO: number of printed information reduced for now
std::string print_sm(const SMInstr &instr) {
return {std::visit<std::string>( //
utils::multifunc{
//
[](const SMInstr::PUBLIC &x) -> std::string {
return "PUBLIC [" + x.name + "]";
},
[](const SMInstr::EXTERN &x) -> std::string {
return "EXTERN [" + x.name + "]";
},
[](const SMInstr::IMPORT &x) -> std::string {
return "IMPORT [" + x.name + "]";
},
[](const SMInstr::CLOSURE &x) -> std::string {
return "CLOSURE [" + x.name +
". args_count=" + std::to_string(x.closure.size()) + "]";
},
[](const SMInstr::CONST &x) -> std::string {
return "CONST [" + std::to_string(x.n) + "]";
},
[](const SMInstr::STRING &x) -> std::string {
return "STRING [" + x.str + "]";
},
[](const SMInstr::LDA &) -> std::string {
// x.v
return "LDA";
},
[](const SMInstr::LD &) -> std::string {
// x.v
return "LD";
},
[](const SMInstr::ST &) -> std::string {
// x.v
return "ST";
},
[](const SMInstr::STA &) -> std::string { return "STA"; },
[](const SMInstr::STI &) -> std::string { return "STI"; },
[](const SMInstr::BINOP &) -> std::string {
// x.opr
return "BINOP";
},
[](const SMInstr::LABEL &x) -> std::string {
return "LABEL [" + x.s + "]";
},
[](const SMInstr::FLABEL &x) -> std::string {
return "FLABEL [" + x.s + "]";
},
[](const SMInstr::SLABEL &x) -> std::string {
return "SLABEL [" + x.s + "]";
},
[](const SMInstr::JMP &x) -> std::string {
return "JMP [" + x.l + "]";
},
[](const SMInstr::CJMP &x) -> std::string {
return "CJMP [" + x.s + ". " + x.l + "]";
},
[](const SMInstr::BEGIN &) -> std::string {
// x.f
// x.nargs
// x.nlocals
// x.closure
// x.args
// x.scopes
return "BEGIN";
},
[](const SMInstr::END &) -> std::string { return "END"; },
[](const SMInstr::RET &) -> std::string { return "RET"; },
[](const SMInstr::ELEM &) -> std::string { return "ELEM"; },
[](const SMInstr::CALL &x) -> std::string {
// x.tail
return "CALL [" + x.fname + ". " + std::to_string(x.n) + "]";
},
[](const SMInstr::CALLC &x) -> std::string {
// x.tail
return "CALLC [" + std::to_string(x.n) + "]";
},
[](const SMInstr::SEXP &x) -> std::string {
return "SEXP [" + x.tag + ". " + std::to_string(x.n) + "]";
},
[](const SMInstr::DROP &) -> std::string { return "DROP"; },
[](const SMInstr::DUP &) -> std::string { return "DUP"; },
[](const SMInstr::SWAP &) -> std::string { return "SWAP"; },
[](const SMInstr::TAG &x) -> std::string {
return "TAG [" + x.tag + ". " + std::to_string(x.n) + "]";
},
[](const SMInstr::ARRAY &x) -> std::string {
return "ARRAY [" + std::to_string(x.n) + "]";
},
[](const SMInstr::PATT &) -> std::string {
// x.patt
return "PATT";
},
[](const SMInstr::LINE &x) -> std::string {
return "LINE [" + std::to_string(x.n) + "]";
},
[](const SMInstr::FAIL &x) -> std::string {
return "FAIL [" + std::to_string(x.line) + ". " +
std::to_string(x.col) + ". " + std::to_string(x.val) + ". " +
"]";
},
// [](auto) -> std::string {
// throw std::bad_any_cast{}; // create another error ?
// },
},
*instr)};
}

View file

@ -1,8 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# TODO: FIXME: error on test 3, possible UB or standard change # TODO: FIXME: error on test 3, possible UB or standard change
xmake build # xmake build
cp "build/linux/x86_64/release/byterun" byterun.exe # cp "build/linux/x86_64/release/byterun" byterun.exe
dune build > /dev/null dune build > /dev/null
@ -25,7 +25,7 @@ for mod in ../stdlib/*.lama; do
done done
echo "Run tests:" echo "Run tests:"
for test in ../stdlib/regression/*03.lama; do for test in ../stdlib/regression/*.lama; do
echo $test echo $test
$compiler -b $test -I ../stdlib/ > /dev/null $compiler -b $test -I ../stdlib/ > /dev/null
test_path="${test%.*}" test_path="${test%.*}"