function [] = boxe7 % boxe7: animated trace of exercise E7 -- % user must type a key to advance from one movie frame to the next if nargin < 1 n = 3; end if n>3 warning('boxfib does not work well for n>3') end f.act_ = 'draw activation record'; f.header_ = strvcat(' ', 'function [x,y] = f(a,b)'); fcode = strvcat(... '% draw pic!', ... 'x = b-a; ', ... 'y = a; ', ... '% draw pic!'); fcode = [repmat(' ',size(fcode,1),4) fcode]; g.act_ = f.act_; g.header_ = strvcat(' ', 'function x = g(a,b)'); gcode = strvcat(... '% draw pic!', ... % 1 'a = 2*a; ', ... % 2 'b = 2*b; ', ... % 3 ' ', ... % 4 '[a,b] = f(b,a);', ... % 5 'a = 2*a; ', ... % 6 'b = 3*b; ', ... % 7 'x = a-b; ', ... % 8 '% draw pic!'); gcode = [repmat(' ',size(gcode,1),4) gcode]; ccode = strvcat(... 'a = 10; ', ... % 1 'b = 20; ', ... % 2 'x = 30; ', ... % 3 '% draw pic!', ... % 4 ' ', ... % 5 'x = g(15, 6); ', ... % 6 '% draw pic!'); ccode = [repmat(' ',size(ccode,1),4) ccode]; big.command.code_ = ccode; boxdisp(big); big.command.a = 10; ccode(:, 1:4) = ' '; ccode(1, 1:3) = '-->'; big.command.code_ = ccode; boxdisp(big); big.command.b = 20; ccode(:, 1:4) = ' '; ccode(2, 1:3) = '-->'; big.command.code_ = ccode; boxdisp(big); big.command.x = 30; ccode(:, 1:4) = ' '; ccode(3, 1:3) = '-->'; big.command.code_ = ccode; boxdisp(big); ccode(:, 1:4) = ' '; ccode(4, 1:3) = '-->'; big.command.code_ = ccode; boxdisp(big); ccode(:, 1:4) = ' '; ccode(5, 1:3) = '-->'; ccode(5, 9:16) = '?'; big.command.code_ = ccode; boxdisp(big); % > call to g -------------------------------------------------- g.code_ = gcode; big.g = g; boxdisp(big); g.act_ = strvcat('draw parameters;', 'fill with arguments'); g.a = 15; g.b = 6; big.g = g; boxdisp(big); g.act_ = 'run function body'; g.code_ = gcode; g.code_(1,1:3) = '-->'; big.g = g; boxdisp(big); g.a = 2*g.a; g.code_ = gcode; g.code_(2,1:3) = '-->'; big.g = g; boxdisp(big); g.b = 2*g.b; g.code_ = gcode; g.code_(3,1:3) = '-->'; big.g = g; boxdisp(big); g.code_ = gcode; g.code_(4,1:3) = '-->'; g.code_(4, 13:18) = '?'; big.g = g; boxdisp(big); % > call to g > call to f -------------------------------------- f.code_ = fcode; big.f = f; boxdisp(big); f.act_ = strvcat('draw parameters;', 'fill with arguments'); f.a = g.b; f.b = g.a; big.f = f; boxdisp(big); f.act_ = 'run function body'; f.code_ = fcode; f.code_(1,1:3) = '-->'; big.f = f; boxdisp(big); f.x = f.b - f.a; f.code_ = fcode; f.code_(2,1:3) = '-->'; big.f = f; boxdisp(big); f.y = f.a; f.code_ = fcode; f.code_(3,1:3) = '-->'; big.f = f; boxdisp(big); f.code_ = fcode; f.code_(4,1:3) = '-->'; big.f = f; boxdisp(big); f.code_ = []; f.act_ = strvcat('copy out return values;', 'kill activation record'); f = rmfield(f,['a' ; 'b']); s = sprintf('[%d, %d]', f.x, f.y); g.code_(4, :) = ' '; g.code_(4, 12+(1:length(s))) = s; g.code_(4, 1:3) = '-->'; big.g = g; big.f = f; boxdisp(big); big = rmfield(big, 'f'); boxdisp(big); % > call to g < call to f -------------------------------------- g.a = f.x; g.b = f.y; g.code_ = gcode; g.code_(5,1:3) = '-->'; g.code_(4, 12+(1:length(s))) = s; big.g = g; boxdisp(big); g.a = 2*g.a; g.code_ = gcode; g.code_(6,1:3) = '-->'; big.g = g; boxdisp(big); g.b = 3*g.b; g.code_ = gcode; g.code_(7,1:3) = '-->'; big.g = g; boxdisp(big); g.x = g.a - g.b; g.code_ = gcode; g.code_(8,1:3) = '-->'; big.g = g; boxdisp(big); g.code_ = gcode; g.code_(9,1:3) = '-->'; big.g = g; boxdisp(big); g.code_ = []; g.act_ = strvcat('copy out return value;', 'kill activation record'); g = rmfield(g,['a' ; 'b']); s = num2str(g.x); ccode(:, 1:4) = ' '; ccode(5, :) = ' '; ccode(5, 8+(1:length(s))) = s; ccode(5, 1:3) = '-->'; big.g = g; big.command.code_ = ccode; boxdisp(big); big = rmfield(big, 'g'); boxdisp(big); % < call to g -------------------------------------------------- big.command.x = g.x; ccode(:, 1:4) = ' '; ccode(6, 1:3) = '-->'; big.command.code_ = ccode; boxdisp(big); ccode(5,:) = ' '; ccode(:, 1:4) = ' '; ccode(7, 1:3) = '-->'; big.command.code_ = ccode; boxdisp(big); function [] = boxdisp(big) s = boxscope(big,1); % pre-compute to avoid delay between $clc$ and $disp$ clc; disp(s); pause