Skip to content

Commit

Permalink
refactor(code-samples): 📝 Revert non-pony code samples
Browse files Browse the repository at this point in the history
Co-authored-by: jemc <joe.eli.mac@gmail.com>

As requested in #544 (comment)
  • Loading branch information
shaedrich committed May 15, 2024
1 parent 45180f5 commit db1eb11
Show file tree
Hide file tree
Showing 37 changed files with 170 additions and 195 deletions.
1 change: 0 additions & 1 deletion code-samples/appendices-compiler-args-ponyc.sh

This file was deleted.

2 changes: 0 additions & 2 deletions code-samples/appendices-compiler-args-stdlib-docs.sh

This file was deleted.

39 changes: 0 additions & 39 deletions code-samples/appendices-serialization-custom-serialization.c

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions code-samples/c-abi-jump-consistent-hashing-header.c

This file was deleted.

17 changes: 0 additions & 17 deletions code-samples/c-abi-jump-consistent-hashing-implementation.c

This file was deleted.

16 changes: 0 additions & 16 deletions code-samples/c-ffi-callbacks-sqlite3-callback.c

This file was deleted.

4 changes: 0 additions & 4 deletions code-samples/c-ffi-callbacks-struct-with-function-pointers.c

This file was deleted.

18 changes: 0 additions & 18 deletions code-samples/calling-c-ffi-functions-raising-errors.c

This file was deleted.

7 changes: 0 additions & 7 deletions code-samples/calling-c-generic-list.c

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

7 changes: 0 additions & 7 deletions code-samples/hello-world-compile.sh

This file was deleted.

2 changes: 0 additions & 2 deletions code-samples/hello-world-create-directory.sh

This file was deleted.

2 changes: 0 additions & 2 deletions code-samples/hello-world-run.sh

This file was deleted.

1 change: 0 additions & 1 deletion code-samples/ponypath-unix-mac.sh

This file was deleted.

1 change: 0 additions & 1 deletion code-samples/ponypath-windows.sh

This file was deleted.

1 change: 0 additions & 1 deletion code-samples/reference-capabilities-file-open.c

This file was deleted.

1 change: 0 additions & 1 deletion code-samples/trust-boundary-safe-packages.sh

This file was deleted.

1 change: 0 additions & 1 deletion code-samples/what-you-need-compile-pony-other-directory.sh

This file was deleted.

1 change: 0 additions & 1 deletion code-samples/what-you-need-compile-pony.sh

This file was deleted.

1 change: 0 additions & 1 deletion code-samples/what-you-need-run-python-shebang.sh

This file was deleted.

1 change: 0 additions & 1 deletion code-samples/what-you-need-run-python.sh

This file was deleted.

5 changes: 3 additions & 2 deletions docs/appendices/compiler-args.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
`ponyc`, the compiler, is usually called in the project directory, where it finds the `.pony` files and its dependencies automatically. There it will create the binary based on the directory name. You can override this and tune the compilation with several options as described via `ponyc --help` and you can pass a separate source directory as an argument.

```bash
--8<-- "appendices-compiler-args-ponyc.sh"
ponyc [OPTIONS] <package directory>
```

The most useful options are `--debug`, `--path` or just `-p`, `--output` or just `-o` and `--docs` or `-g`.
Expand All @@ -19,7 +19,8 @@ The most useful options are `--debug`, `--path` or just `-p`, `--output` or just
Let's study the documentation of the builtin standard library:

```bash
--8<-- "appendices-compiler-args-stdlib-docs.sh"
pip install mkdocs
ponyc packages/stdlib --docs && cd stdlib-docs && mkdocs serve
```

