Skip to content

Commit

Permalink
Merge pull request #180 from KennyOliver/issue-179
Browse files Browse the repository at this point in the history
Issue 179: Implement `try`-`rescue`-`finish` (try-catch)
  • Loading branch information
KennyOliver authored Jan 5, 2025
2 parents 8692dfc + 17204d9 commit e580456
Show file tree
Hide file tree
Showing 26 changed files with 1,769 additions and 894 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ $ cd src
$ make
```

> [!Note]
> [!Warning]
>
> Unless you move `flavor` to `/usr/local/bin/`,
> you'll have to use `./flavor` for commands with relative file paths.
Expand Down Expand Up @@ -160,7 +160,9 @@ $ flavor recipe.flv --debug # Debug mode
$ flavor --about # About FlavorLang
```

The `--debug` flag is really useful for understanding how FlavorLang is executing (tokenizing, parsing, and interpreting) your file.
> [!Note]
>
> The `--debug` flag is really useful for understanding how FlavorLang is executing (tokenizing, parsing, and interpreting) your file.
---

Expand Down
50 changes: 25 additions & 25 deletions docs/language_design.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@ This `docs/` page details the core design of FlavorLang's syntax, the various da

## Syntax Keywords

| Keyword | Usage | Description | Implemented? |
| --------- | ---------------------------- | ------------------------------------------------------------------------------------------- | ------------ |
| `let` | Define variables | Declares and initializes variables. ||
| `const` | Define constants | Declares and initializes constants. ||
| `if` | Conditional logic | Executes code only if a condition is true. ||
| `elif` | Conditional logic fallback | Executes only if a prior `if` condition is false. ||
| `else` | Conditional fallback | Executes code if any prior `if`/`is` conditions are false. ||
| `for` | For-loop | Iterates over a range or sequence, executing a block of code for each step. ||
| `in` | Range declaration | Specifies the range or sequence to iterate over. ||
| `by` | Optional step specifier | Defines the step interval for iteration; defaults to `1`/`-1` (range dependent) if omitted. ||
| `while` | While-loop | Repeatedly runs code while a condition is true. ||
| `check` | Switch-case equivalent | Matches a value to multiple cases. ||
| `is` | Case clause | Defines a case inside `check`. ||
| `break` | Exit control flow | Stops execution of further cases in `check` and exits the current flow. ||
| `create` | Define a function | Creates a reusable block of logic. ||
| `deliver` | Return statement | Returns a value and stops function execution. ||
| `try` | Try block | Executes code that might fail. ||
| `crumbs` | Catch block | Handles errors during execution. | |
| `burn` | Force exit or raise an error | Stops execution immediately with a message. | |
| `serve` | Print or output | Outputs a value or message immediately. | |
| `sample` | Input from console | Reads user input. | |
| `plate` | Write to file | Writes data to a file. | |
| `garnish` | Append to file | Appends data to a file. | |
| `taste` | Read from file | Reads data from a file. | |
| `recipe` | Import `.flv` file | Imports logic from another `.flv` file. | |
| Keyword | Usage | Description |
| --------- | ---------------------------- | ------------------------------------------------------------------------------------------- |
| `let` | Define variables | Declares and initializes variables. |
| `const` | Define constants | Declares and initializes constants. |
| `if` | Conditional logic | Executes code only if a condition is true. |
| `elif` | Conditional logic fallback | Executes only if a prior `if` condition is false. |
| `else` | Conditional fallback | Executes code if any prior `if`/`is` conditions are false. |
| `for` | For-loop | Iterates over a range or sequence, executing a block of code for each step. |
| `in` | Range declaration | Specifies the range or sequence to iterate over. |
| `by` | Optional step specifier | Defines the step interval for iteration; defaults to `1`/`-1` (range dependent) if omitted. |
| `while` | While-loop | Repeatedly runs code while a condition is true. |
| `check` | Switch-case equivalent | Matches a value to multiple cases. |
| `is` | Case clause | Defines a case inside `check`. |
| `break` | Exit control flow | Stops execution of further cases in `check` and exits the current flow. |
| `create` | Define a function | Creates a reusable block of logic. |
| `deliver` | Return statement | Returns a value and stops function execution. |
| `try` | Try block | Executes code that might fail. |
| `rescue` | Catch block | Handles errors during execution. |
| `finish` | Finally block | Optional cleanup & always runs. |
| `burn` | Force exit or raise an error | Stops execution immediately with a message. |
| `serve` | Print or output | Outputs a value or message immediately. |
| `sample` | Input from console | Reads user input. |
| `plate` | Write to file | Writes data to a file. |
| `garnish` | Append to file | Appends data to a file. |
| `taste` | Read from file | Reads data from a file. |

---

Expand Down
10 changes: 10 additions & 0 deletions docs/syntax_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,21 @@ Use `try` and `rescue` to handle errors.

```py
try {
serve("This will run!");
burn("This recipe failed!");
serve("This won't run!");
} rescue {
serve("Caught an error: Recipe needs improvement.");
}

try {
int("abc");
serve("This won't run!");
} rescue {
serve("Runtime error: Can't cast string `abc` to int.");
} finish {
serve("This always executes!");
}
```

### 8. 🔎 Switch-Case Logic <a id="8"></a>
Expand Down
6 changes: 3 additions & 3 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Compiler and Flags
CC = gcc
CFLAGS = -Wall -Wextra -g
LDFLAGS = -lm
CC = clang
CFLAGS = -fsanitize=address -fsanitize=undefined -g -Wall -Wextra -pedantic
LDFLAGS = -fsanitize=address -fsanitize=undefined

# Directories
SRC_DIRS = . shared lexer parser interpreter debug
Expand Down
2 changes: 1 addition & 1 deletion src/debug/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ void debug_print_int(const char *format, ...) {

debug_print(INTERPRETER, "%s", new_format);
}
}
}
Loading

0 comments on commit e580456

Please sign in to comment.