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
9 changes: 6 additions & 3 deletions benches/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ fn bench_increment_value(criterion: &mut Criterion) {

group.bench_function(format!("n = {number}"), |bencher| {
bencher.iter(|| {
black_box(Evaluator::from(Environment::default()).eval(&ast)).unwrap();
black_box(Evaluator::from(Environment::default()).evaluate(&ast))
.unwrap();
});
});
}
Expand Down Expand Up @@ -70,7 +71,8 @@ fn bench_prime_count(criterion: &mut Criterion) {

group.bench_function(format!("n = {number}"), |bencher| {
bencher.iter(|| {
black_box(Evaluator::from(Environment::default()).eval(&ast)).unwrap();
black_box(Evaluator::from(Environment::default()).evaluate(&ast))
.unwrap();
});
});
}
Expand All @@ -90,7 +92,8 @@ fn bench_recursive_factorial(criterion: &mut Criterion) {

group.bench_function(format!("n = {number}"), |bencher| {
bencher.iter(|| {
black_box(Evaluator::from(Environment::default()).eval(&ast)).unwrap();
black_box(Evaluator::from(Environment::default()).evaluate(&ast))
.unwrap();
});
});
}
Expand Down
28 changes: 28 additions & 0 deletions crates/val-wasm/src/ast_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,34 @@ impl From<(&Statement<'_>, &Span)> for AstNode {
}
}

