updated site + added docs/builtins.md + statements can end with ',' to differentiate '1, -2' from '1 - 2' == '-1'.

This commit is contained in:
mark
2023-05-25 00:22:03 +02:00
parent 46653666f0
commit e766e78e96
8 changed files with 203 additions and 33 deletions

View File

@@ -9,7 +9,7 @@
<section class="container">
<section class="container_left2 code-border">
<pre><code class="mers-code-snippet">
fn get_number_input(question string) {<br> println(question)<br> input = read_line()<br> // try to parse to an int, then a float.<br> in = match input {<br> input.parse_int() input<br> input.parse_float() input<br> }<br> // 'in' has type int/float/[] because of the match statement<br> switch! in {<br> int/float in<br> // replace [] with an appropriate error before returning<br> [] Err: "input was not a number."<br> }<br> // return type is int/float/Err(string)<br>}<br><br>answer = get_number_input("What is your favorite number?")<br><br>answer.debug() // type: int/float/Err(string)<br>// switch can be used to branch based on a variables type.<br>// switch! indicates that every possible type must be handled.<br>switch! answer {<br> int {<br> println("Entered an integer")<br> answer.debug() // type: int<br> }<br> float {<br> println("Entered a decimal number")<br> answer.debug() // type: float<br> }<br> Err(string) println("Input was not a number!")<br>}<br><br>// wait one second<br>sleep(1)<br><br><br>// function that returns an anonymous function (function object).<br>// anonymous functions can be used as iterators in for-loops.<br>fn square_numbers() {<br> i = 0<br> () {<br> i = i + 1<br> i * i<br> }<br>}<br><br>for num square_numbers() {<br> println(num.to_string())<br> // once num is greater than 60, the loop stops.<br> num.gt(50)<br>}<br></code></pre>
fn get_number_input(question string) {<br> println(question)<br> input := read_line()<br> // try to parse to an int, then a float.<br> in := match input {<br> input.parse_int() input<br> input.parse_float() input<br> }<br> // 'in' has type int/float/[] because of the match statement<br> switch! in {<br> int/float in<br> // replace [] with an appropriate error before returning<br> [] Err: "input was not a number."<br> }<br> // return type is int/float/Err(string)<br>}<br><br>answer := get_number_input("What is your favorite number?")<br><br>answer.debug() // type: int/float/Err(string)<br>// switch can be used to branch based on a variables type.<br>// switch! indicates that every possible type must be handled.<br>switch! answer {<br> int {<br> println("Entered an integer")<br> answer.debug() // type: int<br> }<br> float {<br> println("Entered a decimal number")<br> answer.debug() // type: float<br> }<br> Err(string) println("Input was not a number!")<br>}<br><br>// wait one second<br>sleep(1)<br><br><br>// function that returns an anonymous function (function object).<br>// anonymous functions can be used as iterators in for-loops.<br>fn square_numbers() {<br> i := 0<br> () {<br> &amp;i = i + 1<br> i * i<br> }<br>}<br><br>for num square_numbers() {<br> println(num.to_string())<br> // once num is greater than 60, the loop stops.<br> num.gt(50)<br>}<br></code></pre>
</section>
<section class="container_right">
<image
@@ -42,7 +42,7 @@ it is by using one of the assume*() functions (similar to unwrap()s).
<pre class="container2_left"><code>
&lt;!DOCTYPE html&gt;<br># This document will be processed by build.mers.<br># Lines starting with hashtags are comments and will be ignored.<br># Lines starting with dollar-signs insert special text.<br># To escape this, put a space before the hashtag or dollar sign.<br>&lt;head&gt;<br> &lt;meta charset=“UTF-8”&gt;<br> &lt;link rel="stylesheet" href="external.css"&gt;<br> &lt;title&gt;Mark :: mers&lt;/title&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br> &lt;h1&gt;Mers&lt;/h1&gt;<br> &lt;section class="container"&gt;<br> &lt;section class="container_left2 code-border"&gt;<br> &lt;pre&gt;&lt;code class="mers-code-snippet"&gt;<br>$welcome_script<br> &lt;/code&gt;&lt;/pre&gt;<br> &lt;/section&gt;<br> &lt;section class="container_right"&gt;<br> &lt;image<br> alt="some picture related to mers (todo)"<br> src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAP//////<br> width="100%" height="100%"<br> &gt;<br> &lt;h3&gt;Mers types&lt;/h3&gt;<br> &lt;div&gt;<br> Mers uses a multiple-types system.<br> It keeps track of which types a variable could have<br> and constructs a type with that information.<br> &lt;br&gt;<br> For example, &lt;code&gt;int/float&lt;/code&gt; can represent a number - int or<br> Optional types can be &lt;code&gt;[]/[t]&lt;/code&gt; - either nothing or one v<br> Mers doesn't have null, it just has the empty tuple &lt;code&gt;[]&lt;/code&gt;<br> &lt;/div&gt;<br> &lt;h3&gt;No exceptions, no crashes&lt;/h3&gt;<br> &lt;div&gt;<br> Errors in mers are passed as values.<br> Because of the type system, you are forced to handle them explicitl<br> Mers will not crash in unexpected places, because the only way to c<br> it is by using one of the assume*() functions (similar to unwrap()s<br> &lt;/div&gt;<br> &lt;/section&gt;<br> &lt;/section&gt;<br> &lt;hr&gt;<br> &lt;h3&gt;HTML preprocessor to help build this document written in mers:&lt;/h3&gt;<br> &lt;section class="container"&gt;<br> &lt;pre class="container2_left"&gt;&lt;code&gt;<br>$index.html<br> &lt;/code&gt;&lt;/pre&gt;<br> &lt;pre class="container2_right"&gt;&lt;code class="mers-code-snippet"&gt;<br>$build_script<br> &lt;/code&gt;&lt;/pre&gt;<br> &lt;/section&gt;<br>&lt;/body&gt;<br><br></code></pre>
<pre class="container2_right"><code class="mers-code-snippet">
#!/usr/bin/env mers<br><br>// helper functions<br><br>fn read_string(path string) {<br> bytes_to_string(fs_read(path).assume_no_enum()).assume_no_enum()<br>}<br>fn code_to_html(code string code_width_limit_chars int) {<br> out = ""<br> for line code.regex(".*").assume_no_enum() {<br> if code_width_limit_chars.gtoe(0).and(line.len().gt(code_width_limit_chars)) {<br> line = line.substring(0 code_width_limit_chars)<br> }<br> line = line<br> .replace("&amp;" "&amp;amp;")<br> .replace("&lt;" "&amp;lt;")<br> .replace("&gt;" "&amp;gt;")<br> out = out.add(line.add("&lt;br&gt;"))<br> }<br> out<br>}<br><br>// data<br><br>index = read_string("index.html")<br><br>index_html = index.code_to_html(75)<br>build_script = read_string("build.mers").code_to_html(-1)<br>welcome_script = read_string("welcome.mers").code_to_html(-1)<br><br>// process index.html<br><br>out = ""<br>for line index.regex("\\S*.*").assume_no_enum() {<br> if line.starts_with("#") {<br> // comment, ignore<br> } else if line.starts_with("$") {<br> if line == "$welcome_script" {<br> out = out + welcome_script<br> } else if line == "$build_script" {<br> out = out + build_script<br> } else if line == "$index.html" {<br> out = out + index_html<br> }<br> } else {<br> // remove spaces<br> loop {<br> if line.starts_with(" ") {<br> line = line.substring(1)<br> } else {<br> true // break<br> }<br> }<br> out = out + line + "\n"<br> }<br>}<br>fs_write("../index.html" string_to_bytes(out)).assume_no_enum()<br><br></code></pre>
#!/usr/bin/env mers<br><br>// helper functions<br><br>fn read_string(path string) {<br> bytes_to_string(fs_read(path).assume_no_enum()).assume_no_enum()<br>}<br>fn code_to_html(code string code_width_limit_chars int) {<br> out := ""<br> for line code.regex(".*").assume_no_enum() {<br> if code_width_limit_chars.gtoe(0).and(line.len().gt(code_width_limit_chars)) {<br> &amp;line = line.substring(0 code_width_limit_chars)<br> }<br> &amp;line = line<br> .replace("&amp;" "&amp;amp;")<br> .replace("&lt;" "&amp;lt;")<br> .replace("&gt;" "&amp;gt;")<br> &amp;out = out.add(line.add("&lt;br&gt;"))<br> }<br> out<br>}<br><br>// data<br><br>index := read_string("index.html")<br><br>index_html := index.code_to_html(75)<br>build_script := read_string("build.mers").code_to_html(-1)<br>welcome_script := read_string("welcome.mers").code_to_html(-1)<br><br>// process index.html<br><br>out := ""<br>for line index.regex("\\S*.*").assume_no_enum() {<br> if line.starts_with("#") {<br> // comment, ignore<br> } else if line.starts_with("$") {<br> if line == "$welcome_script" {<br> &amp;out = out + welcome_script<br> } else if line == "$build_script" {<br> &amp;out = out + build_script<br> } else if line == "$index.html" {<br> &amp;out = out + index_html<br> }<br> } else {<br> // remove spaces<br> loop {<br> if line.starts_with(" ") {<br> &amp;line = line.substring(1)<br> } else {<br> true // break<br> }<br> }<br> &amp;out = out + line + "\n"<br> }<br>}<br>fs_write("../index.html" string_to_bytes(out)).assume_no_enum()<br><br></code></pre>
</section>
</body>