Skip to content

Commit

Permalink
Fix paths to engine
Browse files Browse the repository at this point in the history
  • Loading branch information
pedropark99 committed Sep 7, 2024
1 parent f3b05f4 commit c0a1a01
Show file tree
Hide file tree
Showing 43 changed files with 284 additions and 281 deletions.
112 changes: 24 additions & 88 deletions Chapters/01-base64.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ knitr::opts_chunk$set(
```



# Project 1 - Building a base64 encoder/decoder {#sec-base64}

As our first small project, I want to implement with you a base64 encoder/decoder in Zig.
Expand Down Expand Up @@ -389,61 +390,22 @@ byte in the output.

::: {#tbl-transf-6bit}

```{r}
#| echo: false
library(gt)
library(tibble)
table_data <- tibble(
"Number of bytes in the window" = c(3L, 3L, 3L, 3L,
2L, 2L, 2L, 2L,
1L, 1L, 1L, 1L),
"Byte index in the output" = c(0:3, 0:3, 0:3),
"In code" = c(
"`input[0] >> 2`",
"`((input[0] & 0x03) << 4) +`\n\n`(input[1] >> 4)`",
"`((input[1] & 0x0f) << 2) +`\n\n`(input[2] >> 6)`",
"`input[2] & 0x3f`",
"`input[0] >> 2`",
"`((input[0] & 0x03) << 4) +`\n\n`(input[1] >> 4)`",
"`((input[1] & 0x0f) << 2)`",
"`'='`",
"`input[0] >> 2`",
"`((input[0] & 0x03) << 4)`",
"`'='`",
"`'='`"
)
)
table <- table_data |>
gt() |>
fmt_markdown(columns = "In code") |>
cols_align(align="left") |>
# cols_width(
# `Number of bytes in the window` ~ px(150),
# `Byte index in the output` ~ px(150)
# ) |>
cols_label(
`Number of bytes in the window` = md("**Number of bytes in the window**"),
`Byte index in the output` = md("**Byte index in the output**"),
`In code` = md("**In code**")
)
if (knitr::is_html_output()) {
table |> gt::as_raw_html()
} else {
table |> gt::as_latex()
}
```

How the 6-bit transformation translates into code in different window settings.
| Number of bytes in the window | Byte index in the output | In code |
|-------------------------------|--------------------------|--------------------------------------------|
| 3 | 0 | input[0] >> 2 |
| 3 | 1 | ((input[0] & 0x03) << 4) + (input[1] >> 4) |
| 3 | 2 | ((input[1] & 0x0f) << 2) + (input[2] >> 6) |
| 3 | 3 | input[2] & 0x3f |
| 2 | 0 | input[0] >> 2 |
| 2 | 1 | ((input[0] & 0x03) << 4) + (input[1] >> 4) |
| 2 | 2 | ((input[1] & 0x0f) << 2) |
| 2 | 3 | '=' |
| 1 | 0 | input[0] >> 2 |
| 1 | 1 | ((input[0] & 0x03) << 4) |
| 1 | 2 | '=' |
| 1 | 3 | '=' |

: How the 6-bit transformation translates into code in different window settings.

:::

Expand Down Expand Up @@ -792,44 +754,18 @@ Notice that the output byte is the sequence `ABCDEFGH`, which is the original by

The @tbl-6bit-decode presents how the three steps described ealier translate into Zig code:

::: {#tbl-6bit-decode}

```{r}
#| echo: false
library(gt)
library(tibble)
table_data <- tibble(
`Byte index in the output` = c(0:2),
`In code` = c(
"`(buf[0] << 2) + (buf[1] >> 4)`",
"`(buf[1] << 4) + (buf[2] >> 2)`",
"`(buf[2] << 6) + buf[3]`"
)
)
table <- table_data |>
gt() |>
fmt_markdown(columns = "In code") |>
cols_align(align="left") |>
cols_label(
`Byte index in the output` = md("**Byte index in the output**"),
`In code` = md("**In code**")
)


::: {#tbl-6bit-decode}

| Byte index in the output | In code |
|--------------------------|-------------------------------|
| 0 | (buf[0] << 2) + (buf[1] >> 4) |
| 1 | (buf[1] << 4) + (buf[2] >> 2) |
| 2 | (buf[2] << 6) + buf[3] |

if (knitr::is_html_output()) {
table |> gt::as_raw_html()
} else {
table |> gt::as_latex()
}
```
: The necessary steps for the 6-transformation in the decode process.

The necessary steps for the 6-transformation in the decode process.

:::

Expand Down
1 change: 1 addition & 0 deletions Chapters/01-memory.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ knitr::opts_chunk$set(
```



# Memory and Allocators


Expand Down
1 change: 1 addition & 0 deletions Chapters/01-zig-weird.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ knitr::opts_chunk$set(




# Introducing Zig

In this chapter, I want to introduce you to the world of Zig.
Expand Down
2 changes: 2 additions & 0 deletions Chapters/02-debugging.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ knitr::opts_chunk$set(
```




# Debugging Zig applications

Being able to debug your programs is essential to any programmer who wants to
Expand Down
1 change: 1 addition & 0 deletions Chapters/03-structs.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ knitr::opts_chunk$set(
```



# Structs, Modules and Control Flow

I introduced a lot of the Zig's syntax to you in the last chapter,
Expand Down
4 changes: 4 additions & 0 deletions Chapters/03-unittests.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ knitr::opts_chunk$set(
```



# Unit tests {#sec-unittests}

In this chapter, I want to dive in on how unit tests are developed in
Expand All @@ -39,6 +40,7 @@ Part of this code might be some necessary commands to setup your testing
environment, or just initializing some necessary objects.

```{zig}
#| build_type: "test"
const std = @import("std");
const expect = std.testing.expect;
test "testing simple sum" {
Expand Down Expand Up @@ -213,6 +215,7 @@ then, the `expectError()` function would make the entire test fail.


```{zig}
#| build_type: "test"
const std = @import("std");
const Allocator = std.mem.Allocator;
const expectError = std.testing.expectError;
Expand Down Expand Up @@ -290,6 +293,7 @@ objects (`array1` and `array2`) are equal or not. Since they
are in fact equal, the unit test passed with no errors.

```{zig}
#| build_type: "test"
const std = @import("std");
test "arrays are equal?" {
const array1 = [3]u32{1, 2, 3};
Expand Down
4 changes: 3 additions & 1 deletion Chapters/04-http-server.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ syntax-definition: "../Assets/zig.xml"
source("../zig_engine.R")
knitr::opts_chunk$set(
auto_main = FALSE,
build_type = "run"
build_type = "lib"
)
```

Expand Down Expand Up @@ -229,6 +229,7 @@ tell the function to create a socket for TCP connections.


```{zig}
#| auto_main: false
#| build_type: "lib"
const std = @import("std");
const builtin = @import("builtin");
Expand Down Expand Up @@ -501,6 +502,7 @@ one of these three values: `RED`, `GREEN` or `BLUE`.

```{zig}
#| auto_main: true
#| build_type: "run"
const PrimaryColorRGB = enum {
RED, GREEN, BLUE
};
Expand Down
22 changes: 20 additions & 2 deletions Chapters/05-pointers.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ syntax-definition: "../Assets/zig.xml"
#| include: false
source("../zig_engine.R")
knitr::opts_chunk$set(
auto_main = TRUE,
build_type = "run"
auto_main = FALSE,
build_type = "lib"
)
```



# Pointers and Optionals {#sec-pointer}

On our next project we are going to build a HTTP server from scratch.
Expand Down Expand Up @@ -88,6 +90,8 @@ the value of the object `number` to 6, like in the example below.


```{zig}
#| auto_main: true
#| build_type: "run"
var number: u8 = 5;
const pointer = &number;
pointer.* = 6;
Expand Down Expand Up @@ -145,6 +149,8 @@ If I change the `number` object to be a variable object, by introducing the `var
then I can change the value of this object through a pointer, as demonstrated below:

```{zig}
#| auto_main: true
#| build_type: "run"
var number: u8 = 5;
const pointer = &number;
pointer.* = 6;
Expand Down Expand Up @@ -278,6 +284,8 @@ element of `ar` from this slice, and, the slice itself already is a pointer
behind the hood.

```{zig}
#| auto_main: true
#| build_type: "run"
const ar = [_]i32{1,2,3,4};
const sl = ar[0..ar.len];
_ = sl;
Expand Down Expand Up @@ -366,6 +374,8 @@ That is why, I can actually change the value of this object to null, and,
no errors are raised by the `zig` compiler, as demonstrated below:

```{zig}
#| auto_main: true
#| build_type: "run"
var num: ?i32 = 5;
num = null;
```
Expand Down Expand Up @@ -393,6 +403,8 @@ This variable will always contain a valid `i32` value. But in contrast, the `ptr
value, or, a pointer to an `i32` value.

```{zig}
#| auto_main: true
#| build_type: "run"
var num: i32 = 5;
const ptr: ?*i32 = &num;
_ = ptr;
Expand All @@ -410,6 +422,8 @@ this time. So now, we have a pointer that points to a value that is either null
, or, a signed 32-bits integer.

```{zig}
#| auto_main: true
#| build_type: "run"
var num: ?i32 = 5;
// ptr have type `*?i32`, instead of `?*i32`.
const ptr = &num;
Expand Down Expand Up @@ -439,6 +453,8 @@ object. This `not_null_num` object is garanteed to be not null inside
the scope of the if statement.

```{zig}
#| auto_main: true
#| build_type: "run"
const num: ?i32 = 5;
if (num) |not_null_num| {
try stdout.print(
Expand All @@ -461,6 +477,8 @@ Looking at the example below, since the `x` object is currently null, the
`orelse` decided to use the alternative value, which is the number 15.

```{zig}
#| auto_main: true
#| build_type: "run"
const x: ?i32 = null;
const dbl = (x orelse 15) * 2;
try stdout.print("{d}\n", .{dbl});
Expand Down
22 changes: 0 additions & 22 deletions Chapters/06-vectors.qmd

This file was deleted.

4 changes: 3 additions & 1 deletion Chapters/07-build-system.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ syntax-definition: "../Assets/zig.xml"
source("../zig_engine.R")
knitr::opts_chunk$set(
auto_main = FALSE,
build_type = "run"
build_type = "lib"
)
```




# Build System {#sec-build-system}


Expand Down
1 change: 1 addition & 0 deletions Chapters/09-data-structures.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ knitr::opts_chunk$set(




# Data Structures

In this chapter, we are going to discuss some Data Structures that are available from
Expand Down
6 changes: 4 additions & 2 deletions Chapters/09-error-handling.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ syntax-definition: "../Assets/zig.xml"
source("../zig_engine.R")
knitr::opts_chunk$set(
auto_main = FALSE,
build_type = "run"
build_type = "lib"
)
```




# Error handling and unions {#sec-error-handling}

In this chapter, I want to discuss how error handling is done in Zig.
Expand Down Expand Up @@ -59,6 +60,7 @@ This note is reinforcing that every possible error value must be explicitly hand
```{zig}
#| eval: false
#| auto_main: true
#| build_type: "run"
const dir = std.fs.cwd();
_ = dir.openFile("doesnt_exist.txt", .{});
```
Expand Down Expand Up @@ -565,7 +567,7 @@ const LakeTarget = union {
fn send_event(
event: Event,
lake_target: LakeTarget
) -> bool {
) bool {
// body of the function ...
}
```
Expand Down
Loading

0 comments on commit c0a1a01

Please sign in to comment.