impl From<(&AssignmentTarget<'_>, &Span)> for AstNode {
fn from(value: (&AssignmentTarget<'_>, &Span)) -> Self {
let (target, span) = value;

let range = Range::from(span);

let mut children = Vec::new();

match target {
AssignmentTarget::Identifier(_) => Self {
kind: target.kind(),
range,
children,
},
AssignmentTarget::ListAccess(list, index) => {
children.push(Self::from((&list.0, &list.1)));
children.push(Self::from((&index.0, &index.1)));

Self {
kind: target.kind(),
range,
children,
}
}
}
}
}

impl From<(&Expression<'_>, &Span)> for AstNode {
fn from(value: (&Expression<'_>, &Span)) -> Self {
let (expression, span) = value;
Expand Down
5 changes: 3 additions & 2 deletions crates/val-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use {
serde::Serialize,
serde_wasm_bindgen::to_value,
val::{
Environment, Evaluator, Expression, Program, RoundingMode, Span, Statement,
AssignmentTarget, Environment, Evaluator, Expression, Program,
RoundingMode, Span, Statement,
},
wasm_bindgen::prelude::*,
};
Expand Down Expand Up @@ -50,7 +51,7 @@ pub fn evaluate(input: &str) -> Result<JsValue, JsValue> {
rounding_mode: RoundingMode::FromZero.into(),
}));

match evaluator.eval(&ast) {
match evaluator.evaluate(&ast) {
Ok(value) => Ok(to_value(&value.to_string()).unwrap()),
Err(error) => Err(
to_value(&[ValError {
Expand Down
6 changes: 3 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ check:

[group: 'check']
ci: test clippy forbid
cargo +nightly fmt --all -- --check
cargo fmt --all -- --check
cargo update --locked --package val

[group: 'check']
Expand All @@ -43,11 +43,11 @@ clippy:

[group: 'format']
fmt:
cargo +nightly fmt
cargo fmt

[group: 'format']
fmt-check:
cargo +nightly fmt --all -- --check
cargo fmt --all -- --check

[group: 'format']
fmt-web:
Expand Down
1 change: 0 additions & 1 deletion rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
edition = "2018"
imports_granularity = "One"
max_width = 80
newline_style = "Unix"
tab_spaces = 2
Expand Down
12 changes: 6 additions & 6 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Arguments {
}));

match parse(&content) {
Ok(ast) => match evaluator.eval(&ast) {
Ok(ast) => match evaluator.evaluate(&ast) {
Ok(_) => Ok(()),
Err(error) => {
error
Expand All @@ -94,14 +94,14 @@ impl Arguments {
}
}

fn eval_expression(&self, value: String) -> Result {
fn evaluate_expression(&self, value: String) -> Result {
let mut evaluator = Evaluator::from(Environment::new(Config {
precision: self.precision,
rounding_mode: self.rounding_mode.into(),
}));

match parse(&value) {
Ok(ast) => match evaluator.eval(&ast) {
Ok(ast) => match evaluator.evaluate(&ast) {
Ok(value) => {
if let Value::Null = value {
return Ok(());
Expand Down Expand Up @@ -162,7 +162,7 @@ impl Arguments {
let filename = filename.to_string_lossy().to_string();

match parse(content) {
Ok(ast) => match evaluator.eval(&ast) {
Ok(ast) => match evaluator.evaluate(&ast) {
Ok(_) => {}
Err(error) => {
error
Expand Down Expand Up @@ -194,7 +194,7 @@ impl Arguments {
let line: &'static str = Box::leak(line.into_boxed_str());

match parse(line) {
Ok(ast) => match evaluator.eval(&ast) {
Ok(ast) => match evaluator.evaluate(&ast) {
Ok(value) if !matches!(value, Value::Null) => println!("{value}"),
Ok(_) => {}
Err(error) => error
Expand All @@ -215,7 +215,7 @@ impl Arguments {
pub fn run(self) -> Result {
match (&self.filename, &self.expression) {
(Some(filename), _) => self.eval(filename),
(_, Some(expression)) => self.eval_expression(expression.clone()),
(_, Some(expression)) => self.evaluate_expression(expression.clone()),
_ => {
#[cfg(not(target_family = "wasm"))]
{
Expand Down
51 changes: 50 additions & 1 deletion src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Program<'_> {

#[derive(Debug, Clone)]
pub enum Statement<'a> {
Assignment(Spanned<Expression<'a>>, Spanned<Expression<'a>>),
Assignment(Spanned<AssignmentTarget<'a>>, Spanned<Expression<'a>>),
Block(Vec<Spanned<Statement<'a>>>),
Break,
Continue,
Expand Down Expand Up @@ -179,6 +179,55 @@ impl Statement<'_> {
}
}

#[derive(Debug, Clone)]
pub enum AssignmentTarget<'a> {
Identifier(&'a str),
ListAccess(Box<Spanned<Self>>, Box<Spanned<Expression<'a>>>),
}

impl Display for AssignmentTarget<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
AssignmentTarget::Identifier(identifier) => {
write!(f, "identifier({identifier})")
}
AssignmentTarget::ListAccess(list, index) => {
write!(f, "list_access({}, {})", list.0, index.0)
}
}
}
}

impl AssignmentTarget<'_> {
#[must_use]
pub fn kind(&self) -> String {
String::from(match self {
AssignmentTarget::Identifier(_) => "identifier",
AssignmentTarget::ListAccess(_, _) => "list_access",
})
}
}

impl<'a> AssignmentTarget<'a> {
pub(crate) fn indices(&self) -> Vec<&Spanned<Expression<'a>>> {
match self {
AssignmentTarget::Identifier(_) => Vec::new(),
AssignmentTarget::ListAccess(base, index) => {
let mut indices = base.0.indices();
indices.push(index);
indices
}
}
}

pub(crate) fn root(&self, span: Span) -> (&'a str, Span) {
match self {
AssignmentTarget::Identifier(name) => (name, span),
AssignmentTarget::ListAccess(base, _) => base.0.root(base.1),
}
}
}

#[derive(Debug, Clone)]
pub enum UnaryOp {
Negate,
Expand Down
14 changes: 1 addition & 13 deletions src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,8 @@ pub enum Completion<'a> {
impl<'a> Completion<'a> {
pub(crate) fn unwrap(&self) -> Value<'a> {
match self {
Completion::Value(v) | Completion::Return(v) => v.clone(),
Completion::Return(value) | Completion::Value(value) => value.clone(),
Completion::Break | Completion::Continue => Value::Null,
}
}

pub(crate) fn is_return(&self) -> bool {
matches!(self, Completion::Return(_))
}

pub(crate) fn is_break(&self) -> bool {
matches!(self, Completion::Break)
}

pub(crate) fn is_continue(&self) -> bool {
matches!(self, Completion::Continue)
}
}
Loading
Loading