Compare commits

...
2 Commits
Author SHA1 Message Date
jacob 8af028be47 Added new examples. 2026-11-01 15:00:27 -06:00
jacob 5dda642c8d Fixed tests. 2026-11-01 15:00:11 -06:00
12 changed files with 127 additions and 5 deletions

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
# Church numerals formed by apply a function n times to x.
zero = \f \x x;
one = \f \x x -> f;
two = \f \x x -> f -> f;
three = \f \x x -> f -> f -> f;
# Apply f n times to x, then apply f again.
succ(n) = \f \x x -> n(f) -> f;
# Same as succ, but apply f m times again.
add(m, n) = \f \x x -> n(f) -> m(f);
# Apply m n times to f to x.
multiply(m, n) = \f \x x -> (f -> n -> m);
# Apply +1 n times to 0.
tointeger(n) = (\x x + 1 $ -> n)(0);
tointeger(multiply(add(two, three), succ(three))) == 20
+3
View File
@@ -0,0 +1,3 @@
# Tail recursion doesn't actually improve space complexity in SCL yet.
factorial(n) = (n, 1) -> f(n, a) = ? n == 1 a f(n - 1, a * n);
factorial(5) == 120
+2
View File
@@ -0,0 +1,2 @@
twice(f) = \x f(f(x));
twice(\x x + 3)(10) == 16
+1
View File
@@ -0,0 +1 @@
(\x x * x)(5) == 25
+4
View File
@@ -0,0 +1,4 @@
x = 10;
f(y) = x + y;
g(x) = f(1);
g(100) == 11
+7
View File
@@ -0,0 +1,7 @@
makeadder(x) = {
add(y) = x + y;
add
};
combine(a, b) = a(3) + b(3);
combine(makeadder(2), makeadder(10))
+4
View File
@@ -0,0 +1,4 @@
x = 1;
f(y) = x + y;
x = 10;
f(1)
+4
View File
@@ -0,0 +1,4 @@
double(x) = x * 2;
addone(x) = x + 1;
3 -> double -> addone -> addone -> double
+14
View File
@@ -0,0 +1,14 @@
# !f creates the f's closure in the current scope (gives it access to x).
# @x(x) supplies the unevaluated expression x(x) as self.
Y = \f (\x (!f)(@x(x)))
(\x (!f)(@x(x)));
# A preserved lambda expression.
factorial = @(\self\n
? n == 0
1
n * (!self)(n - 1));
# !factorial unwraps the preserved lambda expression, becoming f inside of Y.
# Not yet a closure.
Y(!factorial)(5) == 120
+11
View File
@@ -0,0 +1,11 @@
# \v (x(x))(v) delays evaluation of the recursive call until the function is
# actually invoked.
Z = \f (\x f(\v (x(x))(v)))
(\x f(\v (x(x))(v)));
factorial = \self\n
? n == 0
1
n * self(n - 1);
Z(factorial)(5) == 120
+35 -3
View File
@@ -103,7 +103,7 @@ class GrammarTests(SCLTestCase):
for source in (
"f(x) x + 1", "f(x) =", "f(1) = 2", "f(x = 1) = x",
"f()(x) = x", r"\1 -> 2", "if c then a else", "f(1,)",
"1#", "1;", "(1,2)", "1\n2", "then = 1",
"1%", "1;", "(1,2)", "1\n2", "then = 1",
"2 -> f(1) = 2", "(2,3) -> f(x,1) = x",
):
with self.subTest(source=source):
@@ -114,6 +114,23 @@ class GrammarTests(SCLTestCase):
self.assertEqual(self.evaluate(" \t"), "")
self.assert_number("1 \t", 1)
def test_comments(self):
for source in ("#", "# comment", " \t# comment\n\n # another\n\t"):
with self.subTest(source=source):
self.assertEqual(self.evaluate(source), "")
for source, expected in (
("1#", 1),
("1 # trailing comment", 1),
("# leading comment\n1", 1),
("# comment\n\n1", 1),
("# comment\n # another\n\t1", 1),
("1 # comment\n + 2", 3),
("1; # comment\n 2", 2),
):
with self.subTest(source=source):
self.assert_number(source, expected)
self.assert_syntax_error("1 # comment\n2")
def test_repl_recovers_after_failed_and_empty_parses(self):
import os
import subprocess
@@ -129,6 +146,21 @@ class GrammarTests(SCLTestCase):
def test_example_files(self):
directory = Path(__file__).resolve().parents[2] / "examples"
for name, expected in (("function.scl", 120), ("test.scl", 2), ("types.scl", 3)):
for name, expected in (
("factorial.scl", 120),
("lexical_scoping_functions.scl", 18),
("lexical_scoping_live.scl", 11),
("pipeline.scl", 16),
):
with self.subTest(name=name):
self.assert_number((directory / name).read_text().rstrip("\n"), expected)
self.assert_number((directory / name).read_text(), expected)
for name in (
"church_numerals.scl",
"higher_order_functions.scl",
"lambda.scl",
"lexical_scoping1.scl",
"ycomb.scl",
"zcomb.scl",
):
with self.subTest(name=name):
self.assert_boolean((directory / name).read_text(), True)
+23 -2
View File
@@ -178,13 +178,33 @@ static void test_equality_chains_and_groups(void) {
cleanup();
}
static void test_comments(void) {
const char* numeric[] = {
"1#", "1 # trailing comment", "# leading comment\n1",
"# comment\n\n1", "# comment\n # another\n\t1",
};
for (size_t i = 0; i < sizeof(numeric) / sizeof(*numeric); i++) {
AST* ast = expression(numeric[i]);
CHECK(ast->type == AST_TYPE_LIT_NUM);
CHECK(*(ASTNumData*)ast->data == 1);
cleanup();
}
const char* empty[] = {
"#", "# comment", " \t# comment\n\n # another\n\t",
};
for (size_t i = 0; i < sizeof(empty) / sizeof(*empty); i++) {
CHECK(parse(empty[i]) == NULL);
cleanup();
}
}
static void test_failures_and_repeated_parses(void) {
const char* invalid[] = {
"f(x) x + 1", "f(x) =", "f(1) = 2", "f(x = 1) = x",
"f()(x) = x", "1(x) = x", "\\1 -> 2", "\\(x,1) x",
"f(1,)", "f(1", "if c then a else", "if c a (b)",
"(a,b)", "1#", "f(x,y,", "x: Num =", "{ x = 1; f(x) = }",
"1\n2",
"(a,b)", "1%", "f(x,y,", "x: Num =", "{ x = 1; f(x) = }",
"1\n2", "1 # comment\n2",
};
for (size_t i = 0; i < sizeof(invalid) / sizeof(*invalid); i++) {
CHECK(parse("1") != NULL);
@@ -208,6 +228,7 @@ int main(void) {
RUN_TEST(test_lambda_forms);
RUN_TEST(test_conditional_boundaries);
RUN_TEST(test_equality_chains_and_groups);
RUN_TEST(test_comments);
RUN_TEST(test_failures_and_repeated_parses);
puts("Parser regressions passed.");
return 0;