Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add complete support for constant dynamic collections #104

Merged
merged 1 commit into from
Sep 24, 2020
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
22 changes: 22 additions & 0 deletions crates/rune/src/compile/const_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ impl Compile<(&ConstValue, Span)> for Compiler<'_> {
ConstValue::Unit => {
self.asm.push(Inst::unit(), span);
}
ConstValue::Byte(b) => {
self.asm.push(Inst::byte(*b), span);
}
ConstValue::Char(c) => {
self.asm.push(Inst::char(*c), span);
}
ConstValue::Integer(n) => {
self.asm.push(Inst::integer(*n), span);
}
Expand All @@ -20,6 +26,10 @@ impl Compile<(&ConstValue, Span)> for Compiler<'_> {
let slot = self.unit.borrow_mut().new_static_string(&s)?;
self.asm.push(Inst::String { slot }, span);
}
ConstValue::Bytes(b) => {
let slot = self.unit.borrow_mut().new_static_bytes(b)?;
self.asm.push(Inst::Bytes { slot }, span);
}
ConstValue::Vec(vec) => {
for value in vec.iter() {
self.compile((value, span))?;
Expand All @@ -34,6 +44,18 @@ impl Compile<(&ConstValue, Span)> for Compiler<'_> {

self.asm.push(Inst::Tuple { count: tuple.len() }, span);
}
ConstValue::Object(object) => {
let slot = self
.unit
.borrow_mut()
.new_static_object_keys(&*object.keys)?;

for value in object.values.iter() {
self.compile((value, span))?;
}

self.asm.push(Inst::Object { slot }, span);
}
}

Ok(())
Expand Down
1 change: 1 addition & 0 deletions crates/rune/src/eval/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ impl Eval<&Ir> for IrInterpreter<'_> {
IrKind::Break(ir_break) => self.eval(ir_break, used),
IrKind::Vec(ir_vec) => self.eval(ir_vec, used),
IrKind::Tuple(ir_tuple) => self.eval(ir_tuple, used),
IrKind::Object(ir_object) => self.eval(ir_object, used),
}
}
}
18 changes: 18 additions & 0 deletions crates/rune/src/eval/ir_object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::eval::prelude::*;

impl Eval<&IrObject> for IrInterpreter<'_> {
fn eval(&mut self, ir_object: &IrObject, used: Used) -> Result<ConstValue, EvalOutcome> {
let mut keys = Vec::new();
let mut values = Vec::new();

for (key, value) in ir_object.assignments.iter() {
keys.push(key.clone());
values.push(self.eval(value, used)?);
}

Ok(ConstValue::Object(ConstObject {
keys: keys.into_boxed_slice(),
values: values.into_boxed_slice(),
}))
}
}
1 change: 1 addition & 0 deletions crates/rune/src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod ir_branches;
mod ir_break;
mod ir_decl;
mod ir_loop;
mod ir_object;
mod ir_scope;
mod ir_set;
mod ir_template;
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/eval/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ pub(crate) use crate::ir::*;
pub(crate) use crate::ir_interpreter::IrInterpreter;
pub(crate) use crate::CompileError;
pub(crate) use crate::Spanned;
pub(crate) use runestick::{ConstValue, Span};
pub(crate) use runestick::{ConstObject, ConstValue, Span};
pub(crate) use std::convert::TryFrom;
17 changes: 15 additions & 2 deletions crates/rune/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ pub struct Ir {

impl Ir {
/// Construct a new intermediate instruction.
pub fn new<K>(span: Span, kind: K) -> Self
pub fn new<S, K>(spanned: S, kind: K) -> Self
where
S: Spanned,
IrKind: From<K>,
{
Self {
span,
span: spanned.span(),
kind: IrKind::from(kind),
}
}
Expand Down Expand Up @@ -79,6 +80,8 @@ decl_kind! {
Vec(IrVec),
/// Constructing a tuple.
Tuple(IrTuple),
/// Constructing an object.
Object(IrObject),
}
}

Expand Down Expand Up @@ -205,6 +208,16 @@ pub struct IrTuple {
pub items: Box<[Ir]>,
}

/// Object expression.
#[derive(Debug, Clone, Spanned)]
pub struct IrObject {
/// Span of the object.
#[rune(span)]
pub span: Span,
/// Field initializations.
pub assignments: Box<[(Box<str>, Ir)]>,
}

/// Vector expression.
#[derive(Debug, Clone, Spanned)]
pub struct IrVec {
Expand Down
Loading