Skip to content

Commit

Permalink
add: wiki docs
Browse files Browse the repository at this point in the history
  • Loading branch information
zestones committed Feb 10, 2025
1 parent e05bf2d commit 1b57c8e
Show file tree
Hide file tree
Showing 22 changed files with 1,373 additions and 225 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,4 @@ example/compilation/.out/actual_outputs/
!example/compilation/.out/expected_outputs/**/
!example/compilation/.out/expected_outputs/*.out

log.txt
argoc
argov
log.txt
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ BIN_DIR = $(SRC_DIR)/bin
LEXER_DIR = $(SRC_DIR)/lexer
PARSER_DIR = $(SRC_DIR)/parser
INCLUDE_DIRS = -I$(PARSER_DIR)
BIN = bin

COMPILER_RULES = lexer parser
INTERPRETER_RULES = vm_lexer vm_parser
Expand Down Expand Up @@ -71,7 +72,7 @@ docs:
compiler: argoc

argoc: $(COMPILER_RULES) $(ALL_OBJ) $(COMPILER_PARSER_GEN_H)
$(CC) $(CFLAGS) $(BIN_DIR)/lex.yy.c $(BIN_DIR)/y.tab.c $(ALL_OBJ) $(INCLUDE_DIRS) -o $@
$(CC) $(CFLAGS) $(BIN_DIR)/lex.yy.c $(BIN_DIR)/y.tab.c $(ALL_OBJ) $(INCLUDE_DIRS) -o $(BIN)/$@

# Rule to compile .c files into .o object files for the compiler
%.o: %.c $(COMPILER_PARSER_GEN_H)
Expand All @@ -93,7 +94,7 @@ parser: $(SRC_DIR)/parser/grammar.y
vm: argov

argov: $(INTERPRETER_RULES) $(ALL_OBJ) $(INTERPRETER_PARSER_GEN_H)
$(CC) $(CFLAGS) $(BIN_DIR)/lex.yy.c $(BIN_DIR)/y.tab.c $(ALL_OBJ) $(INCLUDE_DIRS) -o $@
$(CC) $(CFLAGS) $(BIN_DIR)/lex.yy.c $(BIN_DIR)/y.tab.c $(ALL_OBJ) $(INCLUDE_DIRS) -o $(BIN)/$@

vm_lexer: $(SRC_DIR)/lexer/interpreter_lexer.l
$(LEX) -o $(BIN_DIR)/lex.yy.c $(SRC_DIR)/lexer/interpreter_lexer.l
Expand Down
320 changes: 100 additions & 220 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,244 +1,124 @@
<div align="center">
<img src="./extensions/custom-icons/icons/icon.png" alt="Argonaut">
<img src="./extensions/custom-icons/icons/icon.png" alt="Argonaut">

[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
[![Build Status](https://img.shields.io/github/actions/workflow/status/zestones/argonaut/build.yml?branch=main)](https://github.com/zestones/argonaut/actions)
[![Dependencies](https://img.shields.io/badge/dependencies-GCC%2C%20Flex%2C%20Bison-orange)](https://gcc.gnu.org/)

<p>Argonaut is a procedural programming language designed for educational purposes, featuring a compiler and virtual machine implemented in C. This project aims to provide a comprehensive understanding of how compilers and interpreters function.
</p>
</div>

---

This project is a compiler for the Argonaut programming language, which is a simple language designed for educational purposes. The compiler is implemented in C and uses the YACC parser generator to parse the language grammar. It generates an abstract syntax tree (AST) from the parsed program and interprets the AST using a virtual machine.

## Notes

- The project is still under development and is not yet complete.
- The project is structured into different components, each handling a specific part of the compilation process.
- A Makefile is provided to build the project and run the compiler.

## Folder Structure

```bash
/compiler
├── /example
│ ├── /compilation # Examples and test cases for the compilation process
│ │ ├── /errors # Compilation error examples
│ │ ├── /to-clean # Samples to validate and clean compilation phases
│ │ ├── /todo # Examples of incomplete or pending tasks
│ ├── /interpretation # Examples for the interpretation process
├── /lib # Common libraries
│ ├── colors.h # Color utilities for terminal output
│ ├── table_printer.h # Utilities for table printing
├── /report # Archive of development logs and templates
│ ├── Archive*.txt # Archived logs for project development
│ ├── template.txt # Template file for report creation
├── /src
│ ├── /ast
│ │ ├── ast.c # Abstract syntax tree (AST) creation and manipulation
│ │ ├── ast.h # Header file for AST data structures and functions
│ │ ├── lcrs.c # Left-Child Right-Sibling (LCRS) tree implementation
│ │ ├── lcrs.h # Header file for LCRS tree functions
│ │ ├── node_type.h # Header file for defining AST node types
│ │
│ ├── /bin
│ │ ├── lex.yy.c # Generated lexical analyzer (by flex)
│ │ ├── y.tab.c # Generated parser (by yacc)
│ │ ├── y.tab.h # Header file for parser definitions
│ │
│ ├── /data
│ │ ├── region_stack.c # Region stack management
│ │ ├── region_stack.h # Header file for region stack functions
│ │ ├── region_table.c # Region table implementation (region size, nesting, AST pointers)
│ │ ├── region_table.h # Header file for region table definitions
│ │
│ ├── /lexer
│ │ ├── interpreter_lexer.l # Lexical analyzer for the interpreter
│ │ ├── lexer.l # Lexical analysis (token generation and lexeme identification)
│ │
│ ├── /parser
│ │ ├── grammar.y # YACC grammar file
│ │ ├── interpreter_grammar.y # Grammar file for interpretation phase
│ │ ├── parser.c # Parser implementation (calls lexing functions and constructs AST)
│ │ ├── parser.h # Header file for parser declarations
│ │
│ ├── /semantic_analysis # Semantic analysis and validation phase
│ │ ├── /assignment_checks # Assignment validation utilities
│ │ ├── /condition_checks # Condition validation utilities
│ │ ├── /function_checks # Function and procedure validation utilities
│ │ ├── /scope_checks # Scope validation utilities
│ │ ├── /type_checks # Type validation utilities
│ │ ├── /variable_checks # Variable validation utilities
│ │ ├── validation_utils.h # Common utilities for semantic checks
│ │ ├── semantic_checks.h # Aggregator header for semantic checks
│ │
│ ├── /symbol_table
│ │ ├── declaration # Declaration table management
│ │ ├── hash # Hash table utilities for symbol storage
│ │ ├── lexeme # Lexeme table management
│ │ ├── representation # Type and structure representation table
│ │ ├── utility.c # Common utilities for symbol table
│ │ ├── utility.h # Header file for common utilities
│ │
│ ├── /table_management
│ │ ├── array_manager.c # Array-specific management utilities
│ │ ├── func_proc_manager.c # Function and procedure management utilities
│ │ ├── structure_manager.c # Structure-specific utilities
│ │ ├── variable_manager.c # Variable-specific utilities
│ │
│ ├── /type_system
│ │ ├── /format # Format utilities for messages and output
│ │ ├── /type_inference # Type inference for expressions and structures
│ │
│ └── /utils
│ ├── errors.c # Error handling utilities
│ ├── scope_tracker.c # Scope tracking utilities
│ ├── stack.c # Stack utilities
│ ├── utils.h # General utility macros and functions
├── /tests
│ ├── regression # Regression tests for validating components
├── Makefile # Main Makefile to build the project
├── README.md # Project documentation
└── requirements.txt # Project dependencies
## 🚀 Key Features

### Compiler Architecture

- **Three-phase compilation process**
```mermaid
graph LR
subgraph "Compiler Pipeline"
direction LR
Frontend["Frontend (Lexer/Parser)"] --> Middleend["Middleend (Semantic Analysis)"]
Middleend --> Backend["Backend (Code Generation)"]
end
Frontend -.-> SymbolTable["Symbol Tables"]
Middleend -.-> TypeSystem["Type System"]
Backend -.-> IR["Intermediate Representation"]
IR --> VM["Virtual Machine Execution"]
```

- Modular design with clean separation between:
- Frontend (Lexer/Parser)
- Middleend (Semantic Analysis)
- Backend (Code Generation)

## 📜 Language Overview

### Sample Program
```js
PROG fibonacci;

func int fib(int n) {
if n <= 1 {
return n;
}
return fib(n-1) + fib(n-2);
}

VAR x: int = 10;

MAIN {
print("Fibonacci sequence:");
for var i = 0; i < x; i++ {
print(fib(i));
}
}
```

## Key Components
## 🛠️ Getting Started

1. **`/example`**: Test cases and example programs for both compilation and interpretation phases.
2. **`/lib`**: Helper libraries for enhanced output and table visualization.
3. **`/src`**: Core implementation files, including lexer, parser, semantic analysis, AST, and symbol table.
4. **`/tests`**: Regression tests to ensure the correctness of implemented features.
5. **Makefile**: Build and automation for the compiler and virtual machine.
### Prerequisites

---
Before you begin, ensure you have the following installed:

## Error Messages
- **C Compiler**: gcc
- **Lexical Analyzer**: flex
- **Parser Generator**: bison
- **Documentation Generator**: doxygen (optional)
- **Debugger**: gdb (optional)

- **Title Line**: Error details with a reference to the source code line.
- **Context**: Highlights the issue and its surrounding code snippet.
- **Advice**: Provides suggestions or steps to resolve the error.
### Installation

## AST Node Format in Intermediate Code
```bash
git clone https://github.com/zestones/argonaut.git
cd argonaut

```text
Node(<type>, <lex-index>, <decl-index>) [Child: <child>] [Sibling: <sibling>]
# Build compiler and VM
make
```

This format ensures consistency when interpreting or debugging the AST representation.

---

## Virtual Machine

Pour affecter correctement une valeur à un tableau dans votre pile d'exécution, vous devez calculer l'adresse exacte de l'emplacement où la valeur doit être stockée. Cette adresse est déterminée par les indices donnés lors de l'accès au tableau et les informations de dimensionnalité (comme les bornes des dimensions) dans la table des déclarations et la table de représentation. Voici comment procéder :

---

### **Mathematical Formulas for Address Computation**

#### **1. Address Calculation for Multi-Dimensional Arrays**

To compute the address of an element in a multi-dimensional array $A$:

$$
\text{Address}(A[I_1, I_2, \ldots, I_n]) = B + \sum_{i=1}^n \left( (I_i - L_i) \cdot S_i \right)
$$

Where:

- $B$: Base address of the array.
- $L_i$: Lower bound of the $i^{\text{th}}$ dimension.
- $I_i$: Index in the $i^{\text{th}}$ dimension.
- $S_i = \prod_{j=i+1}^n (U_j - L_j + 1)$: Stride of the $i^{\text{th}}$ dimension, with $S_n = 1$.
- $U_i$: Upper bound of the $i^{\text{th}}$ dimension.

---

#### **2. Address Calculation for Struct Fields**

For a field $f_k$ in a structure $S$:

$$
\text{Address}(S.f_k) = B + O_{f_k}
$$

Where:
## 📂 Project Structure

- $B$: Base address of the structure.
- $O_{f_k}$: Offset of field $f_k$, calculated as:

$$
O_{f_k} = \sum_{i=1}^{k-1} \text{Size of Field } f_i
$$

---

#### **3. Memory Size Calculations**

- **Array**:

$$
\text{Size of Array} = \prod_{i=1}^n (U_i - L_i + 1) \times \text{Element Size}
$$

- **Structure**:

$$
\text{Size of Struct} = \sum_{i=1}^N \text{Size of Field } f_i + \text{Alignment Padding}
$$

---

#### **4. Combined Address Calculations**

- **Nested Arrays in Structs**:

$$
\text{Address}(S.A[I_1, I_2, \ldots, I_n]) = B + O_A + \sum_{i=1}^n \left( (I_i - L_i) \cdot S_i \right)
$$

Where $O_A$: Offset of array $A$ within the structure $S$.

- **Structs in Arrays**:

$$
\text{Address}(A[I].f_k) = B + (I \cdot T_S) + O_{f_k}
$$
```bash
.
├── bin/ # Compiled binaries
├── docs/ # Generated documentation
├── examples/ # Sample programs
│ ├── hello.ag # Basic example
│ └── algorithms/ # Complex programs
├── include/ # Header files
├── lib/ # Utility libraries
├── src/ # Core implementation
│ ├── frontend/ # Lexer/Parser
│ ├── middleend/ # Semantic analysis
│ └── backend/ # Code generation
└── tests/ # Test suite
```

Where $T_S$: Total size of the struct, $O_{f_k}$: Offset of field $f_k$ within the struct.
## 🤝 Contributing

---
We welcome contributions from the community! To contribute:

### **Examples**
1. Fork the repository.
2. Create a new branch for your feature or bug fix.
3. Make your changes and commit them with descriptive messages.
4. Push your branch to your fork.
5. Submit a pull request to the main repository.

#### Example 1: Multi-Dimensional Array
## 📚 Documentation

- Array $A[3][4]$ with:
- $B = 1000$, $L_1 = 0, U_1 = 2$, $L_2 = 0, U_2 = 3$.
- Stride $S_1 = 4$, $S_2 = 1$.
For detailed documentation, including language syntax, compiler architecture, and virtual machine specifications, please visit our [Wiki](link-to-your-wiki).

Address of $A[1][2]$ :
## 📜 License

$$
\text{Address} = 1000 + (1 \cdot 4) + (2 \cdot 1) = 1006
$$
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

---

#### Example 2: Structs in Arrays

- Array of structs $S[5]$:
- Struct size $T_S = 12$, field $f$ offset $O_f = 4$, base $B = 2000$.

Address of $S[3].f$:

$$
\text{Address} = 2000 + (3 \cdot 12) + 4 = 2040
$$

### Notes:

- **Dynamic**: Refers to the function frame index of the caller.
- **Static**: Refers to the frame index of the parent.
<div align="center">
<p>Made with ❤️ by the Argonaut Contributors</p>
<a href="https://github.com/zestones/argonaut/graphs/contributors">
<img src="https://contrib.rocks/image?repo=zestones/argonaut" />
</a>
</div>
Binary file added bin/argoc
Binary file not shown.
Binary file added bin/argov
Binary file not shown.
Binary file modified extensions/custom-icons/icons/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
Loading

0 comments on commit 1b57c8e

Please sign in to comment.