diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-04-17 10:31:17 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-04-17 10:31:17 -0700 |
commit | 1848b18f02b158861008214efd19708585bfcbe5 (patch) | |
tree | 3b5ddc0355f9b2b099156a668761cf45edefefb1 /cpp | |
parent | 9da1b126cc017e14035b94c4615d211e5bc4bb21 (diff) | |
download | mu-1848b18f02b158861008214efd19708585bfcbe5.tar.gz |
1073 - stop fixing the values of primitive recipes
In the process I give up trace stability when I move files around, but I gain in exchange the ability to move files around.
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/010vm | 16 | ||||
-rw-r--r-- | cpp/014arithmetic | 30 | ||||
-rw-r--r-- | cpp/015boolean | 18 | ||||
-rw-r--r-- | cpp/016jump | 18 | ||||
-rw-r--r-- | cpp/017compare | 30 | ||||
-rw-r--r-- | cpp/020container | 39 | ||||
-rw-r--r-- | cpp/022array | 45 | ||||
-rw-r--r-- | cpp/026call_ingredient | 6 | ||||
-rw-r--r-- | cpp/027call_reply | 6 | ||||
-rw-r--r-- | cpp/030brace | 33 | ||||
-rw-r--r-- | cpp/032new | 7 | ||||
-rw-r--r-- | cpp/036length | 8 | ||||
-rw-r--r-- | cpp/042trace | 6 | ||||
-rw-r--r-- | cpp/090debug | 3 |
14 files changed, 102 insertions, 163 deletions
diff --git a/cpp/010vm b/cpp/010vm index df84102b..db274622 100644 --- a/cpp/010vm +++ b/cpp/010vm @@ -125,9 +125,12 @@ struct type_info { type_info() :kind(primitive), size(0) {} }; -:(before "End Globals") -const int IDLE = 0; // always the first entry in the recipe book -const int COPY = 1; +enum primitive_recipes { + IDLE = 0, + COPY, + // End Primitive Recipe Declarations + MAX_PRIMITIVE_RECIPES, +}; :(code) // It's all very well to construct recipes out of other recipes, but we need // to know how to do *something* out of the box. For the following @@ -135,14 +138,9 @@ const int COPY = 1; // what to do for them. void setup_recipes() { Recipe.clear(); Recipe_number.clear(); - Next_recipe_number = 0; Recipe_number["idle"] = IDLE; - assert(Next_recipe_number == IDLE); - Next_recipe_number++; // Primitive Recipe Numbers Recipe_number["copy"] = COPY; - assert(Next_recipe_number == COPY); - Next_recipe_number++; // End Primitive Recipe Numbers } //: We could just reset the recipe table after every test, but that gets slow @@ -151,7 +149,7 @@ void setup_recipes() { //: itself. :(before "End One-time Setup") setup_recipes(); -assert(Next_recipe_number < 100); // level 0 is primitives; until 99 +assert(MAX_PRIMITIVE_RECIPES < 100); // level 0 is primitives; until 99 Next_recipe_number = 100; // End Load Recipes // give tests a consistent starting point diff --git a/cpp/014arithmetic b/cpp/014arithmetic index 915cd9ab..08647cbc 100644 --- a/cpp/014arithmetic +++ b/cpp/014arithmetic @@ -1,10 +1,8 @@ -:(before "End Globals") +:(before "End Primitive Recipe Declarations") // Arithmetic ops. -const int ADD = 2; +ADD, :(before "End Primitive Recipe Numbers") Recipe_number["add"] = ADD; -assert(Next_recipe_number == ADD); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case ADD: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; @@ -44,12 +42,10 @@ recipe main [ +run: product 0 is 57 +mem: storing 57 in location 3 -:(before "End Globals") -const int SUBTRACT = 3; +:(before "End Primitive Recipe Declarations") +SUBTRACT, :(before "End Primitive Recipe Numbers") Recipe_number["subtract"] = SUBTRACT; -assert(Next_recipe_number == SUBTRACT); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case SUBTRACT: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; @@ -89,12 +85,10 @@ recipe main [ +run: product 0 is -11 +mem: storing -11 in location 3 -:(before "End Globals") -const int MULTIPLY = 4; +:(before "End Primitive Recipe Declarations") +MULTIPLY, :(before "End Primitive Recipe Numbers") Recipe_number["multiply"] = MULTIPLY; -assert(Next_recipe_number == MULTIPLY); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case MULTIPLY: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; @@ -135,12 +129,10 @@ recipe main [ +run: product 0 is 24 +mem: storing 24 in location 3 -:(before "End Globals") -const int DIVIDE = 5; +:(before "End Primitive Recipe Declarations") +DIVIDE, :(before "End Primitive Recipe Numbers") Recipe_number["divide"] = DIVIDE; -assert(Next_recipe_number == DIVIDE); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case DIVIDE: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; @@ -181,12 +173,10 @@ recipe main [ +run: product 0 is 9 +mem: storing 9 in location 3 -:(before "End Globals") -const int DIVIDE_WITH_REMAINDER = 6; +:(before "End Primitive Recipe Declarations") +DIVIDE_WITH_REMAINDER, :(before "End Primitive Recipe Numbers") Recipe_number["divide_with_remainder"] = DIVIDE_WITH_REMAINDER; -assert(Next_recipe_number == DIVIDE_WITH_REMAINDER); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case DIVIDE_WITH_REMAINDER: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; diff --git a/cpp/015boolean b/cpp/015boolean index 2b85d9a4..6c677f7e 100644 --- a/cpp/015boolean +++ b/cpp/015boolean @@ -1,10 +1,8 @@ -:(before "End Globals") +:(before "End Primitive Recipe Declarations") // Boolean ops. -const int AND = 7; +AND, :(before "End Primitive Recipe Numbers") Recipe_number["and"] = AND; -assert(Next_recipe_number == AND); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case AND: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; @@ -34,12 +32,10 @@ recipe main [ +run: product 0 is 0 +mem: storing 0 in location 3 -:(before "End Globals") -const int OR = 8; +:(before "End Primitive Recipe Declarations") +OR, :(before "End Primitive Recipe Numbers") Recipe_number["or"] = OR; -assert(Next_recipe_number == OR); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case OR: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; @@ -69,12 +65,10 @@ recipe main [ +run: product 0 is 1 +mem: storing 1 in location 3 -:(before "End Globals") -const int NOT = 9; +:(before "End Primitive Recipe Declarations") +NOT, :(before "End Primitive Recipe Numbers") Recipe_number["not"] = NOT; -assert(Next_recipe_number == NOT); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case NOT: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; diff --git a/cpp/016jump b/cpp/016jump index 80f7aa8d..669c7258 100644 --- a/cpp/016jump +++ b/cpp/016jump @@ -1,10 +1,8 @@ -:(before "End Globals") +:(before "End Primitive Recipe Declarations") // Jump ops. -const int JUMP = 10; +JUMP, :(before "End Primitive Recipe Numbers") Recipe_number["jump"] = JUMP; -assert(Next_recipe_number == JUMP); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case JUMP: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].value; @@ -34,12 +32,10 @@ recipe main [ +run: instruction main/2 +run: instruction main/1 -:(before "End Globals") -const int JUMP_IF = 11; +:(before "End Primitive Recipe Declarations") +JUMP_IF, :(before "End Primitive Recipe Numbers") Recipe_number["jump-if"] = JUMP_IF; -assert(Next_recipe_number == JUMP_IF); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case JUMP_IF: { vector<int> arg0 = read_memory(instructions[pc].ingredients[0]); @@ -76,12 +72,10 @@ recipe main [ +run: instruction main/1 +mem: storing 1 in location 123 -:(before "End Globals") -const int JUMP_UNLESS = 12; +:(before "End Primitive Recipe Declarations") +JUMP_UNLESS, :(before "End Primitive Recipe Numbers") Recipe_number["jump-unless"] = JUMP_UNLESS; -assert(Next_recipe_number == JUMP_UNLESS); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case JUMP_UNLESS: { vector<int> arg0 = read_memory(instructions[pc].ingredients[0]); diff --git a/cpp/017compare b/cpp/017compare index 45deab06..145feaa2 100644 --- a/cpp/017compare +++ b/cpp/017compare @@ -1,10 +1,8 @@ -:(before "End Globals") +:(before "End Primitive Recipe Declarations") // Comparison ops. -const int EQUAL = 13; +EQUAL, :(before "End Primitive Recipe Numbers") Recipe_number["equal"] = EQUAL; -assert(Next_recipe_number == EQUAL); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case EQUAL: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; @@ -46,12 +44,10 @@ recipe main [ +run: product 0 is 1 +mem: storing 1 in location 3 -:(before "End Globals") -const int GREATER_THAN = 14; +:(before "End Primitive Recipe Declarations") +GREATER_THAN, :(before "End Primitive Recipe Numbers") Recipe_number["greater-than"] = GREATER_THAN; -assert(Next_recipe_number == GREATER_THAN); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case GREATER_THAN: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; @@ -95,12 +91,10 @@ recipe main [ +run: product 0 is 0 +mem: storing 0 in location 3 -:(before "End Globals") -const int LESSER_THAN = 15; +:(before "End Primitive Recipe Declarations") +LESSER_THAN, :(before "End Primitive Recipe Numbers") Recipe_number["lesser-than"] = LESSER_THAN; -assert(Next_recipe_number == LESSER_THAN); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case LESSER_THAN: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; @@ -144,12 +138,10 @@ recipe main [ +run: product 0 is 0 +mem: storing 0 in location 3 -:(before "End Globals") -const int GREATER_OR_EQUAL = 16; +:(before "End Primitive Recipe Declarations") +GREATER_OR_EQUAL, :(before "End Primitive Recipe Numbers") Recipe_number["greater-or-equal"] = GREATER_OR_EQUAL; -assert(Next_recipe_number == GREATER_OR_EQUAL); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case GREATER_OR_EQUAL: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; @@ -207,12 +199,10 @@ recipe main [ +run: product 0 is 0 +mem: storing 0 in location 3 -:(before "End Globals") -const int LESSER_OR_EQUAL = 17; +:(before "End Primitive Recipe Declarations") +LESSER_OR_EQUAL, :(before "End Primitive Recipe Numbers") Recipe_number["lesser-or-equal"] = LESSER_OR_EQUAL; -assert(Next_recipe_number == LESSER_OR_EQUAL); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case LESSER_OR_EQUAL: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; diff --git a/cpp/020container b/cpp/020container index ef6b0057..fcdd82f9 100644 --- a/cpp/020container +++ b/cpp/020container @@ -73,12 +73,10 @@ recipe main [ +run: product 0 is 35 +mem: storing 35 in location 15 -:(before "End Globals") -const int GET = 18; +:(before "End Primitive Recipe Declarations") +GET, :(before "End Primitive Recipe Numbers") Recipe_number["get"] = GET; -assert(Next_recipe_number == GET); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case GET: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; @@ -128,13 +126,24 @@ recipe main [ +run: product 0 is 36 +mem: storing 36 in location 15 -:(before "End Globals") -// To write to elements of containers, you need their address. -const int GET_ADDRESS = 19; +//: To write to elements of containers, you need their address. + +:(scenario "get_address") +recipe main [ + 12:integer <- copy 34:literal + 13:integer <- copy 35:literal + 15:address:integer <- get-address 12:point, 1:offset +] ++run: instruction main/2 ++run: ingredient 0 is 12 ++run: ingredient 1 is 1 ++run: address to copy is 13 ++mem: storing 13 in location 15 + +:(before "End Primitive Recipe Declarations") +GET_ADDRESS, :(before "End Primitive Recipe Numbers") Recipe_number["get-address"] = GET_ADDRESS; -assert(Next_recipe_number == GET_ADDRESS); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case GET_ADDRESS: { trace("run") << "ingredient 0 is " << instructions[pc].ingredients[0].name; @@ -156,15 +165,3 @@ case GET_ADDRESS: { write_memory(instructions[pc].products[0], result); break; } - -:(scenario "get_address") -recipe main [ - 12:integer <- copy 34:literal - 13:integer <- copy 35:literal - 15:address:integer <- get-address 12:point, 1:offset -] -+run: instruction main/2 -+run: ingredient 0 is 12 -+run: ingredient 1 is 1 -+run: address to copy is 13 -+mem: storing 13 in location 15 diff --git a/cpp/022array b/cpp/022array index 9c2f1439..3bdfe95e 100644 --- a/cpp/022array +++ b/cpp/022array @@ -31,7 +31,7 @@ if (x.types[0] != Type_number["array"] && size_of(x) != data.size()) return 1 + Memory[r.value]*size_of(array_element(r.types)); } -//: array elements are accessed using 'index' +//: To access elements of an array, use 'index' :(scenario "index") recipe main [ 1:integer <- copy 3:literal @@ -47,7 +47,6 @@ recipe main [ +run: product 0 is 14 +mem: storing 14 in location 5 -//: array elements are accessed using 'index' :(scenario "index_direct_offset") recipe main [ 1:integer <- copy 3:literal @@ -64,13 +63,10 @@ recipe main [ +run: product 0 is 14 +mem: storing 14 in location 6 -:(before "End Globals") -// Operator to look at elements of arrays. -const int INDEX = 20; +:(before "End Primitive Recipe Declarations") +INDEX, :(before "End Primitive Recipe Numbers") Recipe_number["index"] = INDEX; -assert(Next_recipe_number == INDEX); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case INDEX: { static const int ARRAY = Type_number["array"]; @@ -116,13 +112,25 @@ recipe main [ +run: address to copy is 2 +mem: storing 2 in location 5 -:(before "End Globals") -// To write to elements of containers, you need their address. -const int INDEX_ADDRESS = 21; +//: To write to elements of containers, you need their address. + +:(scenario "index_indirect") +recipe main [ + 1:integer <- copy 3:literal + 2:integer <- copy 14:literal + 3:integer <- copy 15:literal + 4:integer <- copy 16:literal + 5:address:array:integer <- copy 1:literal + 6:integer <- index 5:address:array:integer/deref, 1:literal +] ++run: instruction main/5 ++mem: storing 15 in location 6 +// vim:ft=cpp + +:(before "End Primitive Recipe Declarations") +INDEX_ADDRESS, :(before "End Primitive Recipe Numbers") Recipe_number["index-address"] = INDEX_ADDRESS; -assert(Next_recipe_number == INDEX_ADDRESS); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case INDEX_ADDRESS: { static const int ARRAY = Type_number["array"]; @@ -142,16 +150,3 @@ case INDEX_ADDRESS: { write_memory(instructions[pc].products[0], result); break; } - -:(scenario "index_indirect") -recipe main [ - 1:integer <- copy 3:literal - 2:integer <- copy 14:literal - 3:integer <- copy 15:literal - 4:integer <- copy 16:literal - 5:address:array:integer <- copy 1:literal - 6:integer <- index 5:address:array:integer/deref, 1:literal -] -+run: instruction main/5 -+mem: storing 15 in location 6 -// vim:ft=cpp diff --git a/cpp/026call_ingredient b/cpp/026call_ingredient index 963c5fe8..ecf53539 100644 --- a/cpp/026call_ingredient +++ b/cpp/026call_ingredient @@ -25,12 +25,10 @@ for (vector<reagent>::iterator p = instructions[pc].ingredients.begin(); p != in } rr.calls.push(callee); -:(before "End Globals") -const int NEXT_INGREDIENT = 22; +:(before "End Primitive Recipe Declarations") +NEXT_INGREDIENT, :(before "End Primitive Recipe Numbers") Recipe_number["next-ingredient"] = NEXT_INGREDIENT; -assert(Next_recipe_number == NEXT_INGREDIENT); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case NEXT_INGREDIENT: { if (rr.calls.top().next_ingredient_to_process < rr.calls.top().ingredient_atoms.size()) { diff --git a/cpp/027call_reply b/cpp/027call_reply index feb549e6..b44552cc 100644 --- a/cpp/027call_reply +++ b/cpp/027call_reply @@ -14,12 +14,10 @@ recipe f [ +run: result 1 is 3 +mem: storing 3 in location 4 -:(before "End Globals") -const int REPLY = 23; +:(before "End Primitive Recipe Declarations") +REPLY, :(before "End Primitive Recipe Numbers") Recipe_number["reply"] = REPLY; -assert(Next_recipe_number == REPLY); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case REPLY: { vector<vector<int> > callee_results; diff --git a/cpp/030brace b/cpp/030brace index 0d68b54f..d01fe359 100644 --- a/cpp/030brace +++ b/cpp/030brace @@ -1,6 +1,6 @@ //: Structured programming //: -//: Our jump operators are quite inconvenient to use, so mu provides a +//: Our jump recipes are quite inconvenient to use, so mu provides a //: lightweight tool called 'transform_braces' to work in a slightly more //: convenient format with nested braces: //: @@ -168,34 +168,23 @@ void transform_test(string form) { //? cout << "AAA }\n"; //? 1 } -//: Make sure these pseudo recipes get consistent numbers, even though they aren't -//: implemented. -:(before "End Globals") -const int BREAK = 24; -const int BREAK_IF = 25; -const int BREAK_UNLESS = 26; -const int LOOP = 27; -const int LOOP_IF = 28; -const int LOOP_UNLESS = 29; +//: Make sure these pseudo recipes get consistent numbers in all tests, even +//: though they aren't implemented. + +:(before "End Primitive Recipe Declarations") +BREAK, +BREAK_IF, +BREAK_UNLESS, +LOOP, +LOOP_IF, +LOOP_UNLESS, :(before "End Primitive Recipe Numbers") Recipe_number["break"] = BREAK; -assert(Next_recipe_number == BREAK); -Next_recipe_number++; Recipe_number["break-if"] = BREAK_IF; -assert(Next_recipe_number == BREAK_IF); -Next_recipe_number++; Recipe_number["break-unless"] = BREAK_UNLESS; -assert(Next_recipe_number == BREAK_UNLESS); -Next_recipe_number++; Recipe_number["loop"] = LOOP; -assert(Next_recipe_number == LOOP); -Next_recipe_number++; Recipe_number["loop-if"] = LOOP_IF; -assert(Next_recipe_number == LOOP_IF); -Next_recipe_number++; Recipe_number["loop-unless"] = LOOP_UNLESS; -assert(Next_recipe_number == LOOP_UNLESS); -Next_recipe_number++; :(scenario "loop") recipe main [ diff --git a/cpp/032new b/cpp/032new index e75d4148..e0460170 100644 --- a/cpp/032new +++ b/cpp/032new @@ -35,13 +35,10 @@ if (inst.operation == Recipe_number["new"]) { trace("new") << inst.ingredients[0].name << " -> " << inst.ingredients[0].value; } -:(before "End Globals") -// Operator to look at elements of arrays. -const int NEW = 30; +:(before "End Primitive Recipe Declarations") +NEW, :(before "End Primitive Recipe Numbers") Recipe_number["new"] = NEW; -assert(Next_recipe_number == NEW); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case NEW: { vector<int> result; diff --git a/cpp/036length b/cpp/036length index b9e3bc3c..9d3bb714 100644 --- a/cpp/036length +++ b/cpp/036length @@ -1,3 +1,5 @@ +//: Recipe to compute the length of an array. + :(scenario "array_length") recipe main [ 1:integer <- copy 3:literal @@ -9,12 +11,10 @@ recipe main [ +run: instruction main/4 +mem: storing 3 in location 5 -:(before "End Globals") -const int LENGTH = 31; +:(before "End Primitive Recipe Declarations") +LENGTH, :(before "End Primitive Recipe Numbers") Recipe_number["length"] = LENGTH; -assert(Next_recipe_number == LENGTH); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case LENGTH: { reagent x = canonize(instructions[pc].ingredients[0]); diff --git a/cpp/042trace b/cpp/042trace index 3a7b0b0e..bd05d848 100644 --- a/cpp/042trace +++ b/cpp/042trace @@ -4,12 +4,10 @@ recipe main [ ] +foo: this is a trace in mu -:(before "End Globals") -const int TRACE = 32; +:(before "End Primitive Recipe Declarations") +TRACE, :(before "End Primitive Recipe Numbers") Recipe_number["trace"] = TRACE; -assert(Next_recipe_number == TRACE); -Next_recipe_number++; :(before "End Primitive Recipe Implementations") case TRACE: { assert(isa_literal(instructions[pc].ingredients[0])); diff --git a/cpp/090debug b/cpp/090debug index 670b284f..cd5825a0 100644 --- a/cpp/090debug +++ b/cpp/090debug @@ -1,5 +1,6 @@ +//: Recipe to look at elements of containers. + :(before "End Globals") -// Operator to look at elements of containers. const int _PRINT = 99; :(before "End Primitive Recipe Numbers") Recipe_number["$print"] = _PRINT; |