aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2020-11-17 14:20:36 -0500
committerMarshall Lochbaum <mwlochbaum@gmail.com>2020-11-17 14:20:36 -0500
commit8403d48cf5823f631c2a489c68c2e9cea3eba23a (patch)
tree02a1d940210740d10ba7dd0cd2f52b1cdbfc86af /src
parent693fc3e18f575f53815165ddc5c38728a9c6227a (diff)
Move index variable out of _err_, allowing it to be reused
Diffstat (limited to 'src')
-rw-r--r--src/c.bqn67
1 files changed, 35 insertions, 32 deletions
diff --git a/src/c.bqn b/src/c.bqn
index 1d317faf..1c1bcff8 100644
--- a/src/c.bqn
+++ b/src/c.bqn
@@ -32,6 +32,10 @@ vi←⊑bD # Start of identifier numbering
charRole←∾⥊¨˜⟜(≠↑cgl˙)⟨1,2,3,¯1,¯3,¯1‿0,¯2,0,5/⌽↕2⟩ # For first vi chars
T←⌈`× ⋄ IT←↕∘≠⊸T ⋄ I1T←(1+↕∘≠)⊸T
+# Error reporting for mask indicating error positions
+# 𝔽 is the message and 𝔾 returns indices corresponding to the mask
+_err_←{(0!˜∧∘/⟜𝔾≍○<𝔽)⍟(∨´∘⊢)}
+
# Source to ⟨tokens, roles, number of identifiers, literals⟩
# Identifiers then literal tokens are numbered starting at vi
Tokenize←{
@@ -79,15 +83,14 @@ Tokenize←{
# 𝕩 is a list of tokens that contains the numeric literals, each
# preceded by 0. Return the numbers.
ReadNums←{
- ind←𝕨 ⋄ _err_←{(0!˜/⟜𝔾⟜ind≍○<𝔽)⍟(∨´∘⊢)}
EChars←⟨"Letter"⊸Pl," """,⊏⟜charSet,""" not allowed in numbers"⟩_tmpl
e‿p‿i‿n‿d←=⟜𝕩¨((⊑bA)+-´"ea")∾+⟜↕´bN # Masks for eπ∞¯.
- EChars∘(/⟜𝕩)_err_⊢ ¬e∨𝕩<⊑bA
+ EChars∘(/⟜𝕩)_err_𝕨 ¬e∨𝕩<⊑bA
s←d∨c←e∨z←0=𝕩⋄m←¬n∨c
- "Negative sign in the middle of a number"_err_⊢ n>»c
- "Portion of a number is empty"_err_⊢ (1«s)∧n∨s
- "Ill-formed decimal or exponent use"_err_(s⊸/) ¬(0⊸=∨»⊸<)s/𝕩
- "π and ∞ must occur alone"_err_⊢ (p∨i)>1(»∧(p∧«e)∨«)z∨n
+ "Negative sign in the middle of a number"_err_𝕨 n>»c
+ "Portion of a number is empty"_err_𝕨 (1«s)∧n∨s
+ "Ill-formed decimal or exponent use"_err_(s/𝕎) ¬(0⊸=∨»⊸<)s/𝕩
+ "π and ∞ must occur alone"_err_𝕨 (p∨i)>1(»∧(p∧«e)∨«)z∨n
f←(17≥¬(⊢-T)+`)⊸∧(«≤(d<𝕩≠⊑bD)>○I1T¬)⊸∧m # No leading 0s; max 17 digits
l←(¯1∾π‿1∾˜↕10)⊏˜(¬d)/f×𝕩-1-˜⊑bD # Digit lookup, with ∞ as 1 to avoid ∞×0
v←(>⟜«0≤l)/(0⊸≤××⟜10⊸+)`»«l # Numeric values—mantissas and exponents
@@ -99,35 +102,35 @@ ReadNums←{
}
-Parse ← {r‿vn‿ind←𝕨⋄nv←≠vn
+Parse ← {r‿vn‿i‿e←𝕨⋄nv←≠vn
ErrMismatchedBrackets←{
Lcs ← ¯1 ⊑ 0¨∘⊢ {𝕩⌈⌈`𝕨+»𝕩}˝ =⌜⟜⌽
_mis_ ← {"Missing "∾𝕗∾" "∾charSet⊏˜𝕘+⊢/˜≠∘⊣=⊒}
Msg ← >○≠◶⟨"opening"_mis_ 0, "closing"_mis_ 1˜⟩
0 !˜ 𝕨 (Lcs<⌊○≠)◶⟨Msg,"Malformed bracket nesting"⟩ 𝕩
}
+ GI←{𝕤⋄g⊏i}
- g←⍋pd←+`p←(¯1-2×r)×𝕩 M bB⋄gp←g⊏p⋄gb←g⊏r=¯1⋄gx←g⊏𝕩
- gp(>⟜0⊸/ErrMismatchedBrackets⍟≢1-˜<⟜0⊸/)gx
- _err_←{(0!˜∧∘/⟜𝔾⟜ind≍○<𝔽)⍟(∨´∘⊢)}
+ g←⍋pd←+`p←(¯1-2×r)×𝕩 M bB⋄gb←g⊏r=¯1⋄gx←g⊏𝕩
+ (g⊏p)(>⟜0⊸/ErrMismatchedBrackets⍟≢1-˜<⟜0⊸/)gx
"Swapped open and closed brackets" ! 0≤(⊑g)⊑pd
- "Parentheses can't contain separators"_err_(gb/g⊸⊏) ((⊑bB)⊸=»⊸∧sep⊸=)gb/gx
- {"Empty statement or expression"_err_(𝕩/0∾g⊸⊏) (4+⊑bB)≠𝕩/0∾gx}1⊸«⊸∧1∾gb
+ "Parentheses can't contain separators"_err_(gb/GI) ((⊑bB)⊸=»⊸∧sep⊸=)gb/gx
+ {"Empty statement or expression"_err_(𝕩/0∾GI) (4+⊑bB)≠𝕩/0∾gx}1⊸«⊸∧1∾gb
r-↩(𝕩=⊑bG)>ec←«0≤r+p
- "Invalid assignment or stranding use"_err_⊢ ((¯4⊸<∧≤⟜¯2)r)>ec∧»0≤r
+ "Invalid assignment or stranding use"_err_(I) ((¯4⊸<∧≤⟜¯2)r)>ec∧»0≤r
sr←»⌾(g⊸⊏)sl←«⊸∨r=¯2⋄cp←𝕩=1+⊑bB
g⊏˜↩⍋g⊏sl
rev←⍋+`¯1↓(¯1∾g)(⊣⍋⊸⊏⊏˜⟜⍋¬⊏˜)⍋+`⊸+1∾g⊏sl∨r=¯1
gf←⍋fd←+`rev⊏br←p×𝕩M⟨2+⊑bB,2⟩
rev⊏˜↩gf⋄fd⊏˜↩gf
- 𝕩⊏˜↩rev⋄ind⊏˜↩rev⋄bv←rev⊏br⋄b←/bv<0⋄fi←+`c←bv>0
+ 𝕩⊏˜↩rev⋄i⊏˜↩rev⋄bv←rev⊏br⋄b←/bv<0⋄fi←+`c←bv>0
H←¬1(∾/∾˜)c(∨/⊣)=
sn←(0‿3‿4‿5+5+⊑bI)⍋𝕩⋄fx←1 H sn⋄fr←(fx∨0⊸<)⊸+ft←(2⊸H⌈2×3⊸H)sn
- {⟨ind/˜(0=fi)∧𝕩 M bI,"Special name outside of any block"⟩!0}⍟(0<⊑fr) 𝕩
+ {⟨i/˜(0=fi)∧𝕩 M bI,"Special name outside of any block"⟩!0}⍟(0<⊑fr) 𝕩
nf←(⊑bG)H𝕩
- {⟨ind/˜(fi⊏0≠fr)∧𝕩=⊑bG,"Can't export from a non-immediate block"⟩!0}⍟(∨´nf>fr=0) 𝕩
+ {⟨i/˜(fi⊏0≠fr)∧𝕩=⊑bG,"Can't export from a non-immediate block"⟩!0}⍟(∨´nf>fr=0) 𝕩
fsc←(ft⊏0‿2‿3)+3×fx
pt←cp∧ns←¬sl∨sr
r↩sl-˜ns×(1↓fr)⌾((b⊏rev)⊸⊏)r
@@ -139,23 +142,23 @@ Parse ← {r‿vn‿ind←𝕨⋄nv←≠vn
idx←𝕩⊏˜id←/idm←(0⊸≤∧<⟜nv)xv←𝕩-vi⋄sp←/𝕩 M bI
g↩⍋+`rev⊏p-br⋄gr←g⊏rev⋄sll←1+2÷˜0(<-○/>)gr⊏sr-sl⋄l←/g⊏𝕩=5+⊑bB
- g⊏˜↩gs←⍋gr⊏sl⋄gr↩g⊏rev⋄gi←⍋g
+ g⊏˜↩gs←⍋gr⊏sl⋄gr↩g⊏rev⋄ginv←⍋g
𝕩⊏˜↩g⋄r⊏˜↩gr⋄ir⊏˜↩gr⋄l↩(l⊏⍋gs)∾/gr⊏sr>sl
- ni←1+(1↓nf)/bk←b⊏gi
- "Modules must be immediately assigned"_err_(ni⊏g⊸⊏)∘(∨˝¯3‿0‿¯1≠r∾⟜¯1‿1⊸(⊏˜)(↕3)+⌜⊢)⍟(0<≠)ni
- "Modules must be destructured"_err_(ni⊏g⊸⊏) (⊏⟜idm>⊏⟜rev⊏sr˙)(ni+1)⊏g
+ ni←1+(1↓nf)/bk←b⊏ginv
+ "Modules must be immediately assigned"_err_(ni⊏GI)∘(∨˝¯3‿0‿¯1≠r∾⟜¯1‿1⊸(⊏˜)(↕3)+⌜⊢)⍟(0<≠)ni
+ "Modules must be destructured"_err_(ni⊏GI) (⊏⟜idm>⊏⟜rev⊏sr˙)(ni+1)⊏g
a←r≤¯3⋄ps←a<r<0⋄tr←1≤er←ir⊏˜IT»ps
oa←⌽/op←(er<2)∧r≥2⋄ro←op∨«op∧m2←r=3
- "Missing operand"_err_(g⊸⊏) op>(«∧m2≤»)m2<ro∨r∊↕2
- "Double subjects (missing ‿?)"_err_(g⊸⊏) ∧⟜«ro»⊸∨⊸<r=0
+ "Missing operand"_err_(GI) op>(«∧m2≤»)m2<ro∨r∊↕2
+ "Double subjects (missing ‿?)"_err_(GI) ∧⟜«ro»⊸∨⊸<r=0
ma←tr<(𝕩=2+⊑bG)∧«ir≥1
os←↕∘≠⊸(⊣-T)⌾⌽¬ro∨ma⋄at←1+⊏⟜os⊸+ai←/a⋄af←¯4≠ai⊏r
- "Role of the two sides in assignment must match"_err_(at⊏g⊸⊏) (at⊏er)≠ar←at⊏r
+ "Role of the two sides in assignment must match"_err_(at⊏GI) (at⊏er)≠ar←at⊏r
aid←(¯10⊸≤∧<⟜nv)𝕩-vi
- "Function or modifier assignment to a non-name"_err_(at⊏g⊸⊏) ¬(ar=0)∨at⊏aid
- aa←×g⊏ac←»+`gi⊏«⊸-(1+af+(ai∊ni)+2×(⊑bG)-˜ai⊏𝕩)⌾(at⊸⊏)0¨𝕩
- "Assignment target must be a name or list of targets"_err_(g⊸⊏) (aid∨ps∨𝕩∊bB⊑⊸+1‿5)<aa∧0=er
- "Can't use result of function/modifier assignment without parentheses"_err_(g⊸⊏) (0<er)∧(0≤r)∧»⊸>aa
+ "Function or modifier assignment to a non-name"_err_(at⊏GI) ¬(ar=0)∨at⊏aid
+ aa←×g⊏ac←»+`ginv⊏«⊸-(1+af+(ai∊ni)+2×(⊑bG)-˜ai⊏𝕩)⌾(at⊸⊏)0¨𝕩
+ "Assignment target must be a name or list of targets"_err_(GI) (aid∨ps∨𝕩∊bB⊑⊸+1‿5)<aa∧0=er
+ "Can't use result of function/modifier assignment without parentheses"_err_(GI) (0<er)∧(0≤r)∧»⊸>aa
ip←id⊏ac⋄d←ip M 2‿4⋄zic←0¨ic←id/˜icf←2(<∧|)ip
il←ic⊏+`bv<0⋄ex←il⊏(/∾≠)c
@@ -165,7 +168,7 @@ Parse ← {r‿vn‿ind←𝕨⋄nv←≠vn
"Multiple definitions" ! ∧´ixx∨○(»⊸≠((≠d)⊸≤⊸/ig)⊸⊏)ixf
ig↩<⟜(≠d)⊸/(⍋+`∘⊏⟜(d∾-/˜d))⊸⊏ig
d⊏˜↩ig⋄idx⊏˜↩ig
- ("Undefined identifier"Pl/)_err_(ig⊏id⊸⊏) d<»⊸≠idx
+ ("Undefined identifier"Pl/)_err_(ig⊏id⊏I) d<»⊸≠idx
"Imports must have been exported" ! ∧´2>-⟜»(d∨×)⊸/ig⊏(ip M 1‿3)∾2¨ic
ui←(IT d)⊸⊏⌾(ig⊸⊏)id∾ex
lc←(1«c)/dn←c(⊢-T)+`idm∧ac M 2‿4
@@ -177,7 +180,7 @@ Parse ← {r‿vn‿ind←𝕨⋄nv←≠vn
ta←tr∧2(>∨|)ps(⊢-T)+`¬ro
fa←/(fe←ta∨ro∨«⊸∨ps<aa)<ff←(r=1)∨»op
- "Second-level parts of a train must be functions"_err_(g⊸⊏) tr>fe∨ff
+ "Second-level parts of a train must be functions"_err_(GI) tr>fe∨ff
dy←fa⊏«(𝕩=⊑bO)<(tr∧r≥0)∨ro<r=0
ob←pr⊏/¯1(⊢-»)u←⍷∧pr←𝕩⊏˜pi←/𝕩<sep
cn←pi∾lt←/𝕩≥ci←vi+nv⋄ob∾↩(ci-˜≠u)+lt⊏𝕩
@@ -189,7 +192,7 @@ Parse ← {r‿vn‿ind←𝕨⋄nv←≠vn
bc←or⊏∾idbc∾⟨0¨cn,ob,15¨bk,1+↕≠bk,14¨dr,⥊⍉(3+l⊏aa)≍ll
11⌈(11-1+⊑bG)+(af/ai)⊏ma+𝕩,5+oa⊏r,(⊢+9×10⊸=)16+dy-7×fa⊏er,25¨rt⟩
bcl‿el‿ol←LEB bc∾25
- ⟨bcl,u,⟨ft,¬fx,el/˜1∾or≥bc-○≠rt,lc+fsc⟩, ol/(¯1∾˜or⊏oi)⊏¯1(⊢∾⊏)ind⟩
+ ⟨bcl,u,⟨ft,¬fx,el/˜1∾or≥bc-○≠rt,lc+fsc⟩, ol/(¯1∾˜or⊏oi)⊏¯1(⊢∾⊏)i⟩
}
LEB←{
@@ -201,7 +204,7 @@ LEB←{
}
Compile←{
- ⟨tok,role,val,inds,inde⟩←tx←Tokenize 𝕩
- ⟨bc,prim,blk,indb⟩←⟨role,⊑val,inds⟩ Parse tok
+ ⟨tok,role,val,t0,t1⟩←tx←Tokenize 𝕩
+ ⟨bc,prim,blk,indb⟩←⟨role,⊑val,t0,t1⟩ Parse tok
⟨bc, ∾⟨prim⊏𝕨⟩∾1↓val, <˘⍉>blk, indb, tx⟩
}