Skip to content

Commit

Permalink
Merge pull request #212 from calcit-lang/tuple-with-class
Browse files Browse the repository at this point in the history
new function &tuple:with-class
  • Loading branch information
csvwolf committed Jun 26, 2023
2 parents a409d9f + aff900c commit e278fe2
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit"
version = "0.7.0"
version = "0.7.2"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
7 changes: 7 additions & 0 deletions calcit/test.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@
:: :t 1
:: :t 1 2

let
a $ :: :a 1
%r $ defrecord! %demo
:get $ fn (self) 1
b $ &tuple:with-class a %r
assert= %r $ &tuple:class b

|test-effect $ quote
fn ()
log-title "|Testing effect"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@calcit/procs",
"version": "0.7.0",
"version": "0.7.2",
"main": "./lib/calcit.procs.mjs",
"devDependencies": {
"@types/node": "^20.2.5",
Expand Down
1 change: 1 addition & 0 deletions src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ fn handle_proc_internal(name: CalcitProc, args: &CalcitItems, call_stack: &CallS
CalcitProc::NativeTupleAssoc => meta::assoc(args),
CalcitProc::NativeTupleCount => meta::tuple_count(args),
CalcitProc::NativeTupleClass => meta::tuple_class(args),
CalcitProc::NativeTupleWithClass => meta::tuple_with_class(args),
// effects
CalcitProc::NativeDisplayStack => meta::display_stack(args, call_stack),
CalcitProc::Raise => effects::raise(args),
Expand Down
14 changes: 14 additions & 0 deletions src/builtins/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,20 @@ pub fn tuple_class(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
}
}

pub fn tuple_with_class(xs: &CalcitItems) -> Result<Calcit, CalcitErr> {
if xs.len() != 2 {
return CalcitErr::err_str(format!("tuple:with-class expected 2 arguments, got: {xs:?}"));
}
match (&xs[0], &xs[1]) {
(Calcit::Tuple(tag, extra, _), b @ Calcit::Record(..)) => {
Ok(Calcit::Tuple(tag.to_owned(), extra.to_owned(), Arc::new(b.to_owned())))
}
(a, Calcit::Record(..)) => CalcitErr::err_str(format!("&tuple:with-class expected a tuple, got: {a}")),
(Calcit::Tuple(..), b) => CalcitErr::err_str(format!("&tuple:with-class expected second argument in record, got: {b}")),
(a, b) => CalcitErr::err_str(format!("&tuple:with-class expected a tuple and a record, got: {a} {b}")),
}
}

pub fn no_op() -> Result<Calcit, CalcitErr> {
Ok(Calcit::Nil)
}
Expand Down
7 changes: 7 additions & 0 deletions src/cirru/calcit-core.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -1817,3 +1817,10 @@
if (some? y)
range (inc (&- (&+ x x) y)) y
range (inc (negate x)) x

|: $ quote
defmacro : (tag & args)
quasiquote
::
~ $ turn-tag tag
~@ args
2 changes: 1 addition & 1 deletion src/data/cirru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub fn code_to_calcit(xs: &Cirru, ns: Arc<str>, def: Arc<str>, coord: &[u8]) ->
location: Some(coord.to_vec()),
}),
_ => match s.chars().next().expect("load first char") {
':' => Ok(Calcit::tag(&s[1..])),
':' if s.len() > 1 && s.chars().nth(1) != Some(':') => Ok(Calcit::tag(&s[1..])),
'.' => {
if let Some(stripped) = s.strip_prefix(".-") {
Ok(Calcit::Method(stripped.into(), MethodKind::Access))
Expand Down
6 changes: 2 additions & 4 deletions src/primes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,10 @@ impl fmt::Display for Calcit {
Calcit::Tuple(tag, extra, _class) => {
let mut extra_str = String::from("");
for item in extra {
if !extra_str.is_empty() {
extra_str.push(' ');
}
extra_str.push(' ');
extra_str.push_str(&item.to_string())
}
f.write_str(&format!("(:: {tag} {extra_str})"))
f.write_str(&format!("(:: {tag}{extra_str})"))
}
Calcit::Buffer(buf) => {
f.write_str("(&buffer")?;
Expand Down
3 changes: 3 additions & 0 deletions src/primes/proc_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum CalcitProc {
NativeTupleAssoc,
NativeTupleCount,
NativeTupleClass,
NativeTupleWithClass,
NativeDisplayStack,
Raise,
Quit,
Expand Down Expand Up @@ -207,6 +208,7 @@ impl FromStr for CalcitProc {
"&tuple:assoc" => Ok(Self::NativeTupleAssoc),
"&tuple:count" => Ok(Self::NativeTupleCount),
"&tuple:class" => Ok(Self::NativeTupleClass),
"&tuple:with-class" => Ok(Self::NativeTupleWithClass),
// effects
"&display-stack" => Ok(Self::NativeDisplayStack),
"raise" => Ok(Self::Raise),
Expand Down Expand Up @@ -388,6 +390,7 @@ impl Display for CalcitProc {
Self::NativeTupleAssoc => write!(f, "&tuple:assoc"),
Self::NativeTupleCount => write!(f, "&tuple:count"),
Self::NativeTupleClass => write!(f, "&tuple:class"),
Self::NativeTupleWithClass => write!(f, "&tuple:with-class"),
Self::NativeDisplayStack => write!(f, "&display-stack"),
Self::Raise => write!(f, "raise"),
Self::Quit => write!(f, "quit!"),
Expand Down
2 changes: 1 addition & 1 deletion src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn evaluate_expr(expr: &Calcit, scope: &CalcitScope, file_ns: Arc<str>, call
Calcit::Buffer(..) => Ok(expr.to_owned()),
Calcit::CirruQuote(..) => Ok(expr.to_owned()),
Calcit::Recur(_) => unreachable!("recur not expected to be from symbol"),
Calcit::RawCode(_, code) => unreachable!("native code {} not expected to be evaluated", code),
Calcit::RawCode(_, code) => unreachable!("raw code `{}` cannot be called", code),
Calcit::List(xs) => match xs.get(0) {
None => Err(CalcitErr::use_msg_stack(format!("cannot evaluate empty expr: {expr}"), call_stack)),
Some(x) => {
Expand Down
11 changes: 9 additions & 2 deletions ts-src/calcit.procs.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// CALCIT VERSION
export const calcit_version = "0.7.0";
export const calcit_version = "0.7.2";

import { parse, ICirruNode } from "@cirru/parser.ts";
import { writeCirruCode } from "@cirru/writer.ts";
Expand Down Expand Up @@ -299,10 +299,17 @@ export let _$n_tuple_$o_count = function (xs: CalcitValue) {
};

export let _$n_tuple_$o_class = function (x: CalcitTuple) {
if (arguments.length !== 1) throw new Error("&tuple:class takes 1 arguments");
if (arguments.length !== 1) throw new Error("&tuple:class takes 1 argument");
return x.klass;
};

export let _$n_tuple_$o_with_class = function (x: CalcitTuple, y: CalcitRecord) {
if (arguments.length !== 2) throw new Error("&tuple:with-class takes 2 arguments");
if (!(x instanceof CalcitTuple)) throw new Error("&tuple:with-class expects a tuple");
if (!(y instanceof CalcitRecord)) throw new Error("&tuple:with-class expects second argument in record");
return new CalcitTuple(x.tag, x.extra, y);
};

export let _$n_record_$o_get = function (xs: CalcitValue, k: CalcitTag) {
if (arguments.length !== 2) {
throw new Error("record &get takes 2 arguments");
Expand Down

0 comments on commit e278fe2

Please sign in to comment.