aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2020-06-07 14:47:03 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2020-06-07 14:47:03 -0400
commit48438ca216ff1958e84c6e1bf8002bf030334c76 (patch)
treeb55bec29b4b25e3351c2b0c32140badefd76e1e1
parent99f8a78501a1568d4a67178df727b1c6750942d4 (diff)
Add assignment and statement separator handling (single-letter variables)
-rwxr-xr-xc.bqn25
1 files changed, 15 insertions, 10 deletions
diff --git a/c.bqn b/c.bqn
index 3e818899..88f75739 100755
--- a/c.bqn
+++ b/c.bqn
@@ -1,31 +1,36 @@
#!/usr/bin/env bqn
Parse←{
- l←≠x←𝕩
- o←x='('⋄c←x=')'⋄v←x∊•d⋄f←¬o∨c∨v
- na←f×1+l↑0∾c∨v
+ x←𝕩
+ a←x='←'⋄at←1⌽a⋄vars←at/x⋄x(/˜)↩¬a⋄a(/˜)↩¬at
+ l←≠x⋄o←x='('⋄c←x=')'⋄sep←x='⋄'⋄v←a-˜x∊•d∾vars⋄f←¬o∨c∨v∨sep
+ na←(2×sep)+f×1+l↑0∾c∨v
e←/c⋄d←+`o-c
ed←e⊏d⋄b←(⍋⍋ed)⊏(⍋⊏⟜d)⊸⊏/o
s←e-(e∾l)⊏˜{u←(≠e)¨𝕩⋄{(𝕩⊏u)↩𝕨⊏˜𝕨⍋𝕩}¨´˘2↕(∪⍋⊸⊏⊔)𝕩⋄u}⍟(0<≠)ed
fe←f×(l-1)++`(-⊸≍s)⌾((e≍b)⊸⊏)l⥊0
+ fe⌊↩(l-1)-l↑(⌈`↕∘≠⊸×)⌾⌽1∾˜sep
ia←+`l↑0∾f-l↑/⁼∧f/fe
sel←¬∘⊏⟜(o∨c)⊸/⍋(fe⌈↕l)-ia
- sel⊸⊏¨x‿na
+ ⟨sel⊸⊏¨x‿na,vars⟩
}
+d64←127-3
GenFn←{
- x‿na←𝕩
+ ⟨x‿na,vars⟩←𝕩
D←16⊥(•d∾"ABCDEF")⊸⊐
fns←{
n←•d⊐<𝕩
+ n=≠•d:(D"20")∾vars⊐<𝕩
n=0:8⥊0
l←2(⌊⋆⁼)n
(D"44")∾2⊥˘⌽8‿8(⊣⥊×´⊸↑)∾⟨⥊0,(11⥊2)⊤1023+l,(2⥊˜0⌈l)⊤n⟩
}‿{
+ v←vars⊐<𝕩⋄v<≠vars:(D"22")∾v
(⥊¨(D"99")+((↕4)∾6))⊑˜"|-⌈⌊√"⊐<𝕩
}‿{
- (⥊¨(D"A0")+↕6)⊑˜"+-×÷⌊⌈"⊐<𝕩
+ (⥊¨(D"1A")∾(D"A0")+↕6)⊑˜"⋄+-×÷⌊⌈"⊐<𝕩
}
- (D"0B")∾˜∾na⊑⟜fns⊸{𝕎𝕩}¨x
+ ((≠∾∾)⟨vars≠⊸∾d64⟩)∾(D"0B")∾˜∾na⊑⟜fns⊸{𝕎𝕩}¨x
}
Gen←{
@@ -40,12 +45,12 @@ Gen←{
1 S V (96∾⟜∾C¨)¨t
3 S V ⥊¨↕≠n
7 S V I⊸(⊣∾0∾⊢)¨⟜(↕∘≠)n
- 10 S V (C⟨⟩V⊸∾⊢)¨ b
+ 10 S V C¨ b
}
Compile←{
body←GenFn Parse 𝕩
- (1↓∾)(","∾⥊∘⍕)¨ Gen ⟨⟨0‿1⟩/¨¨127-3 ⋄ ⥊<"fn" ⋄ ⥊<body⟩
+ (1↓∾)(","∾⥊∘⍕)¨ Gen ⟨⟨0‿1⟩/¨¨d64 ⋄ ⥊<"fn" ⋄ ⥊<body⟩
}
-•←Compile "((-3)+√(3×3)-4×2×1)÷2×2"
+•←Compile "b←3⋄d←(b×b)-4×2×1⋄((-b)+√d)÷2×2"