25 #include <sys/resource.h> 36 static bool enableTiming =
false;
42 static void logTimeSince(clock_t c1,
const char *msg)
44 if (!enableTiming) {
return; }
47 getrusage(RUSAGE_SELF, &r);
48 auto oldLocale = std::cerr.imbue(std::locale(
"en_GB.UTF-8"));
49 std::cerr << msg <<
" took " 50 << (
static_cast<double>(c2) - static_cast<double>(c1)) / static_cast<double>(CLOCKS_PER_SEC)
51 <<
" seconds. Peak used " << r.ru_maxrss/1024 <<
"KB." << std::endl;
52 std::cerr.imbue(oldLocale);
59 std::cerr <<
"usage: " << cmd <<
" [-himt] [-f {file name}]" << std::endl
60 <<
" -h Display this help" << std::endl
61 <<
" -c Force compilation" << std::endl
62 <<
" -i Interpreter, enable REPL mode" << std::endl
63 <<
" -m Display memory usage stats on exit" << std::endl
64 <<
" -t Display timing information" << std::endl
65 <<
" -f {file} Load and execute file" << std::endl;
68 int main(
int argc,
char **argv)
74 bool memstats =
false;
76 const char *file =
nullptr;
83 pegmatite::ErrorReporter err =
84 [](
const pegmatite::InputRange &r, std::string s) {
85 std::cerr <<
"Syntax error: \n line " << r.start.line
86 <<
", column " << r.start.col;
89 while ((c = getopt(argc, argv,
"chmitf:")) != -1)
122 logTimeSince(c1,
"Setup");
124 std::unique_ptr<AST::Statements> ast = 0;
129 pegmatite::AsciiFileInput input(
open(file, O_RDONLY));
136 logTimeSince(c1,
"Parsing program");
140 logTimeSince(c1,
"Executing program");
144 std::vector<std::unique_ptr<AST::Statements>> replASTs;
155 logTimeSince(c1,
"Garbage collection");
159 std::unique_ptr<char, decltype(free)*> line(
readline(
"\nMysoreScript> "), free);
161 if (line ==
nullptr || line.get()[0] ==
'\0')
166 add_history(line.get());
169 std::string buffer(line.get());
173 std::cout <<
"\nMysoreScript> ";
175 std::getline(std::cin, buffer);
177 if (buffer.size() == 0)
183 pegmatite::StringInput input(std::move(buffer));
184 std::unique_ptr<AST::Statements> ast = 0;
190 logTimeSince(c1,
"Parsing program");
194 logTimeSince(c1,
"Executing program");
197 replASTs.push_back(std::move(ast));
202 std::cerr.imbue(std::locale(
"en_GB.UTF-8"));
203 std::cerr <<
"Allocated a total of " << GC_get_total_bytes()
204 <<
" bytes during execution." << std::endl;
205 std::cerr <<
"GC heap size: " << GC_get_heap_size() <<
" bytes." 209 GC_gcollect_and_unmap();
210 std::cerr <<
"After collection, GC heap size: " << GC_get_heap_size()
211 <<
" bytes." << std::endl;
Rule ignored
Rule for treating both comments and whitespace as ignored tokens.
void interpret(Interpreter::Context &c)
Interprets each of the statements in turn.
Class representing a parser for the MysoreScript language.
bool forceCompiler
Force the compiler to run.
Rule statements
A list of statements: the top-level for programs in this grammar.
int main(int argc, char **argv)
void usage(const char *cmd)
Print the usage message.
const MysoreScriptGrammar & g
The grammar that this parser uses.