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
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "3"
members = ["ndc_macros", "ndc_bin", "ndc_lib", "ndc_lsp", "ndc_lexer", "benches", "tests"]
members = ["ndc_macros", "ndc_bin", "ndc_lib", "ndc_lsp", "ndc_lexer", "ndc_parser", "benches", "tests"]

[workspace.package]
edition = "2024"
Expand All @@ -19,6 +19,7 @@ factorial = "0.4.0"
itertools = "0.14.0"
ndc_lexer = { path = "ndc_lexer" }
ndc_lib = { path = "ndc_lib" }
ndc_parser = { path = "ndc_parser" }
ndc_lsp = { path = "ndc_lsp" }
ndc_macros = { path = "ndc_macros" }
num = "0.4.3"
Expand Down
2 changes: 1 addition & 1 deletion ndc_bin/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use miette::{Diagnostic, LabeledSpan, SourceSpan};
use ndc_lib::interpreter::InterpreterError;
use ndc_lexer::Span;
use ndc_lib::interpreter::InterpreterError;
use std::fmt;

fn span_to_source_span(span: Span) -> SourceSpan {
Expand Down
1 change: 1 addition & 0 deletions ndc_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ factorial.workspace = true
itertools.workspace = true
ndc_lexer.workspace = true
ndc_macros.workspace = true
ndc_parser.workspace = true
num.workspace = true
once_cell.workspace = true
ordered-float.workspace = true
Expand Down
30 changes: 17 additions & 13 deletions ndc_lib/src/interpreter/evaluate/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::{
ast::{Expression, ExpressionLocation},
interpreter::{function::FunctionCarrier, sequence::Sequence, value::Value},
};
use ndc_lexer::Span;
use itertools::Itertools;
use ndc_lexer::Span;
use std::cell::RefCell;
use std::cmp::min;
use std::ops::IndexMut;
Expand Down Expand Up @@ -223,13 +223,19 @@ pub fn get_at_index(
}
};

let value = map.try_borrow().into_evaluation_result(span)?.get(&key).cloned();
let value = map
.try_borrow()
.into_evaluation_result(span)?
.get(&key)
.cloned();

if let Some(value) = value {
Ok(value)
} else if let Some(default) = default {
let default_value = produce_default_value(default, environment, span)?;
map.try_borrow_mut().into_evaluation_result(span)?.insert(key, default_value.clone());
map.try_borrow_mut()
.into_evaluation_result(span)?
.insert(key, default_value.clone());
Ok(default_value)
} else {
Err(EvaluationError::key_not_found(&key, span).into())
Expand All @@ -249,17 +255,15 @@ pub(super) fn produce_default_value(
span: Span,
) -> EvaluationResult {
match default {
Value::Function(function) => {
match function.call_checked(&mut [], environment) {
Err(FunctionCarrier::FunctionTypeMismatch) => {
Err(FunctionCarrier::EvaluationError(EvaluationError::new(
"default function is not callable without arguments".to_string(),
span,
)))
}
a => a,
Value::Function(function) => match function.call_checked(&mut [], environment) {
Err(FunctionCarrier::FunctionTypeMismatch) => {
Err(FunctionCarrier::EvaluationError(EvaluationError::new(
"default function is not callable without arguments".to_string(),
span,
)))
}
}
a => a,
},
value => Ok(value.clone()),
}
}
Expand Down
14 changes: 9 additions & 5 deletions ndc_lib/src/interpreter/evaluate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use crate::interpreter::iterator::mut_value_to_iterator;
use crate::interpreter::num::Number;
use crate::interpreter::sequence::Sequence;
use crate::interpreter::value::Value;
use ndc_lexer::Span;
use index::{Offset, evaluate_as_index, get_at_index, set_at_index};
use itertools::Itertools;
use ndc_lexer::Span;
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
Expand Down Expand Up @@ -165,7 +165,8 @@ pub(crate) fn evaluate_expression(
} => {
let mut lhs_value = evaluate_expression(lhs_expression, environment)?;
let index = evaluate_as_index(index_expression, environment)?;
let value_at_index = get_at_index(&lhs_value, index.clone(), span, environment)?;
let value_at_index =
get_at_index(&lhs_value, index.clone(), span, environment)?;

let right_value = evaluate_expression(r_value, environment)?;

Expand Down Expand Up @@ -338,15 +339,19 @@ pub(crate) fn evaluate_expression(
resolve_and_call(function, evaluated_args, environment, span)?
}
Expression::FunctionDeclaration {
parameters: arguments,
parameters,
body,
resolved_name,
return_type,
pure,
..
} => {
let mut user_function = FunctionBody::Closure {
parameter_names: arguments.try_into_parameters()?,
parameter_names: parameters
.as_parameters()
.into_iter()
.map(|x| x.to_string())
.collect(),
body: *body.clone(),
return_type: return_type.clone().unwrap_or_else(StaticType::unit),
environment: environment.clone(),
Expand Down Expand Up @@ -647,7 +652,6 @@ pub(crate) fn evaluate_expression(
Ok(literal)
}


fn declare_or_assign_variable(
l_value: &Lvalue,
value: Value,
Expand Down
Loading