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

WIP: Adding scalars and scalar indexing #135

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
15 changes: 8 additions & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ jobs:
run: |
mkdir -p dist
ls ./target/${{ matrix.target }}/release/r*
cp ./target/${{ matrix.target }}/release/r dist/
cp ./target/${{ matrix.target }}/release/r dist/r-${{ matrix.build }}

- uses: actions/upload-artifact@v3
with:
Expand All @@ -126,21 +126,22 @@ jobs:
uses: actions/checkout@v4

- uses: actions/download-artifact@v3
with:
path: dist

- name: Check Artifacts
shell: bash
run: |
ls .
- name: Display structure of downloaded files
working-directory: dist
run: ls -R

- name: Upload binaries to release
uses: softprops/action-gh-release@v1
if: env.preview == 'false'
with:
files: r-*
files: dist/**/*

- name: Upload binaries as artifact
uses: actions/upload-artifact@v3
if: env.preview == 'true'
with:
name: release
path: r-*
path: dist/*
2 changes: 1 addition & 1 deletion r_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub fn derive_translate(_input: TokenStream) -> TokenStream {
Rule::negate => en::Rule::negate,
Rule::postfix => en::Rule::postfix,
Rule::call => en::Rule::call,
Rule::index => en::Rule::index,
// Rule::index => en::Rule::index,
Rule::vector_index => en::Rule::vector_index,
Rule::block => en::Rule::block,
Rule::block_exprs => en::Rule::block_exprs,
Expand Down
21 changes: 16 additions & 5 deletions src/callable/operators.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::ops::BitAnd;

use r_derive::*;

use super::core::*;
use crate::context::Context;
use crate::error::Error;
use crate::lang::{CallStack, EvalResult};
use crate::object::types::*;
use crate::object::*;
use crate::{internal_err, object::*};

#[derive(Debug, Clone, PartialEq)]
#[builtin(sym = "<-", kind = Infix)]
Expand Down Expand Up @@ -103,9 +105,14 @@ impl Callable for InfixOr {
fn call(&self, args: ExprList, stack: &mut CallStack) -> EvalResult {
let (lhs, rhs) = stack.eval_binary(args.unnamed_binary_args())?;
let res = match (lhs, rhs) {
(Obj::Scalar(l), Obj::Scalar(r)) => (l.try_into()? && r.try_into()?).into(),
(Obj::Vector(l), Obj::Vector(r)) => {
let Ok(lhs) = l.try_into() else { todo!() };
let Ok(rhs) = r.try_into() else { todo!() };
let Ok(lhs) = l.try_into() else {
return internal_err!();
};
let Ok(rhs) = r.try_into() else {
return internal_err!();
};
Obj::Vector(Vector::from(vec![OptionNA::Some(lhs || rhs)]))
}
_ => Obj::Null,
Expand All @@ -123,8 +130,12 @@ impl Callable for InfixAnd {
let (lhs, rhs) = stack.eval_binary(args.unnamed_binary_args())?;
let res = match (lhs, rhs) {
(Obj::Vector(l), Obj::Vector(r)) => {
let Ok(lhs) = l.try_into() else { todo!() };
let Ok(rhs) = r.try_into() else { todo!() };
let Ok(lhs) = l.try_into() else {
return internal_err!();
};
let Ok(rhs) = r.try_into() else {
return internal_err!();
};
Obj::Vector(Vector::from(vec![OptionNA::Some(lhs && rhs)]))
}
_ => Obj::Null,
Expand Down
31 changes: 26 additions & 5 deletions src/callable/primitive/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl Callable for PrimitiveC {
fn call(&self, args: ExprList, stack: &mut CallStack) -> EvalResult {
// this can be cleaned up quite a bit, but I just need it working with
// more types for now to test vectorized operators using different types
use Scalar::*;

let Obj::List(vals) = stack.eval_list_eager(args)? else {
unreachable!()
Expand All @@ -25,7 +26,7 @@ impl Callable for PrimitiveC {
.iter()
.map(|(_, v)| match v {
Obj::Null => 0,
Obj::Vector(_) => 1,
Obj::Scalar(_) | Obj::Vector(_) => 1,
Obj::List(_) => 2,
_ => 0,
})
Expand All @@ -47,10 +48,18 @@ impl Callable for PrimitiveC {
.borrow()
.iter()
.map(|(_, r)| match r {
Obj::Vector(Vector::Logical(_)) => Vector::from(Vec::<Logical>::new()),
Obj::Vector(Vector::Integer(_)) => Vector::from(Vec::<Integer>::new()),
Obj::Vector(Vector::Double(_)) => Vector::from(Vec::<Double>::new()),
Obj::Vector(Vector::Character(_)) => Vector::from(Vec::<Character>::new()),
Obj::Scalar(Bool(_)) | Obj::Vector(Vector::Logical(_)) => {
Vector::from(Vec::<Logical>::new())
}
Obj::Scalar(I32(_)) | Obj::Vector(Vector::Integer(_)) => {
Vector::from(Vec::<Integer>::new())
}
Obj::Scalar(F64(_)) | Obj::Vector(Vector::Double(_)) => {
Vector::from(Vec::<Double>::new())
}
Obj::Scalar(String(_)) | Obj::Vector(Vector::Character(_)) => {
Vector::from(Vec::<Character>::new())
}
_ => unreachable!(),
})
.fold(Vector::from(Vec::<Logical>::new()), |l, r| match (l, r) {
Expand All @@ -77,6 +86,9 @@ impl Callable for PrimitiveC {
.clone()
.into_iter()
.flat_map(|(_, i)| match i.as_character() {
Ok(Obj::Scalar(String(ScalarType(x)))) => {
vec![OptionNA::Some(x)].into_iter()
}
Ok(Obj::Vector(Vector::Character(v))) => {
v.inner().clone().borrow().clone().into_iter()
}
Expand All @@ -97,6 +109,9 @@ impl Callable for PrimitiveC {
.clone()
.into_iter()
.flat_map(|(_, i)| match i.as_double() {
Ok(Obj::Scalar(F64(ScalarType(x)))) => {
vec![OptionNA::Some(x)].into_iter()
}
Ok(Obj::Vector(Vector::Double(v))) => {
v.inner().clone().borrow().clone().into_iter()
}
Expand All @@ -117,6 +132,9 @@ impl Callable for PrimitiveC {
.clone()
.into_iter()
.flat_map(|(_, i)| match i.as_integer() {
Ok(Obj::Scalar(I32(ScalarType(x)))) => {
vec![OptionNA::Some(x)].into_iter()
}
Ok(Obj::Vector(Vector::Integer(v))) => {
v.inner().clone().borrow().clone().into_iter()
}
Expand All @@ -137,6 +155,9 @@ impl Callable for PrimitiveC {
.clone()
.into_iter()
.flat_map(|(_, i)| match i.as_logical() {
Ok(Obj::Scalar(Bool(ScalarType(x)))) => {
vec![OptionNA::Some(x)].into_iter()
}
Ok(Obj::Vector(Vector::Logical(v))) => {
v.inner().clone().borrow().clone().into_iter()
}
Expand Down
1 change: 1 addition & 0 deletions src/callable/primitive/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl Callable for PrimitiveNames {
match x {
Null => Ok(Null),
Promise(..) => Ok(Null),
Scalar(..) => Ok(Null),
Vector(..) => Ok(Null), // named vectors currently not supported...
Expr(..) => Ok(Null), // handle arg lists?
Function(..) => Ok(Null), // return formals?
Expand Down
4 changes: 2 additions & 2 deletions src/grammar/grammar.pest
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@
prefix = _{ subtract | negate | more }
negate = { "!" }

postfix = _{ call | index | vector_index | more }
postfix = _{ call | vector_index | more } // | index }
call = { "(" ~ pairs ~ ")" }
index = { "[[" ~ pairs ~ "]]" }
vector_index = { "[" ~ pairs ~ "]" }
// index = { "[[" ~ pairs ~ "]]" }

standalone = _{ more }

Expand Down
Loading
Loading