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
32 changes: 23 additions & 9 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", "ndc_parser", "benches", "tests"]
members = ["ndc_macros", "ndc_bin", "ndc_lib", "ndc_lsp", "ndc_lexer", "ndc_parser", "ndc_stdlib", "benches", "tests"]

[workspace.package]
edition = "2024"
Expand All @@ -22,6 +22,7 @@ ndc_lib = { path = "ndc_lib" }
ndc_parser = { path = "ndc_parser" }
ndc_lsp = { path = "ndc_lsp" }
ndc_macros = { path = "ndc_macros" }
ndc_stdlib = { path = "ndc_stdlib" }
num = "0.4.3"
once_cell = "1.21.3"
ordered-float = "5.1.0"
Expand Down
1 change: 1 addition & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ license.workspace = true

[dependencies]
ndc_lib.workspace = true
ndc_stdlib.workspace = true
rand.workspace = true
criterion.workspace = true
rand_chacha.workspace = true
Expand Down
6 changes: 4 additions & 2 deletions benches/src/benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use criterion::{Criterion, criterion_group, criterion_main};
use ndc_lib::interpreter::{Interpreter, InterpreterError};
use ndc_lib::interpreter::Interpreter;
use ndc_lib::interpreter::InterpreterError;
use ndc_stdlib::WithStdlib;
use rand::{RngExt, SeedableRng};
use rand_chacha::ChaCha8Rng;
use std::fs;
Expand All @@ -8,7 +10,7 @@ use std::time::Duration;

fn run_string(input: &str) -> Result<String, InterpreterError> {
let buf: Vec<u8> = vec![];
let mut interpreter = Interpreter::new(buf);
let mut interpreter = Interpreter::new(buf).with_stdlib();
interpreter.run_str(std::hint::black_box(input))
}

Expand Down
1 change: 1 addition & 0 deletions ndc_bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ strsim.workspace = true
miette = { version = "7.6.0", features = ["fancy"] }
ndc_lexer.workspace = true
ndc_lib.workspace = true
ndc_stdlib.workspace = true
ndc_lsp.workspace = true
owo-colors.workspace = true
rustyline.workspace = true
Expand Down
5 changes: 4 additions & 1 deletion ndc_bin/src/docs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ndc_lib::interpreter::Interpreter;
use ndc_lib::interpreter::function::{Parameter, TypeSignature};
use ndc_stdlib::WithStdlib;
use std::cmp::Ordering;
use std::fmt::Write;
use strsim::normalized_damerau_levenshtein;
Expand All @@ -13,7 +14,9 @@ fn string_match(needle: &str, haystack: &str) -> bool {
}

