|
| 1 | +# MAS Programming Language |
| 2 | + |
| 3 | +**MAS** is a minimal, interpreted programming language designed for learning compiler and interpreter implementation. It features a clean syntax inspired by Python, with support for variables, arithmetic, loops, conditionals, lists, and built-in functions like `print`. |
| 4 | + |
| 5 | +> **MAS** stands for **Minimal And Simple** — a language built from scratch to understand the core concepts of programming language design. |
| 6 | +
|
| 7 | +--- |
| 8 | + |
| 9 | +## 🌟 Features |
| 10 | + |
| 11 | +- **Dynamic typing** with automatic type inference |
| 12 | +- **First-class lists** with literal syntax: `[1, 2, 3]` |
| 13 | +- **Control structures**: `if`, `loop`, `each` (for-each loop) |
| 14 | +- **Functions**: Built-in `print`, with user-defined functions (WIP) |
| 15 | +- **Expressions**: Arithmetic (`+`, `-`, `*`, `/`), comparisons (`==`, `!=`, `<`, etc.) |
| 16 | +- **Clean syntax**: No semicolons, indentation-like structure using `end` |
| 17 | +- **Memory management**: Reference counting for automatic cleanup |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## 🚀 Example Program |
| 22 | + |
| 23 | +```python |
| 24 | +# Hello World |
| 25 | +print "Hello, MAS!" |
| 26 | + |
| 27 | +# Variables and math |
| 28 | +x = 10 |
| 29 | +y = x * 2 + 5 |
| 30 | +print "Result:", y |
| 31 | + |
| 32 | +# Lists |
| 33 | +fruits = ["apple", "banana", "cherry"] |
| 34 | +print fruits |
| 35 | + |
| 36 | +# Loop |
| 37 | +i = 0 |
| 38 | +loop i < 3: |
| 39 | + print "Count:", i |
| 40 | + i = i + 1 |
| 41 | +end |
| 42 | + |
| 43 | +# For-each loop |
| 44 | +each f in fruits: |
| 45 | + print "Fruit:", f |
| 46 | +end |
| 47 | + |
| 48 | +give x # return value |
| 49 | +``` |
| 50 | + |
| 51 | +**Output:** |
| 52 | +``` |
| 53 | +Hello, MAS! |
| 54 | +Result: 25 |
| 55 | +[apple, banana, cherry] |
| 56 | +Count: 0 |
| 57 | +Count: 1 |
| 58 | +Count: 2 |
| 59 | +Fruit: apple |
| 60 | +Fruit: banana |
| 61 | +Fruit: cherry |
| 62 | +``` |
| 63 | + |
| 64 | +--- |
| 65 | + |
| 66 | +## 🛠️ Building & Running |
| 67 | + |
| 68 | +### Prerequisites |
| 69 | +- A C compiler (GCC or Clang) |
| 70 | +- Make (optional) |
| 71 | + |
| 72 | +### Build |
| 73 | +```bash |
| 74 | +git clone https://github.com/aditya-raj-arora/MASProgramming.git |
| 75 | +cd MASProgramming/mas |
| 76 | +gcc -o mas main.c lexer.c parser.c interpreter.c -lm |
| 77 | +``` |
| 78 | + |
| 79 | +### Run a Program |
| 80 | +```bash |
| 81 | +./mas your_program.mas |
| 82 | +``` |
| 83 | + |
| 84 | +> Replace `your_program.mas` with the path to your MAS source file. |
| 85 | +
|
| 86 | +--- |
| 87 | + |
| 88 | +## 📁 Project Structure |
| 89 | + |
| 90 | +``` |
| 91 | +mas/ |
| 92 | +├── mas.h # Shared headers and type definitions |
| 93 | +├── lexer.c # Tokenizer (converts source code to tokens) |
| 94 | +├── parser.c # Recursive descent parser (builds AST) |
| 95 | +├── interpreter.c # Tree-walking interpreter (executes AST) |
| 96 | +├── main.c # Entry point and driver |
| 97 | +└── test.mas # Example MAS program |
| 98 | +``` |
| 99 | + |
| 100 | +--- |
| 101 | + |
| 102 | +## 🔧 Language Syntax |
| 103 | + |
| 104 | +### Variables |
| 105 | +```mas |
| 106 | +x = 42 |
| 107 | +name = "MAS" |
| 108 | +``` |
| 109 | + |
| 110 | +### Control Flow |
| 111 | +```mas |
| 112 | +# Conditional |
| 113 | +if x > 10: |
| 114 | + print "Big number" |
| 115 | +end |
| 116 | +
|
| 117 | +# Loop |
| 118 | +i = 0 |
| 119 | +loop i < 5: |
| 120 | + print i |
| 121 | + i = i + 1 |
| 122 | +end |
| 123 | +
|
| 124 | +# For-each |
| 125 | +items = [10, 20, 30] |
| 126 | +each item in items: |
| 127 | + print item |
| 128 | +end |
| 129 | +``` |
| 130 | + |
| 131 | +### Data Types |
| 132 | +- **Number**: `42`, `3.14` |
| 133 | +- **String**: `"hello"` (supports `\n`, `\t`, `\"`) |
| 134 | +- **Boolean**: `true`, `false` |
| 135 | +- **Null**: `null` |
| 136 | +- **List**: `[1, "two", true]` |
| 137 | + |
| 138 | +### Operators |
| 139 | +- Arithmetic: `+`, `-`, `*`, `/` |
| 140 | +- Comparison: `==`, `!=`, `<`, `<=`, `>`, `>=` |
| 141 | +- Assignment: `=` |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +## 🧪 Testing |
| 146 | + |
| 147 | +A sample test file is included: |
| 148 | + |
| 149 | +```bash |
| 150 | +./mas test.mas |
| 151 | +``` |
| 152 | + |
| 153 | +This runs basic examples of variables, expressions, loops, and the `print` function. |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +## 📚 Learning Goals |
| 158 | + |
| 159 | +This project demonstrates core concepts in programming language implementation: |
| 160 | +- Lexical analysis (tokenization) |
| 161 | +- Recursive descent parsing |
| 162 | +- Abstract Syntax Trees (AST) |
| 163 | +- Tree-walking interpreters |
| 164 | +- Runtime object model and memory management |
| 165 | + |
| 166 | +It's ideal for students, hobbyists, or anyone wanting to build their first language! |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +## 🤝 Contributing |
| 171 | + |
| 172 | +Contributions are welcome! Feel free to: |
| 173 | +- Fix bugs |
| 174 | +- Add new features (e.g., user-defined functions, more data types) |
| 175 | +- Improve error messages |
| 176 | +- Write more test cases |
| 177 | + |
| 178 | +Just open a pull request with your changes. |
| 179 | + |
| 180 | +--- |
| 181 | + |
| 182 | +## 📜 License |
| 183 | + |
| 184 | +This project is open-source and available under the **MIT License**. See [LICENSE](LICENSE) for details. |
| 185 | + |
| 186 | +--- |
| 187 | + |
| 188 | +> **Built with ❤️ for learners and creators.** |
| 189 | +> — Aditya Raj Arora |
| 190 | +> — Mukta Motwani |
| 191 | +> — Shivam Saini |
0 commit comments