diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-02-18 14:46:30 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-02-18 14:46:30 -0800 |
commit | 9fc64bbc95bb4e55f28f5262d0f5c660177f0a19 (patch) | |
tree | 966c29138ee7297b305995168dc3293d11e1cfd4 /cpp/literate/012run | |
parent | 96ac511e9e789dfe0dd16e5216b7a957338fe3d3 (diff) | |
download | mu-9fc64bbc95bb4e55f28f5262d0f5c660177f0a19.tar.gz |
781 - first instruction to run in literate C++ version
Diffstat (limited to 'cpp/literate/012run')
-rw-r--r-- | cpp/literate/012run | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/cpp/literate/012run b/cpp/literate/012run new file mode 100644 index 00000000..f7b5469a --- /dev/null +++ b/cpp/literate/012run @@ -0,0 +1,41 @@ +:(scenarios run) +:(scenario copy_literal) +recipe main [ + 1:integer <- copy 23:literal +] ++run: instruction 0 ++run: ingredient 23 ++mem: storing in location 1 + +:(code) +void run(string form) { + run(add_recipe(form)); +} + +void run(recipe_number r) { + vector<instruction>& instructions(Recipe[r].steps); + int n = 0; + vector<instruction>::iterator p; + for (n = 0, p = instructions.begin(); p != instructions.end(); ++p, ++n) { + trace("run") << "instruction " << n; + switch (p->operation) { + case 1: { // copy + int arg = to_int(p->ingredients[0].name); + trace("run") << " ingredient " << arg; + int dest = to_int(p->products[0].name); + trace("mem") << " storing in location " << dest; + Memory[dest] = arg; + break; + } + default: + raise << "undefined operation " << p->operation; + } + } +} + +int to_int(string n) { + char* end = NULL; + int result = strtol(n.c_str(), &end, /*any base*/0); + assert(*end == '\0'); + return result; +} |