Question: Question Problem: Constructing a programming language named Adder. The Adder language only has a few simple statements: quit Exit the REPL or terminate a program.

Question

Problem: Constructing a programming language named Adder.

The Adder language only has a few simple statements:

quit Exit the REPL or terminate a program.

input var Prompt for and allow the user to enter a value for the variable named var.

print val Print the value val.

var gets val variable var is assigned the value val.

var adds val variable var has the value val added to it.

Where:

  • var is always a variable name that contains only letters; and
  • val can be either:

- a variable name that contains only letters; or

- a natural number that contains only digits.

The adder REPL allows the user to enter commands interactively.

Heres the example of the way it needs to appear.

Welcome to the Adder REPL.

??? gets 1

??? input b

Enter a value for b: 2

??? c gets a

??? c adds b

??? print a

a equals 1

??? print b

b equals 2

??? print c

c equals 3

??? print z

z is undefined.

??? print 32

32

??? blerg

Syntax error.

??? 23 gets z

Syntax error.

??? quit

Goodbye.

I have got this code that seems to work but it quite complicated and lengthy, is there anyway of reducing it to seperate functions or reducing lines of code to make it simpler?

def check_result(commandList, vars): n = len(commandList) se = False if n == 1 and commandList[0] != "quit": se = True elif n == 2: if commandList[0] != "print" and commandList[0] != "input": se = True elif commandList[0] == "input" and not commandList[1].isalpha(): se = True if commandList[0] == "print" and commandList[1].isalpha() and commandList[1] not in vars.keys(): print(commandList[1], "is undefined") return False elif n == 3: if not commandList[0].isalpha(): se = True elif commandList[0] not in vars.keys() and commandList[1] != "gets": print(commandList[0], "is undefined") return False if commandList[1] != "gets" and commandList[1] != "adds": se = True if not commandList[2].isdigit(): if not commandList[2].isalpha(): se = True elif commandList[2] not in vars.keys() and commandList[1] != "gets": print(commandList[2], "is undefined") return False else: se = True if se: print("Syntax error.") return False return True def main(): vars = {} command = input("??? ") while command != "quit": commandList = command.split(" ") c = check_result(commandList, vars) if len(commandList) == 2 and c: # case of command input var if commandList[0] == "input": var = input("Enter a value for " + commandList[1] + " : ") if var.isdigit(): vars[commandList[1]] = int(var) else: vars[commandList[1]] = var elif commandList[0] == "print": if commandList[1] in vars.keys(): print(commandList[1], "equals", vars[commandList[1]]) else: print(commandList[1]) # three word commands elif len(commandList) == 3 and c: if commandList[2].isdigit(): val = int(commandList[2]) elif commandList[2] in vars.keys(): val = vars[commandList[2]] else: val = commandList[2] if commandList[1] == "gets": vars[commandList[0]] = val elif commandList[1] == "adds": vars[commandList[0]] += val command = input("??? ") if command == "quit": print("Goodbye") print("Welcome to the Adder REPL") main() 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!