Skip to content

Commit

Permalink
deploy: ab683a8
Browse files Browse the repository at this point in the history
  • Loading branch information
feds01 committed Feb 18, 2024
1 parent 2799445 commit b6ab9e4
Show file tree
Hide file tree
Showing 19 changed files with 453 additions and 453 deletions.
22 changes: 11 additions & 11 deletions advanced/if-statement-transpilation.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ <h1 class="menu-title">The Hash Programming Language</h1>
<main>
<h1 id="if-statement-transpilation"><a class="header" href="#if-statement-transpilation">If Statement transpilation</a></h1>
<p>As mentioned at the start of the conditionals section in the basics chapter, if statements can be
represented as <code>match</code> statements. This is especially advised when you have many <code>if</code> branched and
represented as <code>match</code> statements. This is especially advised when you have many <code>if</code> branched and
more complicated branch conditions.</p>
<p>Internally, the compiler will convert <code>if</code> statements into match cases so that it has to do
less work in the following stages of compilation.</p>
Expand Down Expand Up @@ -206,19 +206,19 @@ <h1 id="if-statement-transpilation"><a class="header" href="#if-statement-transp
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>if conditionA {
print(&quot;conditionA&quot;)
print("conditionA")
} else if conditionB {
print(&quot;conditionB&quot;)
print("conditionB")
} else {
print(&quot;Neither&quot;)
print("Neither")
}

// Internally, this becomes:

match true {
_ if conditionA =&gt; { print(&quot;conditionA&quot;) };
_ if conditionB =&gt; { print(&quot;conditionB&quot;) };
_ =&gt; { print(&quot;Neither&quot;) };
_ if conditionA =&gt; { print("conditionA") };
_ if conditionB =&gt; { print("conditionB") };
_ =&gt; { print("Neither") };
}
<span class="boring">}</span></code></pre></pre>
<p>However, this representation is not entirely accurate because the compiler will optimise out some components
Expand All @@ -232,16 +232,16 @@ <h2 id="missing-else-case"><a class="header" href="#missing-else-case">Missing '
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>if conditionA {
print(&quot;conditionA&quot;)
print("conditionA")
} else if conditionB {
print(&quot;conditionB&quot;)
print("conditionB")
}

// Internally, this becomes:

match true {
_ if conditionA =&gt; { print(&quot;conditionA&quot;) };
_ if conditionB =&gt; { print(&quot;conditionB&quot;) };
_ if conditionA =&gt; { print("conditionA") };
_ if conditionB =&gt; { print("conditionB") };
_ =&gt; { };
}
<span class="boring">}</span></code></pre></pre>
Expand Down
10 changes: 5 additions & 5 deletions advanced/loop-transpilation.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ <h1 class="menu-title">The Hash Programming Language</h1>
<main>
<h1 id="loop-transpilation"><a class="header" href="#loop-transpilation">Loop transpilation</a></h1>
<p>As mentioned at the start of the <a href="./../basics/loops.html">loops</a> section in the <a href="./../basics/intro.html">basics</a> chapter, the <code>loop</code> control flow keyword
is the most universal control flow since to you can use <code>loop</code> to represent
both the <code>for</code> and <code>while</code> loops. </p>
is the most universal control flow since to you can use <code>loop</code> to represent
both the <code>for</code> and <code>while</code> loops.</p>
<h3 id="for-loop-transpilation"><a class="header" href="#for-loop-transpilation">for loop transpilation</a></h3>
<p>Since <code>for</code> loops are used for iterators in hash, we transpile the construct into
a primitive loop. An iterator can be traversed by calling the <code>next</code> function on the
Expand All @@ -206,7 +206,7 @@ <h3 id="for-loop-transpilation"><a class="header" href="#for-loop-transpilation"
</span>i := [1,2,3,5].into_iter();

for x in i {
print(&quot;x is &quot; + x);
print("x is " + x);
}


Expand All @@ -215,13 +215,13 @@ <h3 id="for-loop-transpilation"><a class="header" href="#for-loop-transpilation"

