From 79a9d17ba9e855f267edf8e1b1319aeaad73d58e Mon Sep 17 00:00:00 2001 From: mark Date: Thu, 18 May 2023 02:12:21 +0200 Subject: [PATCH] updated example for github.io site --- index.html | 48 ----------------------------------------------- site/welcome.mers | 25 ++++++++++++------------ 2 files changed, 13 insertions(+), 60 deletions(-) diff --git a/index.html b/index.html index 57f4e04..e69de29 100644 --- a/index.html +++ b/index.html @@ -1,48 +0,0 @@ - - - - -Mark :: mers - - -

Mers

-
-
-

-fn get_number_input(question string) {
println(question)
input = read_line()
// try to parse to an int, then a float.
in = match input {
input.parse_int() input
input.parse_float() input
}
// 'in' has type int/float/[] because of the match statement
switch! in {
int/float in
// replace [] with an appropriate error before returning
[] Err: "input was not a number."
}
// return type is int/float/Err(string)
}

answer = get_number_input("What is your favorite number?")

// switch can be used to branch based on a variables type.
// switch! indicates that every possible type must be handled.
switch! answer {
int {
println("Entered an integer")
answer.debug() // int
}
float {
println("Entered a decimal number")
answer.debug() // float
}
Err(string) println("Input was not a number!")
}

sleep(2)


// function that returns an anonymous function (function object).
// anonymous functions can be used as iterators in for-loops.
fn count_up() {
count = -1
() {
count = count.add(1)
count
}
}

for num count_up() {
println(num.to_string())
// once num is greater than 60,
// the loop stops and returns num.
if num.gt(60) num else []
}

-
-
- -

Mers types

-
-Mers uses a multiple-types system. -It keeps track of which types a variable could have -and constructs a type with that information. -
-For example, int/float can represent a number - int or float. -Optional types can be []/[t] - either nothing or one value (tuple with length 0 or 1). -Mers doesn't have null, it just has the empty tuple []. -
-

No exceptions, no crashes

-
-Errors in mers are passed as values. -Because of the type system, you are forced to handle them explicitly. -Mers will not crash in unexpected places, because the only way to crash -it is by using one of the assume*() functions (similar to unwrap()s). -
-
-
-
-

HTML preprocessor to help build this document written in mers:

-
-

-<!DOCTYPE html>
# This document will be processed by build.mers.
# Lines starting with hashtags are comments and will be ignored.
# Lines starting with dollar-signs insert special text.
# To escape this, put a space before the hashtag or dollar sign.
<head>
<meta charset=“UTF-8”>
<link rel="stylesheet" href="external.css">
<title>Mark :: mers</title>
</head>
<body>
<h1>Mers</h1>
<section class="container">
<section class="container_left2 code-border">
<pre><code class="mers-code-snippet">
$welcome_script
</code></pre>
</section>
<section class="container_right">
<image
alt="some picture related to mers (todo)"
src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAP//////
width="100%" height="100%"
>
<h3>Mers types</h3>
<div>
Mers uses a multiple-types system.
It keeps track of which types a variable could have
and constructs a type with that information.
<br>
For example, <code>int/float</code> can represent a number - int or
Optional types can be <code>[]/[t]</code> - either nothing or one v
Mers doesn't have null, it just has the empty tuple <code>[]</code>
</div>
<h3>No exceptions, no crashes</h3>
<div>
Errors in mers are passed as values.
Because of the type system, you are forced to handle them explicitl
Mers will not crash in unexpected places, because the only way to c
it is by using one of the assume*() functions (similar to unwrap()s
</div>
</section>
</section>
<hr>
<h3>HTML preprocessor to help build this document written in mers:</h3>
<section class="container">
<pre class="container2_left"><code>
$index.html
</code></pre>
<pre class="container2_right"><code class="mers-code-snippet">
$build_script
</code></pre>
</section>
</body>

-

-#!/usr/bin/env mers

// helper functions

fn read_string(path string) {
bytes_to_string(fs_read(path).assume_no_enum()).assume_no_enum()
}
fn code_to_html(code string code_width_limit_chars int) {
out = ""
for line code.regex(".*").assume_no_enum() {
if code_width_limit_chars.gtoe(0).and(line.len().gt(code_width_limit_chars)) {
line = line.substring(0 code_width_limit_chars)
}
line = line
.replace("&" "&amp;")
.replace("<" "&lt;")
.replace(">" "&gt;")
out = out.add(line.add("<br>"))
}
out
}

// data

index = read_string("index.html")

index_html = index.code_to_html(75)
build_script = read_string("build.mers").code_to_html(-1)
welcome_script = read_string("welcome.mers").code_to_html(-1)

// process index.html

out = ""
for line index.regex("\\S*.*").assume_no_enum() {
if line.starts_with("#") {
// comment, ignore
} else if line.starts_with("$") {
if line.eq("$welcome_script") {
out = out.add(welcome_script)
} else if line.eq("$build_script") {
out = out.add(build_script)
} else if line.eq("$index.html") {
out = out.add(index_html)
}
} else {
// remove spaces
loop {
if line.starts_with(" ") {
line = line.substring(1)
} else {
true // break
}
}
out = out.add(line.add("\n"))
}
}

fs_write("../index.html" string_to_bytes(out)).assume_no_enum()

-
- - diff --git a/site/welcome.mers b/site/welcome.mers index 49b27f2..3a282a2 100755 --- a/site/welcome.mers +++ b/site/welcome.mers @@ -17,36 +17,37 @@ fn get_number_input(question string) { answer = get_number_input("What is your favorite number?") +answer.debug() // type: int/float/Err(string) // switch can be used to branch based on a variables type. // switch! indicates that every possible type must be handled. switch! answer { int { println("Entered an integer") - answer.debug() // int + answer.debug() // type: int } float { println("Entered a decimal number") - answer.debug() // float + answer.debug() // type: float } Err(string) println("Input was not a number!") } -sleep(2) +// wait one second +sleep(1) // function that returns an anonymous function (function object). // anonymous functions can be used as iterators in for-loops. -fn count_up() { - count = -1 +fn square_numbers() { + i = 0 () { - count = count.add(1) - count + i = i + 1 + i * i } } -for num count_up() { +for num square_numbers() { println(num.to_string()) - // once num is greater than 60, - // the loop stops and returns num. - if num.gt(60) num else [] -} + // once num is greater than 60, the loop stops. + num.gt(50) +} \ No newline at end of file