From 1c23dbb833410b2638fdb3b2b0fef083b87ab670 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Fri, 14 Aug 2020 12:10:47 -0400 Subject: Add error messages to the runtime --- src/pr.bqn | 2 +- src/r.bqn | 136 +++++++++++++++++++++++++++++++++---------------------------- 2 files changed, 74 insertions(+), 64 deletions(-) (limited to 'src') diff --git a/src/pr.bqn b/src/pr.bqn index a93bd4eb..4c789a3c 100755 --- a/src/pr.bqn +++ b/src/pr.bqn @@ -34,7 +34,7 @@ names ↩ (⥊¨kt⊏chr)⌾(ki⊸⊏)names E_isdef ← (3≤≠)◶⟨0,∧´⟨chr," ","←↩"⟩∊˜¨3⊸↑⟩ E_proc ← ({𝕨∘𝕩}´ {𝕨•_R_𝕩}⟜⥊¨⟜(≠↑chr˜) def)∘{ l←≠chr - q←≠`𝕩∊"""'" ⋄ f←¬∨`q¬⊸∧𝕩='#' + q←≠`𝕩='"' ⋄ q∨↩≠`q<𝕩=''' ⋄ f←¬∨`q<𝕩='#' ∾ (((l×f/q)+chr⊸⊐) (≥⟜l)◶⟨⊑⟜names,⥊∘⊢⟩¨ ⊢) f/𝕩 } E_redef ← { # handles [fmd] [←↩] diff --git a/src/r.bqn b/src/r.bqn index 60a2ba0f..c61034ae 100644 --- a/src/r.bqn +++ b/src/r.bqn @@ -22,7 +22,7 @@ Nat←IsArray◶⟨0⊸≤×⌊⊸=,0⟩ ≠ ← (0<=)◶⟨1⋄0⊑≢⟩ # LIMITED to monadic case _fold←{ - ! 1==𝕩 + "Argument to fold must be a list" ! 1==𝕩 l←≠v←𝕩 ⋄ F←𝔽 r←𝕨 (0)⌜ 𝕨 + "Select indices out of range" ! 1×´⥊ ((-l)⊸≤×l⊸>)⌜ 𝕨 ((⊢+l×0>⊢)⌜𝕨)(1==𝕩)◶{ c←1×´s←1 Cell 𝕩 𝕨((⥊𝕩)⊑˜c⊸×⊸+)⌜s⥊↕c @@ -120,8 +120,15 @@ SelSub←{ }𝕩 } -Reverse ← {!1≤=𝕩 ⋄ l←≠𝕩 ⋄ ((l-1)⊸-⌜↕l) Select 𝕩} -Rot ← {!Int𝕨 ⋄ l←≠𝕩 ⋄ 𝕨-↩l×⌊𝕨÷l ⋄ ((𝕨+⊢-l×(l-𝕨)≤⊢)⌜↕l) Select 𝕩} +Reverse ← { + "Reverse argument must have rank at least 1" ! 1≤=𝕩 + l←≠𝕩 + ((l-1)⊸-⌜↕l) Select 𝕩 +} +Rot ← { + "Amount to rotate must be an integer" ! Int𝕨 + l←≠𝕩 ⋄ 𝕨-↩l×⌊𝕨÷l ⋄ ((𝕨+⊢-l×(l-𝕨)≤⊢)⌜↕l) Select 𝕩 +} _under_←{ i←↕l←1×´s←≢𝕩 @@ -149,7 +156,7 @@ Depth←IsArray◶0‿{1+0(⊣-≤×-)´Depth⌜⥊𝕩} Merge←{ c←≢0⊑⥊𝕩 - ! 1×´(c≡≢)⌜⥊𝕩 + "Elements in Merge argument must have matching shapes" ! 1×´(c≡≢)⌜⥊𝕩 𝕩⊑⟜Deshape˜⌜c⥊↕1×´c }⍟(0<≠∘⥊)⍟IsArray @@ -166,18 +173,18 @@ Merge←{ > ↩ Merge ⊘ ((1-≤) _perv) ≠ ↩ ≠ ⊘ ((1-=) _perv) = ↩ = ⊘ (= _perv) -≥ ← !∘0 ⊘ (≤˜_perv) -≤ ↩ !∘0 ⊘ (≤ _perv) +≥ ← ("No monadic form: ≥"!0˜) ⊘ (≤˜_perv) +≤ ↩ ("No monadic form: ≤"!0˜) ⊘ (≤ _perv) + ↩ + _perv - ↩ - _perv ¬ ← 1+- -identity ← (0⊑⟨!∘0⟩) {(0⊑𝕨){𝕗=𝕩}◶𝕩‿(1⊑𝕨)}´ ⟨+‿0,-‿0,×‿1,÷‿1,⋆‿1,√‿1,∧‿1,∨‿0,|‿0,⌊‿∞,⌈‿¯∞,<‿0,≤‿1,=‿1,≥‿1,>‿0,≠‿0⟩ +identity ← (0⊑⟨"Identity not found"!0˜⟩) {(0⊑𝕨){𝕗=𝕩}◶𝕩‿(1⊑𝕨)}´ ⟨+‿0,-‿0,×‿1,÷‿1,⋆‿1,√‿1,∧‿1,∨‿0,|‿0,⌊‿∞,⌈‿¯∞,<‿0,≤‿1,=‿1,≥‿1,>‿0,≠‿0⟩ Deshape←IsArray◶{⟨𝕩⟩}‿⥊ Reshape←{ - ! 1≥=𝕨 + "Shape argument to Reshape must have rank at most 1" ! 1≥=𝕨 𝕨↩Deshape 𝕨 - ! ∧´Nat⌜𝕨 + "Shape in Reshape must consist of natural numbers" ! ∧´Nat⌜𝕨 l←×´𝕨 n←×´≢𝕩 𝕨⥊{ @@ -187,8 +194,8 @@ Reshape←{ ⥊ ↩ Deshape ⊘ ⥊ Range←{ - I←{!Nat𝕩⋄↕𝕩} - M←{!1==𝕩⋄(<⟨⟩)⥊⊸∾⌜´I⌜𝕩} + I←{"Range argument must consist of natural numbers"!Nat𝕩⋄↕𝕩} + M←{"Range argument must be a number or list"!1==𝕩⋄(<⟨⟩)⥊⊸∾⌜´I⌜𝕩} IsArray◶I‿M 𝕩 } @@ -219,7 +226,7 @@ _rankOp_←{ ⎉ ← _rankOp_ ˘ ← {𝔽⎉¯1} _insert←{ - ! 1≤=𝕩 + "Insert argument must have rank at least 1" ! 1≤=𝕩 𝕨 𝔽´ <˘𝕩 } ˝ ← _insert @@ -228,11 +235,11 @@ _insert←{ _onAxes_←{ F←𝔽 (𝔾<≡)∘⊣◶{ # One axis - ! 1≤=𝕩 + "First-axis function right argument must have rank at least 1" ! 1≤=𝕩 𝕨F𝕩 }‿{ # Multiple axes - ! 1≥=𝕨 - ! 𝕨≤○≠≢𝕩 + "Left argument must have rank at most 1" ! 1≥=𝕨 + "Left argument length must be at most right argument rank" ! 𝕨≤○≠≢𝕩 l←≠𝕨 ⋄ W←⊑⟜(⥊𝕨) 0{(W𝕨)F(1+𝕨)⊸𝕊˘⍟(𝕨0)+(-s)⌈s⌊𝕨)↑𝕩 } -Suffixes ← {!1≤=𝕩 ⋄ Drop⟜𝕩⌜↕1+≠𝕩} +Suffixes ← {"Suffixes argument must have rank at least 1"!1≤=𝕩 ⋄ Drop⟜𝕩⌜↕1+≠𝕩} ↓ ← Suffixes ⊘ Drop Windows←{ - ! IsArray 𝕩 - ! 1≥=𝕨 - ! 𝕨≤○≠≢𝕩 - ! ∧´Nat⌜⥊𝕨 + "Windows right argument must be an array" ! IsArray 𝕩 + "Windows left argument must have rank at most 1" ! 1≥=𝕨 + "Windows left argument length must be at most right argument rank" ! 𝕨≤○≠≢𝕩 + "Windows left argument must consist of natural numbers" ! ∧´Nat⌜⥊𝕨 s←(≠𝕨)↑≢𝕩 - ! ∧´𝕨≤1+s + "Window length must be at most axis length plus one" ! ∧´𝕨≤1+s 𝕨{(∾⟜(𝕨≠⊸↓≢𝕩)∘≢⥊>)<⌜⊸⊏⟜𝕩¨s(¬+⌜○Range⊢)⥊𝕨}⍟(0<≠𝕨)𝕩 } Rep ← Indices⊸⊏ -Replicate ← {0<=𝕨}◶{𝕨⌜⊸Rep𝕩}‿{!𝕨=○≠𝕩⋄𝕨Rep𝕩} _onAxes_ (1-0=≠) +Replicate ← {0<=𝕨}◶{𝕨⌜⊸Rep𝕩}‿{"Replicate argument lengths must match"!𝕨=○≠𝕩⋄𝕨Rep𝕩} _onAxes_ (1-0=≠) ↕ ↩ Range ⊘ Windows ⌽ ← Reverse ⊘ (Rot _onAxes_ 0) @@ -293,33 +300,35 @@ Join←(1≠=)◶⟨∨´1≠=⌜,1⟩◶{ }‿{ # Multidimensional C←(<⟨⟩)⥊⊸∾⌜´⊢ # Cartesian array product - ! IsArray 𝕩 + "Join argument must be an array" ! IsArray 𝕩 s←≢⌜𝕩 d←≠0⊑⥊s - ! ∧´⥊d=≠⌜s - ! d≥=𝕩 + "Join argument elements must all have the same rank" ! ∧´⥊d=≠⌜s + "Join argument element rank must be at least argument rank" ! d≥=𝕩 l←(≢𝕩){(𝕩⊑⟜≢a Pick1˜(j=𝕩)⊸×)⌜↕𝕨}¨j←↕r←=a←𝕩 - ! (r⊸↑⌜s)≡C l + "Join argument element shapes must be compatible" ! (r⊸↑⌜s)≡C l i←C{p←+´⌜↑𝕩⋄(↕0⊑⌽p)-𝕩/¯1↓p}⌜l >i<⌜⊸⊏¨l/𝕩 }⍟(0<≠∘⥊) _group←{ - !1==𝕩⋄!∧´Int⌜𝕩⋄!∧´¯1≤𝕩 + "Grouping argument must be a list" ! 1==𝕩 + "Grouping argument must consist of integers" ! ∧´Int⌜𝕩 + "Grouping argument values cannot be less than ¯1" ! ∧´¯1≤𝕩 d←(l←GroupLen𝕩)GroupOrd𝕩 i←0⋄(𝔽{𝕩⋄(i↩i+1)⊢i⊑d}⌜∘↕)⌜l } GroupInds←{ - ! IsArray 𝕩 + "Group Indices argument must be an array" ! IsArray 𝕩 G←⊢_group (1<≡)◶G‿((<<⟨⟩)⥊⊸∾⌜⌜´G⌜)𝕩 } GroupGen←{ - ! IsArray 𝕩 + "Group right argument must be an array" ! IsArray 𝕩 m←1<≡𝕨 - l←m◶≠‿{!1==𝕩⋄≠⌜𝕩}𝕨 - ! l≤○≠≢𝕩 - ! ∧´l=l≠⊸↑≢𝕩 + l←m◶≠‿{"Group left argument must consist of lists"!1==𝕩⋄≠⌜𝕩}𝕨 + "Group left argument length must be at most right argument rank" ! l≤○≠≢𝕩 + "Group argument lengths must be compatible" ! ∧´l=l≠⊸↑≢𝕩 𝕨m◶(⊏⟜𝕩_group⊣)‿{ ⊏⟜(𝕩⥊˜⟨×´l⟩∾(≠l)Cell𝕩)⌜ +⌜⌜´ (⌽×`⟨1⟩∾⌽1↓l) × ⊢_group⌜𝕨 }𝕩 @@ -329,10 +338,10 @@ GroupGen←{ ⊔ ← GroupInds ⊘ GroupGen Pick1←{ - ! 1==𝕨 - ! 𝕨=○≠s←≢𝕩 - ! ∧´Int⌜𝕨 - ! ∧´𝕨(≥⟜-∧<)s + "Indices in compound Pick must be lists" ! 1==𝕨 + "Pick index length must match right argument rank" ! 𝕨=○≠s←≢𝕩 + "Pick indices must consist of integers" ! ∧´Int⌜𝕨 + "Pick index out of range" ! ∧´𝕨(≥⟜-∧<)s 𝕨↩𝕨+s×𝕨<0 (⥊𝕩)⊑˜0(⊑⟜𝕨+⊑⟜s×⊢)´-↕⊸¬≠𝕨 } @@ -373,11 +382,11 @@ _binSearch ← { } _bins←{ c←1-˜=𝕨 - ! 0≤c - ! c≤=𝕩 + "Bins argument must have rank at least 1" ! 0≤c + "Bins right argument rank is too small" ! c≤=𝕩 lw←×´sw←1 Cell 𝕨 cw←lw 𝔽○(⊑⟜(⥊𝕨)) _getCellCmp 0 - ! 0⊸<◶⟨1,∧´0≤˜·cw⟜(lw⊸+)⌜lw×↕∘-⟜1⟩≠𝕨 + "Bins left argument must be sorted" ! 0⊸<◶⟨1,∧´0≤˜·cw⟜(lw⊸+)⌜lw×↕∘-⟜1⟩≠𝕨 cx←c-˜=𝕩 sx←cx Cell 𝕩 ⋄ lc←sw CmpLen sx cc ← (⊑⟜(⥊𝕨))⊸𝔽⟜(⊑⟜(⥊𝕩)) _getCellCmp´ lc @@ -394,7 +403,7 @@ _search←{ # 0 for ∊˜, 1 for ⊐ red ← 𝕗⊑⟨¬∧˝,+˝∧`⟩ { c←1-˜=𝕨 - ! 0≤c + "Search principal argument must have rank at least 1" ! 0≤c 𝕨 ∧○(8<≠∘⥊)◶⟨ (0<≠𝕨)◶⟨0⎉c∘⊢, Red≢⌜○((0