And point your web browser to [127.0.0.1:8000](http://127.0.0.1:8000) serving a live-reloading local version of the docs.
Expand Down
32 changes: 29 additions & 3 deletions docs/appendices/error-messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ Suppose you wrote:
The error message would be:

```error
--8<-- "error-messages-left-side-must-be-something-that-can-be-assigned-to-error-message.txt"
Error:
main.pony:4:5: can't assign to a let or embed definition more than once
x = 12
^
Error:
main.pony:4:7: left side must be something that can be assigned to
x = 12
^
```

What happened is that you declared `x` as a constant, by writing `let x`, and then tried to assign a new value to it, 12. To fix the error, replace `let` with `var` or reconsider what value you want `x` to have.
Expand All @@ -37,7 +44,10 @@ Suppose you create a class with a mutable field and added a method to change the
The error message would be:

```error
--8<-- "error-messages-left-side-is-immutable-error-message.txt"
Error:
main.pony:4:11: left side is immutable
color = new_color
^
```

To understand this error message, you have to have some background. The field `color` is mutable since it is declared with `var`, but the method `dye` does not have an explicit receiver reference capability. The default receiver reference capability is `box`, which allows `dye` to be called on any mutable or immutable `Wombat`; the `box` reference capability says that the method may read from but not write to the receiver. As a result, it is illegal to attempt to modify the receiver in the method.
Expand All @@ -57,7 +67,23 @@ In this example, rather than trying to change the value of a field, the code cal
The problem is very similar to that of the last section, but the error message is significantly more complicated:

```error
--8<-- "error-messages-receiver-type-is-not-a-subtype-of-target-type-error-message.txt"
Error:
main.pony:4:16: receiver type is not a subtype of target type
colors.push(color)
^
Info:
main.pony:4:5: receiver type: this->Array[String val] ref (which becomes 'Array[String val] box' in this context)
colors.push(color)
^
/root/.local/share/ponyup/ponyc-release-0.58.0-x86_64-linux-musl/packages/builtin/array.pony:623:3: target type: Array[String val] ref^
fun ref push(value: A) =>
^
main.pony:2:15: Array[String val] box is not a subtype of Array[String val] ref^: box is not a subcap of ref^
let colors: Array[String] = Array[String]
^
main.pony:3:3: you are trying to change state in a box function; this would be possible in a ref function
fun add_stripe(color: String) =>
^
```

Once again, Pony is trying to be helpful. The first few lines describe the error, in general terms that only a programming language maven would like: an incompatibility between the receiver type and the target type. However, Pony provides more information: the lines immediately after "Info:" tell you what it believes the receiver type to be and the next few lines describe what it believes the target type to be. Finally, the last few lines describe in detail what the problem is.
Expand Down
4 changes: 2 additions & 2 deletions docs/appendices/ponypath.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Assuming you just placed new Pony code under a directory called `pony` in your h
Edit/add the `rc` file corresponding to your chosen shell (`echo $SHELL` will tell you what shell you are running). For example, if using bash, add the following to your `~/.bashrc`:

```bash
--8<-- "ponypath-unix-mac.sh"
export PONYPATH=$PONYPATH:$HOME/pony
```

(Then run `source ~/.bashrc` to add this variable to a running session. New terminal session will automatically source `~/.bashrc`.)
Expand All @@ -30,5 +30,5 @@ Edit/add the `rc` file corresponding to your chosen shell (`echo $SHELL` will te
You can also add to `PONYPATH` from the command prompt via:

```bash
--8<-- "ponypath-windows.sh"
setx PONYPATH %PONYPATH%;%USERPROFILE%\pony
```
40 changes: 39 additions & 1 deletion docs/appendices/serialisation.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,43 @@ Assume we have a Pony class with a field that is a pointer to a C string. We wou
```

```c
--8<-- "appendices-serialization-custom-serialization.c"
// custser.c

#include <stdlib.h>
#include <string.h>

extern char *get_string()
{
return "hello world\n";
}

extern size_t serialise_space(char *s)
{
// space for the size and the string (without the null)
return 4 + strlen(s);
}

extern void serialise(char *buff, char *s)
{
size_t sz = strlen(s);
unsigned char *ubuff = (unsigned char *) buff;
// write the size as a 32-bit big-endian integer
ubuff[0] = (sz >> 24) & 0xFF;
ubuff[1] = (sz >> 16) & 0xFF;
ubuff[2] = (sz >> 8) & 0xFF;
ubuff[3] = sz & 0xFF;

// copy the string
strncpy(buff + 4, s, sz);
}

extern char *deserialise(char *buff)
{
unsigned char *ubuff = (unsigned char *) buff;
size_t sz = (ubuff[0] << 24) + (ubuff[1] << 16) + (ubuff[2] << 8) + ubuff[3];
char *s = malloc(sizeof(char) * sz + 1);
memcpy(s, buff + 4, sz);
s[sz] = '\0';
return s;
}
```
Loading

0 comments on commit db1eb11

Please sign in to comment.