loop {
match next(i) {
Some(x) =&gt; {print(&quot;x is &quot; + x)};
Some(x) =&gt; {print("x is " + x)};
None =&gt; break;
}
}
<span class="boring">}</span></code></pre></pre>
<h3 id="while-loop-internal-representation"><a class="header" href="#while-loop-internal-representation">While loop internal representation</a></h3>
<p>In general, a while loop transpilation process occurs by transferring the looping
<p>In general, a while loop transpilation process occurs by transferring the looping
condition into a match block, which compares a boolean condition. If the boolean
condition evaluates to <code>false</code>, the loop will immediately <code>break</code>. Otherwise
the body expression is expected. A rough outline of what the transpilation process for a <code>while</code> loop looks like:</p>
Expand Down
2 changes: 1 addition & 1 deletion advanced/type-inference.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ <h1 class="menu-title">The Hash Programming Language</h1>
<div id="content" class="content">
<main>
<h1 id="type-inference"><a class="header" href="#type-inference">Type inference</a></h1>
<p>🚧 Still under construction! 🚧 </p>
<p>🚧 Still under construction! 🚧</p>

</main>

Expand Down
84 changes: 42 additions & 42 deletions features/control-flow.html
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,16 @@ <h1 class="menu-title">The Hash Programming Language</h1>
<div id="content" class="content">
<main>
<h2 id="conditional-statements"><a class="header" href="#conditional-statements">Conditional statements</a></h2>
<p>Conditional statements in the <code>Hash</code> programming language are very similar to other languages such as Python, Javascript, C and Rust. However, there is one subtle <strong>difference</strong>, which is that the statement provided to a conditional statement must always evaluate to an explicit boolean value. </p>
<p>Conditional statements in the <code>Hash</code> programming language are very similar to other languages such as Python, Javascript, C and Rust. However, there is one subtle <strong>difference</strong>, which is that the statement provided to a conditional statement must always evaluate to an explicit boolean value.</p>
<h2 id="if-else-statements"><a class="header" href="#if-else-statements">If-else statements</a></h2>
<p>If statements are very basic constructs in Hash. An example of a basic <code>if-else</code> statement is as follows:</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>// checking if the value 'a' evaluates to being 'true'
if a { print(&quot;a&quot;); } else { print(&quot;b&quot;); }
if a { print("a"); } else { print("b"); }

// using a comparison operator
if b == 2 { print(&quot;b is &quot; + b); } else { print (&quot;b is not &quot; + conv(b)); }
if b == 2 { print("b is " + b); } else { print ("b is not " + conv(b)); }
<span class="boring">}</span></code></pre></pre>
<p>Obviously, this checks if the evaluation of <code>a</code> returns a boolean value. If it does not
evaluate to something to be considered as true, then the block expression defined
Expand All @@ -196,31 +196,31 @@ <h2 id="if-else-statements"><a class="header" href="#if-else-statements">If-else
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>if b == 2 {
print(&quot;b is 2&quot;)
print("b is 2")
} else if b == 3 {
print(&quot;b is 3&quot;)
print("b is 3")
} else {
print(&quot;b isn't 2 or 3 &quot;)
print("b isn't 2 or 3 ")
}
<span class="boring">}</span></code></pre></pre>
<p>As mentioned in the introduction, a conditional statement must evaluate an explicit boolean value. The <code>if</code> statement syntax will not infer a boolean value from a statement within <code>Hash</code>. This design feature is motivated by the fact that in many languages, common bugs and mistakes occur with the automatic inference of conditional statements. </p>
<p>As mentioned in the introduction, a conditional statement must evaluate an explicit boolean value. The <code>if</code> statement syntax will not infer a boolean value from a statement within <code>Hash</code>. This design feature is motivated by the fact that in many languages, common bugs and mistakes occur with the automatic inference of conditional statements.</p>
<p>An example of an invalid program is:</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>a: u8 = 12;

if a { print(&quot;a&quot;) }
if a { print("a") }
<span class="boring">}</span></code></pre></pre>
<h3 id="additional-syntax"><a class="header" href="#additional-syntax">Additional syntax</a></h3>
<p>Furthermore, if you do not want an else statement you can do:</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>if a { print(&quot;a&quot;) } // if a is true, then execute
</span>if a { print("a") } // if a is true, then execute
<span class="boring">}</span></code></pre></pre>
<p>which is syntactic sugar for:</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>if a { print(&quot;a&quot;) } else {}
</span>if a { print("a") } else {}
<span class="boring">}</span></code></pre></pre>
<p>Additionally, since the <code>if</code> statement body's are also equivalent to functional bodies, you
can also specifically return a type as you would normally do within a function body:</p>
Expand Down Expand Up @@ -253,16 +253,16 @@ <h2 id="if-statements-and-enums-"><a class="header" href="#if-statements-and-enu
result: Result&lt;u16, str&gt; = Ok(12);

