-- text editor, particularly text drawing, horizontal wrap, vertical scrolling
Text = {}
require 'search'
require 'select'
require 'undo'
require 'text_tests'
-- draw a line starting from startpos to screen at y between State.left and State.right
-- return the final y, and position of start of final screen line drawn
function Text.draw(State, line_index, y, startpos)
App.color(Text_color)
local line = State.lines[line_index]
local line_cache = State.line_cache[line_index]
line_cache.starty = y
line_cache.startpos = startpos
-- wrap long lines
local x = State.left
local pos = 1
local screen_line_starting_pos = startpos
Text.compute_fragments(State, line_index)
for _, f in ipairs(line_cache.fragments) do
local frag, frag_text = f.data, f.text
local frag_len = utf8.len(frag)
--? print('text.draw:', frag, 'at', line_index,pos, 'after', x,y)
if pos < startpos then
-- render nothing
--? print('skipping', frag)
else
-- render fragment
local frag_width = App.width(frag_text)
if x + frag_width > State.right then
assert(x > State.left) -- no overfull lines
y = y + State.line_height
if y + State.line_height > App.screen.height then
return y, screen_line_starting_pos
end
screen_line_starting_pos = pos
x = State.left
end
if State.selection1.line then
local lo, hi = Text.clip_selection(State, line_index, pos, pos+frag_len)
Text.draw_highlight(State, line, x,y, pos, lo,hi)
end
App.screen.draw(frag_text, x,y)
-- render cursor if necessary
if line_index == State.cursor1.line then
if pos <= State.cursor1.pos and pos + frag_len > State.cursor1.pos then
if State.search_term then
if State.lines[State.cursor1.line].data:sub(State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term)-1) == State.search_term then
local lo_px = Text.draw_highlight(State, line, x,y, pos, State.cursor1.pos, State.cursor1.pos+utf8.len(State.search_term))
App.color(Text_color)
love.graphics.print(State.search_term, x+lo_px,y)
end
else
Text.draw_cursor(State, x+Text.x(frag, State.cursor1.pos-pos+1), y)
end
end
end
x = x + frag_width
end
pos = pos + frag_len
end
if State.search_term == nil then
if line_index == State.cursor1.line and State.cursor1.pos == pos then
Text.draw_cursor(State, x, y)
end
end
return y, screen_line_starting_pos
end
function Text.draw_cursor(State, x, y)
-- blink every 0.5s
if math.floor(Cursor_time*2)%2 == 0 then
App.color(Cursor_color)
love.graphics.rectangle('fill', x,y, 3,State.line_height)
App.color(Text_color)
end
State.cursor_x = x
State.cursor_y = y+State.line_height
end
function Text.populate_screen_line_starting_pos(State, line_index)
local line = State.lines[line_index]
if line.mode ~= 'text' then return end
local line_cache = State.line_cache[line_index]
if line_cache.screen_line_starting_pos then
return
end
-- duplicate some logic from Text.draw
Text.compute_fragments(State, line_index)
line_cache.screen_line_starting_pos = {1}
local x = State.left
local pos = 1
for _, f in ipairs(line_cache.fragments) do
local frag, frag_text = f.data, f.text
-- render fragment
local frag_width = App.width(frag_text)
if x + frag_width > State.right then
x = State.left
table.insert(line_cache.screen_line_starting_pos, pos)
end
x = x + frag_width
local frag_len = utf8.len(frag)
pos = pos + frag_len
end
end
function Text.compute_fragments(State, line_index)
--? print('compute_fragments', line_index, 'between', State.left, State.right)
local line = State.lines[line_index]
if line.mode ~= 'text' then return end
local line_cache = State.line_cache[line_index]
if line_cache.fragments then
return
end
line_cache.fragments = {}
local x = State.left
-- try to wrap at word boundaries
for frag in line.data:gmatch('%S*%s*') do
local frag_text = App.newText(love.graphics.getFont(), frag)
local frag_width = App.width(frag_text)
--? print('x: '..tostring(x)..'; '..tostring(State.right-x)..'px to go')
while x + frag_width > State.right do
--? print(('checking whether to split fragment ^%s$ of width %d when rendering from %d'):format(frag, frag_width, x))
if (x-State.left) < 0.8 * (State.right-State.left) then
--? print('splitting')
-- long word; chop it at some letter
-- We're not going to reimplement TeX here.
local bpos = Text.nearest_pos_less_than(frag, State.right - x)
--? print('bpos', bpos)
if bpos == 0 then break end -- avoid infinite loop when window is too narrow
local boffset = Text.offset(frag, bpos+1) -- byte _after_ bpos
--? print('space for '..tostring(bpos)..' graphemes, '..tostring(boffset-1)..' bytes')
local frag1 = string.sub(frag, 1, boffset-1)
local frag1_text = App.newText(love.graphics.getFont(), frag1)
local frag1_width = App.width(frag1_text)
--? print('extracting ^'..frag1..'$ of width '..tostring(frag1_width)..'px')
assert(x + frag1_width <= State.right)
table.insert(line_cache.fragments, {data=frag1, text=frag1_text})
frag = string.sub(frag, boffset)
frag_text = App.newText(love.graphics.getFont(), frag)
frag_width = App.width(frag_text)
end
x = State.left -- new line
end
if #frag > 0 then
--? print('inserting ^'..frag..'$ of width '..tostring(frag_width)..'px')
table.insert(line_cache.fragments, {data=frag, text=frag_text})
end
x = x + frag_width
end
end
function Text.textinput(State, t)
if App.mouse_down(1) then return end
if App.ctrl_down() or App.alt_down() or App.cmd_down() then return end
local before = snapshot(State, State.cursor1.line)
--? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
Text.insert_at_cursor(State, t)
if State.cursor_y > App.screen.height - State.line_height then
Text.populate_screen_line_starting_pos(State, State.cursor1.line)
Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
--? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
end
record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
end
function Text.insert_at_cursor(State, t)
local byte_offset = Text.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_offset-1)..t..string.sub(State.lines[State.cursor1.line].data, byte_offset)
Text.clear_screen_line_cache(State, State.cursor1.line)
State.cursor1.pos = State.cursor1.pos+1
end
-- Don't handle any keys here that would trigger love.textinput above.
function Text.keychord_pressed(State, chord)
--? print('chord', chord, State.selection1.line, State.selection1.pos)
--== shortcuts that mutate text
if chord == 'return' then
local before_line = State.cursor1.line
local before = snapshot(State, before_line)
Text.insert_return(State)
State.selection1 = {}
if State.cursor_y > App.screen.height - State.line_height then
Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
end
schedule_save(State)
record_undo_event(State, {before=before, after=snapshot(State, before_line, State.cursor1.line)})
elseif chord == 'tab' then
local before = snapshot(State, State.cursor1.line)
--? print(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
Text.insert_at_cursor(State, '\t')
if State.cursor_y > App.screen.height - State.line_height then
Text.populate_screen_line_starting_pos(State, State.cursor1.line)
Text.snap_cursor_to_bottom_of_screen(State, State.left, State.right)
--? print('=>', State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos, State.screen_bottom1.line, State.screen_bottom1.pos)
end
schedule_save(State)
record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
elseif chord == 'backspace' then
if State.selection1.line then
Text.delete_selection(State, State.left, State.right)
schedule_save(State)
return
end
local before
if State.cursor1.pos > 1 then
before = snapshot(State, State.cursor1.line)
local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos-1)
local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
if byte_start then
if byte_end then
State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)..string.sub(State.lines[State.cursor1.line].data, byte_end)
else
State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
end
State.cursor1.pos = State.cursor1.pos-1
end
elseif State.cursor1.line > 1 then
before = snapshot(State, State.cursor1.line-1, State.cursor1.line)
if State.lines[State.cursor1.line-1].mode == 'drawing' then
table.remove(State.lines, State.cursor1.line-1)
table.remove(State.line_cache, State.cursor1.line-1)
else
-- join lines
State.cursor1.pos = utf8.len(State.lines[State.cursor1.line-1].data)+1
State.lines[State.cursor1.line-1].data = State.lines[State.cursor1.line-1].data..State.lines[State.cursor1.line].data
table.remove(State.lines, State.cursor1.line)
table.remove(State.line_cache, State.cursor1.line)
end
State.cursor1.line = State.cursor1.line-1
end
if State.screen_top1.line > #State.lines then
Text.populate_screen_line_starting_pos(State, #State.lines)
local line_cache = State.line_cache[#State.line_cache]
State.screen_top1 = {line=#State.lines, pos=line_cache.screen_line_starting_pos[#line_cache.screen_line_starting_pos]}
elseif Text.lt1(State.cursor1, State.screen_top1) then
local top2 = Text.to2(State, State.screen_top1)
top2 = Text.previous_screen_line(State, top2, State.left, State.right)
State.screen_top1 = Text.to1(State, top2)
Text.redraw_all(State) -- if we're scrolling, reclaim all fragments to avoid memory leaks
end
Text.clear_screen_line_cache(State, State.cursor1.line)
assert(Text.le1(State.screen_top1, State.cursor1))
schedule_save(State)
record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
elseif chord == 'delete' then
if State.selection1.line then
Text.delete_selection(State, State.left, State.right)
schedule_save(State)
return
end
local before
if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
before = snapshot(State, State.cursor1.line)
else
before = snapshot(State, State.cursor1.line, State.cursor1.line+1)
end
if State.cursor1.pos <= utf8.len(State.lines[State.cursor1.line].data) then
local byte_start = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos)
local byte_end = utf8.offset(State.lines[State.cursor1.line].data, State.cursor1.pos+1)
if byte_start then
if byte_end then
State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)..string.sub(State.lines[State.cursor1.line].data, byte_end)
else
State.lines[State.cursor1.line].data = string.sub(State.lines[State.cursor1.line].data, 1, byte_start-1)
end
-- no change to State.cursor1.pos
end
elseif State.cursor1.line < #State.lines then
if State.lines[State.cursor1.line+1].mode == 'text' then
-- join lines
State.lines[State.cursor1.line].data = State.lines[State.cursor1.line].data..State.lines[State.cursor1.line+1].data
end
table.remove(State.lines, State.cursor1.line+1)
table.remove(State.line_cache, State.cursor1.line+1)
end
Text.clear_screen_line_cache(State, State.cursor1.line)
schedule_save(State)
record_undo_event(State, {before=before, after=snapshot(State, State.cursor1.line)})
--== shortcuts that move the cursor
elseif chord == 'left' then
Text.left(State)
State.selection1 = {}
elseif chord == 'right' then
Text.right(State)
State.selection1 = {}
elseif chord == 'S-left' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.left(State)
elseif chord == 'S-right' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.right(State)
-- C- hotkeys reserved for drawings, so we'll use M-
elseif chord == 'M-left' then
Text.word_left(State)
State.selection1 = {}
elseif chord == 'M-right' then
Text.word_right(State)
State.selection1 = {}
elseif chord == 'M-S-left' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.word_left(State)
elseif chord == 'M-S-right' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.word_right(State)
elseif chord == 'home' then
Text.start_of_line(State)
State.selection1 = {}
elseif chord == 'end' then
Text.end_of_line(State)
State.selection1 = {}
elseif chord == 'S-home' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.start_of_line(State)
elseif chord == 'S-end' then
if State.selection1.line == nil then
State.selection1 = {line=State.cursor1.line, pos=State.cursor1.pos}
end
Text.end_of_line(State)
elseif chord == 'up'pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long *///: Phase 3: Start running a loaded and transformed recipe.
//:
//: So far we've seen recipes as lists of instructions, and instructions point
//: at other recipes. To kick things off mu needs to know how to run certain
//: 'primitive' recipes. That will then give the ability to run recipes
//: containing these primitives.
//:
//: This layer defines a skeleton with just two primitive recipes: IDLE which
//: does nothing, and COPY, which can copy numbers from one memory location to
//: another. Later layers will add more primitives.
:(scenario copy_literal)
recipe main [
1:number <- copy 23:literal
]
+run: 1:number <- copy 23:literal
+mem: storing 23 in location 1
:(scenario copy)
recipe main [
1:number <- copy 23:literal
2:number <- copy 1:number
]
+run: 2:number <- copy 1:number
+mem: location 1 is 23
+mem: storing 23 in location 2
:(scenario copy_multiple)
recipe main [
1:number, 2:number <- copy 23:literal, 24:literal
]
+mem: storing 23 in location 1
+mem: storing 24 in location 2
:(before "End Types")
// Book-keeping while running a recipe.
//: Later layers will change this.
struct routine {
recipe_ordinal running_recipe;
long long int running_step_index;
routine(recipe_ordinal r) :running_recipe(r), running_step_index(0) {}
bool completed() const;
};
:(before "End Globals")
routine* Current_routine = NULL;
:(code)
void run(recipe_ordinal r) {
routine rr(r);
Current_routine = &rr;
run_current_routine();
}
void run_current_routine()
{ // curly on a separate line, because later layers will modify header
//? cerr << "AAA 6\n"; //? 3
while (!Current_routine->completed()) // later layers will modify condition
{
//? cerr << "AAA 7: " << current_step_index() << '\n'; //? 1
// Running One Instruction
if (current_instruction().is_label) { ++current_step_index(); continue; }
trace(Initial_callstack_depth+Callstack_depth, "run") << current_instruction().to_string();
assert(Memory[0] == 0);
// Read all ingredients from memory.
// Each ingredient loads a vector of values rather than a single value; mu
// permits operating on reagents spanning multiple locations.
vector<vector<double> > ingredients;
for (long long int i = 0; i < SIZE(current_instruction().ingredients); ++i) {
ingredients.push_back(read_memory(current_instruction().ingredients.at(i)));
}
// Instructions below will write to 'products'.
vector<vector<double> > products;
//? cerr << "AAA 8: " << current_instruction().operation << " ^" << Recipe[current_instruction().operation].name << "$\n"; //? 1
//? cerr << "% " << current_recipe_name() << "/" << current_step_index() << ": " << Memory[1013] << ' ' << Memory[1014] << '\n'; //? 1
switch (current_instruction().operation) {
// Primitive Recipe Implementations
case COPY: {
//? if (!ingredients.empty()) cerr << current_instruction().ingredients.at(0).to_string() << ' ' << ingredients.at(0).at(0) << '\n'; //? 1
copy(ingredients.begin(), ingredients.end(), inserter(products, products.begin()));
break;
}
// End Primitive Recipe Implementations
default: {
cout << "not a primitive op: " << current_instruction().operation << '\n';
}
}
if (SIZE(products) < SIZE(current_instruction().products))
raise << "failed to write to all products! " << current_instruction().to_string();
for (long long int i = 0; i < SIZE(current_instruction().products); ++i) {
write_memory(current_instruction().products.at(i), products.at(i));
}
// End of Instruction
++current_step_index();
}
//? cerr << "AAA 9\n"; //? 2
stop_running_current_routine:;
}
//: Some helpers.
//: We'll need to override these later as we change the definition of routine.
//: Important that they return referrences into the routine.
inline long long int& current_step_index() {
return Current_routine->running_step_index;
}
inline const string& current_recipe_name() {
return Recipe[Current_routine->running_recipe].name;
}
inline const instruction& current_instruction() {
return Recipe[Current_routine->running_recipe].steps.at(Current_routine->running_step_index);
}
inline bool routine::completed() const {
return running_step_index >= SIZE(Recipe[running_recipe].steps);
}
:(before "End Commandline Parsing")
// Loading Commandline Files
if (argc > 1) {
for (int i = 1; i < argc; ++i) {
load_permanently(argv[i]);
}
}
:(before "End Main")
if (!Run_tests) {
setup();
//? Trace_file = "interactive"; //? 1
START_TRACING_UNTIL_END_OF_SCOPE;
//? Trace_stream->dump_layer = "all"; //? 2
transform_all();
recipe_ordinal r = Recipe_ordinal[string("main")];
//? Trace_stream->dump_layer = "all"; //? 1
if (r) run(r);
//? dump_memory(); //? 1
teardown();
}
:(code)
void load_permanently(string filename) {
ifstream fin(filename.c_str());
fin.peek();
//? cerr << "AAA: " << filename << ' ' << static_cast<bool>(fin) << ' ' << fin.fail() << '\n'; //? 1
//? return; //? 1
if (!fin) {
raise << "no such file " << filename << '\n';
return;
}
fin >> std::noskipws;
load(fin);
transform_all();
fin.close();
// freeze everything so it doesn't get cleared by tests
recently_added_recipes.clear();
// End load_permanently.
}
//:: On startup, load everything in core.mu
:(before "End Load Recipes")
load_permanently("core.mu");
:(code)
// helper for tests
void run(string form) {
//? cerr << "AAA 2\n"; //? 2
//? cerr << form << '\n'; //? 1
vector<recipe_ordinal> tmp = load(form);
if (tmp.empty()) return;
transform_all();
//? cerr << "AAA 3\n"; //? 2
run(tmp.front());
//? cerr << "YYY\n"; //? 2
}
//:: Reading from memory, writing to memory.
vector<double> read_memory(reagent x) {
//? cout << "read_memory: " << x.to_string() << '\n'; //? 2
vector<double> result;
if (is_literal(x)) {
result.push_back(x.value);
return result;
}
long long int base = x.value;
long long int size = size_of(x);
for (long long int offset = 0; offset < size; ++offset) {
double val = Memory[base+offset];
trace(Primitive_recipe_depth, "mem") << "location " << base+offset << " is " << val;
result.push_back(val);
}
return result;
}
void write_memory(reagent x, vector<double> data) {
if (is_dummy(x)) return;
if (is_literal(x)) return;
long long int base = x.value;
if (size_mismatch(x, data)) {
raise << current_recipe_name() << ": size mismatch in storing to " << x.to_string() << " at " << current_instruction().to_string() << '\n' << die();
}
for (long long int offset = 0; offset < SIZE(data); ++offset) {
trace(Primitive_recipe_depth, "mem") << "storing " << data.at(offset) << " in location " << base+offset;
Memory[base+offset] = data.at(offset);
}
}
:(code)
long long int size_of(const reagent& r) {
return size_of(r.types);
}
long long int size_of(const vector<type_ordinal>& types) {
// End size_of(types) Cases
return 1;
}
bool size_mismatch(const reagent& x, const vector<double>& data) {
//? if (size_of(x) != SIZE(data)) cerr << size_of(x) << " vs " << SIZE(data) << '\n'; //? 2
return size_of(x) != SIZE(data);
}
bool is_dummy(const reagent& x) {
return x.name == "_";
}
bool is_literal(const reagent& r) {
return SIZE(r.types) == 1 && r.types.at(0) == 0;
}
:(scenario run_label)
recipe main [
+foo
1:number <- copy 23:literal
2:number <- copy 1:number
]
+run: 1:number <- copy 23:literal
+run: 2:number <- copy 1:number
-run: +foo
:(scenario run_dummy)
recipe main [
_ <- copy 0:literal
]
+run: _ <- copy 0:literal
:(scenario run_literal)
recipe main [
0:literal/screen <- copy 0:literal
]
-mem: storing 0 in location 0