From 4531fc04b8be09cba9a33002c64ef90428454815 Mon Sep 17 00:00:00 2001 From: q66 Date: Wed, 24 Aug 2016 19:20:25 +0100 Subject: [PATCH] get rid of shared state for src_file/src_str --- cs_gen.cc | 8 +++--- cs_vm.cc | 34 ++++++++++++++---------- cs_vm.hh | 71 ++++++++++++++++++++++++++++----------------------- cubescript.hh | 3 --- 4 files changed, 63 insertions(+), 53 deletions(-) diff --git a/cs_gen.cc b/cs_gen.cc index f1c45c4..da13ba6 100644 --- a/cs_gen.cc +++ b/cs_gen.cc @@ -781,7 +781,7 @@ static void compileblockmain(GenState &gs, int wordtype, int prevargs) { char c = gs.next_char(); switch (c) { case '\0': - cs_debug_code_line(gs.cs, line, "missing \"]\""); + cs_debug_code_line(gs, line, "missing \"]\""); gs.source--; goto done; case '\"': @@ -810,7 +810,7 @@ static void compileblockmain(GenState &gs, int wordtype, int prevargs) { if (brak > level) { continue; } else if (brak < level) { - cs_debug_code_line(gs.cs, line, "too many @s"); + cs_debug_code_line(gs, line, "too many @s"); } if (!concs && prevargs >= MaxResults) { gs.code.push(CODE_ENTER); @@ -1667,7 +1667,7 @@ endstatement: switch (c) { case '\0': if (c != brak) { - cs_debug_code_line(gs.cs, line, "missing \"%c\"", brak); + cs_debug_code_line(gs, line, "missing \"%c\"", brak); } gs.source--; return; @@ -1676,7 +1676,7 @@ endstatement: if (c == brak) { return; } - cs_debug_code_line(gs.cs, line, "unexpected \"%c\"", c); + cs_debug_code_line(gs, line, "unexpected \"%c\"", c); break; case '/': if (gs.current() == '/') { diff --git a/cs_vm.cc b/cs_vm.cc index 4240a76..37f7ece 100644 --- a/cs_vm.cc +++ b/cs_vm.cc @@ -27,14 +27,15 @@ static inline void cs_pop_alias(Ident *id) { } ostd::ConstCharRange cs_debug_line( - CsState &cs, ostd::ConstCharRange p, ostd::ConstCharRange fmt, + ostd::ConstCharRange src_file, ostd::ConstCharRange src_str, + ostd::ConstCharRange p, ostd::ConstCharRange fmt, ostd::CharRange buf ) { - if (cs.src_str.empty()) { + if (src_str.empty()) { return fmt; } ostd::Size num = 1; - ostd::ConstCharRange line(cs.src_str); + ostd::ConstCharRange line(src_str); for (;;) { ostd::ConstCharRange end = ostd::find(line, '\n'); if (!end.empty()) { @@ -42,8 +43,8 @@ ostd::ConstCharRange cs_debug_line( } if (&p[0] >= &line[0] && &p[0] <= &line[line.size()]) { ostd::CharRange r(buf); - if (!cs.src_file.empty()) { - ostd::format(r, "%s:%d: %s", cs.src_file, num, fmt); + if (!src_file.empty()) { + ostd::format(r, "%s:%d: %s", src_file, num, fmt); } else { ostd::format(r, "%d: %s", num, fmt); } @@ -1569,17 +1570,26 @@ void CsState::run_ret(Bytecode const *code, CsValue &ret) { runcode(*this, reinterpret_cast(code), ret); } -void CsState::run_ret(ostd::ConstCharRange code, CsValue &ret) { - GenState gs(*this); +static void cs_run_ret( + CsState &cs, ostd::ConstCharRange code, ostd::ConstCharRange src_file, + ostd::ConstCharRange src_str, CsValue &ret +) { + GenState gs(cs); + gs.src_file = src_file; + gs.src_str = src_str; gs.code.reserve(64); /* FIXME range */ gs.gen_main(code.data(), VAL_ANY); - runcode(*this, gs.code.data() + 1, ret); + runcode(cs, gs.code.data() + 1, ret); if (int(gs.code[0]) >= 0x100) { gs.code.disown(); } } +void CsState::run_ret(ostd::ConstCharRange code, CsValue &ret) { + cs_run_ret(*this, code, ostd::ConstCharRange(), ostd::ConstCharRange(), ret); +} + void CsState::run_ret(Ident *id, CsValueRange args, CsValue &ret) { int nargs = int(args.size()); ret.set_null(); @@ -1772,7 +1782,6 @@ void CsState::run(Ident *id, CsValueRange args) { static bool cs_run_file( CsState &cs, ostd::ConstCharRange fname, CsValue &ret ) { - ostd::ConstCharRange oldsrcfile = cs.src_file, oldsrcstr = cs.src_str; ostd::Box buf; ostd::Size len; @@ -1788,11 +1797,8 @@ static bool cs_run_file( } buf[len] = '\0'; - cs.src_file = fname; - cs.src_str = ostd::ConstCharRange(buf.get(), len); - cs.run_ret(cs.src_str, ret); - cs.src_file = oldsrcfile; - cs.src_str = oldsrcstr; + ostd::ConstCharRange src_str = ostd::ConstCharRange(buf.get(), len); + cs_run_ret(cs, src_str, fname, src_str, ret); return true; } diff --git a/cs_vm.hh b/cs_vm.hh index 6c7b2aa..a4e241c 100644 --- a/cs_vm.hh +++ b/cs_vm.hh @@ -105,46 +105,18 @@ static void cs_do_args(CsState &cs, F body) { } } -ostd::ConstCharRange cs_debug_line( - CsState &cs, ostd::ConstCharRange p, ostd::ConstCharRange fmt, - ostd::CharRange buf -); - -void cs_debug_alias(CsState &cs); - -template -void cs_debug_code(CsState &cs, ostd::ConstCharRange fmt, A &&...args) { - if (cs.nodebug) { - return; - } - ostd::err.writefln(fmt, ostd::forward(args)...); - cs_debug_alias(cs); -} - -template -void cs_debug_code_line( - CsState &cs, ostd::ConstCharRange p, ostd::ConstCharRange fmt, A &&...args -) { - if (cs.nodebug) { - return; - } - ostd::Array buf; - ostd::err.writefln( - cs_debug_line(cs, p, fmt, ostd::CharRange(buf.data(), buf.size())), - ostd::forward(args)... - ); - cs_debug_alias(cs); -} - ostd::Uint32 *compilecode(CsState &cs, ostd::ConstCharRange str); struct GenState { CsState &cs; + ostd::ConstCharRange src_file, src_str; CsVector code; char const *source; GenState() = delete; - GenState(CsState &csr): cs(csr), code(), source(nullptr) {} + GenState(CsState &csr): + cs(csr), src_file(), src_str(), code(), source(nullptr) + {} void gen_str(ostd::ConstCharRange word, bool macro = false) { if (word.size() <= 3 && !macro) { @@ -239,6 +211,41 @@ struct GenState { } }; +ostd::ConstCharRange cs_debug_line( + ostd::ConstCharRange src_file, ostd::ConstCharRange src_str, + ostd::ConstCharRange p, ostd::ConstCharRange fmt, + ostd::CharRange buf +); + +void cs_debug_alias(CsState &cs); + +template +void cs_debug_code(CsState &cs, ostd::ConstCharRange fmt, A &&...args) { + if (cs.nodebug) { + return; + } + ostd::err.writefln(fmt, ostd::forward(args)...); + cs_debug_alias(cs); +} + +template +void cs_debug_code_line( + GenState &gs, ostd::ConstCharRange p, ostd::ConstCharRange fmt, A &&...args +) { + if (gs.cs.nodebug) { + return; + } + ostd::Array buf; + ostd::err.writefln( + cs_debug_line( + gs.src_file, gs.src_str, p, fmt, + ostd::CharRange(buf.data(), buf.size()) + ), + ostd::forward(args)... + ); + cs_debug_alias(gs.cs); +} + CsString intstr(int v); CsString floatstr(CsFloat v); diff --git a/cubescript.hh b/cubescript.hh index abdfb93..48cc4ef 100644 --- a/cubescript.hh +++ b/cubescript.hh @@ -380,9 +380,6 @@ struct OSTD_EXPORT CsState { IdentLink noalias; IdentLink *p_stack = &noalias; - ostd::ConstCharRange src_file; - ostd::ConstCharRange src_str; - int identflags = 0; int nodebug = 0;