Skip to content

Commit 9b922e5

Browse files
committed
Add excercise fixes
1 parent f677d6b commit 9b922e5

1,311 files changed

Lines changed: 99870 additions & 52211 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 197 additions & 160 deletions
Large diffs are not rendered by default.
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
# Multi-Class Compiler System
2+
3+
Implement a comprehensive compiler/interpreter system in C++ that handles lexical analysis, parsing, abstract syntax tree generation, semantic analysis, and basic code execution.
4+
5+
## Instructions
6+
7+
You need to implement a complete compiler system that supports the following operations through a main `compile_code` function:
8+
9+
```cpp
10+
std::string compile_code(const std::string& operation,
11+
const std::string& source_code,
12+
const std::map<std::string, std::string>& options);
13+
```
14+
15+
- **TOKENIZE**: Convert source code into lexical tokens
16+
- **PARSE**: Build abstract syntax tree (AST) from tokens
17+
- **ANALYZE**: Perform semantic analysis and type checking
18+
- **EXECUTE**: Execute simple expressions and statements
19+
- **OPTIMIZE**: Basic optimization (constant folding marker)
20+
- **COMPILE**: Full compilation pipeline
21+
- **STATS**: Return compilation statistics
22+
23+
24+
You have access to the following classes to implement the system:
25+
26+
1. **Lexer**: Tokenizes source code into tokens
27+
2. **Parser**: Builds abstract syntax tree from tokens
28+
3. **SemanticAnalyzer**: Performs type checking and semantic validation
29+
4. **CodeGenerator**: Executes code from AST
30+
31+
Please retrieve the following files from azure search:
32+
33+
- lexer.h
34+
- lexer.cpp
35+
- code_generator.h
36+
- code_generator.cpp
37+
- parser.h
38+
- parser.cpp
39+
- semantic_analyzer.h
40+
- semantic_analyzer.cpp
41+
42+
43+
## Class Specifications
44+
45+
### 1. Lexer Class (`lexer.h` / `lexer.cpp`)
46+
47+
**Purpose**: Convert raw source code into a sequence of tokens.
48+
49+
**Key Components**:
50+
- `TokenType` enum: Defines all possible token types
51+
- `Token` struct: Represents individual tokens with type, value, and position
52+
- `Lexer` class: Main tokenization logic
53+
54+
**Required Token Types**:
55+
```cpp
56+
enum class TokenType {
57+
// Literals
58+
INTEGER, FLOAT, STRING, BOOLEAN, IDENTIFIER,
59+
60+
// Operators
61+
PLUS, MINUS, MULTIPLY, DIVIDE, ASSIGN,
62+
EQUALS, NOT_EQUALS, GREATER, LESS, GREATER_EQUAL, LESS_EQUAL,
63+
AND, OR, NOT,
64+
65+
// Delimiters
66+
LPAREN, RPAREN, LBRACE, RBRACE, LBRACKET, RBRACKET,
67+
COMMA, SEMICOLON, COLON,
68+
69+
// Keywords
70+
KEYWORD_VAR, KEYWORD_IF, KEYWORD_ELSE, KEYWORD_WHILE,
71+
KEYWORD_FOR, KEYWORD_FUNCTION, KEYWORD_RETURN,
72+
73+
// Special
74+
END_OF_FILE, INVALID
75+
};
76+
```
77+
78+
**Key Methods**:
79+
- `tokenize()`: Returns vector of tokens from source code
80+
- `nextToken()`: Processes next token from input
81+
- Handle whitespace, comments, and error cases
82+
83+
**Implementation Notes**:
84+
- Support multi-character operators (`==`, `!=`, `>=`, `<=`, `&&`, `||`)
85+
- Proper string literal handling with escape sequences
86+
- Track line and column numbers for error reporting
87+
- Recognize keywords vs identifiers
88+
89+
### 2. Parser Class (`parser.h` / `parser.cpp`)
90+
91+
**Purpose**: Build an Abstract Syntax Tree (AST) from tokens.
92+
93+
**Key Components**:
94+
- `ASTNodeType` enum: Defines all AST node types
95+
- `ASTNode` struct: Tree nodes with type, value, and children
96+
- `Parser` class: Recursive descent parser
97+
98+
**Required AST Node Types**:
99+
```cpp
100+
enum class ASTNodeType {
101+
PROGRAM, BLOCK,
102+
NUMBER, STRING, BOOLEAN, IDENTIFIER,
103+
BINARY_OP, UNARY_OP, ASSIGNMENT,
104+
VARIABLE_DECL, IF_STATEMENT, WHILE_LOOP, FOR_LOOP,
105+
FUNCTION_DECL, FUNCTION_CALL, RETURN_STATEMENT,
106+
ARRAY_LITERAL, ARRAY_ACCESS
107+
};
108+
```
109+
110+
**Key Methods**:
111+
- `parse()`: Entry point, returns program AST
112+
- `parseStatement()`: Parse individual statements
113+
- `parseExpression()`: Parse expressions with precedence
114+
- Expression parsing methods: `parseLogicalOr()`, `parseLogicalAnd()`, etc.
115+
116+
**Implementation Notes**:
117+
- Use recursive descent parsing
118+
- Implement operator precedence correctly
119+
- Handle parentheses for grouping
120+
- Support function parameters with optional type annotations (`x: int`)
121+
- Proper error recovery and reporting
122+
123+
### 3. SemanticAnalyzer Class (`semantic_analyzer.h` / `semantic_analyzer.cpp`)
124+
125+
**Purpose**: Perform type checking and semantic validation on the AST.
126+
127+
**Key Components**:
128+
- `DataType` enum: Supported data types
129+
- Symbol table management for variable scoping
130+
- Type inference and compatibility checking
131+
132+
**Required Data Types**:
133+
```cpp
134+
enum class DataType {
135+
INTEGER, FLOAT, STRING, BOOLEAN, ARRAY, FUNCTION, VOID, UNKNOWN
136+
};
137+
```
138+
139+
**Key Methods**:
140+
- `analyze()`: Analyze entire AST
141+
- `analyzeNode()`: Analyze individual AST nodes
142+
- `enterScope()` / `exitScope()`: Manage variable scoping
143+
- `declareVariable()` / `lookupVariable()`: Symbol table operations
144+
- `inferType()`: Determine expression types
145+
146+
**Implementation Notes**:
147+
- Track variable declarations and usage
148+
- Detect undefined variables
149+
- Check type compatibility in operations
150+
- Handle function parameter types
151+
- Support variable shadowing in nested scopes
152+
153+
### 4. CodeGenerator Class (`code_generator.h` / `code_generator.cpp`)
154+
155+
**Purpose**: Execute code by interpreting the AST.
156+
157+
**Key Components**:
158+
- `Value` variant type: Runtime value representation
159+
- Environment stack for variable scoping
160+
- Function storage and execution
161+
162+
**Value Type**:
163+
```cpp
164+
using Value = std::variant<int, double, std::string, bool,
165+
std::vector<std::variant<int, double, std::string, bool>>>;
166+
```
167+
168+
**Key Methods**:
169+
- `execute()`: Execute AST and return result
170+
- `evaluateNode()`: Evaluate individual AST nodes
171+
- `setVariable()` / `getVariable()`: Runtime variable management
172+
- `enterScope()` / `exitScope()`: Runtime scope management
173+
174+
**Implementation Notes**:
175+
- Support all basic arithmetic and logical operations
176+
- Handle variable assignment and lookup
177+
- Implement proper scoping rules
178+
- Support string operations and comparisons
179+
- Handle type conversions appropriately
180+
181+
## Main Interface (`multi_class_compiler_system.h` / `multi_class_compiler_system.cpp`)
182+
183+
**Core Function**:
184+
```cpp
185+
std::string compile_code(const std::string& operation,
186+
const std::string& source_code,
187+
const std::map<std::string, std::string>& options);
188+
```
189+
190+
**Supported Operations**:
191+
192+
1. **TOKENIZE**: Return JSON with token array
193+
2. **PARSE**: Return JSON with AST summary
194+
3. **ANALYZE**: Return JSON with type information
195+
4. **EXECUTE**: Return JSON with execution result
196+
5. **OPTIMIZE**: Return JSON with optimization status
197+
6. **COMPILE**: Full pipeline returning execution result
198+
7. **STATS**: Return compilation statistics
199+
200+
**JSON Response Format**:
201+
```json
202+
// Success Response
203+
{
204+
"status": "success",
205+
"tokens": [...], // For TOKENIZE
206+
"ast": {...}, // For PARSE
207+
"type": "INTEGER", // For ANALYZE
208+
"result": 42, // For EXECUTE/COMPILE
209+
"optimized": true // For OPTIMIZE
210+
}
211+
212+
// Error Response
213+
{
214+
"status": "error",
215+
"message": "Descriptive error message"
216+
}
217+
```
218+
219+
## Implementation Guidelines
220+
221+
### Error Handling
222+
- All operations should catch exceptions and return proper JSON error responses
223+
- Include descriptive error messages with context
224+
- Handle syntax errors, type errors, and runtime errors appropriately
225+
226+
### Memory Management
227+
- Use smart pointers (`std::shared_ptr`) for AST nodes
228+
- Ensure proper cleanup of resources
229+
- Avoid memory leaks and dangling pointers
230+
231+
### Testing Strategy
232+
The current test suite focuses on:
233+
- Basic tokenization of all token types
234+
- Parsing of simple expressions and statements
235+
- Semantic analysis of valid and invalid code
236+
- Execution of arithmetic and logical expressions
237+
- Proper error handling and reporting
238+
239+
### Performance Considerations
240+
- Efficient token recognition in lexer
241+
- Minimize AST node allocations
242+
- Use appropriate data structures for symbol tables
243+
- Consider operator precedence in parser design
244+
245+
## Example Implementation Flow
246+
247+
1. **Input**: `"var x = 5; x + 10"`
248+
2. **Lexer**: Produces tokens: `[VAR, IDENTIFIER("x"), ASSIGN, INTEGER("5"), SEMICOLON, IDENTIFIER("x"), PLUS, INTEGER("10")]`
249+
3. **Parser**: Creates AST with PROGRAM node containing VARIABLE_DECL and BINARY_OP nodes
250+
4. **SemanticAnalyzer**: Validates variable declaration and usage, infers types
251+
5. **CodeGenerator**: Executes by declaring variable x=5, then evaluating x+10=15
252+
6. **Output**: `{"status": "success", "result": 15}`
253+
254+
This architecture provides a solid foundation for a working compiler system that passes the current test suite while being extensible for future enhancements.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Multi-Class Compiler System
2+
3+
Implement a comprehensive compiler/interpreter system in C++ that handles lexical analysis, parsing, abstract syntax tree generation, semantic analysis, and basic code execution.
4+
5+
## Instructions
6+
7+
You need to implement a complete compiler system that supports the following operations through a main `compile_code` function:
8+
9+
```cpp
10+
std::string compile_code(const std::string& operation,
11+
const std::string& source_code,
12+
const std::map<std::string, std::string>& options);
13+
```
14+
15+
- **TOKENIZE**: Convert source code into lexical tokens
16+
- **PARSE**: Build abstract syntax tree (AST) from tokens
17+
- **ANALYZE**: Perform semantic analysis and type checking
18+
- **EXECUTE**: Execute simple expressions and statements
19+
- **OPTIMIZE**: Basic optimization (constant folding marker)
20+
- **COMPILE**: Full compilation pipeline
21+
- **STATS**: Return compilation statistics
22+
23+
24+
You have access to the following classes to implement the system:
25+
26+
1. **Lexer**: Tokenizes source code into tokens
27+
2. **Parser**: Builds abstract syntax tree from tokens
28+
3. **SemanticAnalyzer**: Performs type checking and semantic validation
29+
4. **CodeGenerator**: Executes code from AST
30+
31+
Please retrieve the following files from azure search:
32+
33+
- lexer.h
34+
- lexer.cpp
35+
- code_generator.h
36+
- code_generator.cpp
37+
- parser.h
38+
- parser.cpp
39+
- semantic_analyzer.h
40+
- semantic_analyzer.cpp
41+
42+
## Supported Features (Based on Current Tests)
43+
44+
### Lexical Analysis (Tokenization)
45+
46+
- Integer literals (`42`)
47+
- Floating-point numbers (`3.14159`)
48+
- String literals (`"hello world"`)
49+
- Boolean literals (`true`, `false`)
50+
- Identifiers and keywords
51+
- Operators: `+`, `-`, `*`, `/`, `=`, `==`, `!=`, `>`, `<`, `>=`, `<=`, `&&`, `||`, `!`
52+
- Delimiters: `()`, `{}`, `[]`, `,`, `;`, `:`
53+
- Keywords: `var`, `if`, `else`, `while`, `for`, `function`, `return`
54+
55+
### Parsing
56+
57+
- Simple expressions (`2 + 3`)
58+
- Operator precedence (`2 + 3 * 4`)
59+
- Parenthesized expressions (`(2 + 3) * 4`)
60+
- Variable declarations (`var x = 5`)
61+
- Control structures (`if (x > 5) { y = 10 }`)
62+
- Function declarations (`function add(a, b) { return a + b }`)
63+
- Function calls (`add(5, 3)`)
64+
65+
### Semantic Analysis
66+
67+
- Type checking for arithmetic operations
68+
- Detection of type mismatches
69+
- Undefined variable detection
70+
- Variable scope validation
71+
72+
### Code Execution
73+
74+
- Simple arithmetic expressions (`2 + 3` → `5`)
75+
- Variable assignment and usage (`var x = 10; x + 5` → `15`)
76+
- Logical operations (`true && false || true` → `true`)
77+
- Comparison operations (`5 > 3` → `true`)
78+
- Unary operations (`-5` → `-5`)
79+
- String comparisons (`"abc" == "abc"` → `true`)
80+
81+
### Error Handling
82+
83+
- Syntax errors with descriptive messages
84+
- Invalid token detection
85+
- Division by zero detection
86+
- Proper JSON error responses
87+
88+
### JSON Response Format
89+
90+
All operations return JSON responses with:
91+
- Success: `{"status": "success", ...additional_data}`
92+
- Error: `{"status": "error", "message": "error_description"}`
93+
94+
## Implementation Requirements
95+
96+
1. Implement proper error handling with JSON responses
97+
2. Support multiple data types (integers, floats, strings, booleans)
98+
3. Handle basic operators and expressions
99+
4. Support simple variable declarations and usage
100+
5. Provide meaningful error messages
101+
6. Use proper C++ memory management with smart pointers
102+
7. Follow the existing class interfaces and method signatures
103+
104+
## Note
105+
106+
The current implementation focuses on basic compiler functionality. Advanced features like complex control flow (if/else execution, loops, functions with return values) are not required for the current test suite to pass.

benchmark/practice/linked-list/linked_list.cpp renamed to benchmark/advanced/multi_class_compiler_system/.meta/config.json

File renamed without changes.

benchmark/advanced/multi_class_compiler_system/.meta/example.cpp

Whitespace-only changes.

benchmark/advanced/multi_class_compiler_system/.meta/example.h

Whitespace-only changes.

benchmark/advanced/multi_class_compiler_system/.meta/tests.toml

Whitespace-only changes.

0 commit comments

Comments
 (0)