From 1f9a652ac604db0b43d0e0163766e9263b85b116 Mon Sep 17 00:00:00 2001 From: shrehanrajsingh Date: Fri, 20 Mar 2026 01:41:06 +0530 Subject: [PATCH 1/2] add: interpreter task --- .../Programming Fundamentals/interpreter.md | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 Teaching/Programming Fundamentals/interpreter.md diff --git a/Teaching/Programming Fundamentals/interpreter.md b/Teaching/Programming Fundamentals/interpreter.md new file mode 100644 index 0000000..8340a4c --- /dev/null +++ b/Teaching/Programming Fundamentals/interpreter.md @@ -0,0 +1,107 @@ +# Interpreted Programming Language + +## Background +An interpreter is a program that directly executes instructions written in a programming or scripting language, without requiring them to have been compiled into machine-level code. It reads the source code line by line, translates it, and performs the specified operations immediately, allowing for easier debugging and rapid development. + +## What you need to do + +As a part of this task you will have to create your own runtime for a programming language. +You need to define: +- A language syntax +- A memory store +- Abstract Syntax Trees +- Bytecode engine (optional) + +Example syntax: + +```basic +INPUT "Enter the first number: ", a +INPUT "Enter the second number: ", b +sum = a + b +PRINT "The sum of these numbers is: ", sum +END +``` + +This example is from the classic language QBasic, which is a begineer-friendly language that uses a "top-down" approach, meaning the computer reads and executes your code line by line. + +## Example: AST Breakdown + +Here's a simple Abstract Syntax Tree (AST) representation for the QBasic code above: + +``` +Program +├── InputStatement(prompt="Enter the first number: ", variable="a") +├── InputStatement(prompt="Enter the second number: ", variable="b") +├── AssignmentStatement(variable="sum", expression=BinaryOperation(left="a", operator="+", right="b")) +├── PrintStatement(values=["The sum of these numbers is: ", "sum"]) +└── EndStatement() +``` + +Each node represents a statement or expression in the code, capturing its structure and relationships. +In the execution phase, the interpreter can be tuned as follows. +```cpp +if (root->type == InputStatement) { + std::string inp; + std::cout << root->prompt; + std::cin >> inp; + store (root->variable, inp); +} +``` + +## Example: Bytecode Generation (Optional) + +Here's a sample bytecode representation for the QBasic program above. Each instruction is a simple operation that the interpreter's virtual machine would execute: + +``` +0: INPUT "Enter the first number: " a +1: INPUT "Enter the second number: " b +2: LOAD a +3: LOAD b +4: ADD +5: STORE sum +6: PRINT "The sum of these numbers is: " +7: LOAD sum +8: PRINTLN +9: END +``` + +**Explanation of Bytecode Instructions:** +- `INPUT `: Display prompt and store user input in ``. +- `LOAD `: Push the value of `` onto the stack. +- `ADD`: Pop two values from the stack, add them, and push the result. +- `STORE `: Pop value from the stack and store in ``. +- `PRINT `: Print the value (string or variable) without newline. +- `PRINTLN`: Print the value on top of the stack with a newline. +- `END`: Terminate the program. + +This bytecode can be interpreted by a simple stack-based virtual machine. + +The task should implement either AST generation and execution of the AST, or Bytecode generation from AST and execution of said bytecode. +> [!TIP] +> As this is task involves some coding, you are encouraged to use version control +> and make your project open-source. You are free to use any programming language you like. + +Please create a basic implementation of the language along with a presentation, either using PPT or preferably an Open Source +tool such as [RevealJS](https://revealjs.com/). The interviewee needs to keep +in mind that the crowd he will be presenting to, will have mixed people of +different knowledge levels, so it is advised to keep the content balanced +for all rather than becoming very technical. + +## Some Resources +- https://ruslanspivak.com/lsbasi-part1/ +- https://github.com/codecrafters-io/build-your-own-x?tab=readme-ov-file#build-your-own-programming-language +- https://www.craftinginterpreters.com/contents.html + +## Learning from the Task + +Completing this task will help you gain practical experience in several key areas of computer science and software engineering: + +- **Language Design:** You'll learn how to define the syntax and semantics of a programming language, making decisions about how code should be structured and interpreted. +- **Parsing and AST Construction:** You'll understand how to convert source code into an Abstract Syntax Tree (AST), which is a fundamental concept in compilers and interpreters. +- **Interpreter Implementation:** You'll gain hands-on experience in building an interpreter that can execute code by traversing the AST or by running bytecode on a virtual machine. +- **Memory Management:** You'll explore how variables and values are stored and managed during program execution. +- **Error Handling:** You'll learn how to detect and report errors in user code, an essential skill for building robust software. +- **Software Architecture:** You'll practice structuring your codebase for clarity and extensibility, which is crucial for larger projects. +- **Presentation Skills:** By preparing a presentation, you'll develop the ability to communicate technical concepts to audiences with varying levels of expertise. + +This task provides a strong foundation for understanding how programming languages work under the hood, and will be valuable for anyone interested in compilers, interpreters, or language design. \ No newline at end of file From 3a3d53f48e21bc684aab39770c63a9bd46b19731 Mon Sep 17 00:00:00 2001 From: shrehanrajsingh Date: Thu, 26 Mar 2026 04:13:16 +0530 Subject: [PATCH 2/2] added changes to proposed reviews --- .../Programming Fundamentals/interpreter.md | 84 ++++++++----------- 1 file changed, 35 insertions(+), 49 deletions(-) diff --git a/Teaching/Programming Fundamentals/interpreter.md b/Teaching/Programming Fundamentals/interpreter.md index 8340a4c..4e06c88 100644 --- a/Teaching/Programming Fundamentals/interpreter.md +++ b/Teaching/Programming Fundamentals/interpreter.md @@ -24,57 +24,37 @@ END This example is from the classic language QBasic, which is a begineer-friendly language that uses a "top-down" approach, meaning the computer reads and executes your code line by line. -## Example: AST Breakdown - -Here's a simple Abstract Syntax Tree (AST) representation for the QBasic code above: - -``` -Program -├── InputStatement(prompt="Enter the first number: ", variable="a") -├── InputStatement(prompt="Enter the second number: ", variable="b") -├── AssignmentStatement(variable="sum", expression=BinaryOperation(left="a", operator="+", right="b")) -├── PrintStatement(values=["The sum of these numbers is: ", "sum"]) -└── EndStatement() +### What is an AST? +An Abstract Syntax Tree (AST) is a tree representation of the structure of source code. Each node in the tree represents a construct in the code (like a variable assignment, function call, or operator), and the edges represent the relationships between these constructs. The AST abstracts away syntactic details like parentheses and semicolons, focusing only on the meaningful structure of the program. This makes it easier for interpreters and compilers to analyze and execute the code. +Consider a statement like the following: +```py +a = 20 +``` +then one appropriate AST could be: +```py +VariableDeclarationStatement (name = 'a', value = Integer (20)) ``` - -Each node represents a statement or expression in the code, capturing its structure and relationships. -In the execution phase, the interpreter can be tuned as follows. -```cpp -if (root->type == InputStatement) { - std::string inp; - std::cout << root->prompt; - std::cin >> inp; - store (root->variable, inp); +This can be represented as a C struct: +```c +struct VariableDeclarationStatement { + char *name; + value_type value; } ``` -## Example: Bytecode Generation (Optional) - -Here's a sample bytecode representation for the QBasic program above. Each instruction is a simple operation that the interpreter's virtual machine would execute: - -``` -0: INPUT "Enter the first number: " a -1: INPUT "Enter the second number: " b -2: LOAD a -3: LOAD b -4: ADD -5: STORE sum -6: PRINT "The sum of these numbers is: " -7: LOAD sum -8: PRINTLN -9: END -``` +## Bytecode Generation (Optional) -**Explanation of Bytecode Instructions:** -- `INPUT `: Display prompt and store user input in ``. -- `LOAD `: Push the value of `` onto the stack. -- `ADD`: Pop two values from the stack, add them, and push the result. -- `STORE `: Pop value from the stack and store in ``. -- `PRINT `: Print the value (string or variable) without newline. -- `PRINTLN`: Print the value on top of the stack with a newline. -- `END`: Terminate the program. +Bytecode is a numerical representation of AST. +ASTs are complex data structures and its computationally expensive working with them. +Bytecode simplifies logic by providing a linear execution order which: +- is computationally inexpensive to catch. +- is computationally inexpensive to execute. -This bytecode can be interpreted by a simple stack-based virtual machine. +Instead of doing a big task, bytecode splits the task into smaller tasks that do not involve extensive use of data structures. +For learning more about bytecode, you can refer the following resources: +- [Medium: The Life of a Bytecode Language by Better Programming](https://medium.com/better-programming/the-life-of-a-bytecode-language-fca666928e7b) +- [Medium: What Do we Know about ByteCode by Vikas Taank](https://medium.com/@vikas.taank_40391/what-do-we-know-about-bytecode-283a00f69e97) +- [Writing a compiler. Bytecode basics](https://www.youtube.com/watch?v=ZID0IJiOJdE) The task should implement either AST generation and execution of the AST, or Bytecode generation from AST and execution of said bytecode. > [!TIP] @@ -87,10 +67,16 @@ in mind that the crowd he will be presenting to, will have mixed people of different knowledge levels, so it is advised to keep the content balanced for all rather than becoming very technical. -## Some Resources -- https://ruslanspivak.com/lsbasi-part1/ -- https://github.com/codecrafters-io/build-your-own-x?tab=readme-ov-file#build-your-own-programming-language -- https://www.craftinginterpreters.com/contents.html +## Resources + +To help you build your interpreter, here are some excellent resources: + +- **[Crafting Interpreters](https://www.craftinginterpreters.com/contents.html)** - A comprehensive guide covering language design, lexing, parsing, and execution with practical examples. +- **[Let's Build a Simple Interpreter](https://ruslanspivak.com/lsbasi-part1/) by Ruslan Spivak** - A detailed series that walks through building an interpreter step-by-step from scratch. +- **[Build Your Own X](https://github.com/codecrafters-io/build-your-own-x?tab=readme-ov-file#build-your-own-programming-language)** - A curated collection of projects including building programming languages with community implementations. + +These resources provide different approaches and perspectives, from theoretical foundations to practical implementations. We recommend starting with one that matches your learning style—whether you prefer narrative tutorials or hands-on project-based learning. + ## Learning from the Task