Write a program with subroutines

Write a console application in Visual Studio 2008 C++. Make a class Sub, representing subroutines in a statically scoped programming language that supports nested subroutines. A Sub object has a name, a static parent's name, a collection of static children's names (initially empty), a number of arguments, a number of local variables, and a static depth. Assume that the only data type in this language is integer, and subroutines do not return a value. If the static parent's name is an empty value, this Sub object does not have a static parent.

Supply a constructor with 4 arguments: name, static parent's name, number of arguments, and number of local variables. The constructor will initialize the instance variables corresponding to its arguments, will calculate and initialize the static depth, and will add its name argument to the collection of its parent's children names.

Supply methods call and return, which will maintain a run-time stack passed by reference. You should chose a suitable data structure to implement the runtime stack. Method call will check if this sub's name is visible from the subroutine with the activation record currently at the top of the runtime stack, and if so will place an activation record for itself as new top of the runtime stack. Otherwise this call is an error. Method return will remove the activation record of this subroutine from the runtime stack.

Supply any necessary get methods.

Each activation record should contain:
• an integer encoding of the subroutine name. For example, you could use an STL map, or store the names in a vector and use the index as an encoding of the name, etc.
• static link: the index in the runtime stack of the first element of the activation record of the static parent. Use a negative number to indicate no static parent.
• dynamic link: the index in the runtime stack of the last element of the activation record of the caller (the old stack top). A Sub object with name main is always the first called (otherwise there is an error), and the dynamic link of the first activation record on the stack is -1
• one integer element for each argument
• one integer element for each local variable

Your main program should initialize an empty runtime stack, declare a number of Sub objects, and call their call and return methods to test the class Sub. Supply a convenient way to print the activation records currently on the stack in a readable format, resembling the stack vertically. For each activation record print “Sub” followed by the subroutine name (not the integer encoding), the static link as an integer preceded by the words “static link”, the dynamic link as an integer preceded by the words “dynamic link”, for each argument print “argument” followed by the sequence number of this argument, and for each local variable print “local var” followed by the sequence number of this variable.
And?
lol, you would ask this; if you can't tell by my username, I'm in your class...
Topic archived. No new replies allowed.