//: Arithmetic primitives :(before "End Primitive Recipe Declarations") ADD, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "add", ADD); :(before "End Primitive Recipe Checks") case ADD: { // primary goal of these checks is to forbid address arithmetic for (int i = 0; i < SIZE(inst.ingredients); ++i) { if (!is_mu_number(inst.ingredients.at(i))) { raise << maybe(get(Recipe, r).name) << "'add' requires number ingredients, but got '" << inst.ingredients.at(i).original_string << "'\n" << end(); goto finish_checking_instruction; } } if (SIZE(inst.products) > 1) { raise << maybe(get(Recipe, r).name) << "'add' yields exactly one product in '" << to_original_string(inst) << "'\n" << end(); break; } if (!inst.products.empty() && !is_dummy(inst.products.at(0)) && !is_mu_number(inst.products.at(0))) { raise << maybe(get(Recipe, r).name) << "'add' should yield a number, but got '" << inst.products.at(0).original_string << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case ADD: { double result = 0; for (int i = 0; i < SIZE(ingredients); ++i) { result += ingredients.at(i).at(0); } products.resize(1); products.at(0).push_back(result); break; } :(code) void test_add_literal() { run( "def main [\n" " 1:num <- add 23, 34\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 57 in location 1\n" ); } void test_add() { run( "def main [\n" " 1:num <- copy 23\n" " 2:num <- copy 34\n" " 3:num <- add 1:num, 2:num\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 57 in location 3\n" ); } void test_add_multiple() { run( "def main [\n" " 1:num <- add 3, 4, 5\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 12 in location 1\n" ); } void test_add_checks_type() { Hide_errors = true; run( "def main [\n" " 1:num <- add 2:bool, 1\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: main: 'add' requires number ingredients, but got '2:bool'\n" ); } void test_add_checks_return_type() { Hide_errors = true; run( "def main [\n" " 1:&:num <- add 2, 2\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: main: 'add' should yield a number, but got '1:&:num'\n" ); } :(before "End Primitive Recipe Declarations") SUBTRACT, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "subtract", SUBTRACT); :(before "End Primitive Recipe Checks") case SUBTRACT: { if (inst.ingredients.empty()) { raise << maybe(get(Recipe, r).name) << "'subtract' has no ingredients\n" << end(); break; } for (int i = 0; i < SIZE(inst.ingredients); ++i) { if (!is_mu_number(inst.ingredients.at(i))) { raise << maybe(get(Recipe, r).name) << "'subtract' requires number ingredients, but got '" << inst.ingredients.at(i).original_string << "'\n" << end(); goto finish_checking_instruction; } } if (SIZE(inst.products) > 1) { raise << maybe(get(Recipe, r).name) << "'subtract' yields exactly one product in '" << to_original_string(inst) << "'\n" << end(); break; } if (!inst.products.empty() && !is_dummy(inst.products.at(0)) && !is_mu_number(inst.products.at(0))) { raise << maybe(get(Recipe, r).name) << "'subtract' should yield a number, but got '" << inst.products.at(0).original_string << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case SUBTRACT: { double result = ingredients.at(0).at(0); for (int i = 1; i < SIZE(ingredients); ++i) result -= ingredients.at(i).at(0); products.resize(1); products.at(0).push_back(result); break; } :(code) void test_subtract_literal() { run( "def main [\n" " 1:num <- subtract 5, 2\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 3 in location 1\n" ); } void test_subtract() { run( "def main [\n" " 1:num <- copy 23\n" " 2:num <- copy 34\n" " 3:num <- subtract 1:num, 2:num\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing -11 in location 3\n" ); } void test_subtract_multiple() { run( "def main [\n" " 1:num <- subtract 6, 3, 2\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 1 in location 1\n" ); } :(before "End Primitive Recipe Declarations") MULTIPLY, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "multiply", MULTIPLY); :(before "End Primitive Recipe Checks") case MULTIPLY: { for (int i = 0; i < SIZE(inst.ingredients); ++i) { if (!is_mu_number(inst.ingredients.at(i))) { raise << maybe(get(Recipe, r).name) << "'multiply' requires number ingredients, but got '" << inst.ingredients.at(i).original_string << "'\n" << end(); goto finish_checking_instruction; } } if (SIZE(inst.products) > 1) { raise << maybe(get(Recipe, r).name) << "'multiply' yields exactly one product in '" << to_original_string(inst) << "'\n" << end(); break; } if (!inst.products.empty() && !is_dummy(inst.products.at(0)) && !is_mu_number(inst.products.at(0))) { raise << maybe(get(Recipe, r).name) << "'multiply' should yield a number, but got '" << inst.products.at(0).original_string << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case MULTIPLY: { double result = 1; for (int i = 0; i < SIZE(ingredients); ++i) { result *= ingredients.at(i).at(0); } products.resize(1); products.at(0).push_back(result); break; } :(code) void test_multiply_literal() { run( "def main [\n" " 1:num <- multiply 2, 3\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 6 in location 1\n" ); } void test_multiply() { run( "def main [\n" " 1:num <- copy 4\n" " 2:num <- copy 6\n" " 3:num <- multiply 1:num, 2:num\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 24 in location 3\n" ); } void test_multiply_multiple() { run( "def main [\n" " 1:num <- multiply 2, 3, 4\n" "]\n" ); CHECK_TRACE_CONTENTS( "mem: storing 24 in location 1\n" ); } :(before "End Primitive Recipe Declarations") DIVIDE, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "divide", DIVIDE); :(before "End Primitive Recipe Checks") case DIVIDE: { if (inst.ingredients.empty()) { raise << maybe(get(Recipe, r).name) << "'divide' has no ingredients\n" << end(); break; } for (int i = 0; i < SIZE(inst.ingredients); ++i) { if (!is_mu_number(inst.ingredients.at(i))) { raise << maybe(get(Recipe, r).name) << "'divide' requires number ingredients, but got '" << inst.ingredients.at(i).original_string << "'\n" << end(); goto finish_checking_instruction; } } if (SIZE(inst.products) > 1) { raise << maybe(get(Recipe, r).name) << "'divide' yields exactly one product in '" << to_original_string(inst) << "'\n" << end(); break; } if (!inst.products.empty() && !is_dummy(inst.products.at(0)) && !is_mu_number(inst.products.at(0))) { raise << maybe(get(Recipe, r).name) << "'divide' should yield a number, but got '" << inst.products.at(0).original_string << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case DIVIDE: { double result = ingredients.at(0).at(0); for (int i = 1; i < SIZE(ingredients); ++i) result /= ingredients.at(i).at(0); products.resize(1); products.at(0).push_back(result); break; } :(code) void test_divide_literal() { run( "def main [\n" "
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 090scenario_filesystem_test.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Delimiter { color: #800080; }
.muControl { color: #c0a020; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #c00000; }
.muRecipe { color: #ff8700; }
.muScenario { color: #00af00; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># Check our support for fake file systems in scenarios.</span>
<span class="muScenario">scenario</span> read-from-fake-file [
<span class="Constant">local-scope</span>
assume-filesystem [
<span class="Constant">[a]</span><span class="Special"> <- </span>[
<span class="Constant"> |xyz|</span>
]
]
contents:&:source:char<span class="Special"> <- </span>start-reading filesystem:&:filesystem, <span class="Constant">[a]</span>
<span class="Constant">1</span>:char/<span class="Special">raw <- </span>read contents
<span class="Constant">2</span>:char/<span class="Special">raw <- </span>read contents
<span class="Constant">3</span>:char/<span class="Special">raw <- </span>read contents
<span class="Constant">4</span>:char/<span class="Special">raw <- </span>read contents
_, <span class="Constant">5</span>:bool/<span class="Special">raw <- </span>read contents
memory-should-contain [
<span class="Constant">1</span><span class="Special"> <- </span><span class="Constant">120</span> <span class="Comment"># x</span>
<span class="Constant">2</span><span class="Special"> <- </span><span class="Constant">121</span> <span class="Comment"># y</span>
<span class="Constant">3</span><span class="Special"> <- </span><span class="Constant">122</span> <span class="Comment"># z</span>
<span class="Constant">4</span><span class="Special"> <- </span><span class=