if let Ok(value) = result {
print(&quot;Got '&quot; + conv(value) + &quot;' from operation&quot;)
print("Got '" + conv(value) + "' from operation")
} else let Err(e) = result {
panic(&quot;Failed to get result: &quot; + e);
panic("Failed to get result: " + e);
}
<span class="boring">}</span></code></pre></pre>
<p>Furthermore, for more complicated conditional statements, you can include an expression
block which is essentially treated as if it was a functional body, like so:</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>f: str = &quot;file.txt&quot;;
</span>f: str = "file.txt";

if { a = open(f); is_ok(a) } {
// run if is_ok(a) returns true
Expand Down Expand Up @@ -290,17 +290,17 @@ <h2 id="match-cases"><a class="header" href="#match-cases">Match cases</a></h2>
</span>a := input&lt;u8&gt;();

m2 := match a {
1 =&gt; &quot;one&quot;;
2 =&gt; &quot;two&quot;;
_ =&gt; &quot;not one or two&quot;;
1 =&gt; "one";
2 =&gt; "two";
_ =&gt; "not one or two";
}

// Or as a function

convert: (x: u8) =&gt; str = (x) =&gt; match x {
1 =&gt; &quot;one&quot;;
2 =&gt; &quot;two&quot;;
_ =&gt; &quot;not one or two&quot;;
1 =&gt; "one";
2 =&gt; "two";
_ =&gt; "not one or two";
}

m := convert(input&lt;u8&gt;());
Expand All @@ -310,8 +310,8 @@ <h2 id="match-cases"><a class="header" href="#match-cases">Match cases</a></h2>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>match x {
1 =&gt; &quot;one&quot;;
2 =&gt; &quot;two&quot;;
1 =&gt; "one";
2 =&gt; "two";
_ =&gt; unreachable(); // we know that 'x' should never be 1 or 2.
}
<span class="boring">}</span></code></pre></pre>
Expand All @@ -320,13 +320,13 @@ <h2 id="match-cases"><a class="header" href="#match-cases">Match cases</a></h2>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>convert: (x: u8) =&gt; str = (x) =&gt; match x {
_ =&gt; &quot;not one or two&quot;;
1 =&gt; &quot;one&quot;;
2 =&gt; &quot;two&quot;;
_ =&gt; "not one or two";
1 =&gt; "one";
2 =&gt; "two";
}
<span class="boring">}</span></code></pre></pre>
<p>The value of <code>m</code> will always evaluate as <code>&quot;not one or two&quot;</code> since the wildcard matches any condition.</p>
<p>Match statements are also really good for destructing enum types in Hash.
<p>The value of <code>m</code> will always evaluate as <code>"not one or two"</code> since the wildcard matches any condition.</p>
<p>Match statements are also really good for destructing enum types in Hash.
For example,</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
Expand All @@ -341,8 +341,8 @@ <h2 id="match-cases"><a class="header" href="#match-cases">Match cases</a></h2>
result: Result&lt;u16, str&gt; = Ok(12);

match result {
Ok(value) =&gt; print(&quot;Got '&quot; + conv(value) + &quot;' from operation&quot;);
Err(e) =&gt; panic(&quot;Failed to get result: &quot; + e);
Ok(value) =&gt; print("Got '" + conv(value) + "' from operation");
Err(e) =&gt; panic("Failed to get result: " + e);
}
<span class="boring">}</span></code></pre></pre>
<p>To specify multiple conditions for a single case within a <code>match</code> statement, you can do so by
Expand All @@ -352,9 +352,9 @@ <h2 id="match-cases"><a class="header" href="#match-cases">Match cases</a></h2>
</span>x: u32 = input&lt;u32&gt;();

match x {
1 | 2 | 3 =&gt; print(&quot;x is 1, 2, or 3&quot;);
4 | 5 | {2 | 4} =&gt; print(&quot;x is either 4, 5 or 6&quot;); // using bitwise or operator
_ =&gt; print(&quot;x is something else&quot;);
1 | 2 | 3 =&gt; print("x is 1, 2, or 3");
4 | 5 | {2 | 4} =&gt; print("x is either 4, 5 or 6"); // using bitwise or operator
_ =&gt; print("x is something else");
}
<span class="boring">}</span></code></pre></pre>
<p>To specify more complex conditional statements like and within the match case, you
Expand All @@ -365,10 +365,10 @@ <h2 id="match-cases"><a class="header" href="#match-cases">Match cases</a></h2>
y: bool = true;

