Skip to content

Commit 745ffb3

Browse files
committed
✨ Use ndc_parser and ndc_lexer crates directly across the codebase
1 parent af286a1 commit 745ffb3

12 files changed

Lines changed: 14 additions & 62 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ndc_lib/src/interpreter/environment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::interpreter::function::{Function, StaticType};
22

3-
use crate::ast::ResolvedVar;
3+
use ndc_parser::ResolvedVar;
44
use crate::interpreter::value::Value;
55
use std::cell::RefCell;
66
use std::fmt;

ndc_lib/src/interpreter/evaluate/index.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
//! | Backward index | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
1010
//! +----------------+-----+----+----+----+----+----+----+----+----+----+
1111
12+
use ndc_parser::{Expression, ExpressionLocation};
1213
use super::{EvaluationError, EvaluationResult, IntoEvaluationResult, evaluate_expression};
1314
use crate::interpreter::environment::Environment;
1415
use crate::{
15-
ast::{Expression, ExpressionLocation},
1616
interpreter::{function::FunctionCarrier, sequence::Sequence, value::Value},
1717
};
1818
use itertools::Itertools;
@@ -300,8 +300,7 @@ pub fn set_at_index(
300300
Offset::Range(from_usize, to_usize) => {
301301
let tail = list.drain(from_usize..).collect::<Vec<_>>();
302302

303-
// TODO: why is this unwrap safe
304-
list.extend(rhs.try_into_iter().unwrap());
303+
list.extend(rhs.try_into_vec().expect("this must succeed, but not sure why").into_iter());
305304

306305
list.extend_from_slice(&tail[(to_usize - from_usize)..]);
307306
}

ndc_lib/src/interpreter/evaluate/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::ast::{
1+
use ndc_parser::{
22
Binding, Expression, ExpressionLocation, ForBody, ForIteration, LogicalOperator, Lvalue,
33
};
44
use crate::hash_map::HashMap;

ndc_lib/src/interpreter/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::ast::{ExpressionLocation, ResolvedVar};
1+
use ndc_parser::{ExpressionLocation, ResolvedVar};
22
use crate::hash_map::{DefaultHasher, HashMap};
33
use crate::interpreter::environment::Environment;
44
use crate::interpreter::evaluate::{

ndc_lib/src/interpreter/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::cell::RefCell;
22
use std::rc::Rc;
33

4-
use crate::ast::ExpressionLocation;
4+
use ndc_parser::ExpressionLocation;
55
use crate::interpreter::environment::{Environment, InterpreterOutput};
66
use crate::interpreter::evaluate::{EvaluationError, evaluate_expression};
77
use crate::interpreter::function::FunctionCarrier;
@@ -62,7 +62,7 @@ impl Interpreter {
6262
input: &str,
6363
) -> Result<Vec<ExpressionLocation>, InterpreterError> {
6464
let tokens = Lexer::new(input).collect::<Result<Vec<TokenLocation>, _>>()?;
65-
let mut expressions = crate::ast::Parser::from_tokens(tokens).parse()?;
65+
let mut expressions = ndc_parser::Parser::from_tokens(tokens).parse()?;
6666

6767
let checkpoint = self.analyser.checkpoint();
6868
for e in &mut expressions {
@@ -123,7 +123,7 @@ pub enum InterpreterError {
123123
#[error("Error while parsing source")]
124124
Parser {
125125
#[from]
126-
cause: crate::ast::Error,
126+
cause: ndc_parser::Error,
127127
},
128128
#[error("Error during static analysis")]
129129
Resolver {

ndc_lib/src/interpreter/num.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::hash::{Hash, Hasher};
44
use std::num::TryFromIntError;
55
use std::ops::{Add, Div, Mul, Neg, Not, Rem, Sub};
66

7-
use crate::ast::BinaryOperator;
7+
use ndc_parser::BinaryOperator;
88
use crate::interpreter::evaluate::EvaluationError;
99
use crate::interpreter::function::StaticType;
1010
use crate::interpreter::int::Int;

ndc_lib/src/interpreter/semantic/analyser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::ast::{
1+
use ndc_parser::{
22
Binding, Expression, ExpressionLocation, ForBody, ForIteration, Lvalue, ResolvedVar,
33
};
44
use crate::interpreter::function::StaticType;

ndc_lib/src/interpreter/value.rs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -77,53 +77,6 @@ impl Value {
7777
}
7878
}
7979

80-
// The alternate solution in this SO thread has a nice way to iterate over the contents of a
81-
// RefCell but the iterator would not be compatible with non refcell items
82-
// https://stackoverflow.com/questions/33541492/returning-iterator-of-a-vec-in-a-refcell
83-
//
84-
// Note: this method is called `try_into_iter` but it doesn't always create an iterator over
85-
// the original value. In most cases you get an iterator over a copy of the data.
86-
#[must_use]
87-
#[deprecated = "use try_into_vec"]
88-
pub fn try_into_iter(self) -> Option<impl Iterator<Item = Self>> {
89-
match self {
90-
Self::Sequence(Sequence::List(list)) => match Rc::try_unwrap(list) {
91-
// This short circuit is almost certainly wrong because take will panic if list is borrowed
92-
Ok(list) => Some(list.into_inner().into_iter()),
93-
Err(list) => Some(Vec::clone(&*list.borrow()).into_iter()),
94-
},
95-
Self::Sequence(Sequence::Tuple(list)) => match Rc::try_unwrap(list) {
96-
Ok(list) => Some(list.into_iter()),
97-
Err(list) => Some(Vec::clone(&list).into_iter()),
98-
},
99-
Self::Sequence(Sequence::Map(map, _)) => {
100-
let x = map.borrow().keys().cloned().collect_vec().into_iter();
101-
Some(x)
102-
}
103-
Self::Sequence(Sequence::String(string)) => match Rc::try_unwrap(string) {
104-
// This implementation is peak retard, we don't want collect_vec here
105-
// ^-- WTF: is this comment, we collect_vec here anyways?
106-
Ok(string) => Some(
107-
string
108-
.into_inner()
109-
.chars()
110-
.map(Self::from)
111-
.collect_vec()
112-
.into_iter(),
113-
),
114-
Err(string) => Some(
115-
string
116-
.borrow()
117-
.chars()
118-
.map(Self::from)
119-
.collect_vec()
120-
.into_iter(),
121-
),
122-
},
123-
_ => None,
124-
}
125-
}
126-
12780
pub fn try_into_vec(self) -> Option<Vec<Self>> {
12881
match self {
12982
Self::Sequence(Sequence::List(list)) => match Rc::try_unwrap(list) {

ndc_lib/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
pub use ndc_parser as ast;
21
mod compare;
32
mod hash_map;
43
pub mod interpreter;

0 commit comments

Comments
 (0)