map can have function members by placing function declaration between the curly braces

This commit is contained in:
Francesco Cozzuto
2023-01-13 03:02:28 +01:00
parent 189bfa030b
commit 920b88c006
8 changed files with 208 additions and 131 deletions
+13 -9
View File
@@ -352,13 +352,17 @@ fun parse(src: String) {
return newRequest(method, url, version, headers);
}
sample = cat(
"GET /search?client=firefox-b-d&q=http+request+dataset HTTP/2\r\n",
"Host: www.google.com\r\n",
"user-agent: curl/7.84.0\r\n",
"accept: */*\r\n",
"\r\n");
fun test() {
sample = cat(
"GET /search?client=firefox-b-d&q=http+request+dataset HTTP/2\r\n",
"Host: www.google.com\r\n",
"user-agent: curl/7.84.0\r\n",
"accept: */*\r\n",
"\r\n");
request, error = parse(sample);
print("request=", request, "\n");
print("error=", error, "\n");
request, error = parse(sample);
print("request=", request, "\n");
print("error=", error, "\n");
}
return {parse: parse, test: test};
+3 -1
View File
@@ -328,4 +328,6 @@ fun test() {
print("passed: ", passed, "\n");
print("failed: ", failed, "\n");
print(" total: ", total, "\n");
}
}
return {parse: parse, test: test};
+33 -35
View File
@@ -8,38 +8,40 @@ Scanner = {
consumeSpaces: Callable
};
fun hint(scan: Scanner, n: int = 1) {
k = scan.i + n;
if k < count(scan.src):
return scan.src[k];
return none;
}
fun current(scan: Scanner)
return hint(scan, 0);
fun consume(scan: Scanner, n: int = 1) {
assert(n > 0);
limit = count(scan.src);
scan.i = min(scan.i + n, limit);
return current(scan);
}
fun consumeSpaces(scan: Scanner) {
char = current(scan);
while isSpace(char):
char = consume(scan);
return char;
}
fun newScanner(src: String) {
fun isSpace(c: String)
return c == ' '
or c == '\t'
or c == '\n';
scan = {
src: src,
i: 0,
hint: hint,
consume: consume,
current: current,
consumeSpaces: consumeSpaces
fun hint(scan: Scanner, n: int = 1) {
k = scan.i + n;
if k < count(scan.src):
return scan.src[k];
return none;
}
fun current(scan: Scanner)
return scan->hint(0);
fun consume(scan: Scanner, n: int = 1) {
assert(n > 0);
limit = count(scan.src);
scan.i = min(scan.i + n, limit);
return scan->current();
}
fun consumeSpaces(scan: Scanner) {
char = scan->current();
while isSpace(char):
char = scan->consume();
return char;
}
};
assert(istypeof(Scanner, scan));
return scan;
@@ -48,11 +50,7 @@ fun newScanner(src: String) {
fun isDigit(char: ?String) {
if char == none:
return false;
return string.ord(char) >= string.ord('0')
and string.ord(char) <= string.ord('9');
ord = string.ord;
return ord(char) >= ord('0')
and ord(char) <= ord('9');
}
fun isSpace(c: String)
return c == ' '
or c == '\t'
or c == '\n';
+15
View File
@@ -0,0 +1,15 @@
Person = {name: String, age: int, sayHello: Callable};
fun newPerson(name: String, age: int)
return {
name: name,
age: age,
fun sayHello(self)
print("Hello from ", self.name, "!\n");
};
me = newPerson("Francesco", 24);
me->sayHello();
assert(istypeof(Person, me));