match x {
1 | 2 | 3 if y =&gt; print(&quot;x is 1, 2, or 3 when y is true&quot;);
{4 if y} | y =&gt; print(&quot;x is 4 and y is true, or x is equal to y&quot;); // using bitwise or operator
{2 | 4 if y} =&gt; print(&quot;x is 6 and y is true&quot;);
_ =&gt; print(&quot;x is something else&quot;);
1 | 2 | 3 if y =&gt; print("x is 1, 2, or 3 when y is true");
{4 if y} | y =&gt; print("x is 4 and y is true, or x is equal to y"); // using bitwise or operator
{2 | 4 if y} =&gt; print("x is 6 and y is true");
_ =&gt; print("x is something else");
}
<span class="boring">}</span></code></pre></pre>
<h1 id="loop-constructs"><a class="header" href="#loop-constructs">Loop constructs</a></h1>
Expand All @@ -386,15 +386,15 @@ <h2 id="general"><a class="header" href="#general">General</a></h2>
<h2 id="for-loop"><a class="header" href="#for-loop">For loop</a></h2>
<h3 id="basics"><a class="header" href="#basics">Basics</a></h3>
<p>For loops are special loop control statements that are designed to be used
with iterators. </p>
with iterators.</p>
<p>For loops can be defined as:</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>for i in range(1, 10) { // range is a built in iterator
print(i);
}
<span class="boring">}</span></code></pre></pre>
<p>Iterating over lists is also quite simple using the <code>iter</code> function to
<p>Iterating over lists is also quite simple using the <code>iter</code> function to
convert the list into an iterator:</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
Expand All @@ -412,7 +412,7 @@ <h3 id="basics"><a class="header" href="#basics">Basics</a></h3>
<span class="boring">}</span></code></pre></pre>
<h3 id="iterators"><a class="header" href="#iterators">iterators</a></h3>
<p>Iterators ship with the standard library, but you can define your own iterators via the Hash generic typing system.</p>
<p>An iterator <code>I</code> of <code>T</code> it means to have an implementation <code>next&lt;I, T&gt;</code> in scope the current scope.
<p>An iterator <code>I</code> of <code>T</code> it means to have an implementation <code>next&lt;I, T&gt;</code> in scope the current scope.
So, for the example above, the <code>range</code> function is essentially a <code>RangeIterator</code> of the <code>u8</code>, <code>u16</code>, <code>u32</code>, <code>...</code> types.</p>
<p>More details about generics are <a href="./generics-polymorphism.html">here</a>.</p>
<h2 id="while-loop"><a class="header" href="#while-loop">While loop</a></h2>
Expand Down Expand Up @@ -470,7 +470,7 @@ <h3 id="expression-blocks-and-behaviour"><a class="header" href="#expression-blo
print(i);
}
<span class="boring">}</span></code></pre></pre>
<p>It is worth noting that the looping expression whether block or not must explicitly have the
<p>It is worth noting that the looping expression whether block or not must explicitly have the
<code>boolean</code> return type. For example, the code below will fail typechecking:</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
Expand Down Expand Up @@ -498,7 +498,7 @@ <h2 id="loop"><a class="header" href="#loop">Loop</a></h2>
</span>c: u64 = 1;

loop {
print(&quot;I looped &quot; + c + &quot; times!&quot;);
print("I looped " + c + " times!");
c += 1;
}
<span class="boring">}</span></code></pre></pre>
Expand All @@ -511,7 +511,7 @@ <h2 id="loop"><a class="header" href="#loop">Loop</a></h2>
loop {
if c == 10 { break }

print(&quot;I looped &quot; + c + &quot; times!&quot;);
print("I looped " + c + " times!");
c += 1;
} // this will loop 10 times, and print all 10 times
<span class="boring">}</span></code></pre></pre>
Expand All @@ -523,7 +523,7 @@ <h2 id="loop"><a class="header" href="#loop">Loop</a></h2>
c += 1;
if c % 2 != 0 { continue };

print(&quot;I loop and I print when I get a &quot; + c);
print("I loop and I print when I get a " + c);
} // this will loop 10 times, and print only when c is even
<span class="boring">}</span></code></pre></pre>

Expand Down
Loading

0 comments on commit b6ab9e4

Please sign in to comment.