Files
scl/test/integration/test_grammar.py
T
2026-11-01 01:06:36 -06:00

135 lines
5.5 KiB
Python

from pathlib import Path
from scl_test import SCLTestCase
class GrammarTests(SCLTestCase):
def test_zero_parameter_functions(self):
self.assert_number("answer() = 42; answer()", 42)
self.assert_number(r"(\() -> 42)()", 42)
def test_optional_lambda_separators(self):
for source in (
r"(\x x + 1)(2)", r"(\x -> x + 1)(2)",
r"(\(x) x + 1)(2)", r"(\(x) -> x + 1)(2)",
r"\x x + 1 $ (2)", r"\x -> x + 1 $ (2)",
):
with self.subTest(source=source):
self.assert_number(source, 3)
def test_compact_and_explicit_conditionals(self):
definitions = "f(x) = x + 1; g(x) = x * 2; "
for separator in ("", "then "):
for alternative in ("", "else "):
for keyword in ("if", "?"):
for predicate, expected in (("TRUE", 3), ("FALSE", 6)):
source = (
f"{keyword} {predicate} {separator}f(2) {alternative}g(3)"
)
with self.subTest(source=source):
self.assert_number(definitions + source, expected)
def test_nested_and_lazy_conditionals(self):
self.assert_number("if TRUE then if FALSE then 1 else 2 else 3", 2)
self.assert_number("if TRUE then 1 else missing", 1)
self.assert_number("if FALSE then missing else -2", -2)
def test_operator_precedence(self):
self.assert_number("x = 1 + 2; !x + 1", 4)
self.assert_number("-2 * 3 + 10", 4)
self.assert_number("f(x) = x + 2; f(3) - 1", 4)
self.assert_number("f(x) = x * 2; 1 + 2 -> f", 6)
self.assert_number("f(x) = x + 1; g(x) = x * 2; 3 -> f -> g", 8)
self.assert_number("f(x) = x + 1; g(x) = x * 2; f <- g <- 3", 7)
self.assert_number(r"f(x) = \y x - y; 2 -> f <- 10", 8)
def test_arrow_argument_bundles(self):
self.assert_number("sum(a,b) = a + b; (2,3) -> sum", 5)
self.assert_number("f(x) = x + 1; (2) -> f", 3)
self.assert_number(
"fac(n) = (n,1) -> (f(n,a) = if n == 1 a f(n - 1,a * n)); fac(5)",
120,
)
def test_named_function_definitions_as_arrow_targets(self):
for source, expected in (
("2 -> f(x) = x + 1", 3),
("(2,3) -> f(x,y) = x + y", 5),
("g(x) = x * 2; 2 -> f(x) = x + 1 -> g; f(4)", 10),
("g(x) = x * 2; 2 -> f(x) = x + 1 $ -> g; f(4)", 5),
("g(x) = x * 2; (2,3) -> f(x,y) = x + y $ -> g", 10),
(
"factorial(n) = (n,1) -> f(n,a) = "
"if n == 1 then a else f(n - 1,a * n); factorial(5)",
120,
),
):
with self.subTest(source=source):
self.assert_number(source, expected)
def test_typed_binding_and_nested_function_definition(self):
self.assert_number("x: Num = 3; x", 3)
self.assert_number("apply(f,x) = f(x); apply(g(y) = y + 1,2)", 3)
def test_numeric_equality_chains(self):
for source, expected in (
("1 + 2 == 3 * 1 == 3", True),
("1 == 1 == 2", False),
("1 == 2 == missing", False),
("eq(2,2,3)", False),
):
with self.subTest(source=source):
self.assert_boolean(source, expected)
def test_boolean_equality_and_grouped_comparisons(self):
for a in (False, True):
for b in (False, True):
for c in (False, True):
values = ["TRUE" if value else "FALSE" for value in (a, b, c)]
self.assert_boolean(" == ".join(values), a == b == c)
self.assert_boolean("(1 == 1) == TRUE", True)
self.assert_exception("1 == (1 == 1)", "different types")
def test_equality_preserves_errors(self):
for source in ("missing == 1", "1 == 1 == missing"):
with self.subTest(source=source):
self.assert_exception(
source, "`eq` encountered an exception.",
cause="Could not find value in scope for `missing`.",
)
self.assert_exception("1 == TRUE", "Cannot compare values of different types.")
def test_invalid_syntax_never_enters_repl(self):
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",
"2 -> f(1) = 2", "(2,3) -> f(x,1) = x",
):
with self.subTest(source=source):
self.assert_syntax_error(source)
def test_empty_input_and_trailing_whitespace(self):
self.assertEqual(self.evaluate(""), "")
self.assertEqual(self.evaluate(" \t"), "")
self.assert_number("1 \t", 1)
def test_repl_recovers_after_failed_and_empty_parses(self):
import os
import subprocess
binary = os.environ["SCL_TEST_BINARY"]
result = subprocess.run(
[binary], input="f(x) =\n \t\n2\n", capture_output=True,
text=True, timeout=5, check=False,
)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertIn("Parse error", result.stderr)
self.assertIn("2.000000", result.stdout)
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)):
with self.subTest(name=name):
self.assert_number((directory / name).read_text().rstrip("\n"), expected)