Skip to content

Commit

Permalink
Merge pull request #228 from calcit-lang/sort-fields
Browse files Browse the repository at this point in the history
sort map fields in custom formatter
  • Loading branch information
NoEgAm authored Nov 27, 2023
2 parents 884deb6 + cf65ed3 commit 6548c28
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 42 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ jobs:
- run: cargo run --bin cr calcit/editor/compact.cirru --once
- run: cargo run --bin cr calcit/test.cirru --once

- uses: actions-rs/clippy-check@v1
- uses: giraffate/clippy-action@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features
reporter: 'github-pr-review'
github_token: ${{ secrets.GITHUB_TOKEN }}

- name: "try js"
run: >
Expand Down
17 changes: 12 additions & 5 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "calcit"
version = "0.8.9"
version = "0.8.12"
authors = ["jiyinyiyong <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down Expand Up @@ -30,7 +30,7 @@ notify = "6.1.1"
notify-debouncer-mini = "0.4.1"
walkdir = "2.4.0"
hex = "0.4.3"
rpds = "1.0.1"
rpds = "1.1.0"
im_ternary_tree = "0.0.11"
# im_ternary_tree = { path = "/Users/chenyong/repo/calcit-lang/ternary-tree.rs" }
colored = "2.0.4"
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@calcit/procs",
"version": "0.8.9",
"version": "0.8.12",
"main": "./lib/calcit.procs.mjs",
"devDependencies": {
"@types/node": "^20.8.6",
"typescript": "^5.2.2"
"@types/node": "^20.9.4",
"typescript": "^5.3.2"
},
"scripts": {
"compile": "rm -rfv lib/* && tsc",
Expand Down
3 changes: 3 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

[toolchain]
channel = "nightly"
8 changes: 4 additions & 4 deletions src/bin/bundle_calcit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,23 @@ fn load_files_to_edn(package_file: &Path, base_dir: &Path, verbose: bool) -> Res
let xs = cirru_parser::parse(&content).map_err(io_err)?;

let mut file = EdnRecordView::new(EdnTag::new("FileEntry"));
let (ns_name, ns_code) = if let Some(Cirru::List(ns_form)) = xs.get(0) {
match (ns_form.get(0), ns_form.get(1)) {
let (ns_name, ns_code) = if let Some(Cirru::List(ns_form)) = xs.first() {
match (ns_form.first(), ns_form.get(1)) {
(Some(Cirru::Leaf(x0)), Some(Cirru::Leaf(x1))) if &**x0 == "ns" => (x1.to_string(), ns_form),
(a, b) => return Err(io_err(format!("in valid ns starts {a:?} {b:?}"))),
}
} else {
return Err(io_err(format!(
"first expression of file should be a ns form, got: {:?}",
xs.get(0)
xs.first()
)));
};
file.insert(EdnTag::new("ns"), CodeEntry::from_code(Cirru::List(ns_code.to_owned())).into());

let mut defs = EdnMapView::default();
for line in xs.iter().skip(1) {
if let Cirru::List(ys) = line {
match (ys.get(0), ys.get(1)) {
match (ys.first(), ys.get(1)) {
(Some(Cirru::Leaf(x0)), Some(Cirru::Leaf(x1))) => {
let x0 = &**x0;
if x0 == "def" || x0 == "defn" || x0 == "defmacro" || x0 == "defatom" || x0 == "defrecord" || x0.starts_with("def") {
Expand Down
8 changes: 4 additions & 4 deletions src/builtins/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ pub fn foldl_shortcut(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Ca
match pair {
Calcit::Tuple(x0, extra, _class) => match &*x0 {
Calcit::Bool(b) => {
let x1 = extra.get(0).ok_or(CalcitErr::use_msg_stack_location(
let x1 = extra.first().ok_or(CalcitErr::use_msg_stack_location(
"foldl-shortcut list expected value in tuple",
call_stack,
x0.get_location(),
Expand Down Expand Up @@ -326,7 +326,7 @@ pub fn foldl_shortcut(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Ca
match pair {
Calcit::Tuple(x0, extra, _class) => match &*x0 {
Calcit::Bool(b) => {
let x1 = extra.get(0).ok_or(CalcitErr::use_msg_stack_location(
let x1 = extra.first().ok_or(CalcitErr::use_msg_stack_location(
"foldl set expected value in tuple",
call_stack,
x0.get_location(),
Expand Down Expand Up @@ -369,7 +369,7 @@ pub fn foldl_shortcut(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Ca
match pair {
Calcit::Tuple(x0, extra, _class) => match &*x0 {
Calcit::Bool(b) => {
let x1 = extra.get(0).ok_or(CalcitErr::use_msg_stack_location(
let x1 = extra.first().ok_or(CalcitErr::use_msg_stack_location(
"foldl map expected value in tuple",
call_stack,
x0.get_location(),
Expand Down Expand Up @@ -439,7 +439,7 @@ pub fn foldr_shortcut(xs: &CalcitItems, call_stack: &CallStackList) -> Result<Ca
match pair {
Calcit::Tuple(x0, extra, _class) => match &*x0 {
Calcit::Bool(b) => {
let x1 = extra.get(0).ok_or(CalcitErr::use_msg_stack_location(
let x1 = extra.first().ok_or(CalcitErr::use_msg_stack_location(
"foldl shortcut expected value in tuple",
call_stack,
x0.get_location(),
Expand Down
2 changes: 1 addition & 1 deletion src/data/edn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn calcit_to_edn(x: &Calcit) -> Result<Edn, String> {
match &**tag {
Calcit::Symbol { sym, .. } => {
if &**sym == "quote" {
let data = extra.get(0).ok_or(format!("quote expected 1 argument, got: {:?}", extra))?; // TODO more types to handle
let data = extra.first().ok_or(format!("quote expected 1 argument, got: {:?}", extra))?; // TODO more types to handle
match cirru::calcit_data_to_cirru(data) {
Ok(v) => Ok(Edn::Quote(v)),
Err(e) => Err(format!("failed to create quote: {e}")), // TODO more types to handle
Expand Down
4 changes: 2 additions & 2 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn extract_import_rule(nodes: &Cirru) -> Result<Vec<ImportMapPair>, String> {
Cirru::Leaf(_) => Err(String::from("Expected import rule in expr")),
Cirru::List(rule_nodes) => {
let mut xs = rule_nodes.to_owned();
match xs.get(0) {
match xs.first() {
// strip leading `[]` symbols
Some(Cirru::Leaf(s)) if &**s == "[]" => xs = xs[1..4].to_vec(),
_ => (),
Expand Down Expand Up @@ -76,7 +76,7 @@ fn extract_import_rule(nodes: &Cirru) -> Result<Vec<ImportMapPair>, String> {
fn extract_import_map(nodes: &Cirru) -> Result<HashMap<Arc<str>, Arc<ImportRule>>, String> {
match nodes {
Cirru::Leaf(_) => unreachable!("Expected expr for ns"),
Cirru::List(xs) => match (xs.get(0), xs.get(1), xs.get(2)) {
Cirru::List(xs) => match (xs.first(), xs.get(1), xs.get(2)) {
// Too many clones
(Some(x), Some(Cirru::Leaf(_)), Some(Cirru::List(xs))) if x.eq_leaf("ns") => {
if !xs.is_empty() && xs[0].eq_leaf(":require") {
Expand Down
14 changes: 10 additions & 4 deletions ts-src/calcit.procs.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// CALCIT VERSION
export const calcit_version = "0.8.9";
import pkg from "./package.json" assert { type: "json" };

export const calcit_version = pkg.version;
export const calcit_package_json = pkg;

import { parse, ICirruNode } from "@cirru/parser.ts";
import { writeCirruCode } from "@cirru/writer.ts";
Expand Down Expand Up @@ -1348,13 +1350,17 @@ function lookup_class(obj: CalcitValue): [CalcitRecord, string] {
tag = "&core-map-class";
klass = calcit_builtin_classes.map;
} else {
throw new Error("Cannot find class for this object for invoking");
return null;
}
return [klass, tag];
}

export function invoke_method(p: string, obj: CalcitValue, ...args: CalcitValue[]) {
let [klass, tag] = lookup_class(obj);
let pair = lookup_class(obj);
if (pair == null) {
throw new Error(`No class for ${obj?.toString() || JSON.stringify(obj)} to lookup .${p}`);
}
let [klass, tag] = pair;
let method = klass.getOrNil(p);
if (method == null) {
throw new Error(`No method '.${p}' for '${tag}' object '${obj}'.\navailable fields are: ${klass.fields.map((fd: CalcitTag) => fd.value).join(" ")}`);
Expand Down
11 changes: 11 additions & 0 deletions ts-src/custom-formatter.mts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ export let load_console_formatter_$x_ = () => {
if (obj instanceof CalcitMap || obj instanceof CalcitSliceMap) {
let ret: any[] = ["div", { style: "color: hsl(280, 80%, 60%)" }];
let pairs = obj.pairs();
pairs.sort((pa, pb) => {
let ka = pa[0].toString();
let kb = pb[0].toString();
if (ka < kb) {
return -1;
} else if (ka > kb) {
return 1;
} else {
return 0;
}
});
for (let idx = 0; idx < pairs.length; idx++) {
let [k, v] = pairs[idx];
ret.push([
Expand Down
1 change: 1 addition & 0 deletions ts-src/package.json
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
"strictNullChecks": false,
"moduleResolution": "node",
"module": "esnext",
"target": "es2015",
"target": "es2020",
"jsx": "react",
"lib": ["es2016", "dom"],
"types": ["node"],
"baseUrl": "./ts-src/",
"resolveJsonModule": true,
"verbatimModuleSyntax": false
},
"include": ["./ts-src/*"]
Expand Down
26 changes: 13 additions & 13 deletions yarn.lock

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

0 comments on commit 6548c28

Please sign in to comment.