write a scripting language in python

main.py
# Define a simple interpreter for a scripting language in Python

# Initialize the environment
variables = {}

# Define a function to parse and evaluate the input
def evaluate(input_str):
    tokens = input_str.split()

    # Check if the input is a variable assignment
    if tokens[1] == "=":
        variables[tokens[0]] = " ".join(tokens[2:])
    else:
        # Check if the input is a variable reference
        if tokens[0] == "print":
            if tokens[1] in variables:
                print(variables[tokens[1]])
                
# Test the interpreter
evaluate("x = 5")
evaluate("y = Hello, World!")
evaluate("print x")
evaluate("print y")
654 chars
24 lines

This code demonstrates a basic interpreter for a simple scripting language implemented in Python. The script can perform variable assignment and printing values of variables.

related categories

gistlibby LogSnag