Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add IO and Swap #702

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
111 changes: 111 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,117 @@ This means that you can carry on splitting as much as you like.
const var [[[getScore, setScore], setScore], setScore] = use(0)!
```

## IO


As any good programming language should, DreamBerd allows IO operations.
You can open file descriptors:
```java
const const in = IO(0)! // Open stdin
const const in2 = IO(5)! // Open file descriptor 5
```
For cross-platform purposes, you can also use `stdin`, `stdout` and `stderr` in place of 0, 1 and 2.

You can also provide file path:
```java
const const file = IO("exemple.txt")! // Will create the file if it doesn't exist
```

To open for reading, just provide the fd / file path, for writing provide a negative parameter.

```java
const const in = IO(5)! // Open for reading
const const out = IO(-1)! // Open for writing

const const stdinOut = IO(-0)! // Open stdin for writing

//Also works for file paths !
const const file = IO(-"exemple.txt")!
```

To read/write from an IO stream, just provide the number of words to read/write as parameter, and the value in the case of writing.
Reading return an array of bytes.
**Note:** In DreamBerd, words are 3 bytes long.

```java
const const in = IO("exemple.txt")!
const const out = IO(-"exemple2.txt")!

const const foo = in(3)! // Read 3 words (9 bytes)

in(4/3)? // Read 4/3 words (4 bytes) and print them
out(2, foo)! // Write the first 2 words (6 bytes) of foo to the "exemple2.txt" file

in(Infinity)? // Read whole file and print it
```

You can handle the closing with variable lifetime.
```java
const const file<1> = IO("exemple.txt")!
print(file(5))!
print(file(4))! // Throw exception as the stream "file" is closed
```

You can also delete a file with the `delete` statement.
```java
delete IO("exemple.txt")!
```

## Swap
To avoid the use of temporary variables, you can use the swap statement.
```java
var var foo = 5!
var var bar = 6!

swap foo bar!

print(foo)! // 6
print(bar)! // 5
```
It can also be used for functions.
```java
var var foo = 5!
funct bar() => {
return 6!
}

swap foo bar!

print(bar)! // 5
print(foo())! // 6
```

Even keywords are swappable.
```java
swap fn if!

if foo() => {
print("DreamBerd is great !")!
}

fn (5 === 5) {
print("Neat")!
}

// Even swap can be swap
swap swap foo!

swap() // Prints "DreamBerd is great !"
foo fn if! // Reswap fn and if
```

Everything is swappable in DreamBerd, even the language !
```python
var var foo = 5!

swap DreamBerd Python

for i in range(foo):
print("Welcome to Python") # Will print "Welcome to Python" 5 times
```
**Note**: in the program above, language cannot be swapped back to DreamBerd as Python cannot swap languages
**Note**: swapping to a language that require an entry point (such as the main function in C) is undefined behavior

## AI

DreamBerd features AEMI, which stands for Automatic-Exclamation-Mark-Insertion. If you forget to end a statement with an exclamation mark, DreamBerd will helpfully insert one for you!
Expand Down