From ea7e50dd0d2badcfe9a9b412b02986f844921b80 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Thu, 17 Sep 2020 20:00:06 -0400 Subject: Add array-based numeric literal verification --- src/c.bqn | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/c.bqn b/src/c.bqn index 2bff6d27..4b6674a5 100644 --- a/src/c.bqn +++ b/src/c.bqn @@ -48,7 +48,7 @@ Tokenize←{ tw←na⌊∘÷˜(⊑bA)-˜w/c c-↩na×l∧c≥na+⊑bA li←l>in←l∧(+`w)⊏0∾tw<0 - num←ReadNum¨¬⊸(+`⊸×-⊣)∘×⊸⊔ in∨⟜»⊸/c×l + num←ReadNums in∨⟜»⊸/○(∾⟜0)c×l ti←(us/˜«⊸us)×+`w>in)⊔c ki←((⍋⊏⟜in)⊸⊏/w)∾(ci∾/si)⊏+`»f @@ -61,13 +61,26 @@ Tokenize←{ ⟨c,t,≠⊑k,∾1↓k⟩ } +ErrNumericChars←{ + ⟨"Letter","s"/˜1<≠𝕩," """,𝕩⊏charSet,""" not allowed in numbers"⟩∾⊸!0 +} +ReadNums←{ + e‿n‿d‿p‿i←=⟜𝕩¨((⊑bA)+-´"ea")∾+⟜↕´bN # e¯.π∞ + ErrNumericChars∘(/⟜𝕩)⍟(∨´) ¬e∨𝕩<⊑bA + st←𝕩/˜s←d∨c←e∨z←0=𝕩 + "Negative sign in the middle of a number" ! ∧´n≤1»c + "Portion of a number is empty" ! ¬∨´(1»s∨n)∧s + "Ill-formed decimal or exponent use" ! ∧´(0⊸=∨»⊸<)st + "π and ∞ must occur alone" ! ∧´(p∨i)≤1(»∧(p∧«e)∨«)z∨n + ReadNum¨(¬⊸(+`⊸×-⊣)×𝕩)⊔𝕩-⊑bD +} ReadNum←{ - n‿d‿p‿i←+⟜↕´bN # ¯.π∞ - Nat←+⟜(10⊸×)´∘⌽ -⟜(⊑bD) + n‿d‿p‿i←10+↕4 # ¯.π∞ + Nat←+⟜(10⊸×)´∘⌽ Int←(n=⊑)◶⟨Nat,-·Nat 1⊸↓⟩ Dec←⊐⟜(