Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # stonyBrookScript
- For this assignment, we add support for variables, assignments, conditionals, loops, and output to our language.
- For now, our language has only a single scope for variables: *a global static scope*.
- ## Variables and Assignments
- Variable names must begin with an ASCII letter, which may be followed by zero or more ASCII letters, digits and underscore.
- A regular expression that matches variable names is `'[A-Za-z][A-Za-z0-9_]*'`.
- Our expressions from the previous assignment need to be extended in two ways:
- 1. We need to add support for assignments to variables
- * For example, `x = 1`
- * Including assignment to indexed list variables, for example: `x[2] = 5` where `x` was a variable that contained a list
- * For example, `x` was previously assigned a list: `x=[1,2,3];` after the assignment `x[2] = 5`, `x` contains `[1,2,5]`
- 2. We need to add support for variables used in expressions
- * For example, if `x` was assigned `1`, then `print(x)` will print `1`
- * Similarly, we should evaluate indexed variables if they occur in a place where their value is needed
- * For example, if `x` was assigned `[1,2,5]`, then `print(l[0]+l[1]+l[1+1])` should print `8`
- Evaluating a variable for its value should have the following behavior:
- * **If** the variable has had a value assigned to it, the value should be returned.
- * **Else**, a `Semantic Error` should be generated and the program should stop.
- When an indexed list variable is used in an expression, then both the list and the index are evaluated to their value
- and the indexed list expression is evaluated.
- If the variable is not a list (or a string), or the index is not a integer number, then a `Semantic Error` should be produced.
- If the index is outside the bounds of the array, a `Semantic Error` should be produced.
- ## Statements
- The new types of statements are:
- * **Block:** a block statement consists of zero-or-more statements enclosed in curly braces {...}. When the block executes, each of the statements in the block is executed in sequential order.
- * **Assignment:** an assignment statement consists of an expression, an equals sign, an expression, and a semicolon. When the assignment statement executes, the left expression is assigned the value evaluated of the right-expression.
- * **Print:** a print statement consists of the "print" keyword, a left-parenthesis, an expression, a right-parenthesis, and a semicolon. When the print statement executes, the expression is evaluated for its value.
- * **Conditional statements:**
- * **If statements:** consists of the keyword `if`, a left-parenthesis, an expression, a right-parenthesis, and a body block statement. When the `if` statement executes, if the expression is `True`, the body block statement is executed.
- * **If-else statements:** consists of the keyword `if`, a left-parenthesis, an expression, a right-parenthesis, a body block statement, an `else` keyword and a body block statement . When the `if-else` statement executes, if the expression is `True`, the first body block statement is executed, else the second body is executed. The `else` goes with the closest previous `if` in the same block.
- - **While:** a `while` statement consists of the keyword `while`, a left-parenthesis, an expression, a right-parenthesis, and a body block statement. Executing the `while` statement begins by evaluating the condition for its value. If that value is `False`, the `while` statement terminates. Otherwise, the `while` statement executes its body block statement, and execution repeats.
- ## Running
- An input program consists of a single outermost block statement.
- Executing the program consists of executing this block.
- Your interpreter will be called with a single argument.
- This argument will be a file containing an input program.
- * If the program contains syntax error, your interpreter should print out: `Syntax Error` and stop.
- * Otherwise, you should execute the program.
- * If the execution of the program causes a semantic error, then your interpreter should print out `Semantic Error` and stop.
- Execution of the program may cause print statements to execute and the output from print statements should be sent to standard output. **No other output should be produced!**
- ## Sample Input and Output
- ### Example 1
- ```c
- {
- number = 33;
- isPrime = 1;
- i = 2;
- while(isPrime==1 and i<number){
- if (number%i==0) {
- isPrime = 0;
- }
- i = i + 1;
- }
- if(isPrime==1){
- print("isPrime is true");
- } else {
- print("isPrime is false");
- }
- }
- ```
- The output from this file should be only:
- ```c
- isPrime is false
- ```
- ### Example 2
- ```c
- {
- data = [ [ 100, 42 ], [ 100, 50 ], [ 123, 456 ], [ 300, 9000 ] ];
- result = [ 0, 0, 0, 0 ];
- i = 0;
- while (i < 4) {
- a = data[i][0];
- b = data[i][1];
- if (a > 0){
- while (b > 0){
- if (a > b){
- a = a - b;
- } else {
- b = b - a;
- }
- }
- }
- result[i] = a;
- i = i + 1;
- }
- print(result);
- }
- ```
- The program will print:
- ```c
- [2, 50, 3, 300]
- ```
- ### Example 3
- Printing strings should execute as in python (no quotes around single strings printed and single quotes around strings in lists):
- ```c
- {print("a");}
- ```
- The output should be:
- ```c
- a
- ```
- Another input:
- ```c
- {print([1, "a", 2]);}
- ```
- the output should be:
- ```c
- [1, 'a', 2]
- ```
- ## Tips
- To complete this assignment, I suggest you:
- * Create a global map/dictionary to store the values of variables.
- * Implement the `evaluate()` method of the `Variable` class so that it returns the values of variables.
- * Implement the execute method of each of the statements.
- ## Submission
- You should include your name and ID as the first line (commented out) in your python file.
- The program must run under Python 3.x.
- The program must be named `main.py`.
- Your program will be run with a command like:
- ```sh
- python3 main.py inputProgram.txt
- ```
- ## Grading
- There are a total of 50 points.
- Your program will be run against 5 use cases using similar programs to the sample input.
- If it generates a correct result you will get 10 points for each use case.
- If the solution is incorrect, you will lose the points for that use case.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement