Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
281 changes: 251 additions & 30 deletions crates/pine-sema/src/analyzer.rs

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions crates/pine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,13 @@ impl<O: PineOutput> ScriptBuilder<O> {
builtins.insert(name.clone(), value.clone());
}

// Semantic pre-check: reject invalid programs before execution.
let diagnostics = pine_sema::analyze(&program, &builtins);
if !diagnostics.is_empty() {
return Err(Error::Sema(diagnostics));
// Semantic pre-check: reject if sema produces errors
let errors: Vec<_> = pine_sema::analyze(&program, &builtins)
.into_iter()
.filter(|diagnostic| diagnostic.severity == pine_diagnostics::Severity::Error)
.collect();
if !errors.is_empty() {
return Err(Error::Sema(errors));
}

// Create interpreter and load builtin namespace objects
Expand Down
8 changes: 8 additions & 0 deletions tests/testdata/errors/duplicate_parameter.pine
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@version=5
indicator("errors/duplicate_parameter")
// A parameter name may appear only once.

f(x, x) => x

// Expected error:
// error [duplicate-parameter]: parameter `x` is declared more than once
9 changes: 9 additions & 0 deletions tests/testdata/errors/not_callable.pine
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@version=5
indicator("errors/not_callable")
// A value cannot be called like a function.

x = 5
y = x()

// Expected error:
// error [not-callable] 6:6: `x` is a variable, not a function
8 changes: 8 additions & 0 deletions tests/testdata/errors/recursion.pine
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@version=5
indicator("errors/recursion")
// Pine does not allow a function to call itself.

f(x) => f(x - 1)

// Expected error:
// error [recursion] 5:10: `f` calls itself; Pine does not allow recursion
10 changes: 10 additions & 0 deletions tests/testdata/errors/recursion_through_helper.pine
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@version=5
indicator("errors/recursion_through_helper")
// Recursion is found even when the self-call is nested inside another call; the
// non-recursive helper is not flagged.

double(x) => x * 2
f(x) => double(f(x))

// Expected error:
// error [recursion] 7:17: `f` calls itself; Pine does not allow recursion
8 changes: 8 additions & 0 deletions tests/testdata/errors/unknown_type.pine
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//@version=5
indicator("errors/unknown_type")
// A type annotation must name a built-in or a declared type.

UnknownType x = na

// Expected error:
// error [unknown-type]: unknown type `UnknownType`
9 changes: 9 additions & 0 deletions tests/testdata/errors/user_function_too_few_args.pine
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@version=5
indicator("errors/user_function_too_few_args")
// A user function requires all of its non-defaulted parameters.

f(a, b) => a + b
x = f(1)

// Expected error:
// error [too-few-arguments] 6:6: `f` requires at least 2 arguments, found 1
9 changes: 9 additions & 0 deletions tests/testdata/errors/user_function_too_many_args.pine
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@version=5
indicator("errors/user_function_too_many_args")
// A user function rejects more arguments than it declares.

f(a) => a
x = f(1, 2)

// Expected error:
// error [too-many-arguments] 6:6: `f` takes at most 1 arguments, found 2
Loading