From f2f5af94a7d5c5e66c15741d94e8cdbd5d1f081e Mon Sep 17 00:00:00 2001 From: mark Date: Thu, 18 May 2023 02:31:04 +0200 Subject: [PATCH] . --- index.html | 48 +++++++++++++++++++++++++++++++++++++++ mers/src/lang/builtins.rs | 6 ++--- site/build.mers | 15 ++++++------ 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/index.html b/index.html index e69de29..948dbd4 100644 --- a/index.html +++ b/index.html @@ -0,0 +1,48 @@ + + + + +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?")

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() // type: int
}
float {
println("Entered a decimal number")
answer.debug() // type: float
}
Err(string) println("Input was not a number!")
}

// wait one second
sleep(1)


// function that returns an anonymous function (function object).
// anonymous functions can be used as iterators in for-loops.
fn square_numbers() {
i = 0
() {
i = i + 1
i * i
}
}

for num square_numbers() {
println(num.to_string())
// once num is greater than 60, the loop stops.
num.gt(50)
}
+
+
+ +

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 == "$welcome_script" {
out = out + welcome_script
} else if line == "$build_script" {
out = out + build_script
} else if line == "$index.html" {
out = out + index_html
}
} else {
// remove spaces
loop {
if line.starts_with(" ") {
line = line.substring(1)
} else {
true // break
}
}
out = out + line + "\n"
}
}
fs_write("../index.html" string_to_bytes(out)).assume_no_enum()

+
+ + diff --git a/mers/src/lang/builtins.rs b/mers/src/lang/builtins.rs index aeaefc0..0790dab 100755 --- a/mers/src/lang/builtins.rs +++ b/mers/src/lang/builtins.rs @@ -1523,7 +1523,7 @@ impl BuiltinFunction { } Self::Contains => args[0].run(info).operate_on_data_immut(|a1| { args[1].run(info).operate_on_data_immut(|a2| { - if let VDataEnum::String(a1) = a2 { + if let VDataEnum::String(a1) = a1 { if let VDataEnum::String(a2) = a2 { VDataEnum::Bool(a1.contains(a2.as_str())).to() } else { @@ -1536,7 +1536,7 @@ impl BuiltinFunction { }), Self::StartsWith => args[0].run(info).operate_on_data_immut(|a1| { args[1].run(info).operate_on_data_immut(|a2| { - if let VDataEnum::String(a1) = a2 { + if let VDataEnum::String(a1) = a1 { if let VDataEnum::String(a2) = a2 { VDataEnum::Bool(a1.starts_with(a2.as_str())).to() } else { @@ -1549,7 +1549,7 @@ impl BuiltinFunction { }), Self::EndsWith => args[0].run(info).operate_on_data_immut(|a1| { args[1].run(info).operate_on_data_immut(|a2| { - if let VDataEnum::String(a1) = a2 { + if let VDataEnum::String(a1) = a1 { if let VDataEnum::String(a2) = a2 { VDataEnum::Bool(a1.ends_with(a2.as_str())).to() } else { diff --git a/site/build.mers b/site/build.mers index 0edd92e..458db5c 100755 --- a/site/build.mers +++ b/site/build.mers @@ -35,12 +35,12 @@ 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) + if line == "$welcome_script" { + out = out + welcome_script + } else if line == "$build_script" { + out = out + build_script + } else if line == "$index.html" { + out = out + index_html } } else { // remove spaces @@ -51,8 +51,7 @@ for line index.regex("\\S*.*").assume_no_enum() { true // break } } - out = out.add(line.add("\n")) + out = out + line + "\n" } } - fs_write("../index.html" string_to_bytes(out)).assume_no_enum()