Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e0dc0c8
[refactoring] Adding a new definition for environment.
rbonifacio May 15, 2025
7c9b722
[refactoring] Adapting the type checker to the new environment
rbonifacio May 16, 2025
15ee2ea
[feature] Introduce syntax and parser rules for simple 'for' statements.
rbonifacio May 27, 2025
276f3f3
[refactoring] Refactoring the parser code.
rbonifacio May 27, 2025
cfcc330
[refactoring] Split the parser into different sub-parsers (e.g., pars…
rbonifacio Jun 3, 2025
1481bcd
[refactoring] Split the 'parser_type'
rbonifacio Jun 9, 2025
dc2d380
[refactoring] Add a definition for 'FormalArgument'
rbonifacio Jun 10, 2025
de434aa
[refactoring] Move function definition
rbonifacio Jun 10, 2025
3f6409a
[refactoring] Split the parsers for 'stmt' and 'expr'
rbonifacio Jun 10, 2025
e3c460c
[refactoring] Better structure for the 'parser' module
rbonifacio Jun 10, 2025
a413ad6
[feature] Add documentation for the parser module.
rbonifacio Jun 10, 2025
5f5b129
[unit-testing] Mark some test as 'ignored'
rbonifacio Jun 10, 2025
e6c4d68
[fix] New implementation for the 'lookup_variable' function
rbonifacio Jun 10, 2025
5abb696
[fix] Fix the type checker for IfThenElse
rbonifacio Jun 10, 2025
b4d73d0
[feature] Implementation of the type checker for variables
rbonifacio Jun 10, 2025
ed09227
[fix] Function definitions must have a type.
rbonifacio Jun 10, 2025
14beac4
[fix] Function arguments are only a vector of FormalArgument, instead…
rbonifacio Jun 10, 2025
7c401f5
[feature] Definition of 'ListValue'
rbonifacio Jun 10, 2025
7fff304
[docs] Adding documentation for the 'environment' and 'type_checker' …
rbonifacio Jun 10, 2025
0404f05
[refactoring] Format fix.
rbonifacio Jun 10, 2025
6fcd567
[feature] Type checker for the 'ForStmt'
rbonifacio Jun 10, 2025
a216242
[refactoring] centralize parser constants to improve maintainability
rbonifacio Jun 10, 2025
45e315d
[docs] Refactoring opportunity.
rbonifacio Jun 10, 2025
a41c4b0
[refactoring] Split the type checker module into a checker for expres…
rbonifacio Jun 15, 2025
a1afc7d
[refactoring] Enhance environment to support mutable and non-mutable …
rbonifacio Jun 16, 2025
fca8f06
[refactoring] Use of the correct environment to interpret R-Python.
rbonifacio Jun 16, 2025
a3f1850
[refactoring] Removed the old environment definition.
rbonifacio Jun 16, 2025
6483888
[feature] Implementation of the interpreter for the 'For' statement.
rbonifacio Jun 17, 2025
3e9ef51
[refactoring] Move eval test cases to 'expression_eval.rs'
rbonifacio Jun 17, 2025
129fdd8
[unit-testing] Add unit tests for arithmetic expressions.
rbonifacio Jun 17, 2025
da1f8f6
[unit-testing]: implement NEQ operator and a comprehensive relational…
rbonifacio Jun 17, 2025
b57cc53
[unit-testing] Add comprehensive boolean expression tests with 32 tes…
rbonifacio Jun 17, 2025
11045e5
[refactoring] Rename and reorganize expression evaluation functions
rbonifacio Jun 17, 2025
859da5a
[refactoring]: clean up expression evaluator / statement execution AP…
rbonifacio Jun 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ RPython é um projeto educacional que visa:
- Explorar conceitos fundamentais de técnicas de programação
- Criar uma linguagem com sintaxe amigável similar ao Python

## 📚 Documentação

Para uma compreensão mais profunda dos componentes do projeto, consulte nossa documentação técnica:

- **[Environment Module](docs/environment.md)** - Sistema de gerenciamento de escopo lexical com tabela de símbolos para variáveis e funções. Implementa uma pilha de escopos com resolução adequada da cadeia de escopo.

- **[Parser Component](docs/parser.md)** - Componente de análise sintática que transforma código fonte em Árvore de Sintaxe Abstrata (AST). Usa a biblioteca `nom` e segue um design modular com funcionalidades especializadas para expressões, tipos e declarações.

- **[Type Checker Module](docs/type_checker.md)** - Sistema de verificação de tipos estática que analisa expressões e declarações para garantir segurança de tipos em tempo de compilação. Implementa regras de tipagem bem definidas para todos os construtos da linguagem. *(Em desenvolvimento)*

## 🤝 Contribuindo

Adoraríamos contar com sua contribuição! Por favor, leia nossos guias de contribuição:
Expand Down
189 changes: 189 additions & 0 deletions docs/PARSER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
# Parser Component Documentation

## Overview

The parser component is responsible for transforming source code text into an Abstract Syntax Tree (AST). It is implemented using the `nom` parser combinator library and follows a modular design pattern, breaking down the parsing logic into several specialized modules.

## Architecture

The parser is organized into the following modules:

- `parser.rs`: The main entry point that coordinates the parsing process
- `parser_common.rs`: Common parsing utilities and shared functions
- `parser_expr.rs`: Expression parsing functionality
- `parser_type.rs`: Type system parsing
- `parser_stmt.rs`: Statement and control flow parsing

### Module Responsibilities and Public Interface

#### 1. parser.rs
The main parser module that provides the entry point for parsing complete programs:
```rust
pub fn parse(input: &str) -> IResult<&str, Vec<Statement>>
```

#### 2. parser_common.rs
Common parsing utilities used across other modules:
```rust
pub fn is_string_char(c: char) -> bool
pub fn separator<'a>(sep: &'static str) -> impl FnMut(&'a str) -> IResult<&'a str, &'a str>
pub fn keyword<'a>(kw: &'static str) -> impl FnMut(&'a str) -> IResult<&'a str, &'a str>
pub fn identifier(input: &str) -> IResult<&str, &str>
```

#### 3. parser_expr.rs
Expression parsing functionality:
```rust
pub fn parse_expression(input: &str) -> IResult<&str, Expression>
pub fn parse_actual_arguments(input: &str) -> IResult<&str, Vec<Expression>>
```

#### 4. parser_type.rs
Type system parsing:
```rust
pub fn parse_type(input: &str) -> IResult<&str, Type>
```

#### 5. parser_stmt.rs
Statement and control flow parsing:
```rust
pub fn parse_statement(input: &str) -> IResult<&str, Statement>
```

## Parser Features

### Statement Parsing
The parser supports various types of statements:
- Variable declarations and assignments
- Control flow (if-else, while, for)
- Function definitions
- Assert statements
- ADT (Algebraic Data Type) declarations

### Expression Parsing
Handles different types of expressions:
- Arithmetic expressions
- Boolean expressions
- Function calls
- Variables
- Literals (numbers, strings, booleans)
- ADT constructors and pattern matching

### Type System
Supports a rich type system including:
- Basic types (Int, Real, Boolean, String, Unit, Any)
- Complex types (List, Tuple, Maybe)
- ADT declarations
- Function types

## nom Parser Combinators

The parser extensively uses the `nom` parser combinator library. Here are the key combinators used:

### Basic Combinators
- `tag`: Matches exact string patterns
- `char`: Matches single characters
- `digit1`: Matches one or more digits
- `alpha1`: Matches one or more alphabetic characters
- `space0/space1`: Matches zero or more/one or more whitespace characters

### Sequence Combinators
- `tuple`: Combines multiple parsers in sequence
- `preceded`: Matches a prefix followed by a value
- `terminated`: Matches a value followed by a suffix
- `delimited`: Matches a value between two delimiters

### Branch Combinators
- `alt`: Tries multiple parsers in order
- `map`: Transforms the output of a parser
- `opt`: Makes a parser optional

### Multi Combinators
- `many0/many1`: Matches zero or more/one or more occurrences
- `separated_list0`: Matches items separated by a delimiter

## Example Usage

Here's an example of how the parser handles a simple assignment statement:

```python
x = 42
```

This is parsed using the following combinators:
```rust
fn parse_assignment_statement(input: &str) -> IResult<&str, Statement> {
map(
tuple((
preceded(multispace0, identifier),
preceded(multispace0, tag("=")),
preceded(multispace0, parse_expression),
)),
|(var, _, expr)| Statement::Assignment(var.to_string(), Box::new(expr)),
)(input)
}
```

## AST Structure

The parser produces an Abstract Syntax Tree (AST) with the following main types:

### Statements
```rust
pub enum Statement {
VarDeclaration(Name),
ValDeclaration(Name),
Assignment(Name, Box<Expression>),
IfThenElse(Box<Expression>, Box<Statement>, Option<Box<Statement>>),
While(Box<Expression>, Box<Statement>),
For(Name, Box<Expression>, Box<Statement>),
Block(Vec<Statement>),
Assert(Box<Expression>, Box<Expression>),
FuncDef(Function),
Return(Box<Expression>),
ADTDeclaration(Name, Vec<ValueConstructor>),
// ... other variants
}
```

### Types
```rust
pub enum Type {
TInteger,
TReal,
TBool,
TString,
TList(Box<Type>),
TTuple(Vec<Type>),
TMaybe(Box<Type>),
TResult(Box<Type>, Box<Type>),
TFunction(Box<Option<Type>>, Vec<Type>),
// ... other variants
}
```

## Error Handling

The parser implements error handling through the `nom` error system:
```rust
pub enum ParseError {
IndentationError(usize),
UnexpectedToken(String),
InvalidExpression(String),
}
```

## Testing

The parser includes a comprehensive test suite in `tests/parser_tests.rs` that verifies:
- Simple assignments
- Complex expressions
- Control flow structures
- Type annotations
- Complete programs
- Error handling
- Whitespace handling


> **Documentation Generation Note**
> This documentation was automatically generated by Claude (Anthropic), an AI assistant, through analysis of the codebase. While the content accurately reflects the implementation, it should be reviewed and maintained by the development team. Last generated: June 2025.
135 changes: 135 additions & 0 deletions docs/environment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Environment Module Documentation

The environment module provides a lexically-scoped symbol table implementation for the R-Python language. It supports both variable and function bindings, with proper scope chain resolution.

## Overview

The module implements two main structures:
- `Scope<A>`: Represents a single scope with its variable and function bindings
- `Environment<A>`: Manages a stack of scopes with a global scope at the bottom

The generic parameter `A` allows the environment to be used for different purposes:
- Type checking: `Environment<Type>`
- Interpretation: `Environment<Expression>`

## Structures

### Scope<A>

A single scope containing mappings for variables and functions.

```rust
pub struct Scope<A> {
pub variables: HashMap<Name, A>,
pub functions: HashMap<Name, Function>,
}
```

#### Methods

- `new() -> Scope<A>`: Creates a new empty scope
- `map_variable(var: Name, value: A)`: Binds a variable in the current scope
- `map_function(function: Function)`: Binds a function in the current scope
- `lookup_var(var: &Name) -> Option<&A>`: Looks up a variable in this scope
- `lookup_function(name: &Name) -> Option<&Function>`: Looks up a function in this scope

### Environment<A>

Manages a stack of scopes with lexical scoping rules.

```rust
pub struct Environment<A> {
pub globals: Scope<A>,
pub stack: LinkedList<Scope<A>>,
}
```

#### Methods

- `new() -> Environment<A>`: Creates a new environment with empty global scope
- `map_variable(var: Name, value: A)`: Maps a variable in the current scope
- `map_function(function: Function)`: Maps a function in the current scope
- `lookup(var: &Name) -> Option<&A>`: Looks up a variable through the scope chain
- `lookup_function(name: &Name) -> Option<&Function>`: Looks up a function through the scope chain
- `push()`: Creates a new scope at the top of the stack
- `pop()`: Removes the topmost scope
- `scoped_function() -> bool`: Checks if we're in a function scope

## Scoping Rules

1. **Variable Resolution**:
- First checks the local scopes from innermost to outermost
- Falls back to global scope if not found in any local scope
- Returns None if the variable is not found anywhere

2. **Function Resolution**:
- Follows the same rules as variable resolution
- Functions can be defined in any scope
- Inner functions can shadow outer functions

3. **Scope Management**:
- New scopes are pushed when entering a function or block
- Scopes are popped when exiting their block
- Global scope always remains at the bottom

## Usage Examples

### Type Checking

```rust
let mut type_env: Environment<Type> = Environment::new();

// In global scope
type_env.map_variable("x".to_string(), Type::TInteger);

// In function scope
type_env.push();
type_env.map_variable("y".to_string(), Type::TReal);
```

### Interpretation

```rust
let mut runtime_env: Environment<Expression> = Environment::new();

// In global scope
runtime_env.map_variable("x".to_string(), Expression::CInt(42));

// In function scope
runtime_env.push();
runtime_env.map_variable("y".to_string(), Expression::CReal(3.14));
```

### Nested Scopes

```rust
let mut env: Environment<i32> = Environment::new();

// Global scope
env.map_variable("x".to_string(), 1);

// Outer function scope
env.push();
env.map_variable("y".to_string(), 2);

// Inner function scope
env.push();
env.map_variable("z".to_string(), 3);

// Variables from all scopes are accessible
assert!(env.lookup(&"x".to_string()).is_some()); // from global
assert!(env.lookup(&"y".to_string()).is_some()); // from outer
assert!(env.lookup(&"z".to_string()).is_some()); // from inner

// Pop scopes to clean up
env.pop(); // removes inner scope
env.pop(); // removes outer scope
```

## Implementation Notes

1. The environment uses a `LinkedList` for the scope stack to efficiently push/pop scopes
2. All lookups traverse the entire scope chain for proper lexical scoping
3. The generic parameter `A` must implement `Clone` for the environment to work
4. Functions are treated similarly to variables but stored in a separate map
5. The global scope is always accessible, regardless of the current scope depth
Loading