Skip to content
/ b3 Public
Open
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
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,19 @@ SPDX-License-Identifier: AGPL-3.0-only
- [x] Function (without destructuring and ellipsis)
- [x] Function Application
- [x] Deferred Values (Laziness)
- [ ] Control flow (if then else)
- Built-ins:
- [x] Addition (+)
- Binary operators:
- [x] Addition (+)
- [x] Subtraction (-)
- [x] Multiplication (*)
- [ ] Division (/)
- [ ] Concatenation (++)
- [ ] EqualTo (==)
- ...
- Unary operators:
- [ ] Negation (-)
- [ ] Not (!)
- Store interface:
- [ ] Rust trait
- Store implementations:
Expand Down
106 changes: 92 additions & 14 deletions src/interpreter/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,66 @@
use std::collections::LinkedList;
use std::rc::Rc;

use nixel::ast::StringPart;

use super::runtime_stack_frame::RuntimeStackFrame;
use crate::interpreter::error::Error;
use crate::interpreter::location::Location;
use crate::interpreter::scope::ScopeKind;
use crate::interpreter::value::Value;



macro_rules! build_math_fn {
($name:tt, $method:tt, $action:literal, $symbol:literal) => {
fn $name(
&mut self,
mut args: Vec<Rc<Value>>,
location: &Location,
) -> Result<Rc<Value>, Error> {
let rhs = args.remove(1);
let lhs = args.remove(0);

let lhs = self.advance_monotonically(lhs)?;
let rhs = self.advance_monotonically(rhs)?;

let action = $action;
let symbol = $symbol;

match (&*lhs, &*rhs) {
(Value::Int(lhs_value), Value::Int(rhs_value)) => {
//match lhs_value.checked_add(*rhs_value) {
//match lhs_value.checked_mul(*rhs_value) {
match lhs_value.$method(*rhs_value) {
Some(value) => Ok(Rc::new(Value::Int(value))),
None => Err(Error::Interpreter {
description: format!(
"integer overflow while {action} {lhs_value} and \
{rhs_value}"
),
location: location.clone(),
stack: self.stack.clone(),
}),
}
}
_ => Err(Error::Interpreter {
description: format!(
"built-in {symbol} is not implemented for operands of type {:?} \
and {:?}",
lhs.kind(),
rhs.kind(),
),
location: location.clone(),
stack: self.stack.clone(),
}),
}

}
};
}



