about summary refs log tree commit diff stats
path: root/html/015literal_noninteger.cc.html
blob: 57488dd41a94209b2f5732abcb82c7165cd188e6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!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 - 015literal_noninteger.cc</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="cpp">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal-light">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #c6c6c6; }
body { font-size:12pt; font-family: monospace; color: #000000; background-color: #c6c6c6; }
a { color:inherit; }
* { font-size:12pt; font-size: 1em; }
.cSpecial { color: #008000; }
.LineNr { }
.Constant { color: #008787; }
.muRecipe { color: #ff8700; }
.Delimiter { color: #c000c0; }
.Special { color: #d70000; }
.Identifier { color: #af5f00; }
.Normal { color: #000000; background-color: #c6c6c6; padding-bottom: 1px; }
.Comment { color: #005faf; }
.traceContains { color: #005f00; }
-->
</style>

<script type='text/javascript'>
<!--

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  return true;
}
if ('onhashchange' in window) {
  window.onhashchange = JumpToLine;
}

-->
</script>
</head>
<body onload='JumpToLine();'>
<a href='https://github.com/akkartik/mu/blob/master/015literal_noninteger.cc'>https://github.com/akkartik/mu/blob/master/015literal_noninteger.cc</a>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr"> 1 </span><span class="Comment">//: Support literal non-integers.</span>
<span id="L2" class="LineNr"> 2 </span>
<span id="L3" class="LineNr"> 3 </span><span class="Delimiter">:(scenarios load)</span>
<span id="L4" class="LineNr"> 4 </span><span class="Delimiter">:(scenario noninteger_literal)</span>
<span id="L5" class="LineNr"> 5 </span><span class="muRecipe">def</span> <a href='000organization.cc.html#L113'>main</a> [
<span id="L6" class="LineNr"> 6 </span>  <span class="Constant">1</span>:number<span class="Special"> &lt;- </span>copy <span class="Constant">3.14159</span>
<span id="L7" class="LineNr"> 7 </span>]
<span id="L8" class="LineNr"> 8 </span><span class="traceContains">+parse:   ingredient: {3.14159: &quot;literal-fractional-number&quot;}</span>
<span id="L9" class="LineNr"> 9 </span>
<span id="L10" class="LineNr">10 </span><span class="Delimiter">:(after &quot;Parsing reagent(string s)&quot;)</span>
<span id="L11" class="LineNr">11 </span><span class="Normal">if</span> <span class="Delimiter">(</span><a href='015literal_noninteger.cc.html#L19'>is_noninteger</a><span class="Delimiter">(</span>s<span class="Delimiter">))</span> <span class="Delimiter">{</span>
<span id="L12" class="LineNr">12 </span>  name = s<span class="Delimiter">;</span>
<span id="L13" class="LineNr">13 </span>  type = <span class="Normal">new</span> type_tree<span class="Delimiter">(</span><span class="Constant">&quot;literal-fractional-number&quot;</span><span class="Delimiter">,</span> <span class="Constant">0</span><span class="Delimiter">);</span>
<span id="L14" class="LineNr">14 </span>  <a href='010vm.cc.html#L67'>set_value</a><span class="Delimiter">(</span><a href='015literal_noninteger.cc.html#L26'>to_double</a><span class="Delimiter">(</span>s<span class="Delimiter">));</span>
<span id="L15" class="LineNr">15 </span>  <span class="Identifier">return</span><span class="Delimiter">;</span>
<span id="L16" class="LineNr">16 </span><span class="Delimiter">}</span>
<span id="L17" class="LineNr">17 </span>
<span id="L18" class="LineNr">18 </span><span class="Delimiter">:(code)</span>
<span id="L19" class="LineNr">19 </span><span class="Normal">bool</span> <a href='015literal_noninteger.cc.html#L19'>is_noninteger</a><span class="Delimiter">(</span><span class="Normal">const</span> string&amp; s<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span id="L20" class="LineNr">20 </span>  <span class="Identifier">return</span> s<span class="Delimiter">.</span>find_first_not_of<span class="Delimiter">(</span><span class="Constant">&quot;0123456789-.&quot;</span><span class="Delimiter">)</span> == string::npos  <span class="Comment">// no other characters</span>
<span id="L21" class="LineNr">21 </span>      &amp;&amp; s<span class="Delimiter">.</span>find_first_of<span class="Delimiter">(</span><span class="Constant">&quot;0123456789&quot;</span><span class="Delimiter">)</span> != string::npos  <span class="Comment">// at least one digit</span>
<span id="L22" class="LineNr">22 </span>      &amp;&amp; s<span class="Delimiter">.</span>find<span class="Delimiter">(</span><span class="Constant">'-'</span><span class="Delimiter">,</span> <span class="Constant">1</span><span class="Delimiter">)</span> == string::npos  <span class="Comment">// '-' only at first position</span>
<span id="L23" class="LineNr">23 </span>      &amp;&amp; std::count<span class="Delimiter">(</span>s<span class="Delimiter">.</span>begin<span class="Delimiter">(),</span> s<span class="Delimiter">.</span><a href='003trace.cc.html#L225'>end</a><span class="Delimiter">(),</span> <span class="Constant">'.'</span><span class="Delimiter">)</span> == <span class="Constant">1</span><span class="Delimiter">;</span>  <span class="Comment">// exactly one decimal point</span>
<span id="L24" class="LineNr">24 </span><span class="Delimiter">}</span>
<span id="L25" class="LineNr">25 </span>
<span id="L26" class="LineNr">26 </span><span class="Normal">double</span> <a href='015literal_noninteger.cc.html#L26'>to_double</a><span class="Delimiter">(</span>string n<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span id="L27" class="LineNr">27 </span>  <span class="Normal">char</span>* <a href='003trace.cc.html#L225'>end</a> = <span class="Constant">NULL</span><span class="Delimiter">;</span>
<span id="L28" class="LineNr">28 </span>  <span class="Comment">// safe because string.c_str() is guaranteed to be null-terminated</span>
<span id="L29" class="LineNr">29 </span>  <span class="Normal">double</span> result = strtod<span class="Delimiter">(</span>n<span class="Delimiter">.</span>c_str<span class="Delimiter">(),</span> &amp;end<span class="Delimiter">);</span>
<span id="L30" class="LineNr">30 </span>  assert<span class="Delimiter">(</span>*end == <span class="cSpecial">'\0'</span><span class="Delimiter">);</span>
<span id="L31" class="LineNr">31 </span>  <span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span id="L32" class="LineNr">32 </span><span class="Delimiter">}</span>
<span id="L33" class="LineNr">33 </span>
<span id="L34" class="LineNr">34 </span><span class="Normal">void</span> <a href='015literal_noninteger.cc.html#L34'>test_is_noninteger</a><span class="Delimiter">()</span> <span class="Delimiter">{</span>
<span id="L35" class="LineNr">35 </span>  <a href='002test.cc.html#L24'>CHECK</a><span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;1234&quot;</span><span class="Delimiter">));</span>
<span id="L36" class="LineNr">36 </span>  <a href='002test.cc.html#L24'>CHECK</a><span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;1a2&quot;</span><span class="Delimiter">));</span>
<span id="L37" class="LineNr">37 </span>  <a href='002test.cc.html#L24'>CHECK</a><span class="Delimiter">(</span><a href='015literal_noninteger.cc.html#L19'>is_noninteger</a><span class="Delimiter">(</span><span class="Constant">&quot;234.0&quot;</span><span class="Delimiter">));</span>
<span id="L38" class="LineNr">38 </span>  <a href='002test.cc.html#L24'>CHECK</a><span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;...&quot;</span><span class="Delimiter">));</span>
<span id="L39" class="LineNr">39 </span>  <a href='002test.cc.html#L24'>CHECK</a><span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;.&quot;</span><span class="Delimiter">));</span>
<span id="L40" class="LineNr">40 </span>  <a href='002test.cc.html#L24'>CHECK</a><span class="Delimiter">(</span><a href='015literal_noninteger.cc.html#L19'>is_noninteger</a><span class="Delimiter">(</span><span class="Constant">&quot;2.&quot;</span><span class="Delimiter">));</span>
<span id="L41" class="LineNr">41 </span>  <a href='002test.cc.html#L24'>CHECK</a><span class="Delimiter">(</span><a href='015literal_noninteger.cc.html#L19'>is_noninteger</a><span class="Delimiter">(</span><span class="Constant">&quot;.2&quot;</span><span class="Delimiter">));</span>
<span id="L42" class="LineNr">42 </span>  <a href='002test.cc.html#L24'>CHECK</a><span class="Delimiter">(</span><a href='015literal_noninteger.cc.html#L19'>is_noninteger</a><span class="Delimiter">(</span><span class="Constant">&quot;-.2&quot;</span><span class="Delimiter">));</span>
<span id="L43" class="LineNr">43 </span>  <a href='002test.cc.html#L24'>CHECK</a><span class="Delimiter">(</span><a href='015literal_noninteger.cc.html#L19'>is_noninteger</a><span class="Delimiter">(</span><span class="Constant">&quot;-2.&quot;</span><span class="Delimiter">));</span>
<span id="L44" class="LineNr">44 </span>  <a href='002test.cc.html#L24'>CHECK</a><span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;--.2&quot;</span><span class="Delimiter">));</span>
<span id="L45" class="LineNr">45 </span>  <a href='002test.cc.html#L24'>CHECK</a><span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;.-2&quot;</span><span class="Delimiter">));</span>
<span id="L46" class="LineNr">46 </span>  <a href='002test.cc.html#L24'>CHECK</a><span class="Delimiter">(</span>!is_noninteger<span class="Delimiter">(</span><span class="Constant">&quot;..2&quot;</span><span class="Delimiter">));</span>
<span id="L47" class="LineNr">47 </span><span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
span> location 3 :(scenario dilated_reagent_with_new) def main [ 1:address:address:num <- new {(address number): type} ] +new: size of ("address" "number") is 1 //: 'new' takes a weird 'type' as its first ingredient; don't error on it :(before "End Mu Types Initialization") put(Type_ordinal, "type", 0); :(code) bool is_mu_type_literal(const reagent& r) { return is_literal(r) && r.type && r.type->name == "type"; } :(before "End Primitive Recipe Declarations") NEW, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "new", NEW); :(before "End Primitive Recipe Checks") case NEW: { const recipe& caller = get(Recipe, r); if (inst.ingredients.empty() || SIZE(inst.ingredients) > 2) { raise << maybe(caller.name) << "'new' requires one or two ingredients, but got '" << inst.original_string << "'\n" << end(); break; } // End NEW Check Special-cases const reagent& type = inst.ingredients.at(0); if (!is_mu_type_literal(type)) { raise << maybe(caller.name) << "first ingredient of 'new' should be a type, but got '" << type.original_string << "'\n" << end(); break; } if (inst.products.empty()) { raise << maybe(caller.name) << "result of 'new' should never be ignored\n" << end(); break; } if (!product_of_new_is_valid(inst)) { raise << maybe(caller.name) << "product of 'new' has incorrect type: '" << inst.original_string << "'\n" << end(); break; } break; } :(code) bool product_of_new_is_valid(const instruction& inst) { reagent/*copy*/ product = inst.products.at(0); // Update NEW product in Check if (!product.type || product.type->atom || product.type->left->value != get(Type_ordinal, "address")) return false; drop_from_type(product, "address"); if (SIZE(inst.ingredients) > 1) { // array allocation if (!product.type || product.type->atom || product.type->left->value != get(Type_ordinal, "array")) return false; drop_from_type(product, "array"); } reagent/*local*/ expected_product; expected_product.type = new_type_tree(inst.ingredients.at(0).name); return types_strictly_match(product, expected_product); } void drop_from_type(reagent& r, string expected_type) { assert(!r.type->atom); if (r.type->left->name != expected_type) { raise << "can't drop2 " << expected_type << " from '" << to_string(r) << "'\n" << end(); return; } type_tree* tmp = r.type; r.type = tmp->right; tmp->right = NULL; delete tmp; } :(scenario new_with_type_abbreviation) def main [ 1:address:num/raw <- new num:type ] $error: 0 //: To implement 'new', a Mu transform turns all 'new' instructions into //: 'allocate' instructions that precompute the amount of memory they want to //: allocate. //: Ensure that we never call 'allocate' directly, and that there's no 'new' //: instructions left after the transforms have run. :(before "End Primitive Recipe Checks") case ALLOCATE: { raise << "never call 'allocate' directly'; always use 'new'\n" << end(); break; } :(before "End Primitive Recipe Implementations") case NEW: { raise << "no implementation for 'new'; why wasn't it translated to 'allocate'? Please save a copy of your program and send it to Kartik.\n" << end(); break; } :(after "Transform.push_back(check_instruction)") // check_instruction will guard against direct 'allocate' instructions below Transform.push_back(transform_new_to_allocate); // idempotent :(code) void transform_new_to_allocate(const recipe_ordinal r) { trace(9991, "transform") << "--- convert 'new' to 'allocate' for recipe " << get(Recipe, r).name << end(); for (int i = 0; i < SIZE(get(Recipe, r).steps); ++i) { instruction& inst = get(Recipe, r).steps.at(i); // Convert 'new' To 'allocate' if (inst.name == "new") { inst.operation = ALLOCATE; string_tree* type_name = new string_tree(inst.ingredients.at(0).name); type_name = parse_string_tree(type_name); type_tree* type = new_type_tree(type_name); inst.ingredients.at(0).set_value(size_of(type)); trace(9992, "new") << "size of " << to_string(type_name) << " is " << inst.ingredients.at(0).value << end(); delete type; delete type_name; } } } //: implement 'allocate' based on size :(before "End Globals") extern const int Reserved_for_tests = 1000; int Memory_allocated_until = Reserved_for_tests; int Initial_memory_per_routine = 100000; :(before "End Setup") Memory_allocated_until = Reserved_for_tests; Initial_memory_per_routine = 100000; :(before "End routine Fields") int alloc, alloc_max; :(before "End routine Constructor") alloc = Memory_allocated_until; Memory_allocated_until += Initial_memory_per_routine; alloc_max = Memory_allocated_until; trace(9999, "new") << "routine allocated memory from " << alloc << " to " << alloc_max << end(); :(before "End Primitive Recipe Declarations") ALLOCATE, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "allocate", ALLOCATE); :(before "End Primitive Recipe Implementations") case ALLOCATE: { // compute the space we need int size = ingredients.at(0).at(0); if (SIZE(ingredients) > 1) { // array allocation trace(9999, "mem") << "array length is " << ingredients.at(1).at(0) << end(); size = /*space for length*/1 + size*ingredients.at(1).at(0); } int result = allocate(size); if (SIZE(current_instruction().ingredients) > 1) { // initialize array length trace(9999, "mem") << "storing " << ingredients.at(1).at(0) << " in location " << result+/*skip refcount*/1 << end(); put(Memory, result+/*skip refcount*/1, ingredients.at(1).at(0)); } products.resize(1); products.at(0).push_back(result); break; } :(code) int allocate(int size) { // include space for refcount ++size; trace(9999, "mem") << "allocating size " << size << end(); //? Total_alloc += size; //? ++Num_alloc; // Allocate Special-cases // compute the region of memory to return // really crappy at the moment ensure_space(size); const int result = Current_routine->alloc; trace(9999, "mem") << "new alloc: " << result << end(); // initialize allocated space for (int address = result; address < result+size; ++address) { trace(9999, "mem") << "storing 0 in location " << address << end(); put(Memory, address, 0); } Current_routine->alloc += size; // no support yet for reclaiming memory between routines assert(Current_routine->alloc <= Current_routine->alloc_max); return result; } //: statistics for debugging //? :(before "End Globals") //? int Total_alloc = 0; //? int Num_alloc = 0; //? int Total_free = 0; //? int Num_free = 0; //? :(before "End Setup") //? Total_alloc = Num_alloc = Total_free = Num_free = 0; //? :(before "End Teardown") //? cerr << Total_alloc << "/" << Num_alloc //? << " vs " << Total_free << "/" << Num_free << '\n'; //? cerr << SIZE(Memory) << '\n'; :(code) void ensure_space(int size) { if (size > Initial_memory_per_routine) { tb_shutdown(); cerr << "can't allocate " << size << " locations, that's too much compared to " << Initial_memory_per_routine << ".\n"; exit(0); } if (Current_routine->alloc + size > Current_routine->alloc_max) { // waste the remaining space and create a new chunk Current_routine->alloc = Memory_allocated_until; Memory_allocated_until += Initial_memory_per_routine; Current_routine->alloc_max = Memory_allocated_until; trace(9999, "new") << "routine allocated memory from " << Current_routine->alloc << " to " << Current_routine->alloc_max << end(); } } :(scenario new_initializes) % Memory_allocated_until = 10; % put(Memory, Memory_allocated_until, 1); def main [ 1:address:num <- new number:type ] +mem: storing 0 in location 10 :(scenario new_array) def main [ 1:address:array:num/raw <- new number:type, 5 2:address:num/raw <- new number:type 3:num/raw <- subtract 2:address:num/raw, 1:address:array:num/raw ] +run: {1: ("address" "array" "number"), "raw": ()} <- new {number: "type"}, {5: "literal"} +mem: array length is 5 # don't forget the extra location for array length, and the second extra location for the refcount +mem: storing 7 in location 3 :(scenario new_empty_array) def main [ 1:address:array:num/raw <- new number:type, 0 2:address:num/raw <- new number:type 3:num/raw <- subtract 2:address:num/raw, 1:address:array:num/raw ] +run: {1: ("address" "array" "number"), "raw": ()} <- new {number: "type"}, {0: "literal"} +mem: array length is 0 # one location for array length, and one for the refcount +mem: storing 2 in location 3 //: If a routine runs out of its initial allocation, it should allocate more. :(scenario new_overflow) % Initial_memory_per_routine = 3; // barely enough room for point allocation below def main [ 1:address:num/raw <- new number:type 2:address:point/raw <- new point:type # not enough room in initial page ] +new: routine allocated memory from 1000 to 1003 +new: routine allocated memory from 1003 to 1006