This project is a Python-like interpreter implemented in C++. It parses and executes a subset of Python syntax, supporting basic variable management, arithmetic and logical operations, loops, conditional statements, and custom functions. It provides a way to evaluate Python-like code dynamically while implementing the logic and syntax handling in C++.
-
Python-Like Syntax Support:
- Handles Python-style variable assignments,
if-elsestatements,whileloops, andPrintstatements. - Evaluates mathematical and logical expressions.
- Handles Python-style variable assignments,
-
Variable Management:
- Supports types similar to Python:
Int,Double, andStr(Strings). - Dynamically stores and rewrites variable values during execution.
- Supports types similar to Python:
-
Expression Parsing:
- Converts and evaluates infix mathematical expressions using postfix notation.
- Evaluates logical expressions with operators like
and,or,==,!=,<,<=,>,>=.
-
Custom Built-In Functions:
Print: Outputs variables, expressions, or strings.Size: Calculates the size of strings.Type: Identifies the type of a variable.IntandDouble: Type casting functions.
-
Control Flow:
- Implements
if-elselogic and while loops. - Uses a
gotomechanism to manage loops and block indentation.
- Implements
-
Input Parsing:
- Accepts Python-like code through standard input.
- Processes line by line to interpret commands or store them for later evaluation.
-
Expression Evaluation:
- Arithmetic and logical expressions are evaluated using stack-based approaches.
- Supports variable replacement, type casting, and advanced operations.
-
Execution Flow:
- Loops and conditionals are handled with custom logic to mimic Python’s behavior.
- Goto-based loop management ensures efficient handling of repeated code blocks.
-
Core Components:
- Expression Evaluation:
rewrite: Replaces variables with their values in expressions.postfix: Converts infix to postfix expressions.doMath: Computes mathematical results from postfix expressions.solveLogic: Evaluates logical conditions.
- Variable Management:
variables: Stores variable names, types, and values.save: Parses and stores variable assignments dynamically.
- Built-In Functions:
- Functions like
toInt,toDouble,getSize, andgetTypehandle Python-style type casting and introspection.
- Functions like
- Expression Evaluation:
-
Execution Control:
- Implements conditional (
if-else) and looping (while) constructs. - Detects and prevents infinite loops.
- Implements conditional (
x = 10
y = 20
if (x < y):
Print("x is less than y")
else:
Print("x is greater than or equal to y")
while (x < y):
Print(x)
x = x + 2
Print("Done")
#x is less than y
10
12
14
16
18
Done
-
Compilation: Use a C++ compiler to build the program:
g++ -o python_interpreter Untitled-2.cpp
-
Execution: Run the program:
./python_interpreter
-
Input:
- Enter Python-like code interactively.
- End the program input with a
#symbol.
- Only supports a subset of Python syntax and constructs.
- Indentation rules are simulated using underscores (
_) for block management.
- Extend support for more Python features like lists, dictionaries, and advanced control flow.
- Add error reporting for syntax and runtime errors.
- Support file-based input for larger Python scripts.