Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilturek committed Jul 12, 2024
1 parent 5a3e916 commit 96bb0f1
Showing 1 changed file with 48 additions and 26 deletions.
74 changes: 48 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,43 @@ A Monkey programming language interpreter from the book [Writing An Interpreter
## Table of Contents

- [Standard Types](#standard-types)
- [Integer](#integer)
- [Boolean](#boolean)
- [Integer](#integer)
- [Boolean](#boolean)
- [Operators](#operators)
- [Basic Arithmetic Operators](#basic-arithemtic-operators)
- [Comparison Operators](#comparison-operators)
- [Operator Precedence](#operator-precedence)
- [Grouped Expressions](#grouped-expressions)
- [Basic Arithmetic Operators](#basic-arithemtic-operators)
- [Comparison Operators](#comparison-operators)
- [Operator Precedence](#operator-precedence)
- [Grouped Expressions](#grouped-expressions)
- [Flow Control](#flow-control)
- [If Expressions](#if-expressions)
- [If Expressions](#if-expressions)
- [Functions](#functions)
- [Defining Functions](#defining-functions)
- [Anonymous Functions](#anonymous-functions)
- [First-Class Functions](#first-class-functions)
- [Defining Functions](#defining-functions)
- [Anonymous Functions](#anonymous-functions)
- [First-Class Functions](#first-class-functions)

## Standard Types

Monkey supports several basic data types.

### Integer

```
```javascript
let x = 12345;
```

### Boolean

```
```javascript
let x = true;
let y = false;
```

### String

```javascript
let x = "Hello World";
```

## Operators

Monkey includes a variety of operators for performing arithmetic and comparisons.
Expand All @@ -48,15 +54,26 @@ Monkey includes a variety of operators for performing arithmetic and comparisons

You can use the following arithmetic operators: `+`, `-`, `*`, and `/`.

```
```javascript
let x = 1 + 2 - 3 * 4;
```

Strings can be concatenated using the `+` operator. `==` and `!=` can be used to
compare two strings.

```javascript
let x = "Hello" + " " + "World";

if (x == "Hello World") {
// ...
}
```

### Comparison Operators

Monkey supports comparison operators such as `>`, `<`, `==`, and `!=`.

```
```javascript
let x = 5 > 5;
let y = 5 < 5;
let z = 5 == 5;
Expand All @@ -80,19 +97,20 @@ The following table shows the operator precedence in Monkey, from lowest to high

You can use parentheses to influence the order of executing arithmetic operations.

```
```javascript
let x = (2 / (5 + 5));
```

## Flow Control

### If Expressions

Monkey supports `if` expressions for flow control. An `if` expression evaluates a condition and executes the corresponding block of code.
Monkey supports `if` expressions for flow control. An `if` expression evaluates
a condition and executes the corresponding block of code.

The syntax for an `if` expression is as follows:

```
```javascript
if (condition) {
// block of code
} else {
Expand All @@ -102,11 +120,12 @@ if (condition) {

- The `else` block is optional.
- Each block can contain multiple expressions or statements.
- The value of the `if` expression is the value of the last expression in the executed block.
- The value of the `if` expression is the value of the last expression in the
executed block.

#### Example

```
```javascript
let x = 10;
let y = 20;

Expand All @@ -121,21 +140,23 @@ In this example, max will be set to `20` because `y` is greater than `x`.

## Functions

Monkey supports functions, which can be defined, assigned to variables, called, and passed as arguments to other functions.
Monkey supports functions, which can be defined, assigned to variables, called,
and passed as arguments to other functions.

### Defining Functions

Functions can be defined using the `fn` keyword:

```
```javascript
let add = fn(x, y) {
return x + y;
};
```

The `return` statement is optional. If omitted, the last expression in the function block will be returned:
The `return` statement is optional. If omitted, the last expression in the
function block will be returned:

```
```javascript
let add = fn(x, y) {
x + y;
};
Expand All @@ -145,17 +166,18 @@ let add = fn(x, y) {

Functions can be called immediately without being assigned to a variable:

```
```javascript
let three = fn(x, y) {
x + y;
}(1, 2);
```

### First-Class Functions

Functions in Monkey are first-class objects, meaning they can be passed as arguments to other functions:
Functions in Monkey are first-class objects, meaning they can be passed as
arguments to other functions:

```
```javascript
let sayHello = fn() {
print("hello");
};
Expand Down

0 comments on commit 96bb0f1

Please sign in to comment.