From 66ccf639c80521d8c5d5bbac95d4559d6e6a723b Mon Sep 17 00:00:00 2001 From: ProgramSnail Date: Sat, 24 May 2025 17:53:32 +0300 Subject: [PATCH] fixes, command for sm parser, basic sm printer --- byterun/include/sm_parser.hpp | 2 + byterun/src/cli.cpp | 9 +++ byterun/src/sm_parser.cpp | 109 +++++++++++++++++++++++++++++ byterun/stdlib_regression_check.sh | 6 +- 4 files changed, 123 insertions(+), 3 deletions(-) diff --git a/byterun/include/sm_parser.hpp b/byterun/include/sm_parser.hpp index 05eb560b0..61187121c 100644 --- a/byterun/include/sm_parser.hpp +++ b/byterun/include/sm_parser.hpp @@ -240,3 +240,5 @@ struct SMInstr { std::vector parse_sm(std::istream &in); std::optional parse_sm(const std::string &line); + +std::string print_sm(const SMInstr &instr); diff --git a/byterun/src/cli.cpp b/byterun/src/cli.cpp index 70261f034..af52ba733 100644 --- a/byterun/src/cli.cpp +++ b/byterun/src/cli.cpp @@ -1,7 +1,9 @@ +#include #include #include "analyzer.hpp" #include "parser.hpp" +#include "sm_parser.hpp" extern "C" { #include "../../runtime/runtime.h" @@ -22,6 +24,13 @@ int main(int argc, char **argv) { bool do_print = false; if (strcmp(argv[1], "-i") == 0) { 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 else if (strcmp(argv[1], "-vi") == 0) { diff --git a/byterun/src/sm_parser.cpp b/byterun/src/sm_parser.cpp index 1c549d84e..22e88ad65 100644 --- a/byterun/src/sm_parser.cpp +++ b/byterun/src/sm_parser.cpp @@ -543,3 +543,112 @@ std::optional parse_sm(const std::string &line) { 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( // + 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)}; +} diff --git a/byterun/stdlib_regression_check.sh b/byterun/stdlib_regression_check.sh index 92382f1cb..24e875f37 100755 --- a/byterun/stdlib_regression_check.sh +++ b/byterun/stdlib_regression_check.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash # TODO: FIXME: error on test 3, possible UB or standard change -xmake build -cp "build/linux/x86_64/release/byterun" byterun.exe +# xmake build +# cp "build/linux/x86_64/release/byterun" byterun.exe dune build > /dev/null @@ -25,7 +25,7 @@ for mod in ../stdlib/*.lama; do done echo "Run tests:" -for test in ../stdlib/regression/*03.lama; do +for test in ../stdlib/regression/*.lama; do echo $test $compiler -b $test -I ../stdlib/ > /dev/null test_path="${test%.*}"