#[derive(Debug)]
pub(crate) struct Runtime {
pub(crate) stack: LinkedList<RuntimeStackFrame>,
Expand Down Expand Up @@ -57,6 +111,9 @@ impl Runtime {
if *expected_arguments == arguments.len() {
let function = match identifier.as_str() {
"built-in +" => Runtime::built_in_addition,
"built-in -" => Runtime::built_in_subtraction,
"built-in *" => Runtime::built_in_multiplication,
//"built-in ++" => Runtime::built_in_concat,
_ => unreachable!(),
};

Expand Down Expand Up @@ -113,7 +170,7 @@ impl Runtime {
}
_ => Err(Error::Interpreter {
description: format!(
"calling a {:?} is not currently possible",
"attempt to call something which is not a function but a {:?}",
function.kind(),
),
location: location.clone(),
Expand Down Expand Up @@ -177,7 +234,28 @@ impl Runtime {
Ok(value)
}

fn built_in_addition(
build_math_fn!(
built_in_addition,
checked_add,
"adding",
"+"
);

build_math_fn!(
built_in_subtraction,
checked_sub,
"subtracting",
"-"
);

build_math_fn!(
built_in_multiplication,
checked_mul,
"multiplying",
"*"
);

fn built_in_concat(
&mut self,
mut args: Vec<Rc<Value>>,
location: &Location,
Expand All @@ -189,23 +267,23 @@ impl Runtime {
let rhs = self.advance_monotonically(rhs)?;

match (&*lhs, &*rhs) {
(Value::Int(lhs_value), Value::Int(rhs_value)) => {
match lhs_value.checked_add(*rhs_value) {
Some(value) => Ok(Rc::new(Value::Int(value))),
None => Err(Error::Interpreter {
description: format!(
"integer overflow while adding {lhs_value} and \
{rhs_value}"
),
location: location.clone(),
stack: self.stack.clone(),
}),
(Value::String { parts: lhs_value }, Value::String { parts: rhs_value }) => {
let mut new_value = LinkedList::<StringPart>::new();
for val in lhs_value {
new_value.push_back(*val);
// error[E0507]: cannot move out of `*val` which is behind a shared reference
// move occurs because `*val` has type `StringPart`, which does not implement the `Copy` trait
}
for val in rhs_value {
new_value.push_back(*val);
}
Ok(Rc::new(Value::String { parts: new_value }))
}
// TODO list + list
_ => Err(Error::Interpreter {
description: format!(
"built-in + is not implemented for operands of type {:?} \
and {:?}",
and {:?}",
lhs.kind(),
rhs.kind(),
),
Expand Down
118 changes: 73 additions & 45 deletions src/interpreter/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// SPDX-License-Identifier: AGPL-3.0-only

use std::rc::Rc;
use std::collections::LinkedList;

use nixel::ast::BinaryOperator;
use nixel::ast::UnaryOperator;
use nixel::ast::StringPart;
use nixel::ast::AST;

use super::location::Location;
Expand All @@ -13,6 +16,35 @@ use crate::interpreter::bindings::Bindings;
use crate::interpreter::scope::Scope;
use crate::interpreter::scope::ScopeKind;

macro_rules! value_function_application {
($arguments:tt, $function:expr, $path:tt, $scope:tt, $position:tt) => {
Value::FunctionApplication {
argument_index: 0,
arguments: $arguments
.into_iter()
.map(|ast| {
Rc::new(Value::DeferredValue {
ast, // error[E0308]: mismatched types
// expected enum `AST`, found struct `Box`
// help: consider unboxing the value
//ast: *ast, // error[E0614]: type `AST` cannot be dereferenced
path: $path.clone(),
scope: $scope.clone(),
})
})
.collect(),
function: Rc::new($function),
location: Location::InFileFragment(
LocationInFileFragment {
column: $position.column,
line: $position.line,
$path,
},
),
}
}
}

#[derive(Debug)]
pub(crate) enum Value {
Boolean(bool),
Expand Down Expand Up @@ -45,11 +77,16 @@ pub(crate) enum Value {
location: Location,
scope: Scope,
},
String {
parts: LinkedList<StringPart>,
}
}

impl Value {
pub(crate) fn from_ast(path: Rc<String>, ast: AST, scope: &Scope) -> Value {
match ast {
// https://github.com/kamadorueda/nixel/blob/main/src/ast.rs

AST::BinaryOperation { operands, operator, position } => {
let identifier = match operator {
BinaryOperator::Addition => "built-in +",
Expand All @@ -69,33 +106,38 @@ impl Value {
BinaryOperator::Update => "built-in //",
};

Value::FunctionApplication {
argument_index: 0,
arguments: operands
.into_iter()
.map(|ast| {
Rc::new(Value::DeferredValue {
ast,
path: path.clone(),
scope: scope.clone(),
})
})
.collect(),

function: Rc::new(Value::BuiltInFunction {
value_function_application!(
operands,
Value::BuiltInFunction {
identifier: identifier.to_string(),
expected_arguments: 2,
}),
location: Location::InFileFragment(
LocationInFileFragment {
column: position.column,
line: position.line,
path,
},
),
}
},
path, scope, position
)
}

AST::UnaryOperation { operand, operator, position } => {
let identifier = match operator {
UnaryOperator::Not => "built-in not",
UnaryOperator::Negate => "built-in negate",
};
let operands = vec![*operand];
value_function_application!(
operands,
Value::BuiltInFunction {
identifier: identifier.to_string(),
expected_arguments: 2,
},
path, scope, position
)
}

/*
AST::IfThenElse { predicate, then, else_, position } => {
// ...
}
*/

AST::Function { argument, definition, .. } => {
Value::Function {
bind_to: argument,
Expand All @@ -108,36 +150,21 @@ impl Value {

AST::FunctionApplication { arguments, function } => {
let position = function.position();

Value::FunctionApplication {
argument_index: 0,
arguments: arguments
.into_iter()
.map(|ast| {
Rc::new(Value::DeferredValue {
ast,
path: path.clone(),
scope: scope.clone(),
})
})
.collect(),
function: Rc::new(Value::from_ast(
value_function_application!(
arguments,
Value::from_ast(
path.clone(),
*function,
scope,
)),
location: Location::InFileFragment(
LocationInFileFragment {
column: position.column,
line: position.line,
path,
},
),
}
path, scope, position
)
}

AST::Int { value, .. } => Value::Int(value),

AST::String { parts, .. } => Value::String { parts },

AST::LetIn { bindings, target, position: _ } => {
let bindings = Bindings::new(bindings);

Expand Down Expand Up @@ -184,6 +211,7 @@ impl Value {
Value::FunctionApplication { .. } => "FunctionApplication",
Value::Int { .. } => "Int",
Value::Variable { .. } => "Variable",
Value::String { .. } => "String",
}
}
}
2 changes: 2 additions & 0 deletions tests/built_in_*/overflow/cli.args
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eval
tests/built_in_*/overflow/input.nix
5 changes: 5 additions & 0 deletions tests/built_in_*/overflow/input.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2022 Kevin Amado <kamadorueda@gmail.com>
#
# SPDX-License-Identifier: AGPL-3.0-only

9223372036854775807 * 2
11 changes: 11 additions & 0 deletions tests/built_in_*/overflow/output.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ERROR]: Interpreter error, most recent action last:

At "tests/built_in_*/overflow/input.nix", evaluating "built-in *"
> 5 | 9223372036854775807 * 2
^

At "tests/built_in_*/overflow/input.nix", integer overflow while multiplying 9223372036854775807 and 2
> 5 | 9223372036854775807 * 2
^


2 changes: 2 additions & 0 deletions tests/built_in_*/success/cli.args
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eval
tests/built_in_*/success/input.nix
5 changes: 5 additions & 0 deletions tests/built_in_*/success/input.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2022 Kevin Amado <kamadorueda@gmail.com>
#
# SPDX-License-Identifier: AGPL-3.0-only

2 * 3
3 changes: 3 additions & 0 deletions tests/built_in_*/success/output.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[INFO]: value = Int(
6,
)
2 changes: 2 additions & 0 deletions tests/built_in_-/overflow/cli.args
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eval
tests/built_in_-/overflow/input.nix
5 changes: 5 additions & 0 deletions tests/built_in_-/overflow/input.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# SPDX-FileCopyrightText: 2022 Kevin Amado <kamadorueda@gmail.com>
#
# SPDX-License-Identifier: AGPL-3.0-only

0 - 9223372036854775807 - 2
Loading