pub fn docs(query: Option<&str>) -> anyhow::Result<()> {
let interpreter = Interpreter::new(Vec::new()); // Discard the output
let interpreter = Interpreter::new(Vec::new()) // Discard the output
.with_stdlib();

let functions = interpreter.environment().borrow().get_all_functions();

let matched_functions = functions
Expand Down
3 changes: 2 additions & 1 deletion ndc_bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use clap::{Parser, Subcommand};
use highlighter::{AndycppHighlighter, AndycppHighlighterState};
use miette::{NamedSource, highlighters::HighlighterState};
use ndc_lib::interpreter::{Interpreter, InterpreterError};
use ndc_stdlib::WithStdlib;
use std::path::PathBuf;
use std::process;
use std::{fs, io::Write};
Expand Down Expand Up @@ -118,7 +119,7 @@ fn main() -> anyhow::Result<()> {
let string = fs::read_to_string(path)?;

let stdout = std::io::stdout();
let mut interpreter = Interpreter::new(stdout);
let mut interpreter = Interpreter::new(stdout).with_stdlib();
match into_miette_result(interpreter.run_str(&string)) {
// we can just ignore successful runs because we have print statements
Ok(_final_value) => {}
Expand Down
3 changes: 2 additions & 1 deletion ndc_bin/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use itertools::Itertools;
use miette::highlighters::HighlighterState;
use ndc_lib::interpreter::Interpreter;
use ndc_stdlib::WithStdlib;
use rustyline::Helper;
use rustyline::config::Configurer;
use rustyline::error::ReadlineError;
Expand Down Expand Up @@ -36,7 +37,7 @@ pub fn run() -> anyhow::Result<()> {
rl.set_helper(Some(h));

let stdout = std::io::stdout();
let mut interpreter = Interpreter::new(stdout);
let mut interpreter = Interpreter::new(stdout).with_stdlib();
loop {
match rl.readline("λ ") {
Ok(line) => {
Expand Down
14 changes: 1 addition & 13 deletions ndc_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,15 @@ ahash = { workspace = true, optional = true }
anyhow.workspace = true
derive_more.workspace = true
derive_builder.workspace = true
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
rand.workspace = true
regex.workspace = true
ryu.workspace = true
self_cell.workspace = true
serde_json.workspace = true
tap.workspace = true
thiserror.workspace = true

# Crypto
md5 = { version = "0.8.0", optional = true }
sha1 = { version = "0.10.6", optional = true }

[features]
default = ["ahash", "crypto"]
default = ["ahash"]
ahash = ["dep:ahash"]
crypto = ["dep:md5", "dep:sha1"]
14 changes: 5 additions & 9 deletions ndc_lib/src/interpreter/environment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::interpreter::function::{Function, StaticType};

use ndc_parser::ResolvedVar;
use crate::interpreter::value::Value;
use ndc_parser::ResolvedVar;
use std::cell::RefCell;
use std::fmt;
use std::fmt::Formatter;
Expand Down Expand Up @@ -50,21 +50,17 @@ impl Environment {
}

#[must_use]
pub fn new_with_stdlib(writer: Box<dyn InterpreterOutput>) -> Self {
pub fn new(writer: Box<dyn InterpreterOutput>) -> Self {
let root = RootEnvironment {
output: writer,
global_functions: Default::default(),
};

let mut env = Self {
Self {
root: Rc::new(RefCell::new(root)),
parent: None,
values: Default::default(),
};

crate::stdlib::register(&mut env);

env
}
}

pub fn get_global_identifiers(&self) -> Vec<(String, StaticType)> {
Expand Down Expand Up @@ -181,7 +177,7 @@ impl Environment {

impl Default for Environment {
fn default() -> Self {
Self::new_with_stdlib(Box::new(stdout()))
Self::new(Box::new(stdout()))
}
}

Expand Down
12 changes: 7 additions & 5 deletions ndc_lib/src/interpreter/evaluate/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
//! | Backward index | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
//! +----------------+-----+----+----+----+----+----+----+----+----+----+

use ndc_parser::{Expression, ExpressionLocation};
use super::{EvaluationError, EvaluationResult, IntoEvaluationResult, evaluate_expression};
use crate::interpreter::environment::Environment;
use crate::{
interpreter::{function::FunctionCarrier, sequence::Sequence, value::Value},
};
use crate::interpreter::{function::FunctionCarrier, sequence::Sequence, value::Value};
use itertools::Itertools;
use ndc_lexer::Span;
use ndc_parser::{Expression, ExpressionLocation};
use std::cell::RefCell;
use std::cmp::min;
use std::ops::IndexMut;
Expand Down Expand Up @@ -300,7 +298,11 @@ pub fn set_at_index(
Offset::Range(from_usize, to_usize) => {
let tail = list.drain(from_usize..).collect::<Vec<_>>();

list.extend(rhs.try_into_vec().expect("this must succeed, but not sure why").into_iter());
list.extend(
rhs.try_into_vec()
.expect("this must succeed, but not sure why")
.into_iter(),
);

list.extend_from_slice(&tail[(to_usize - from_usize)..]);
}
Expand Down
6 changes: 3 additions & 3 deletions ndc_lib/src/interpreter/evaluate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use ndc_parser::{
Binding, Expression, ExpressionLocation, ForBody, ForIteration, LogicalOperator, Lvalue,
};
use crate::hash_map::HashMap;
use crate::interpreter::environment::Environment;
use crate::interpreter::function::{Function, FunctionBody, FunctionCarrier, StaticType};
Expand All @@ -12,6 +9,9 @@ use crate::interpreter::value::Value;
use index::{Offset, evaluate_as_index, get_at_index, set_at_index};
use itertools::Itertools;
use ndc_lexer::Span;
use ndc_parser::{
Binding, Expression, ExpressionLocation, ForBody, ForIteration, LogicalOperator, Lvalue,
};
use std::cell::RefCell;
use std::fmt;
use std::rc::Rc;
Expand Down
2 changes: 1 addition & 1 deletion ndc_lib/src/interpreter/function.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use ndc_parser::{ExpressionLocation, ResolvedVar};
use crate::hash_map::{DefaultHasher, HashMap};
use crate::interpreter::environment::Environment;
use crate::interpreter::evaluate::{
Expand All @@ -9,6 +8,7 @@ use crate::interpreter::sequence::Sequence;
use crate::interpreter::value::Value;
use derive_builder::Builder;
use ndc_lexer::Span;
use ndc_parser::{ExpressionLocation, ResolvedVar};
pub use ndc_parser::{Parameter, StaticType, TypeSignature};
use std::cell::{BorrowError, BorrowMutError, RefCell};
use std::fmt;
Expand Down
17 changes: 13 additions & 4 deletions ndc_lib/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::cell::RefCell;
use std::rc::Rc;

use ndc_parser::ExpressionLocation;
use crate::interpreter::environment::{Environment, InterpreterOutput};
use crate::interpreter::evaluate::{EvaluationError, evaluate_expression};
use crate::interpreter::function::FunctionCarrier;
use crate::interpreter::semantic::analyser::{Analyser, ScopeTree};
use crate::interpreter::value::Value;
use ndc_lexer::{Lexer, TokenLocation};
use ndc_parser::ExpressionLocation;
pub mod environment;
pub mod evaluate;
pub mod function;
pub(crate) mod heap;
pub mod heap;
pub mod int;
pub mod iterator;
pub mod num;
Expand All @@ -30,15 +30,24 @@ impl Interpreter {
where
T: InterpreterOutput + 'static,
{
let environment = Environment::new_with_stdlib(Box::new(dest));
let global_identifiers = environment.get_global_identifiers();
Self::from_env(Environment::new(Box::new(dest)))
}

#[must_use]
pub fn from_env(environment: Environment) -> Self {
let global_identifiers = environment.get_global_identifiers();
Self {
environment: Rc::new(RefCell::new(environment)),
analyser: Analyser::from_scope_tree(ScopeTree::from_global_scope(global_identifiers)),
}
}

pub fn configure<F: FnOnce(&mut Environment)>(&mut self, f: F) {
f(&mut self.environment.borrow_mut());
let global_identifiers = self.environment.borrow().get_global_identifiers();
self.analyser = Analyser::from_scope_tree(ScopeTree::from_global_scope(global_identifiers));
}

#[must_use]
pub fn environment(self) -> Rc<RefCell<Environment>> {
self.environment
Expand Down
2 changes: 1 addition & 1 deletion ndc_lib/src/interpreter/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use std::hash::{Hash, Hasher};
use std::num::TryFromIntError;
use std::ops::{Add, Div, Mul, Neg, Not, Rem, Sub};

use ndc_parser::BinaryOperator;
use crate::interpreter::evaluate::EvaluationError;
use crate::interpreter::function::StaticType;
use crate::interpreter::int::Int;
use ndc_lexer::Span;
use ndc_parser::BinaryOperator;
use num::bigint::TryFromBigIntError;
use num::complex::{Complex64, ComplexFloat};
use num::{BigInt, BigRational, Complex, FromPrimitive, Signed, ToPrimitive, Zero};
Expand Down
Loading