From 88663269262e882f63df23ea0ce93f3e95d74a2c Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 16 Feb 2015 15:27:02 -0800 Subject: 763 - start of C++ version --- cpp/000test.cc | 26 ++++ cpp/001trace.cc | 338 +++++++++++++++++++++++++++++++++++++++++++++++++++ cpp/001trace.test.cc | 164 +++++++++++++++++++++++++ cpp/002main.cc | 56 +++++++++ cpp/boot.cc | 63 ++++++++++ cpp/makefile | 23 ++++ 6 files changed, 670 insertions(+) create mode 100644 cpp/000test.cc create mode 100644 cpp/001trace.cc create mode 100644 cpp/001trace.test.cc create mode 100644 cpp/002main.cc create mode 100644 cpp/boot.cc create mode 100644 cpp/makefile diff --git a/cpp/000test.cc b/cpp/000test.cc new file mode 100644 index 00000000..2754b254 --- /dev/null +++ b/cpp/000test.cc @@ -0,0 +1,26 @@ +typedef void (*test_fn)(void); + +const test_fn Tests[] = { + #include "test_list" // auto-generated; see makefile +}; + +bool Passed = true; + +long Num_failures = 0; + +#define CHECK(X) \ + if (!(X)) { \ + ++Num_failures; \ + cerr << "\nF " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " << #X << '\n'; \ + Passed = false; \ + return; \ + } + +#define CHECK_EQ(X, Y) \ + if ((X) != (Y)) { \ + ++Num_failures; \ + cerr << "\nF " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): " << #X << " == " << #Y << '\n'; \ + cerr << " got " << (X) << '\n'; /* BEWARE: multiple eval */ \ + Passed = false; \ + return; \ + } diff --git a/cpp/001trace.cc b/cpp/001trace.cc new file mode 100644 index 00000000..a99951e0 --- /dev/null +++ b/cpp/001trace.cc @@ -0,0 +1,338 @@ +bool Hide_warnings = false; + +struct trace_stream { + vector > > past_lines; // [(layer label, frame, line)] + unordered_map frame; + // accumulator for current line + ostringstream* curr_stream; + string curr_layer; + string dump_layer; + trace_stream() :curr_stream(NULL) {} + ~trace_stream() { if (curr_stream) delete curr_stream; } + + ostringstream& stream(string layer) { + newline(); + curr_stream = new ostringstream; + curr_layer = layer; + return *curr_stream; + } + + // be sure to call this before messing with curr_stream or curr_layer or frame + void newline() { + if (!curr_stream) return; + past_lines.push_back(pair >(curr_layer, pair(frame[curr_layer], curr_stream->str()))); + if (curr_layer == "dump") + cerr << with_newline(curr_stream->str()); + else if ((!dump_layer.empty() && prefix_match(dump_layer, curr_layer)) + || (!Hide_warnings && curr_layer == "warn")) + cerr << curr_layer << "/" << frame[curr_layer] << ": " << with_newline(curr_stream->str()); + delete curr_stream; + curr_stream = NULL; + } + + string readable_contents(string layer) { // missing layer = everything, frame, hierarchical layers + newline(); + ostringstream output; + string real_layer, frame; + parse_layer_and_frame(layer, &real_layer, &frame); + for (vector > >::iterator p = past_lines.begin(); p != past_lines.end(); ++p) + if (layer.empty() || prefix_match(real_layer, p->first)) + output << p->first << "/" << p->second.first << ": " << with_newline(p->second.second); + return output.str(); + } + + void dump_browseable_contents(string layer) { + ofstream dump("dump"); + dump << "
start
\n"; + for (vector > >::iterator p = past_lines.begin(); p != past_lines.end(); ++p) { + if (p->first != layer) continue; + dump << "
"; + dump << p->second.second; + dump << "
\n"; + } + dump.close(); + } + + string with_newline(string s) { + if (s[s.size()-1] != '\n') return s+'\n'; + return s; + } +}; + + + +trace_stream* Trace_stream = NULL; + +// Top-level helper. IMPORTANT: can't nest. +#define trace(layer) !Trace_stream ? cerr /*print nothing*/ : Trace_stream->stream(layer) +// Warnings should go straight to cerr by default since calls to trace() have +// some unfriendly constraints (they delay printing, they can't nest) +#define RAISE ((!Trace_stream || !Hide_warnings) ? cerr /*do print*/ : Trace_stream->stream("warn")) << __FILE__ << ":" << __LINE__ << " " +// Just debug logging without any test support. +#define dbg cerr << __FUNCTION__ << '(' << __FILE__ << ':' << __LINE__ << ") " + +// RAISE << die exits after printing -- unless Hide_warnings is set. +struct die {}; +ostream& operator<<(ostream& os, unused die) { + if (Hide_warnings) return os; + os << "dying"; + exit(1); +} + +#define CLEAR_TRACE delete Trace_stream, Trace_stream = new trace_stream; + +#define DUMP(layer) cerr << Trace_stream->readable_contents(layer) + +// Trace_stream is a resource, lease_tracer uses RAII to manage it. +struct lease_tracer { + lease_tracer() { Trace_stream = new trace_stream; } + ~lease_tracer() { delete Trace_stream, Trace_stream = NULL; } +}; + +#define START_TRACING_UNTIL_END_OF_SCOPE lease_tracer leased_tracer; + +void trace_all(const string& label, const list& in) { + for (list::const_iterator p = in.begin(); p != in.end(); ++p) + trace(label) << *p; +} + +bool check_trace_contents(string FUNCTION, string FILE, int LINE, string expected) { // missing layer == anywhere, frame, hierarchical layers + vector expected_lines = split(expected, ""); + size_t curr_expected_line = 0; + while (curr_expected_line < expected_lines.size() && expected_lines[curr_expected_line].empty()) + ++curr_expected_line; + if (curr_expected_line == expected_lines.size()) return true; + Trace_stream->newline(); + ostringstream output; + string layer, frame, contents; + parse_layer_frame_contents(expected_lines[curr_expected_line], &layer, &frame, &contents); + for (vector > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { + if (!layer.empty() && !prefix_match(layer, p->first)) + continue; + + if (!frame.empty() && strtol(frame.c_str(), NULL, 0) != p->second.first) + continue; + + if (contents != p->second.second) + continue; + + ++curr_expected_line; + while (curr_expected_line < expected_lines.size() && expected_lines[curr_expected_line].empty()) + ++curr_expected_line; + if (curr_expected_line == expected_lines.size()) return true; + parse_layer_frame_contents(expected_lines[curr_expected_line], &layer, &frame, &contents); + } + + ++Num_failures; + cerr << "\nF " << FUNCTION << "(" << FILE << ":" << LINE << "): missing [" << contents << "] in trace:\n"; + DUMP(layer); + Passed = false; + return false; +} + +void parse_layer_frame_contents(const string& orig, string* layer, string* frame, string* contents) { + string layer_and_frame; + parse_contents(orig, ": ", &layer_and_frame, contents); + parse_layer_and_frame(layer_and_frame, layer, frame); +} + +void parse_contents(const string& s, const string& delim, string* prefix, string* contents) { + string::size_type pos = s.find(delim); + if (pos == NOT_FOUND) { + *prefix = ""; + *contents = s; + } + else { + *prefix = s.substr(0, pos); + *contents = s.substr(pos+delim.size()); + } +} + +void parse_layer_and_frame(const string& orig, string* layer, string* frame) { + size_t last_slash = orig.rfind('/'); + if (last_slash == NOT_FOUND + || last_slash == orig.size()-1 // trailing slash indicates hierarchical layer + || orig.find_last_not_of("0123456789") != last_slash) { + *layer = orig; + *frame = ""; + } + else { + *layer = orig.substr(0, last_slash); + *frame = orig.substr(last_slash+1); + } +} + + + +bool check_trace_contents(string FUNCTION, string FILE, int LINE, string layer, string expected) { // empty layer == everything, multiple layers, hierarchical layers + vector expected_lines = split(expected, ""); + size_t curr_expected_line = 0; + while (curr_expected_line < expected_lines.size() && expected_lines[curr_expected_line].empty()) + ++curr_expected_line; + if (curr_expected_line == expected_lines.size()) return true; + Trace_stream->newline(); + ostringstream output; + vector layers = split(layer, ","); + for (vector > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { + if (!layer.empty() && !any_prefix_match(layers, p->first)) + continue; + if (p->second.second != expected_lines[curr_expected_line]) + continue; + ++curr_expected_line; + while (curr_expected_line < expected_lines.size() && expected_lines[curr_expected_line].empty()) + ++curr_expected_line; + if (curr_expected_line == expected_lines.size()) return true; + } + + ++Num_failures; + cerr << "\nF " << FUNCTION << "(" << FILE << ":" << LINE << "): missing [" << expected_lines[curr_expected_line] << "] in trace:\n"; + DUMP(layer); + Passed = false; + return false; +} + +#define CHECK_TRACE_CONTENTS(...) check_trace_contents(__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__) + +int trace_count(string layer) { + return trace_count(layer, ""); +} + +int trace_count(string layer, string line) { + Trace_stream->newline(); + long result = 0; + vector layers = split(layer, ","); + for (vector > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { + if (any_prefix_match(layers, p->first)) + if (line == "" || p->second.second == line) + ++result; + } + return result; +} + +int trace_count(string layer, int frame, string line) { + Trace_stream->newline(); + long result = 0; + vector layers = split(layer, ","); + for (vector > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { + if (any_prefix_match(layers, p->first) && p->second.first == frame) + if (line == "" || p->second.second == line) + ++result; + } + return result; +} + +#define CHECK_TRACE_WARNS() CHECK(trace_count("warn") > 0) +#define CHECK_TRACE_DOESNT_WARN() \ + if (trace_count("warn") > 0) { \ + ++Num_failures; \ + cerr << "\nF " << __FUNCTION__ << "(" << __FILE__ << ":" << __LINE__ << "): unexpected warnings\n"; \ + DUMP("warn"); \ + Passed = false; \ + return; \ + } + +bool trace_doesnt_contain(string layer, string line) { + return trace_count(layer, line) == 0; +} + +bool trace_doesnt_contain(string expected) { + vector tmp = split(expected, ": "); + return trace_doesnt_contain(tmp[0], tmp[1]); +} + +bool trace_doesnt_contain(string layer, int frame, string line) { + return trace_count(layer, frame, line) == 0; +} + +#define CHECK_TRACE_DOESNT_CONTAIN(...) CHECK(trace_doesnt_contain(__VA_ARGS__)) + + + +// manage layer counts in Trace_stream using RAII +struct lease_trace_frame { + string layer; + lease_trace_frame(string l) :layer(l) { + if (!Trace_stream) return; + Trace_stream->newline(); + ++Trace_stream->frame[layer]; + } + ~lease_trace_frame() { + if (!Trace_stream) return; + Trace_stream->newline(); + --Trace_stream->frame[layer]; + } +}; +#define new_trace_frame(layer) lease_trace_frame leased_frame(layer); + +bool check_trace_contents(string FUNCTION, string FILE, int LINE, string layer, int frame, string expected) { // multiple layers, hierarchical layers + vector expected_lines = split(expected, ""); // hack: doesn't handle newlines in embedded in lines + size_t curr_expected_line = 0; + while (curr_expected_line < expected_lines.size() && expected_lines[curr_expected_line].empty()) + ++curr_expected_line; + if (curr_expected_line == expected_lines.size()) return true; + Trace_stream->newline(); + ostringstream output; + vector layers = split(layer, ","); + for (vector > >::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) { + if (!layer.empty() && !any_prefix_match(layers, p->first)) + continue; + if (p->second.first != frame) + continue; + if (p->second.second != expected_lines[curr_expected_line]) + continue; + ++curr_expected_line; + while (curr_expected_line < expected_lines.size() && expected_lines[curr_expected_line].empty()) + ++curr_expected_line; + if (curr_expected_line == expected_lines.size()) return true; + } + + ++Num_failures; + cerr << "\nF " << FUNCTION << "(" << FILE << ":" << LINE << "): missing [" << expected_lines[curr_expected_line] << "] in trace/" << frame << ":\n"; + DUMP(layer); + Passed = false; + return false; +} + +#define CHECK_TRACE_TOP(layer, expected) CHECK_TRACE_CONTENTS(layer, 1, expected) + + + +vector split(string s, string delim) { + vector result; + string::size_type begin=0, end=s.find(delim); + while (true) { + if (end == NOT_FOUND) { + result.push_back(string(s, begin, NOT_FOUND)); + break; + } + result.push_back(string(s, begin, end-begin)); + begin = end+delim.size(); + end = s.find(delim, begin); + } + return result; +} + +bool any_prefix_match(const vector& pats, const string& needle) { + if (pats.empty()) return false; + if (*pats[0].rbegin() != '/') + // prefix match not requested + return find(pats.begin(), pats.end(), needle) != pats.end(); + // first pat ends in a '/'; assume all pats do. + for (vector::const_iterator p = pats.begin(); p != pats.end(); ++p) + if (headmatch(needle, *p)) return true; + return false; +} + +bool prefix_match(const string& pat, const string& needle) { + if (*pat.rbegin() != '/') + // prefix match not requested + return pat == needle; + return headmatch(needle, pat); +} + +bool headmatch(const string& s, const string& pat) { + if (pat.size() > s.size()) return false; + return std::mismatch(pat.begin(), pat.end(), s.begin()).first == pat.end(); +} diff --git a/cpp/001trace.test.cc b/cpp/001trace.test.cc new file mode 100644 index 00000000..e0db457c --- /dev/null +++ b/cpp/001trace.test.cc @@ -0,0 +1,164 @@ +void test_trace_check_compares() { + CHECK_TRACE_CONTENTS("test layer", ""); + trace("test layer") << "foo"; + CHECK_TRACE_CONTENTS("test layer", "foo"); +} + +void test_trace_check_filters_layers() { + trace("test layer 1") << "foo"; + trace("test layer 2") << "bar"; + CHECK_TRACE_CONTENTS("test layer 1", "foo"); +} + +void test_trace_check_ignores_other_lines() { + trace("test layer 1") << "foo"; + trace("test layer 1") << "bar"; + CHECK_TRACE_CONTENTS("test layer 1", "foo"); +} + +void test_trace_check_always_finds_empty_lines() { + CHECK_TRACE_CONTENTS("test layer 1", ""); +} + +void test_trace_check_treats_empty_layers_as_wildcards() { + trace("test layer 1") << "foo"; + CHECK_TRACE_CONTENTS("", "foo"); +} + +void test_trace_check_multiple_lines_at_once() { + trace("test layer 1") << "foo"; + trace("test layer 2") << "bar"; + CHECK_TRACE_CONTENTS("", "foobar"); +} + +void test_trace_check_always_finds_empty_lines2() { + CHECK_TRACE_CONTENTS("test layer 1", ""); +} + +void test_trace_orders_across_layers() { + trace("test layer 1") << "foo"; + trace("test layer 2") << "bar"; + trace("test layer 1") << "qux"; + CHECK_TRACE_CONTENTS("", "foobarqux"); +} + +void test_trace_orders_across_layers2() { + trace("test layer 1") << "foo"; + trace("test layer 2") << "bar"; + trace("test layer 1") << "qux"; + CHECK_TRACE_CONTENTS("foobarqux"); +} + +void test_trace_checks_ordering_spanning_multiple_layers() { + trace("layer1") << "foo"; + trace("layer2") << "bar"; + trace("layer1") << "qux"; + CHECK_TRACE_CONTENTS("layer1: foolayer2: barlayer1: qux"); +} + +void test_trace_segments_within_layers() { + trace("test layer 1") << "foo"; + trace("test layer 2") << "bar"; + new_trace_frame("test layer 1"); + trace("test layer 1") << "qux"; + CHECK_TRACE_CONTENTS("test layer 1", "fooqux"); + CHECK_TRACE_CONTENTS("test layer 1", 0, "foo"); + CHECK_TRACE_DOESNT_CONTAIN("test layer 1", 1, "foo"); +} + +void test_trace_checks_ordering_across_layers_and_frames() { + trace("test layer 1") << "foo"; + trace("test layer 2") << "bar"; + new_trace_frame("test layer 1"); + trace("test layer 1") << "qux"; + CHECK_TRACE_CONTENTS("test layer 1/0: footest layer 2: bartest layer 1: qux"); + CHECK_TRACE_CONTENTS("test layer 1: footest layer 2: bartest layer 1/1: qux"); +} + +void trace_test_fn(int n) { + if (n == 0) return; + new_trace_frame("foo"); + trace("foo") << "before: " << n; + trace_test_fn(n-1); + trace("foo") << "after: " << n; +} + +void test_trace_keeps_level_together() { + CHECK_TRACE_CONTENTS("foo", ""); + trace_test_fn(4); + CHECK_TRACE_CONTENTS("foo", 2, "before: 3after: 3"); +} + +void test_trace_supports_multiple_layers() { + trace("test layer 1") << "foo"; + trace("test layer 2") << "bar"; + trace("test layer 1") << "qux"; + CHECK_TRACE_CONTENTS("test layer 1,test layer 2", "foobarqux"); +} + +void test_trace_supports_hierarchical_layers() { + trace("test layer/a") << "foo"; + trace("different layer/c") << "foo 2"; + trace("test layer/b") << "bar"; + CHECK_TRACE_CONTENTS("test layer/", "foobar"); +} + +void test_trace_supports_count() { + trace("test layer 1") << "foo"; + trace("test layer 1") << "foo"; + CHECK_EQ(trace_count("test layer 1", "foo"), 2); +} + +void test_trace_supports_count2() { + trace("test layer 1") << "foo"; + trace("test layer 1") << "bar"; + CHECK_EQ(trace_count("test layer 1"), 2); +} + +// pending: DUMP tests +// pending: readable_contents() adds newline if necessary. +// pending: RAISE also prints to stderr. +// pending: RAISE doesn't print to stderr if Hide_warnings is set. +// pending: RAISE doesn't have to be saved if Hide_warnings is set, just printed. +// pending: RAISE prints to stderr if Trace_stream is NULL. +// pending: RAISE prints to stderr if Trace_stream is NULL even if Hide_warnings is set. +// pending: RAISE << ... die() doesn't die if Hide_warnings is set. + + + +// can't check trace because trace methods call 'split' + +void test_split_returns_at_least_one_elem() { + vector result = split("", ","); + CHECK_EQ(result.size(), 1); + CHECK_EQ(result[0], ""); +} + +void test_split_returns_entire_input_when_no_delim() { + vector result = split("abc", ","); + CHECK_EQ(result.size(), 1); + CHECK_EQ(result[0], "abc"); +} + +void test_split_works() { + vector result = split("abc,def", ","); + CHECK_EQ(result.size(), 2); + CHECK_EQ(result[0], "abc"); + CHECK_EQ(result[1], "def"); +} + +void test_split_works2() { + vector result = split("abc,def,ghi", ","); + CHECK_EQ(result.size(), 3); + CHECK_EQ(result[0], "abc"); + CHECK_EQ(result[1], "def"); + CHECK_EQ(result[2], "ghi"); +} + +void test_split_handles_multichar_delim() { + vector result = split("abc,,def,,ghi", ",,"); + CHECK_EQ(result.size(), 3); + CHECK_EQ(result[0], "abc"); + CHECK_EQ(result[1], "def"); + CHECK_EQ(result[2], "ghi"); +} diff --git a/cpp/002main.cc b/cpp/002main.cc new file mode 100644 index 00000000..2f9e614c --- /dev/null +++ b/cpp/002main.cc @@ -0,0 +1,56 @@ +int main(int argc, const char* argv[]) { + if (argc == 2 && string(argv[1]) == "test") { + run_tests(); + return 0; + } + + for (int i = 1; i < argc; ++i) { + load(argv[i]); + } + run("main"); +} + +void load(const char* filename) { +} + +void run(const char* function_name) { +} + + + +//// test harness + +void run_tests() { + for (unsigned long i=0; i < sizeof(Tests)/sizeof(Tests[0]); ++i) { + START_TRACING_UNTIL_END_OF_SCOPE; + setup(); + CLEAR_TRACE; + (*Tests[i])(); + verify(); + } + cerr << '\n'; + if (Num_failures > 0) + cerr << Num_failures << " failure" + << (Num_failures > 1 ? "s" : "") + << '\n'; +} + +void verify() { + if (!Passed) + ; + else + cerr << "."; +} + +void setup() { + Passed = true; +} + +string time_string() { + time_t t; + time(&t); + char buffer[10]; + if (!strftime(buffer, 10, "%H:%M:%S", localtime(&t))) + return ""; + return buffer; +} diff --git a/cpp/boot.cc b/cpp/boot.cc new file mode 100644 index 00000000..89f943a8 --- /dev/null +++ b/cpp/boot.cc @@ -0,0 +1,63 @@ +// C++ style: +// no pointers except cell* +// use long as the default integer type; it's always large enough to hold pointers + +#define unused __attribute__((unused)) + +#include +#include +#include +#include +#include +#include +#include +using std::vector; +#include +using std::list; +#include +using std::stack; +#include +using std::pair; + +#include +using std::tr1::unordered_map; +#include +using std::tr1::unordered_set; +#include + +#include +using std::string; +const size_t NOT_FOUND = string::npos; + +#include +using std::istream; +using std::ostream; +using std::iostream; +using std::cin; +using std::cout; +using std::cerr; + +#include +using std::stringstream; +using std::istringstream; +using std::ostringstream; + +#include +using std::ifstream; +using std::ofstream; + + + +// interpreter decls + +#include "type_list" + +#include "function_list" + +// interpreter impl + +#include "file_list" + +// interpreter tests + +#include "test_file_list" diff --git a/cpp/makefile b/cpp/makefile new file mode 100644 index 00000000..1c7e1380 --- /dev/null +++ b/cpp/makefile @@ -0,0 +1,23 @@ +mu: makefile type_list function_list file_list test_file_list test_list + g++ -O3 -Wall -Wextra -fno-strict-aliasing boot.cc -o mu + +type_list: boot.cc [0-9]*.cc + @# assumes struct decl has space before '{' + @grep -h "^struct .* {" [0-9]*.cc |perl -pwe 's/(struct *[^ ]*).*/$$1;/' > type_list + @grep -h typedef [0-9]*.cc >> type_list + +function_list: boot.cc [0-9]*.cc + @# assumes function decl has space before '{' + @grep -h "^[^ #].*) {" [0-9]*.cc |perl -pwe 's/ {.*/;/' > function_list + +file_list: boot.cc [0-9]*.cc + @ls [0-9]*.cc |grep -v "\.test\.cc$$" |perl -pwe 's/.*/#include "$$&"/' > file_list + +test_file_list: [0-9]*.test.cc + @ls [0-9]*.test.cc |perl -pwe 's/.*/#include "$$&"/' > test_file_list + +test_list: [0-9]*.cc + @grep -h "^[[:space:]]*void test_" [0-9]*.cc |perl -pwe 's/^\s*void (.*)\(\) {$$/$$1,/' > test_list + +clean: + rm -rf mu* *_list -- cgit 1.4.1-2-gfad0 :25 -0800 796 - jump instructions done' href='/akkartik/mu/commit/cpp/015jump?h=hlt&id=201b2e6c7ce94c986fe7888a60f0e1ba24a05ce9'>201b2e6c ^
1
2
3
4
5
6
7
8
9