Skip to content

Commit d1ed71b

Browse files
committed
adding Ark::State::doString, thus updating REPL to use the new State API
1 parent e4fa32c commit d1ed71b

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

include/Ark/VM/State.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ namespace Ark
2525

2626
// to compile file *only* if needed and use the resulting bytecode
2727
bool doFile(const std::string& filename);
28+
// compile string and store resulting bytecode in m_bytecode
29+
bool doString(const std::string& code);
2830

2931
void loadFunction(const std::string& name, internal::Value::ProcType function);
3032
void setDebug(bool value);

src/REPL/Repl.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ namespace Ark
99

1010
print_repl_header();
1111

12-
while(true)
12+
while (true)
1313
{
14-
Ark::Compiler compiler(false, m_lib_dir, m_options);
1514
Ark::State state(m_lib_dir, /* filename */ "FILE", m_options);
1615
Ark::VM vm(&state, m_options);
1716
state.setDebug(false);
@@ -48,25 +47,10 @@ namespace Ark
4847
std::cout << continuing_prompt;
4948
}
5049

51-
try
52-
{
53-
compiler.feed("{" + code.str() + "}");
54-
compiler.compile();
55-
56-
state.feed(compiler.bytecode());
50+
if (state.doString("{" + code.str() + "}"))
5751
vm.run();
58-
}
59-
catch (const std::runtime_error& e) {
60-
std::cerr << e.what() << std::endl;
61-
}
62-
catch (const std::exception& e)
63-
{
64-
std::cerr << e.what() << std::endl;
65-
}
66-
catch (...)
67-
{
68-
std::cerr << "Unknown lexer-parser-or-compiler error" << std::endl;
69-
}
52+
else
53+
std::cerr << "Ark::State::doString failed" << std::endl;
7054
}
7155
}
7256

src/VM/State.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,31 @@ namespace Ark
142142
return false;
143143
}
144144

145+
bool State::doString(const std::string& code)
146+
{
147+
Compiler compiler(m_debug, m_libdir, m_options);
148+
149+
try
150+
{
151+
compiler.feed(code);
152+
compiler.compile();
153+
}
154+
catch (const std::exception& e)
155+
{
156+
std::cerr << e.what() << std::endl;
157+
return false;
158+
}
159+
catch (...)
160+
{
161+
std::cerr << "Unknown lexer-parser-or-compiler error" << std::endl;
162+
return false;
163+
}
164+
165+
state.feed(compiler.bytecode());
166+
167+
return true;
168+
}
169+
145170
void State::loadFunction(const std::string& name, internal::Value::ProcType function)
146171
{
147172
m_binded_functions[name] = std::move(function);

0 commit comments

Comments
 (0)