0

Given the following [arbitrary language, although I think this is ALGOL] program:

program main;                               // A main parent level
  var i : integer;                          // A 'global' variable

  (* Note that all parameters are passed by value here *)

  function f1 (j : integer) : integer;      // A Child function
  begin { f1 }
    i := i + 3;
    f1 := 2 * j - i;
  end; { f1 }

  function f2 (k : integer) : integer;      // Another Child function, same level as f1
    var i : integer;                        // Here, there is a variable that is declared
  begin { f2 }                                 // but no value assigned
    i := k / 2;
    f2 := f1(i) + f1(k);
  end; { f2 }

begin { main }                              // Running/Calling/Executing the code
  i := 8;
  i := i + f2(i);
  writeln(i);
end. { main }

How would you show the values throughout the program using Dynamic scoping in both directions (evaluating left to right and then evaluating right to left) using javascript, so I can interactively change the values and watch what happens?

I learn a bit slower on some problems like this where the values are asked of the output, but don't show the value states throughout the program, and trying to get a better understanding, especially in an example I can play around with interactively.

I have created a plnkr for Static Scoping Left to Right
and another for Static Scoping Right to Left
feel free to fork them interactive answers :).

chris Frisina
  • 19,086
  • 22
  • 87
  • 167
  • Huh? This is JavaScript? – Evan Davis Oct 23 '14 at 16:44
  • the program is arbitrary. The plnkrs are JS implementations of this arbitrary code. – chris Frisina Oct 23 '14 at 16:47
  • 1
    I guess I don't understand what you are trying to achieve with this and what this has to do with dynamic scope. JS has only lexical scope. – Felix Kling Oct 23 '14 at 16:52
  • @FelixKling I am wondering if it is possible to force JS to exhibit dynamic scope, even though JS is static/lexical. This would enable an interactive example to learn through trial/error, hands on, as opposed to trying to read definitions or examples that do not provide all of the details within each scope that I am looking for. – chris Frisina Oct 23 '14 at 16:56
  • If "the program is arbitrary" it sounds like this belongs on [Computer Science](http://cs.stackexchange.com/); I don't have any idea what you're talking about. – Evan Davis Oct 23 '14 at 17:12
  • Wouldn't it just be a case of moving `i` to the global scope (in javascript)? – KooiInc Oct 23 '14 at 18:35
  • 1
    Cross-posted on SO: http://cs.stackexchange.com/q/32244/755. Please don't cross-post, as it violates site rules, fragments answers, and causes people to waste their time answering one question without realizing the other copy has already gotten an answer. – D.W. Oct 23 '14 at 22:37
  • @Mathletics, if you suggest another site, please remember to remind people not to cross-post. You might let them know that they can migrate their question to another site by clicking "flag" underneath to flag the question for moderator attention and ask the moderators migrate it. – D.W. Oct 23 '14 at 22:37
  • 1
    This is cross-posted on Computer Science StackExchange. – Patrick87 Oct 23 '14 at 22:38
  • 1
    I have flagged for moderator support to do whatever it is that they do to deal with the cross listing. – chris Frisina Oct 26 '14 at 14:49

0 Answers0