aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGavin Howard <yzena.tech@gmail.com>2018-08-08 00:15:50 -0600
committerRob Landley <rob@landley.net>2018-08-08 19:56:35 -0500
commit151e3f782333acbe349fdaac3ec843e42e95be3f (patch)
tree25e49c3292fafde0613fbcf601f8ab03da082e62
parent49d02c41fe60684e1f43cab38e4c1d80a607a921 (diff)
downloadtoybox-151e3f782333acbe349fdaac3ec843e42e95be3f.tar.gz
Add bc (and its tests) to pending
-rw-r--r--tests/bc.test72
-rw-r--r--tests/files/bc/add.txt24
-rw-r--r--tests/files/bc/add_results.txt36
-rwxr-xr-xtests/files/bc/arctan.bc12
-rw-r--r--tests/files/bc/basic.txt7
-rw-r--r--tests/files/bc/basic_results.txt9
-rw-r--r--tests/files/bc/decimal.txt34
-rw-r--r--tests/files/bc/decimal_results.txt48
-rw-r--r--tests/files/bc/divide.txt31
-rw-r--r--tests/files/bc/divide_results.txt31
-rw-r--r--tests/files/bc/modulus.txt66
-rw-r--r--tests/files/bc/modulus_results.txt65
-rw-r--r--tests/files/bc/multiply.txt39
-rw-r--r--tests/files/bc/multiply_results.txt40
-rwxr-xr-xtests/files/bc/parse.bc16
-rw-r--r--tests/files/bc/pi.txt7
-rw-r--r--tests/files/bc/power.txt42
-rw-r--r--tests/files/bc/power_results.txt385
-rwxr-xr-xtests/files/bc/print.bc21
-rw-r--r--tests/files/bc/screen.bc19
-rw-r--r--tests/files/bc/script.sh24
-rw-r--r--tests/files/bc/sqrt.txt12
-rw-r--r--tests/files/bc/sqrt_results.txt11
-rw-r--r--tests/files/bc/subtract.txt30
-rw-r--r--tests/files/bc/subtract_results.txt34
-rw-r--r--toys/pending/bc.c5892
26 files changed, 7007 insertions, 0 deletions
diff --git a/tests/bc.test b/tests/bc.test
new file mode 100644
index 00000000..213b21ab
--- /dev/null
+++ b/tests/bc.test
@@ -0,0 +1,72 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+BDIR="$FILES/bc"
+TESTDIR="./"
+
+results=$(cat "$BDIR/decimal_results.txt")
+testcmd "decimal" "-l $BDIR/decimal.txt" "$results\n" "$BDIR/decimal.txt" ""
+
+results=$(cat "$BDIR/add_results.txt")
+testcmd "add" "-l $BDIR/add.txt" "$results\n" "$BDIR/add.txt" ""
+
+results=$(cat "$BDIR/subtract_results.txt")
+testcmd "subtract" "-l $BDIR/subtract.txt" "$results\n" "$BDIR/subtract.txt" ""
+
+results=$(cat "$BDIR/multiply_results.txt")
+testcmd "multiply" "-l $BDIR/multiply.txt" "$results\n" "$BDIR/multiply.txt" ""
+
+results=$(cat "$BDIR/divide_results.txt")
+testcmd "divide" "-l $BDIR/divide.txt" "$results\n" "$BDIR/divide.txt" ""
+
+results=$(cat "$BDIR/modulus_results.txt")
+testcmd "modulus" "-l $BDIR/modulus.txt" "$results\n" "$BDIR/modulus.txt" ""
+
+results=$(cat "$BDIR/power_results.txt")
+testcmd "power" "-l $BDIR/power.txt" "$results\n" "$BDIR/power.txt" ""
+
+results=$(cat "$BDIR/sqrt_results.txt")
+testcmd "sqrt" "-l $BDIR/sqrt.txt" "$results\n" "$BDIR/sqrt.txt" ""
+
+results=$(cat "$BDIR/basic_results.txt")
+testcmd "basic" "-l $BDIR/basic.txt" "$results\n" "$BDIR/basic.txt" ""
+
+if [ ! -f "$TESTDIR/parse.txt" ]; then
+ echo "Generating bc parse..."
+ cat "$BDIR/parse.bc" | bc -l > "$TESTDIR/parse.txt"
+fi
+results=$(cat "$TESTDIR/parse.txt" | bc -l)
+testcmd "parse" "-l $TESTDIR/parse.txt" "$results\n" "$TESTDIR/parse.txt" ""
+
+if [ ! -f "$TESTDIR/print.txt" ]; then
+ echo "Generating bc print..."
+ cat "$BDIR/print.bc" | bc -l > "$TESTDIR/print.txt"
+fi
+results=$(cat "$TESTDIR/print.txt" | bc -l)
+testcmd "print" "-l $TESTDIR/print.txt" "$results\n" "$TESTDIR/print.txt" ""
+
+results=$(cat "$BDIR/pi.txt" | bc -l)
+testcmd "pi" "-l $BDIR/pi.txt" "$results\n" "$BDIR/pi.txt" ""
+
+if [ ! -f "$TESTDIR/arctan.txt" ]; then
+ echo "Generating bc arctan..."
+ cat "$BDIR/arctan.bc" | bc -l > "$TESTDIR/arctan.txt"
+fi
+results=$(cat "$TESTDIR/arctan.txt" | bc -l)
+testcmd "arctan" "-l $TESTDIR/arctan.txt" "$results\n" "$TESTDIR/arctan.txt" ""
+
+results=$(cat "$TESTDIR/arctan.txt")
+testcmd "arctan script" "-l $BDIR/arctan.bc" "$results" "$BDIR/arctan.bc" ""
+
+results=$(cat "$TESTDIR/parse.txt")
+testcmd "parse script" "-l $BDIR/parse.bc" "$results\n" "$BDIR/parse.bc" ""
+
+results=$(cat "$TESTDIR/print.txt")
+testcmd "print script" "-l $BDIR/print.bc" "$results\n" "$BDIR/print.bc" ""
+
+results=$(cat "$BDIR/screen.bc" | bc -l)
+testcmd "screen script" "-l $BDIR/screen.bc" "$results\n" "$BDIR/screen.bc" ""
+
diff --git a/tests/files/bc/add.txt b/tests/files/bc/add.txt
new file mode 100644
index 00000000..01ea6c80
--- /dev/null
+++ b/tests/files/bc/add.txt
@@ -0,0 +1,24 @@
+0 + 0
+0 + 1
+1 + 1
+1 + 0
+2 + 5
+237 + 483
+999 + 999
+2374623 + 324869356734856
+2378639084586723980562 + 23468729367839
+37298367203972395108367910823465293084561329084561390845613409516734503870691837451 + 785621394067928346918023476190834672198467134908618723249671349062187346898241093486139046139084613490817356023871869102746182749061872609129847
+1.1 + 0
+0 + 1.1
+457283.731284923576 + 37842934672834.3874629385672354
+1.0 + 0.1
+3746289134067138046 + 0.138375863945672398456712389456273486293
+-1 + -1
+-4 + -15
+-1346782 + -1287904651762468913476
+99999999999999999999999999999999999999999999999999999999999.999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 + 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
+99999999999999999999999999999999999999999999999999999999999.999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999995 + 0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005
+99999999999999999999999999999999999999999999999999999999999.999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 + 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
+99999999999999999999999999999999999999999999999999999999999.999999999999999999999999999999999999999999999999999999999899999999999999999999999999999999999999999999999999999999999999 + 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
+99999999999999999999999999999999999989999999999999999999999.999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 + 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
+-1889985797 + 2012747315
diff --git a/tests/files/bc/add_results.txt b/tests/files/bc/add_results.txt
new file mode 100644
index 00000000..313fa450
--- /dev/null
+++ b/tests/files/bc/add_results.txt
@@ -0,0 +1,36 @@
+0
+1
+2
+1
+7
+720
+1998
+324869359109479
+2378639108055453348401
+78562139406792834691802347619083467219846713490861872324967138636055\
+45508706362018540498696043776980521464405852627147161556994835657433\
+00967298
+1.1
+1.1
+37842935130118.1187478621432354
+1.1
+3746289134067138046.138375863945672398456712389456273486293
+-2
+-19
+-1287904651762470260258
+100000000000000000000000000000000000000000000000000000000000.0000000\
+00000000000000000000000000000000000000000000000000000000000000000000\
+000000000000000000000000000000000000000000000
+100000000000000000000000000000000000000000000000000000000000.0000000\
+00000000000000000000000000000000000000000000000000000000000000000000\
+000000000000000000000000000000000000000000000
+100000000000000000000000000000000000000000000000000000000000.0000000\
+00000000000000000000000000000000000000000000000000000000000000000000\
+000000000000000000000000000000000000000009999
+99999999999999999999999999999999999999999999999999999999999.99999999\
+99999999999999999999999999999999999999999999999999000000000000000000\
+00000000000000000000000000000000000000009999
+99999999999999999999999999999999999990000000000000000000000.00000000\
+00000000000000000000000000000000000000000000000000000000000000000000\
+00000000000000000000000000000000000000009999
+122761518
diff --git a/tests/files/bc/arctan.bc b/tests/files/bc/arctan.bc
new file mode 100755
index 00000000..11a76953
--- /dev/null
+++ b/tests/files/bc/arctan.bc
@@ -0,0 +1,12 @@
+#! /usr/bin/bc -q
+
+i = 0
+for (i = 1; i <= 100; ++i) {
+ print "scale = ", i, "\n"
+ print "a(.267)\n"
+ print "a(1)\n"
+}
+
+"halt"
+
+halt
diff --git a/tests/files/bc/basic.txt b/tests/files/bc/basic.txt
new file mode 100644
index 00000000..f3d957a2
--- /dev/null
+++ b/tests/files/bc/basic.txt
@@ -0,0 +1,7 @@
+scale=10;123981239.981273987 * 12983791827398.29836472638
+scale=100;759634576394.3946587934658364895 / 9834759834895386.36459364958346
+34895734986539489834759837489573498573.398475984759837485734987598345 + 9823749832749872384234872934.28347982374987239847982374
+a=123123123.987239874; b=123123123.239479823748; a+b
+20347023.23498723984 - 28934723874.234720384
+scale=100;a=739534985.895347284957;b=238746782364.2374623784; c = a / b; c+0
+
diff --git a/tests/files/bc/basic_results.txt b/tests/files/bc/basic_results.txt
new file mode 100644
index 00000000..1e0d92f1
--- /dev/null
+++ b/tests/files/bc/basic_results.txt
@@ -0,0 +1,9 @@
+1609746610419572350599.59456175545
+.0000772397688552681359718594121969204138521230712049526233926741658\
+845368495051158801794834809672994
+34895734996363239667509709873808371507.68195580850970988421481133834\
+5
+246246247.226719697748
+-28914376850.99973314416
+.0030975704827179453786044330548590249517387192084765414205077089498\
+482709063381782183114683451531598
diff --git a/tests/files/bc/decimal.txt b/tests/files/bc/decimal.txt
new file mode 100644
index 00000000..a58f9304
--- /dev/null
+++ b/tests/files/bc/decimal.txt
@@ -0,0 +1,34 @@
+0
+0.0
+000000000000000000000000.00000000000000000000000
+000000000000000000000000000135482346782356
+000000000000000000000000002
+1
+11
+123
+7505
+1023468723275435238491972521917846
+4343472432431705867392073517038270398027352709027389273920739037937960379637893607893607893670530278200795207952702873892786172916728961783907893607418973587857386079679267926737520730925372983782793652793
+-1
+-203
+-57
+-18586
+-31378682943772818461924738352952347258
+-823945628745673589495067238723986520375698237620834674509627345273096287563846592384526349872634895763257893467523987578690283762897568459072348758071071087813501875908127359018715023841710239872301387278
+.123521346523546
+0.1245923756273856
+-.1024678456387
+-0.8735863475634587
+4.0
+-6.0
+234237468293576.000000000000000000000000000000
+23987623568943567.00000000000000000005677834650000000000000
+23856934568940675.000000000000000435676782300000000000000456784
+77567648698496.000000000000000000587674750000000000458563800000000000000
+2348672354968723.2374823546000000000003256987394502346892435623870000000034578
+-2354768.000000000000000000000000000000000000
+-96739874567.000000000347683456
+-3764568345.000000000004573845000000347683460
+-356784356.934568495770004586495678300000000
+74325437345273852773827101738273127312738521733017537073520735207307570358738257390761276072160719802671980267018728630178.7082681027680521760217867841276127681270867827821768173178207830710978017738178678012767377058785378278207385237085237803278203782037237582795870
+-756752732785273851273728537852738257837283678965738527385272983678372867327835672967385278372637862738627836279863782673862783670.71738178361738718367186378610738617836781603760178367018603760178107735278372832783728367826738627836278378260736270367362073867097307925
diff --git a/tests/files/bc/decimal_results.txt b/tests/files/bc/decimal_results.txt
new file mode 100644
index 00000000..01bfa586
--- /dev/null
+++ b/tests/files/bc/decimal_results.txt
@@ -0,0 +1,48 @@
+0
+0
+0
+135482346782356
+2
+1
+11
+123
+7505
+1023468723275435238491972521917846
+43434724324317058673920735170382703980273527090273892739207390379379\
+60379637893607893607893670530278200795207952702873892786172916728961\
+78390789360741897358785738607967926792673752073092537298378279365279\
+3
+-1
+-203
+-57
+-18586
+-31378682943772818461924738352952347258
+-8239456287456735894950672387239865203756982376208346745096273452730\
+96287563846592384526349872634895763257893467523987578690283762897568\
+45907234875807107108781350187590812735901871502384171023987230138727\
+8
+.123521346523546
+.1245923756273856
+-.1024678456387
+-.8735863475634587
+4.0
+-6.0
+234237468293576.000000000000000000000000000000
+23987623568943567.00000000000000000005677834650000000000000
+23856934568940675.000000000000000435676782300000000000000456784
+77567648698496.00000000000000000058767475000000000045856380000000000\
+0000
+2348672354968723.237482354600000000000325698739450234689243562387000\
+0000034578
+-2354768.000000000000000000000000000000000000
+-96739874567.000000000347683456
+-3764568345.000000000004573845000000347683460
+-356784356.934568495770004586495678300000000
+74325437345273852773827101738273127312738521733017537073520735207307\
+570358738257390761276072160719802671980267018728630178.7082681027680\
+52176021786784127612768127086782782176817317820783071097801773817867\
+8012767377058785378278207385237085237803278203782037237582795870
+-7567527327852738512737285378527382578372836789657385273852729836783\
+72867327835672967385278372637862738627836279863782673862783670.71738\
+17836173871836718637861073861783678160376017836701860376017810773527\
+8372832783728367826738627836278378260736270367362073867097307925
diff --git a/tests/files/bc/divide.txt b/tests/files/bc/divide.txt
new file mode 100644
index 00000000..4d0caddc
--- /dev/null
+++ b/tests/files/bc/divide.txt
@@ -0,0 +1,31 @@
+0 / 1
+0 / 321566
+0 / 0.3984567238456
+1 / 1
+1 / 1287469297356
+1 / 0.2395672438567234
+1 / 237586239856.0293596728392360
+1249687284356 / 3027949207835207
+378617298617396719 / 35748521
+9348576237845624358 / 0.9857829375461
+35768293846193284 / 2374568947.045762839567823
+-78987234567812345 / 876542837618936
+-356789237555535468 / 0.3375273860984786903
+-5203475364850390 / 435742903748307.70869378534043296404530458
+-0.37861723347576903 / 7385770896
+-0.399454682043962 / 0.34824389304
+-0.6920414523873204 / 356489645223.76076045304879030
+-35872917389671.7573280963748 / 73924708
+-78375896314.4836709876983 / 0.78356798637817
+-2374123896417.143789621437581 / 347821469423789.1473856783960
+-896729350238549726 / -34976289345762
+-2374568293458762348596 / -0.8792370647234987679
+-237584692306721845726038 / -21783910782374529637.978102738746189024761
+-0.23457980123576298375682 / -1375486293874612
+-0.173897061862478951264 / -0.8179327486017634987516298745
+-0.9186739823576829347586 / -0.235678293458756239846
+-0.9375896183746982374568 / -13784962873546.0928729395476283745
+-2930754618923467.12323745862937465 / -734869238465
+-23745861923467.874675129834675 / -0.23542357869124756
+-3878923750692883.7238596702834756902 / -7384192674957215364986723.9738461923487621983
+1 / 0.00000000000000000000000000000000000000000002346728372937352457354204563027
diff --git a/tests/files/bc/divide_results.txt b/tests/files/bc/divide_results.txt
new file mode 100644
index 00000000..c19f2f9b
--- /dev/null
+++ b/tests/files/bc/divide_results.txt
@@ -0,0 +1,31 @@
+0
+0
+0
+1.00000000000000000000
+.00000000000077671755
+4.17419336592637110778
+.00000000000420899796
+.00041271738677857404
+10591131829.40901859967857131767
+9483402361494453751.52388015648196297248
+15063068.13735316451497043884
+-90.11223545260531110575
+-1057067521778623447.45138528213564485251
+-11.94161814246320631346
+-.00000000005126306228
+-1.14705437777218917343
+-.00000000000194126663
+-485262.88923145638029569727
+-100024372711.74763635544535424582
+-.00682569681609989277
+25638.20711150436682153521
+2700714504347599627864.24626421085374010264
+10906.42973524078145692731
+.00000000000000017054
+.21260557443109085166
+3.89799997647407910677
+.00000000000006801538
+3988.13076601933678578945
+100864416620775.31076855630746548983
+.00000000052530099381
+42612515855353136519261264261472677699404182.78776061098893912189
diff --git a/tests/files/bc/modulus.txt b/tests/files/bc/modulus.txt
new file mode 100644
index 00000000..e2539892
--- /dev/null
+++ b/tests/files/bc/modulus.txt
@@ -0,0 +1,66 @@
+1 % 1
+2 % 1
+16 % 4
+15 % 4
+17 % 4
+2389473 % 5
+39240687239 % 1
+346728934 % 23958
+3496723859067234 % 298375462837546928347623059375486
+-1 % 1
+-2 % 1
+-47589634875689345 % 37869235
+-1274852934765 % 2387628935486273546
+-6324758963 % 237854962
+1 % -1
+2 % -1
+2 % -2
+2 % -3
+16 % 5
+15 % 5
+14 % 5
+89237423 % -237856923854
+123647238946 % -12467
+-1 % -1
+-2 % -1
+-2 % -2
+-2 % -3
+-13 % -7
+-14 % -7
+-15 % -7
+-12784956 % -32746
+-127849612 % -23712347682193
+scale = 0
+1 % 1
+2 % 1
+16 % 4
+15 % 4
+17 % 4
+2389473 % 5
+39240687239 % 1
+346728934 % 23958
+3496723859067234 % 298375462837546928347623059375486
+-1 % 1
+-2 % 1
+-47589634875689345 % 37869235
+-1274852934765 % 2387628935486273546
+-6324758963 % 237854962
+1 % -1
+2 % -1
+2 % -2
+2 % -3
+16 % 5
+15 % 5
+14 % 5
+89237423 % -237856923854
+123647238946 % -12467
+-1 % -1
+-2 % -1
+-2 % -2
+-2 % -3
+-13 % -7
+-14 % -7
+-15 % -7
+-12784956 % -32746
+-127849612 % -23712347682193
+-3191280681 % 641165986
diff --git a/tests/files/bc/modulus_results.txt b/tests/files/bc/modulus_results.txt
new file mode 100644
index 00000000..323b3a7d
--- /dev/null
+++ b/tests/files/bc/modulus_results.txt
@@ -0,0 +1,65 @@
+0
+0
+0
+0
+0
+0
+0
+.00000000000000002026
+2747189239559.46904933397471305894
+0
+0
+-.00000000000011057855
+-.00076922992566770712
+-.00000000000050364144
+0
+0
+0
+.00000000000000000002
+0
+0
+0
+.00000000070585524350
+.00000000000000002898
+0
+0
+0
+-.00000000000000000002
+-.00000000000000000005
+0
+-.00000000000000000002
+-.00000000000000011722
+-.00000002640923745817
+0
+0
+0
+3
+1
+3
+0
+8758
+3496723859067234
+0
+0
+-8236960
+-1274852934765
+-140529951
+0
+0
+0
+2
+1
+0
+4
+89237423
+6692
+0
+0
+0
+-2
+-6
+0
+-1
+-14016
+-127849612
+-626616737
diff --git a/tests/files/bc/multiply.txt b/tests/files/bc/multiply.txt
new file mode 100644
index 00000000..5ef49c4c
--- /dev/null
+++ b/tests/files/bc/multiply.txt
@@ -0,0 +1,39 @@
+0 * 0
+1 * 0
+0 * 1
+0 * 2498752389672835476
+873246913745129084576134 * 0
+1 * 472638590273489273456
+12374861230476103672835496 * 1
+1 * 1
+2 * 1
+1 * 2
+2 * 2
+3 * 14
+17 * 8
+1892467513846753 * 1872439821374591038746
+328962735862.2973546835638947635 * 1728465791348762356
+38745962374538.387427384672934867234 * 0.1932476528394672837568923754
+9878894576289457634856.2738627161689017387608947567654 * 37842939768237596237854203.29874372139852739126739621793162
+-1 * 1
+-1 * 2
+78893457 * -34876238956
+235678324957634 * -0.2349578349672389576
+-12849567821934 * 12738462937681
+1274861293467.927843682937462 * -28935678239
+2936077239872.12937462836 * -0.012842357682435762
+2387692387566.2378569237546 * -272189345628.123875629835876
+0.012348629356782835962 * -23487692356
+0.4768349567348675934 * -0.23756834576934857638495
+0.98748395367485962735486 * -4675839462354867.376834956738456
+-321784627934586 * -235762378596
+-32578623567892356 * -0.32567384579638456
+-35768232346876 * -2348672935602387620.28375682349576237856
+-0.2356728394765234 * -238759624356978
+-0.2345768212346780 * -0.235768124697074385948943532045
+-0.370873860736785306278630 * -7835678398607.7086378076867096270
+-78365713707.7089637863786730 * -738580798679306780
+-73867038956790490258249 * -0.7379862716391723672803679
+-378621971598721837710387 * -98465373878350798.09743896037963078560
+37164201 * 2931559660
+679468076118972457796560530571.46287161642138401685 * 93762.2836
diff --git a/tests/files/bc/multiply_results.txt b/tests/files/bc/multiply_results.txt
new file mode 100644
index 00000000..c8167dbe
--- /dev/null
+++ b/tests/files/bc/multiply_results.txt
@@ -0,0 +1,40 @@
+0
+0
+0
+0
+0
+472638590273489273456
+12374861230476103672835496
+1
+2
+2
+4
+42
+136
+3543531533584430580556128344529291738
+568600835566479683035874339053.4411638427543228060
+7487566285885.8557453089005171423976251098
+373846412427291014394738378015501363938345620046.7869650248829232267\
+2297002026819
+-1
+-2
+-2751507058396910892
+-55374468980751.0837656919743223184
+-163683743464924630346895054
+-36888976187143312550878.567134791289418
+-37706154097.69662826215753378160
+-649904428532907022680241.94791869424754101064
+-290040807.350385412976669306472
+-.11328089187650139309272
+-4617316439035114.40320367843985107357898
+75864709277486862054521256
+10610005628108234.92015040406042336
+84007879267445533366251128067927.91168012197674537856
+56269158624557.1027018519702852
+.055305737239900889424090264801
+2906048299183.472237078104362540110129
+57879411419313585866282299201.3825582163029400
+54512860676747314187949.9414724679950990587298071
+37281153992026463004361915151761464058058.54968338992209002720
+108949072447731660
+63708478450213482928510139572007971.83536929222529239687
diff --git a/tests/files/bc/parse.bc b/tests/files/bc/parse.bc
new file mode 100755
index 00000000..8cc7472f
--- /dev/null
+++ b/tests/files/bc/parse.bc
@@ -0,0 +1,16 @@
+#! /usr/bin/bc -q
+
+for (b = 2; b <= 16; ++b) {
+ if (b == 10) continue
+ obase = 10
+ print "ibase = A; ibase = ", b, "\n"
+ obase = b
+ for (i = 0; i <= 65536; ++i) {
+ i
+ print "0.", i, "\n"
+ print "1.", i, "\n"
+ print i, ".", i, "\n"
+ }
+}
+
+halt
diff --git a/tests/files/bc/pi.txt b/tests/files/bc/pi.txt
new file mode 100644
index 00000000..4ae0fa64
--- /dev/null
+++ b/tests/files/bc/pi.txt
@@ -0,0 +1,7 @@
+for (i = 0; i <= 100; ++i) {
+ scale = i
+ 4 * a(1)
+}
+
+scale = 1000
+4 * a(1)
diff --git a/tests/files/bc/power.txt b/tests/files/bc/power.txt
new file mode 100644
index 00000000..3222e9d7
--- /dev/null
+++ b/tests/files/bc/power.txt
@@ -0,0 +1,42 @@
+0 ^ 0
+0 ^ 1
+0 ^ 1894
+1 ^ 0
+39746823 ^ 0
+0.238672983047682 ^ 0
+18394762374689237468.97354862973846 ^ 0
+1 ^ 1
+2 ^ 1
+18927361346 ^ 1
+0.23523785962738592635777 ^ 1
+328956734869213746.89782398457234 ^ 1
+8937 ^ 98
+0.124876812394 ^ 2396
+93762.2836 ^ 13
+1 ^ -1
+2 ^ -1
+10 ^ -1
+683734768 ^ -1
+38579623756.897937568235 ^ -1
+1 ^ -32467
+2 ^ -53
+23897 ^ -213
+-1 ^ 1
+-1 ^ 2
+-2 ^ 1
+-2 ^ 2
+-237 ^ 294
+-3746 ^ 28
+-0.3548 ^ 35
+-4267.234 ^ 2837
+-326.3246 ^ 2378
+-1 ^ -1
+-1 ^ -2
+-2 ^ -1
+-2 ^ -2
+-237 ^ -293
+-784 ^ -23
+-0.2357689 ^ -8723
+-0.23424398 ^ -781
+-178.234786 ^ -879
+-1274.346 ^ -768
diff --git a/tests/files/bc/power_results.txt b/tests/files/bc/power_results.txt
new file mode 100644
index 00000000..28f9ad73
--- /dev/null
+++ b/tests/files/bc/power_results.txt
@@ -0,0 +1,385 @@
+1
+0
+0
+1
+1
+1
+1
+1
+2
+18927361346
+.23523785962738592635777
+328956734869213746.89782398457234
+16473742664221279051571200630760751138799221376964991600670000200609\
+08006052596520320731708604393844468006290371918262741885989163144389\
+39367835091560809036359941703341471396407660150658436796925310445979\
+21333166245765946557344383284626113908419359990042883048537750217279\
+17481980123593363177308481941550382845381799410533956718500484099889\
+610805653325917409549921909941664118421333562129
+0
+43287877285033571298394739716218787350087163435619825150259705419.98\
+016445740928054425
+1.00000000000000000000
+.50000000000000000000
+.10000000000000000000
+.00000000146255543348
+.00000000002592041867
+1.00000000000000000000
+.00000000000000011102
+0
+-1
+1
+-2
+4
+14997322375665265051328725757939209353051902095893907150382724666290\
+49749481660976421019742616298227588464420182758442163654172400528243\
+00885441207762486233574213374503090372518590691583139696652847404883\
+08573912261119588874308960204159666762789603037188471170006223907416\
+60492840269152716750700089148882139254399347568222390231015487895905\
+73727080561379177721440905866857248917982113340543176658480139248897\
+54802503253413282808814063861470711399810899724515727713334909764746\
+27910290211411231279325882505708287941671508154740003122373284699097\
+78346501539634198926772266511968381368929692275950529960923432771985\
+12597189390708050983487158873301681237787429436264801751664042999180\
+3448659818912436089
+11478830555358864333472551120140548480416206583184496764727387456058\
+792742209537931243951391229607936
+-.00000000000000017759
+-5315000202937163259492745003279421369106067669145129859648420636529\
+90445623045796466912212328506740453231539873473645734499753872948065\
+49633577959910694192145181126036108234067752197600736121017297080808\
+17616863795402188466673465041737963185044596535417380459631266160640\
+14583650452617054961289909930913477463730596242904393535447326927217\
+89173834172615425952449908691097034304917795485671070317830033446935\
+38310140064407341431340535164186443832804681667156458972977303614472\
+33265448905074750219542877570352782981931513698676869313385161238014\
+33972388487066619195467353034148880813325180423582595649900987746182\
+41895615903666063666862790223026863393142524825702604904791179988750\
+45809820128887237302302157495251041558294866524648134606244499204091\
+49083321829864412304204374908788049385821238476680431773652392070264\
+59357384450300571070886634991808217727920058131365837495515494533981\
+36075073475798690610073101157985973126714728174798097385600505077064\
+64307865500312391435488397915044572948634144019685716400039399263051\
+47069168529330946438107251496721589630778010562251104749454115537505\
+09733953415921981476370056761128775090287101279301783155189276038704\
+41895108419474778802941709888668845300019024827549284756327304572867\
+96869370102131426204516615827055555481884884048493606181911952577877\
+05397677851197539883332750824024563555696883835248240415293162263750\
+05514521919787836514087683462282073201968011400045654810616450219205\
+73232304423399284026556121979893723420267398521035723282740403251520\
+70294763262021087574735376398655633958565910985905690707543972327381\
+79651823478575002271270347555847440100290072055641291249615250133821\
+23680505794038719878415117091425492978380126284031828475714412518947\
+51403219894454213518071933476255317428650882367543271627040950041490\
+91372087962670790674328089839025158796625170118099611830170979543268\
+46838632558303335286513165259961630072343847303015053999848541735021\
+39379960402818223313798877113839753031848265476145371954184400351975\
+19977868370176657443035408745111796788080163762225487710398592741290\
+98787560930947073645198429428485551148558897458881325360238752512610\
+98690991255078245972980209265660013809958812534609566576738101048147\
+34329168149020134400358076493774220564483240987538866711281966656859\
+18228754994689923361234391823268535969284464590048851347740342490135\
+80582121673049738942667229825249768347409932358345160950361775166418\
+82751455678372618713022132536591723169934677105756691317738344397268\
+90573948285753449856526392182699265739146286909855515863327591370172\
+96513402830426867299550070873218083912846316696859579237689097606617\
+51412181799181999491584413579614146497226592868301697388159623700483\
+00659079928861331453476112481313601846831595328497558270598024459232\
+74912696765475545648721620753770739038715242528380011572795805287890\
+01939075629577724714913609448633168350641667779462272317624977977687\
+89411614882740247487664271240314348194082962843296263500386625878796\
+16008267844410696232952338235393887568658739373008887867945112312649\
+37512501534803268323910539533256177216274619007193744007547951718027\
+44891444064662982576153272050128040514240063258499485242019550507676\
+49205138815607756069725427984311319511383142516469030500318130266868\
+09562452698690462723828096105664810553482713948643515707658305658393\
+72023560005542444849685206743523113508500669188917639514697867955895\
+12373464625905759443093770382990555315516365416332558133979562433906\
+64399570722334065625238452042436862494620574134988589848680778801045\
+80364738912102881123102375480846598525717754089210605941752731049392\
+56508983631805570091285332387678472351633908797365562850039083300108\
+47232419881746103565282587619297019434166937029533469166882663817564\
+29976915091973096170480140645736257668317363421806536290197863436706\
+26329935979085366547105772889986581827630974873885363455721321518675\
+74550918001246595763248153435251121743997710894306721046034483557443\
+41864814444085298842519861926734021561007690300579493326968172112502\
+95349794378935063903999801566960185173164130751935856368624282418902\
+49903427392381027457836738642462080633422197014627613031666173271030\
+24009983647554746216502223968131796915055226880729894075452499140925\
+19177060502090392977689000070401037945555335784892959624056769694880\
+70127948818327506368318902831799150505770877874320893074064721292530\
+76004340604038727565364771854369404877106769882488933769119954174458\
+63427198989142502713770105419055459625123528877838096228199336886214\
+21837202020595779957506625129762358884285607071464800971415209072458\
+75682887352693221792690653281585020879608438468426360254454386930843\
+54976719034954514257351084680941537598410460432624632742664122097832\
+55471067840289495535313044781584274249832292545673979710554622543455\
+30516485658045903022569236826984516014156085335693691217261175850028\
+11509249909703901135898031685959221507256600883997280093471285352593\
+74651564252383941896194282297958682783629524012219104563242048510868\
+49404888188055550910540455332990660903744972091115431351121998533490\
+10628770561915126917221852108705651086200022632698873237836290310011\
+50397167016292123727570196128842632496685647047378603671979173369902\
+38344528655539740311539345421161242532069895829237353049373101325730\
+58493431814287223494709459895137424641564383576690784600424038087145\
+68158626235336913952048107753521197980942527673210440048833533401989\
+77194468283364506459394258867172301466872892027180482209222506949673\
+75540211531086133182859182501150200938696263997726250563093539326927\
+72416498048175683546939420898769717737493711540907129536293416724813\
+97334892336321395426368347878890663066224459524345472570152264395565\
+82460457205896704725983507812401218371956212982033047108572121728288\
+68439360992844654681941401782292533682559747777726117649955921552419\
+55376528393134227258758852448268802426432183531325943771999071974220\
+84507900584510094182786537515367929613158392549027805302398374451653\
+29783084211098081095916445508682076609084201827750535494005579802641\
+02110253224910370222254741046671205720360236688797968947854015063183\
+74781883451633687835151401181076347221311394223209721077894188865562\
+17163237549065972085718513359358745797320834007267934729637068457958\
+78217344964677096738194254363183885544424572157628941346479343797930\
+63230768723171941706715546034642365335802368178916578666998370822061\
+72862620144866941168433360449388831342015135002439921450475536956234\
+85718095823776355549608105131404093038257448195903825875318154784464\
+57935967137213602336432368420092393199865275518568170524736528628808\
+93046789512886643769514221911060706901882607274461392115657530222519\
+60506897541913211652548871872655185050770561256446930362103759873477\
+20814169286537387159054802380892512900817347470322088751411585458882\
+41036364127218851060253574463952502666758564753936431041853733829336\
+81956327037910407368879304536602331340358193253227646166149675880981\
+90185334787495657054886233085573272038907945218357219923287605428049\
+35666687006406359493768055467531146025838548653407558188013071660477\
+24887464015842344700561688728471808389891252725983936114487839466957\
+40043539434901358379294391964732252971766132528295626269595832591134\
+56027086653085411942665786654648139554565734237361808707377581887789\
+66923318292898929469635931606196495932835130228371557394460176225814\
+11294729746332614993610933221574816059609941245722612553947822056329\
+45166526051215867786737290871927622630571958311944341999538582128241\
+88101449037438663818287018817893377018470050800767265593919107803658\
+99179719652514229604754127018714522533683024867676021795442147409531\
+82665068198462837070231146555812259357357244425469897484784424722789\
+98092162511423506856070449076457969548678211615932082662896053695730\
+59596107990542324416612202410744156365868863162658463321070228706136\
+03710190425407794885324375605255084573684494837484778909503277766179\
+03805931454654223652089029461399110825127848153069688576228655184611\
+79768137149996093510064025168479068628086773645884984425319145539556\
+76075842624219168086936568629614756427244958470902064700287889315121\
+42821595391061266909080096092842976758284557671645207398033087646044\
+17795953284430870706941395475756240133534497464444148185165836807395\
+71914117065975311130980817438007210936836173788977246078579157885631\
+72554614326985076533529062730320433321718255208710180221207959776564\
+44807136643850248843075432242048108865528202646121118339881280138404\
+82631669965543003783709309604010072045746432183362756236508231558635\
+61612161495534067211577721563585202440442091745682170086700119976895\
+75305033539518807762552637355861809986052721463214055606925010502791\
+07576812492087807771863865045627658966172004291057058152115216759979\
+22319079703493506449231900704401329270559051817722834844916790378159\
+11859709258213481757435116529558560545817146375060752889351017308411\
+95572062296153349066166356419465693735841939197159220983287655122200\
+09840635157449290677165408337833131188667931637453023949628963892756\
+63193160301363072401750285289993232161836627416139186835622928460896\
+48149321238238359576069768524810647473234302169351941202863446100303\
+53232244538087380921705800082227661328078185734538075621775853483863\
+46009590875234025712706120859992692401627589733758997890952517701102\
+02016569612428376053082696146484227576336230217783445219505920601019\
+33095515519409035408457025188297805207228192604390482809071392611771\
+33574590494165516290378201746154276638463914544405829940734889440058\
+50872804307391483948705360196306393676853047662331937698157275120923\
+48711155405917301727814008897384220075085940864758781426442918315337\
+44390460389070271397096746357700721225995274575108538008722000506426\
+47404684409899926300364430394284048326877036744378234790013121512243\
+33164520619956221756712877328384924842769057299918312486776801292033\
+39413452695345490240752153799893116785749303845400056553954464555994\
+39334000822859108507894822667482270295636516694380611524569121800656\
+78897195293770217125145562613393065181685018248294372660081402258855\
+48853815860093731077664651690349598684633625439324574755046646013201\
+06398457066352039259763533826806131024950715685749198907167018006511\
+02464285979192745974664572565800070933275878035489902288536459657148\
+10685133636383678846628804163294690525771415724388606805062681265155\
+37392172544282115243622208997041455133123289393606286284500392860748\
+62119813421171900660703096743899221542487565865013924144916275687604\
+64479606566082627831225860827957.17168783602794452821
+28791115786912240198245990478525647696448607532847787454795539441976\
+53008508268300461139912183674125065861791306990382838955098345250249\
+22502669615923663783199016557590859598868092987910291249798821013403\
+47170755689306294929681899092787860167594484443520025764276997309005\
+71232591347006382641934629802477111942681146373278874858958991309971\
+42218657973486847296406258064331676882178534298665203090265502510842\
+61357125721516691133409251768860513967679408456139499213904923923576\
+37499277430205645222804747709288259277839665497374416103934921664053\
+88731269133493437149540799596101039891946001868119671990803717566748\
+68467317062896591145978297935654103133461783237181415461118621447827\
+16566513542336675866067109631200815886090268983547210391679827916177\
+66749211382337550790076502678641642759218859737882068043983365758093\
+88115252413477398218126191256394635905372596424544841499726539685941\
+78846216282131573581580140976129717830765415534880473794126349434060\
+97681722453558939398648461893368494055862371927814819807297911399492\
+95580520229158808988334006735101124776362181960799247867627735191693\
+73903485711688121338008278287587497284624296263925302024072761974353\
+61966862903296298157098997084877146402382401378955497440970708541715\
+71060260791739883205083920081207497221052029803497230694536402248293\
+84189292235575350657775157901460538210293883451036457246928408437463\
+57025709490435640319319731216408410871212349724010449894980914894111\
+45126169053678363169035062112633812855552434451031074552786171143660\
+17791316677936527412873416758988896839052262917297431626073985184825\
+68730935720280511977117820747425896122905967666049570524572855538268\
+71933166116690914412078365573528353249200818122694099666956346567349\
+56010083783703566732405340529840912472403938999295934861826136988528\
+52894398797143227990144946007954597506649176236568417409173396261740\
+09944204836056027440284138866913080493697596579842739525086068210108\
+30917026182518589497356978161501183235810718665383370604175757091568\
+10504733159455864451885520846599280592234061718671428498049587028363\
+09597188469848024202986163560176441060995115838886952815416199737130\
+85184131031637903681209647592228571844358102702795272489240540680335\
+22995408987458398979652061771760715657085982221294267557650384941211\
+16571782590299783628409645657075923536327475626565993147833196817893\
+84890960043718955708485355506889614382188765539323874543288395613135\
+48259205614690288575840557276052824284532091372945666583648761976586\
+54283058035504963407488664771183492661235835415687745843736315114774\
+43686975428521437310363210532146903026713880922536383314487398789950\
+21702186636914955380336514259253343897789306312138193893174316891009\
+25090195344105820648333601730709055646550997346188323397099605767390\
+34894823825457067510931521240790321078124435027433519041465778273927\
+23319532476569131737943328836549868951187283843748101565050501127139\
+39788055683460535125272798566822769537496548960358061381835527396784\
+44159701828925988029710177774963814786204991523055795795848219525653\
+78564959121442581906399531806160230589018845426048851169697986777287\
+74075545396657630131400229956599605344545338728285946750023266326131\
+18395556301061966915885165125087447008943040958729761732405732133453\
+05659206180031669466579706301575290530027196513949455331037954675305\
+91429773751487919865699329769483827518561578140696380487766857058838\
+31309495041955167145108840698853798192292390926661492994257145343218\
+75197591439357956390053018804271252634995353886475156597147490672190\
+84005441103601046351701967370002688361030978781507248572868925129931\
+45212430472187369787725343203192539974915241551259671056853308250443\
+11579521168679966954003584316378530310813735804407853338924589172695\
+49587287454029232631399437877164701878506916307005029866776607783174\
+00990236971979307623094886527377840855865891785310660470287570263965\
+61154240289857785082315249341545762749489375521484585792993526397150\
+25291795278405033005502446233161932054427925917356980644302691002421\
+05338681858378618032104503307510725895517575751909671470465133180844\
+45243480750024854699238912226469242734228059627483302616513195012834\
+43802954961131534089020537879306473924379457087764802237333653274089\
+09872997910414155414383580061821406683789130399702013589496974143998\
+44210014079488461309789159536423690801631777295441838031615666359452\
+91792585173201915395374115631184500874352677152997721623175685942803\
+12895271487864578322544981918732876539884531639075213978691991567128\
+92573298522996863101920878033796664977571688329072100951103604405455\
+04594983831740226312202494100656829982197377383796775671017926797756\
+33549687557826719410938591588030077844865002785718028430426129760210\
+36066302087600569325663160491853875481246546462291564965235321897400\
+09821990048706319068186530740803926837337644235256457047575804469608\
+57918916920499725776957856681677440330511211984210012143987263574481\
+71709887053711071662845885541874087944270034896684123101730242288672\
+65579256713582552909399156304710870435571862371857243373965327959283\
+24373963625438574863307319451700340730565660031867078113256711758690\
+18796202639316990441856414190865637384044006457067135125967606919472\
+95170121458841562486321464164304264236784363298699561094832596396303\
+92360868258336925172544977314262275517987378331545670817629321398756\
+15182133973429985062471316188494603822719972823331439920331498101624\
+22111613425281021340447127502486056789900343682995162436423970012504\
+90978225961351399347128920481166268040828775885589315967277308416623\
+10747904592254184017050343694378092682731532047514967157960793406652\
+36139056722846794201187751078359641314204515940335354669085175926977\
+07798383659826703423446297688966624848039546300824464419495212357576\
+30053757647015611767992745708562501425118059217142265688975505274910\
+97787536006751801909703819658921934635314177988896852619491725638370\
+71808330540021305608094054601852463148765060366588983799418357589432\
+50483622298669607953178468063724713066810575740314939366769021165167\
+07815391414524210692762963631699377023760137647543023883110883.71538\
+917119055084587
+-1.00000000000000000000
+1.00000000000000000000
+-.50000000000000000000
+.25000000000000000000
+0
+0
+-6311170951758340301695281786963968566677422268510931490731625646715\
+38125720985932037093927317044573426510551289791217448529282843416440\
+05520241029621417251047599699565382090122980340754774043142594913023\
+41451303393302883218750389898179604908116730833849497339744904526958\
+92448895393345637572643345481921080373277750542706722066894818520149\
+55418007617650491691821446193721233611264616251750844770017173787564\
+04232060714472103058158955208768415961630196604514972898509827500777\
+27629930054547832303627233403424962009295500700592022031464695225081\
+69492417968838445910538898321612151030444695696921499051881571008127\
+66125471882262561112705701582131968122295251908747948325161616700850\
+85967720641482842278602425546630702337458673286944405688069366413844\
+74467291289141708874382307277925684201714645138026540159746689295598\
+49229569543693814809740194976378989712946860327540588853786160311862\
+26553843014447253417608142170534232089237645726271317586792866436463\
+57906771436600573193316692947877521038683967731837711063242972271670\
+96055668939700794111101379234021535739329350922700763647464255738367\
+45664949133751858862157294162807244687731159626486653201155582499714\
+79484408164370429277582140594961024021235193104960984535030512698140\
+29991642773255454178297132207171199052829597210364476553497942817268\
+62690611849309055824536998344110574782877812780572822768074693914603\
+93441675889749948913356381877977663664670923405154024034819183077630\
+07222467129739709730595197712146877807485841798099359485655082017504\
+57266468940919731337521917893149437593088571899978481009627569104752\
+83788737229048754086398051323802242295499011906844043041969326497378\
+66830847485298392258392207609286448536144199906732162033971327749980\
+72409681682525277718455729872598195785847026351837425524957713502283\
+35808405846301401511404101208274836584641837898337018415760457721775\
+34793070383077489542736177701902455308320830139111584515391609551696\
+93786802204354185248901167656622471020051493389173955741467861607741\
+06154885234719288084392388173679574839263106373769157250023377763195\
+33722849246313238409188148501441642227744274888764386850047852926601\
+66259132899777197674037156711065912041415912906771682871632890618266\
+52534846581370718069768101784480970852297316593250497344665499975249\
+28847715738269148419095709788743634068235772638161673973508170592990\
+08150460394328942539838852965761260991598586792881336905193093363807\
+68065001971035985210175312847093321437455851062753166316674526831405\
+22643924906227066856960275731105243745519239095725263849049284368192\
+69981817924601931034908526477849233791352299993888953628294142682398\
+32015215644146196271258557701759614063395810649987412865008620060711\
+96106494559320848767208198924865288907648098617871697423778043460357\
+69278505913120523758525932088676939064487208006723185721403213528197\
+47356185573586878001575522729281782905160737709112298608240868302736\
+14912060528477110966342331323542913308637670012031732420815525850428\
+29985502836060351264856351721233689681302128443493923953325682146378\
+90500497094869973228157090077323643982214655247057954479537461140251\
+22391223645059066176683097828439526347990270348890072988824089134075\
+53045635915091127616051632821157251240715937718601438621807151809470\
+70020273648673726614138773619623873071209988854639064449056946951244\
+08154683439806851412045718493101194516264182543687642102731940333675\
+02204434515648501183682202170295476412677952210259771498961159478693\
+39533360510234199050863187055497001898319590310626558410825947546499\
+25866422095282655331612522936168323192019066427954358596287913432824\
+23986416514347998122918766123490242000799600412705715403792955802428\
+68680411940581906418239192566545624215426795832902852897359879579206\
+29806298084768133342364905840962242909627360003836496432663468404056\
+75003716322755561456947876758325012806130640785145137044014345149729\
+64472421537485940360688927943947253997322254484456390238538599823516\
+53783313664554244804188239596232973290117637825103346417364845445537\
+63874844173295342500665949524580816903010072450214234574076441210052\
+76312238941224960108202455846209231258733492737852892758457199956062\
+64745301484120814116015376716640043059840936868045425494746747743167\
+47635509486725886548517712648186478000538173675010335580427072223374\
+22683297174260468174666990490676363371877116991078914450165283451094\
+38053584777926914783233278650205938696956998820904630096199992054805\
+15352923265061131541221436151100238443368250876934012537521701653429\
+83749419904181832047886489040835511827487858712692455203809244256381\
+95363676537203584858079014204774516763297210884678721481745464679151\
+34153801765303831091048471166149310881939079992670658056687054966453\
+64078260772450818012184785716598737407583554314901714361312982863209\
+08568827536105214917228565402326151858487667522128825392025654986079\
+94364340694436809828167006177389014092285574489110459783543308691830\
+81359774673499877856622602227027179803067114877976882524477854727554\
+43145812466491583566468289389559643610433010688516435918531866978568\
+98694778365921976545970326296067215777099795140100793885931248501330\
+88092521136829832995633382275605096697602165804297877578997817111968\
+18131992614391087249610046250364652601740222742376049435751285932886\
+51855281108498217714451092451205316423498083202395699293550040438603\
+96607653833910364834434269652078271541907585050569750193777851038475\
+94658423552329776425242871169305484855793189639257265149554708344704\
+30807466779976397658781768927423667079487402547120198173689660436452\
+23216183977421942478485697014756283.15080425429190208981
+-1945134149489344891879057554905782841936258356736314337975569799825\
+94091939572752348215929683891336730843553721422164737465108229034947\
+87333189564755763444242676978610321731298729194092653999616928308494\
+26419468484566422775668903315088810746121307679948574976162519479931\
+18935243698160094347216562490000767121041786977792546155155934655909\
+14123833869470494708767968978717730012864171105540029928688274136791\
+98175053824022144065005509214813689232148489884560100200475909009813\
+340098100705258138.98542904577525702068
+0
+0
diff --git a/tests/files/bc/print.bc b/tests/files/bc/print.bc
new file mode 100755
index 00000000..e8423c21
--- /dev/null
+++ b/tests/files/bc/print.bc
@@ -0,0 +1,21 @@
+#! /usr/bin/bc -q
+
+for (b = 2; b <= 16; ++b) {
+
+ if (b == 10) continue
+
+ s = b * b
+
+ print "obase = ", b, "\n"
+
+ for (i = 0; i <= s; ++i) {
+ i
+ print "0.", i, "\n"
+ print "1.", i, "\n"
+ print i, ".", i, "\n"
+ }
+
+ 2189432174861923048671023498128347619023487610234689172304.192748960128745108927461089237469018723460
+}
+
+halt
diff --git a/tests/files/bc/screen.bc b/tests/files/bc/screen.bc
new file mode 100644
index 00000000..916b4f6a
--- /dev/null
+++ b/tests/files/bc/screen.bc
@@ -0,0 +1,19 @@
+define a(i, j) {
+ scale = 0
+ if(i % 2 == 0) return i;
+ if(j - i >= 0.5) return i + 1;
+ return i - 1;
+}
+
+define x(w, h, n) {
+ scale = 20
+ f = w / n
+ scale = 0
+ i = h / f
+ scale = 1
+ j = h / f
+ return a(i, j);
+}
+
+x(720, 576, 600)
+
diff --git a/tests/files/bc/script.sh b/tests/files/bc/script.sh
new file mode 100644
index 00000000..d279b6a2
--- /dev/null
+++ b/tests/files/bc/script.sh
@@ -0,0 +1,24 @@
+#! /bin/sh
+
+if [ "$#" -lt 4 ]; then
+ echo "usage: script.sh <bc> <test_output1> <test_output2> <script>"
+ exit 1
+fi
+
+set -e
+
+bc="$1"
+shift
+
+out1="$1"
+shift
+
+out2="$1"
+shift
+
+script="$1"
+
+echo "quit" | bc -lq "$script" > "$out1"
+echo "quit" | "$bc" -lq "$script" > "$out2"
+
+diff "$out1" "$out2"
diff --git a/tests/files/bc/sqrt.txt b/tests/files/bc/sqrt.txt
new file mode 100644
index 00000000..8be84ca6
--- /dev/null
+++ b/tests/files/bc/sqrt.txt
@@ -0,0 +1,12 @@
+scale = 20
+sqrt(0)
+sqrt(2)
+sqrt(4)
+sqrt(9)
+sqrt(16)
+sqrt(25)
+sqrt(121)
+sqrt(48765)
+sqrt(9287356207356)
+sqrt(0.189274385967238956872354)
+sqrt(12389467137496823.134567829387456283946)
diff --git a/tests/files/bc/sqrt_results.txt b/tests/files/bc/sqrt_results.txt
new file mode 100644
index 00000000..8005a310
--- /dev/null
+++ b/tests/files/bc/sqrt_results.txt
@@ -0,0 +1,11 @@
+0
+1.41421356237309504880
+2.00000000000000000000
+3.00000000000000000000
+4.00000000000000000000
+5.00000000000000000000
+11.00000000000000000000
+220.82798735667542192643
+3047516.39985021245496456781
+.435056761776252544285578
+111307983.260397019622398608908
diff --git a/tests/files/bc/subtract.txt b/tests/files/bc/subtract.txt
new file mode 100644
index 00000000..4fdebea8
--- /dev/null
+++ b/tests/files/bc/subtract.txt
@@ -0,0 +1,30 @@
+0 - 0
+0 - 1
+1 - 0
+1 - 1
+5 - 2
+2 - 9
+321974 - 12845976238457
+2874519803456710938465- 384723854
+10000000000000000000000000000000000000000 - 999999999999999999999999999999999999999
+10000000000000000000000000000000000000000 - 9999999999999999999999999999999999999999
+10000000000000000000000000000000000000000 - 999999999999999999999999999999999999999.99999999999999999999999999999999999
+10000000000000000000000000000000000000000 - 9999999999999999999999999999999999999999.9999999999999999999999999999999999
+10000000000000000000000000000000000000000 - 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000001
+10000000000000000000000000000000000000001 - 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000001
+10000000000000000000000000000000000000000.0000000001 - 0.0000000000000000000000000000000000000000000000000000000000000000000000000001
+-2 - 6
+-23784692345 - 182934721309467230894628735496027345
+-224352354962873059862 - -1245723576829456278354960278345
+-3468273598 - -12354243
+-0.92345768293 - -2354768923
+-712384634.123476823 - -24768293376
+-1879234638 - -0.917234869234
+-0.9172438692134 - -0.971284967124
+-0.1283475123465 - -0.937462346
+-124765829346.2837468293562 - -0.923467829346
+-12476829385769 - -1928476259034.8378629356
+-0.38476284395876345 - -94875394587623.2357869324857
+-4674596708467.34754789403674343567 - -48672394852354698.237548629345
+979519669 - 3018100865
+929002449 - 3280677283
diff --git a/tests/files/bc/subtract_results.txt b/tests/files/bc/subtract_results.txt
new file mode 100644
index 00000000..71488d50
--- /dev/null
+++ b/tests/files/bc/subtract_results.txt
@@ -0,0 +1,34 @@
+0
+-1
+1
+0
+3
+-7
+-12845975916483
+2874519803456326214611
+9000000000000000000000000000000000000001
+1
+9000000000000000000000000000000000000000.000000000000000000000000000\
+00000001
+.0000000000000000000000000000000001
+9999999999999999999999999999999999999999.999999999999999999999999999\
+99999999999999999999999999999999999999999999999999999999999
+10000000000000000000000000000000000000000.99999999999999999999999999\
+999999999999999999999999999999999999999999999999999999999999
+10000000000000000000000000000000000000000.00000000009999999999999999\
+99999999999999999999999999999999999999999999999999
+-8
+-182934721309467230894628759280719690
+1245723576605103923392087218483
+-3455919355
+2354768922.07654231707
+24055908741.876523177
+-1879234637.082765130766
+.0540410979106
+.8091148336535
+-124765829345.3602790000102
+-10548353126734.1621370644
+94875394587622.85102408852693655
+48667720255646230.89000073530825656433
+-2038581196
+-2351674834
diff --git a/toys/pending/bc.c b/toys/pending/bc.c
new file mode 100644
index 00000000..0130d80a
--- /dev/null
+++ b/toys/pending/bc.c
@@ -0,0 +1,5892 @@
+/* bc.c - An implementation of POSIX bc.
+ *
+ * Copyright 2018 Gavin D. Howard <yzena.tech@gmail.com>
+ *
+ * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/bc.html
+
+USE_BC(NEWTOY(bc, "i(interactive)l(mathlib)q(quiet)s(standard)w(warn)", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE))
+
+config BC
+ bool "bc"
+ default n
+ help
+ usage: bc [-ilqsw] [file ...]
+
+ bc is a command-line calculator with a Turing-complete language.
+
+ options:
+
+ -i --interactive force interactive mode
+ -l --mathlib use predefined math routines:
+
+ s(expr) = sine of expr in radians
+ c(expr) = cosine of expr in radians
+ a(expr) = arctangent of expr, returning radians
+ l(expr) = natural log of expr
+ e(expr) = raises e to the power of expr
+ j(n, x) = Bessel function of integer order n of x
+
+ -q --quiet don't print version and copyright
+ -s --standard error if any non-POSIX extensions are used
+ -w --warn warn if any non-POSIX extensions are used
+
+*/
+
+#define FOR_bc
+#include "toys.h"
+
+GLOBALS(
+ long tty;
+
+ unsigned long sig;
+ unsigned long sigc;
+ unsigned long signe;
+ long sig_other;
+)
+
+typedef enum BcStatus {
+
+ BC_STATUS_SUCCESS,
+
+ BC_STATUS_MALLOC_FAIL,
+ BC_STATUS_IO_ERR,
+ BC_STATUS_BINARY_FILE,
+
+ BC_STATUS_LEX_BAD_CHARACTER,
+ BC_STATUS_LEX_NO_STRING_END,
+ BC_STATUS_LEX_NO_COMMENT_END,
+ BC_STATUS_LEX_EOF,
+
+ BC_STATUS_PARSE_BAD_TOKEN,
+ BC_STATUS_PARSE_BAD_EXPR,
+ BC_STATUS_PARSE_BAD_PRINT,
+ BC_STATUS_PARSE_BAD_FUNC,
+ BC_STATUS_PARSE_BAD_ASSIGN,
+ BC_STATUS_PARSE_NO_AUTO,
+ BC_STATUS_PARSE_DUPLICATE_LOCAL,
+
+ BC_STATUS_MATH_NEGATIVE,
+ BC_STATUS_MATH_NON_INTEGER,
+ BC_STATUS_MATH_OVERFLOW,
+ BC_STATUS_MATH_DIVIDE_BY_ZERO,
+ BC_STATUS_MATH_NEG_SQRT,
+ BC_STATUS_MATH_BAD_STRING,
+
+ BC_STATUS_EXEC_FILE_ERR,
+ BC_STATUS_EXEC_MISMATCHED_PARAMS,
+ BC_STATUS_EXEC_UNDEFINED_FUNC,
+ BC_STATUS_EXEC_FILE_NOT_EXECUTABLE,
+ BC_STATUS_EXEC_SIGACTION_FAIL,
+ BC_STATUS_EXEC_BAD_SCALE,
+ BC_STATUS_EXEC_BAD_IBASE,
+ BC_STATUS_EXEC_BAD_OBASE,
+ BC_STATUS_EXEC_STRING_LEN,
+ BC_STATUS_EXEC_ARRAY_LEN,
+ BC_STATUS_EXEC_BAD_READ_EXPR,
+ BC_STATUS_EXEC_NESTED_READ,
+ BC_STATUS_EXEC_BAD_TYPE,
+ BC_STATUS_EXEC_SIGNAL,
+
+ BC_STATUS_POSIX_NAME_LEN,
+ BC_STATUS_POSIX_SCRIPT_COMMENT,
+ BC_STATUS_POSIX_BAD_KEYWORD,
+ BC_STATUS_POSIX_DOT_LAST,
+ BC_STATUS_POSIX_RETURN_PARENS,
+ BC_STATUS_POSIX_BOOL_OPS,
+ BC_STATUS_POSIX_REL_OUTSIDE,
+ BC_STATUS_POSIX_MULTIPLE_REL,
+ BC_STATUS_POSIX_NO_FOR_INIT,
+ BC_STATUS_POSIX_NO_FOR_COND,
+ BC_STATUS_POSIX_NO_FOR_UPDATE,
+ BC_STATUS_POSIX_HEADER_BRACE,
+
+ BC_STATUS_VEC_OUT_OF_BOUNDS,
+ BC_STATUS_VEC_ITEM_EXISTS,
+
+ BC_STATUS_QUIT,
+ BC_STATUS_LIMITS,
+
+} BcStatus;
+
+#define BC_ERR_IDX_BC (0)
+#define BC_ERR_IDX_LEX (1)
+#define BC_ERR_IDX_PARSE (2)
+#define BC_ERR_IDX_MATH (3)
+#define BC_ERR_IDX_EXEC (4)
+#define BC_ERR_IDX_POSIX (5)
+
+#define BC_VEC_INITIAL_CAP (32)
+
+typedef void (*BcVecFreeFunc)(void*);
+typedef int (*BcVecCmpFunc)(void*, void*);
+
+typedef struct BcVec {
+
+ uint8_t *array;
+ size_t len;
+ size_t cap;
+ size_t size;
+
+ BcVecFreeFunc dtor;
+
+} BcVec;
+
+typedef struct BcVecO {
+
+ BcVec vec;
+ BcVecCmpFunc cmp;
+
+} BcVecO;
+
+#define bc_veco_item(v, idx) (bc_vec_item(&(v)->vec, (idx)))
+#define bc_veco_free(v) (bc_vec_free(&(v)->vec))
+
+typedef signed char BcDigit;
+
+typedef struct BcNum {
+
+ BcDigit *num;
+ size_t rdx;
+ size_t len;
+ size_t cap;
+ int neg;
+
+} BcNum;
+
+#define BC_NUM_MIN_BASE (2)
+#define BC_NUM_MAX_INPUT_BASE (16)
+#define BC_NUM_DEF_SIZE (16)
+
+#define BC_NUM_PRINT_WIDTH (69)
+
+#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
+
+typedef BcStatus (*BcNumUnaryFunc)(BcNum*, BcNum*, size_t);
+typedef BcStatus (*BcNumBinaryFunc)(BcNum*, BcNum*, BcNum*, size_t);
+typedef BcStatus (*BcNumDigitFunc)(size_t, size_t, int, size_t*, size_t);
+
+BcStatus bc_num_init(BcNum *n, size_t request);
+BcStatus bc_num_expand(BcNum *n, size_t request);
+BcStatus bc_num_copy(BcNum *d, BcNum *s);
+void bc_num_free(void *num);
+
+BcStatus bc_num_ulong(BcNum *n, unsigned long *result);
+BcStatus bc_num_ulong2num(BcNum *n, unsigned long val);
+
+BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *result, size_t scale);
+BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *result, size_t scale);
+BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *result, size_t scale);
+BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *result, size_t scale);
+BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *result, size_t scale);
+BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *result, size_t scale);
+
+ssize_t bc_num_cmp(BcNum *a, BcNum *b);
+
+void bc_num_zero(BcNum *n);
+void bc_num_one(BcNum *n);
+void bc_num_ten(BcNum *n);
+
+typedef enum BcInst {
+
+ BC_INST_INC_PRE,
+ BC_INST_DEC_PRE,
+ BC_INST_INC_POST,
+ BC_INST_DEC_POST,
+
+ BC_INST_NEG,
+
+ BC_INST_POWER,
+ BC_INST_MULTIPLY,
+ BC_INST_DIVIDE,
+ BC_INST_MODULUS,
+ BC_INST_PLUS,
+ BC_INST_MINUS,
+
+ BC_INST_REL_EQ,
+ BC_INST_REL_LE,
+ BC_INST_REL_GE,
+ BC_INST_REL_NE,
+ BC_INST_REL_LT,
+ BC_INST_REL_GT,
+
+ BC_INST_BOOL_NOT,
+ BC_INST_BOOL_OR,
+ BC_INST_BOOL_AND,
+
+ BC_INST_ASSIGN_POWER,
+ BC_INST_ASSIGN_MULTIPLY,
+ BC_INST_ASSIGN_DIVIDE,
+ BC_INST_ASSIGN_MODULUS,
+ BC_INST_ASSIGN_PLUS,
+ BC_INST_ASSIGN_MINUS,
+ BC_INST_ASSIGN,
+
+ BC_INST_PUSH_NUM,
+ BC_INST_PUSH_VAR,
+ BC_INST_PUSH_ARRAY_ELEM,
+
+ BC_INST_CALL,
+
+ BC_INST_SCALE_FUNC,
+ BC_INST_PUSH_IBASE,
+ BC_INST_PUSH_SCALE,
+ BC_INST_PUSH_LAST,
+ BC_INST_LENGTH,
+ BC_INST_READ,
+ BC_INST_PUSH_OBASE,
+ BC_INST_SQRT,
+
+ BC_INST_PRINT,
+ BC_INST_PRINT_EXPR,
+ BC_INST_STR,
+ BC_INST_PRINT_STR,
+
+ BC_INST_JUMP,
+ BC_INST_JUMP_ZERO,
+
+ BC_INST_POP,
+
+ BC_INST_RETURN,
+ BC_INST_RETURN_ZERO,
+
+ BC_INST_HALT,
+
+} BcInst;
+
+typedef struct BcEntry {
+
+ char *name;
+ size_t idx;
+
+} BcEntry;
+
+typedef struct BcAuto {
+
+ char *name;
+ int var;
+
+} BcAuto;
+
+typedef struct BcFunc {
+
+ BcVec code;
+ BcVec labels;
+ size_t nparams;
+ BcVec autos;
+
+} BcFunc;
+
+typedef enum BcResultType {
+
+ BC_RESULT_TEMP,
+
+ BC_RESULT_CONSTANT,
+
+ BC_RESULT_ARRAY_AUTO,
+ BC_RESULT_VAR_AUTO,
+
+ BC_RESULT_VAR,
+ BC_RESULT_ARRAY,
+
+ BC_RESULT_SCALE,
+ BC_RESULT_LAST,
+ BC_RESULT_IBASE,
+ BC_RESULT_OBASE,
+
+ BC_RESULT_ONE,
+
+} BcResultType;
+
+typedef struct BcResult {
+
+ BcResultType type;
+
+ union {
+
+ BcNum num;
+ BcVec array;
+ BcEntry id;
+
+ } data;
+
+} BcResult;
+
+typedef struct BcInstPtr {
+
+ size_t func;
+ size_t idx;
+ size_t len;
+
+} BcInstPtr;
+
+void bc_auto_free(void *auto1);
+
+// BC_LEX_OP_NEGATE is not used in lexing; it is only for parsing.
+typedef enum BcLexToken {
+
+ BC_LEX_OP_INC,
+ BC_LEX_OP_DEC,
+
+ BC_LEX_OP_NEG,
+
+ BC_LEX_OP_POWER,
+ BC_LEX_OP_MULTIPLY,
+ BC_LEX_OP_DIVIDE,
+ BC_LEX_OP_MODULUS,
+ BC_LEX_OP_PLUS,
+ BC_LEX_OP_MINUS,
+
+ BC_LEX_OP_REL_EQUAL,
+ BC_LEX_OP_REL_LESS_EQ,
+ BC_LEX_OP_REL_GREATER_EQ,
+ BC_LEX_OP_REL_NOT_EQ,
+ BC_LEX_OP_REL_LESS,
+ BC_LEX_OP_REL_GREATER,
+
+ BC_LEX_OP_BOOL_NOT,
+ BC_LEX_OP_BOOL_OR,
+ BC_LEX_OP_BOOL_AND,
+
+ BC_LEX_OP_ASSIGN_POWER,
+ BC_LEX_OP_ASSIGN_MULTIPLY,
+ BC_LEX_OP_ASSIGN_DIVIDE,
+ BC_LEX_OP_ASSIGN_MODULUS,
+ BC_LEX_OP_ASSIGN_PLUS,
+ BC_LEX_OP_ASSIGN_MINUS,
+ BC_LEX_OP_ASSIGN,
+
+ BC_LEX_NEWLINE,
+
+ BC_LEX_WHITESPACE,
+
+ BC_LEX_LEFT_PAREN,
+ BC_LEX_RIGHT_PAREN,
+
+ BC_LEX_LEFT_BRACKET,
+ BC_LEX_COMMA,
+ BC_LEX_RIGHT_BRACKET,
+
+ BC_LEX_LEFT_BRACE,
+ BC_LEX_SEMICOLON,
+ BC_LEX_RIGHT_BRACE,
+
+ BC_LEX_STRING,
+ BC_LEX_NAME,
+ BC_LEX_NUMBER,
+
+ BC_LEX_KEY_AUTO,
+ BC_LEX_KEY_BREAK,
+ BC_LEX_KEY_CONTINUE,
+ BC_LEX_KEY_DEFINE,
+ BC_LEX_KEY_ELSE,
+ BC_LEX_KEY_FOR,
+ BC_LEX_KEY_HALT,
+ BC_LEX_KEY_IBASE,
+ BC_LEX_KEY_IF,
+ BC_LEX_KEY_LAST,
+ BC_LEX_KEY_LENGTH,
+ BC_LEX_KEY_LIMITS,
+ BC_LEX_KEY_OBASE,
+ BC_LEX_KEY_PRINT,
+ BC_LEX_KEY_QUIT,
+ BC_LEX_KEY_READ,
+ BC_LEX_KEY_RETURN,
+ BC_LEX_KEY_SCALE,
+ BC_LEX_KEY_SQRT,
+ BC_LEX_KEY_WHILE,
+
+ BC_LEX_EOF,
+ BC_LEX_INVALID,
+
+} BcLexToken;
+
+typedef struct BcLex {
+
+ const char *buffer;
+ size_t idx;
+ size_t line;
+ int newline;
+ const char *file;
+ size_t len;
+
+ struct {
+ BcLexToken type;
+ char *string;
+ } token;
+
+} BcLex;
+
+typedef struct BcLexKeyword {
+
+ const char name[9];
+ const char len;
+ const int posix;
+
+} BcLexKeyword;
+
+#define BC_LEX_KW_ENTRY(a, b, c) { .name = a, .len = (b), .posix = (c) }
+
+#define BC_PROGRAM_BUF_SIZE (1024)
+
+typedef struct BcProgram {
+
+ BcVec ip_stack;
+
+ size_t line_len;
+
+ size_t scale;
+
+ BcNum ibase;
+ size_t ibase_t;
+ BcNum obase;
+ size_t obase_t;
+
+ BcVec results;
+ BcVec stack;
+
+ BcVec funcs;
+ BcVecO func_map;
+
+ BcVec vars;
+ BcVecO var_map;
+
+ BcVec arrays;
+ BcVecO array_map;
+
+ BcVec strings;
+ BcVec constants;
+
+ const char *file;
+
+ BcNum last;
+
+ BcNum zero;
+ BcNum one;
+
+ size_t nchars;
+
+} BcProgram;
+
+#define BC_PROGRAM_MAIN (0)
+#define BC_PROGRAM_READ (1)
+
+#define BC_PROGRAM_SEARCH_VAR (1<<0)
+#define BC_PROGRAM_SEARCH_ARRAY (1<<1)
+
+typedef unsigned long (*BcProgramBuiltInFunc)(BcNum*);
+typedef void (*BcNumInitFunc)(BcNum*);
+
+BcStatus bc_program_addFunc(BcProgram *p, char *name, size_t *idx);
+BcStatus bc_program_reset(BcProgram *p, BcStatus status);
+
+BcStatus bc_program_exec(BcProgram *p);
+
+#define BC_PARSE_TOP_FLAG_PTR(parse) ((uint8_t*) bc_vec_top(&(parse)->flags))
+
+#define BC_PARSE_TOP_FLAG(parse) (*(BC_PARSE_TOP_FLAG_PTR(parse)))
+
+#define BC_PARSE_FLAG_FUNC_INNER (0x01)
+
+#define BC_PARSE_FUNC_INNER(parse) \
+ (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC_INNER)
+
+#define BC_PARSE_FLAG_FUNC (0x02)
+
+#define BC_PARSE_FUNC(parse) \
+ (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_FUNC)
+
+#define BC_PARSE_FLAG_BODY (0x04)
+
+#define BC_PARSE_BODY(parse) \
+ (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_BODY)
+
+#define BC_PARSE_FLAG_LOOP (0x08)
+
+#define BC_PARSE_LOOP(parse) \
+ (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP)
+
+#define BC_PARSE_FLAG_LOOP_INNER (0x10)
+
+#define BC_PARSE_LOOP_INNER(parse) \
+ (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_LOOP_INNER)
+
+#define BC_PARSE_FLAG_IF (0x20)
+
+#define BC_PARSE_IF(parse) \
+ (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF)
+
+#define BC_PARSE_FLAG_ELSE (0x40)
+
+#define BC_PARSE_ELSE(parse) \
+ (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_ELSE)
+
+#define BC_PARSE_FLAG_IF_END (0x80)
+
+#define BC_PARSE_IF_END(parse) \
+ (BC_PARSE_TOP_FLAG(parse) & BC_PARSE_FLAG_IF_END)
+
+#define BC_PARSE_CAN_EXEC(parse) \
+ (!(BC_PARSE_TOP_FLAG(parse) & (BC_PARSE_FLAG_FUNC_INNER | \
+ BC_PARSE_FLAG_FUNC | \
+ BC_PARSE_FLAG_BODY | \
+ BC_PARSE_FLAG_LOOP | \
+ BC_PARSE_FLAG_LOOP_INNER | \
+ BC_PARSE_FLAG_IF | \
+ BC_PARSE_FLAG_ELSE | \
+ BC_PARSE_FLAG_IF_END)))
+
+// We can calculate the conversion between tokens and exprs
+// by subtracting the position of the first operator in the
+// lex enum and adding the position of the first in the expr
+// enum. Note: This only works for binary operators.
+#define BC_PARSE_TOKEN_TO_INST(type) ((type) - BC_LEX_OP_NEG + BC_INST_NEG)
+
+typedef struct BcOp {
+
+ uint8_t prec;
+ int left;
+
+} BcOp;
+
+typedef struct BcParse {
+
+ BcLex lex;
+
+ BcVec flags;
+
+ BcVec exits;
+ BcVec conds;
+
+ BcVec ops;
+
+ BcProgram *prog;
+ size_t func;
+
+ size_t num_braces;
+
+ int auto_part;
+
+} BcParse;
+
+#define BC_PARSE_EXPR_POSIX_REL (1<<0)
+#define BC_PARSE_EXPR_PRINT (1<<1)
+#define BC_PARSE_EXPR_NOCALL (1<<2)
+#define BC_PARSE_EXPR_NOREAD (1<<3)
+
+BcStatus bc_parse_expr(BcParse *p, BcVec *code, uint8_t flags);
+
+#define maxof_BASE (999)
+#define maxof_DIM (INT_MAX)
+#define maxof_SCALE (LONG_MAX)
+#define maxof_STRING (INT_MAX)
+
+#define BC_BUF_SIZE (1024)
+
+typedef struct Bc {
+
+ BcParse parse;
+ BcProgram prog;
+
+} Bc;
+
+BcStatus bc_error(BcStatus st);
+BcStatus bc_error_file(BcStatus st, const char *file, size_t line);
+
+BcStatus bc_posix_error(BcStatus s, const char *file,
+ size_t line, const char *msg);
+
+BcStatus bc_io_fread(const char *path, char **buf);
+
+const char *bc_header =
+ "bc 1.0\n"
+ "bc copyright (c) 2018 Gavin D. Howard and contributors\n"
+ "Report bugs at: https://github.com/gavinhoward/bc\n\n"
+ "This is free software with ABSOLUTELY NO WARRANTY.\n\n";
+
+const char bc_err_fmt[] = "\n%s error: %s\n\n";
+
+const char *bc_errs[] = {
+ "bc",
+ "Lex",
+ "Parse",
+ "Math",
+ "Runtime",
+ "POSIX",
+};
+
+const uint8_t bc_err_indices[] = {
+ BC_ERR_IDX_BC, BC_ERR_IDX_BC, BC_ERR_IDX_BC, BC_ERR_IDX_BC,
+ BC_ERR_IDX_LEX, BC_ERR_IDX_LEX, BC_ERR_IDX_LEX, BC_ERR_IDX_LEX,
+ BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE,
+ BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE, BC_ERR_IDX_PARSE,
+ BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, BC_ERR_IDX_MATH, BC_ERR_IDX_MATH,
+ BC_ERR_IDX_MATH, BC_ERR_IDX_MATH,
+ BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC,
+ BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC,
+ BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC, BC_ERR_IDX_EXEC,
+ BC_ERR_IDX_EXEC,
+ BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX,
+ BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX,
+ BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX, BC_ERR_IDX_POSIX,
+};
+
+const char *bc_err_descs[] = {
+ NULL,
+ "memory allocation error",
+ "I/O error",
+ "file is not text",
+
+ "bad character",
+ "string end could not be found",
+ "comment end could not be found",
+ "end of file",
+
+ "bad token",
+ "bad expression",
+ "bad print statement",
+ "bad function definition",
+ "bad assignment: left must be scale, ibase, "
+ "obase, last, var, or array element",
+ "no auto variable found",
+ "function parameter or auto var has the same name as another",
+
+ "negative number",
+ "non integer number",
+ "overflow",
+ "divide by zero",
+ "negative square root",
+ "bad number string",
+
+ "could not open file",
+ "mismatched parameters",
+ "undefined function",
+ "file is not executable",
+ "could not install signal handler",
+ "bad scale; must be [0, BC_SCALE_MAX]",
+ "bad ibase; must be [2, 16]",
+ "bad obase; must be [2, BC_BASE_MAX]",
+ "string too long: must be [1, BC_STRING_MAX]",
+ "array too long; must be [1, BC_DIM_MAX]",
+ "bad read() expression",
+ "read() call inside of a read() call",
+ "variable is wrong type",
+
+ "POSIX only allows one character names; the following is bad:",
+ "POSIX does not allow '#' script comments",
+ "POSIX does not allow the following keyword:",
+ "POSIX does not allow a period ('.') as a shortcut for the last result",
+ "POSIX requires parentheses around return expressions",
+ "POSIX does not allow intean operators; the following is bad:",
+ "POSIX does not allow comparison operators outside if or loops",
+ "POSIX requires exactly one comparison operator per condition",
+ "POSIX does not allow an empty init expression in a for loop",
+ "POSIX does not allow an empty condition expression in a for loop",
+ "POSIX does not allow an empty update expression in a for loop",
+ "POSIX requires the left brace be on the same line as the function header",
+};
+
+const char bc_sig_msg[34] = "\ninterrupt (type \"quit\" to exit)\n";
+
+const char bc_lang_func_main[7] = "(main)";
+const char bc_lang_func_read[7] = "(read)";
+
+const BcLexKeyword bc_lex_keywords[20] = {
+ BC_LEX_KW_ENTRY("auto", 4, 1),
+ BC_LEX_KW_ENTRY("break", 5, 1),
+ BC_LEX_KW_ENTRY("continue", 8, 0),
+ BC_LEX_KW_ENTRY("define", 6, 1),
+ BC_LEX_KW_ENTRY("else", 4, 0),
+ BC_LEX_KW_ENTRY("for", 3, 1),
+ BC_LEX_KW_ENTRY("halt", 4, 0),
+ BC_LEX_KW_ENTRY("ibase", 5, 1),
+ BC_LEX_KW_ENTRY("if", 2, 1),
+ BC_LEX_KW_ENTRY("last", 4, 0),
+ BC_LEX_KW_ENTRY("length", 6, 1),
+ BC_LEX_KW_ENTRY("limits", 6, 0),
+ BC_LEX_KW_ENTRY("obase", 5, 1),
+ BC_LEX_KW_ENTRY("print", 5, 0),
+ BC_LEX_KW_ENTRY("quit", 4, 1),
+ BC_LEX_KW_ENTRY("read", 4, 0),
+ BC_LEX_KW_ENTRY("return", 6, 1),
+ BC_LEX_KW_ENTRY("scale", 5, 1),
+ BC_LEX_KW_ENTRY("sqrt", 4, 1),
+ BC_LEX_KW_ENTRY("while", 5, 1),
+};
+
+const char bc_num_hex_digits[] = "0123456789ABCDEF";
+
+// This is an array that corresponds to token types. An entry is
+// 1 if the token is valid in an expression, 0 otherwise.
+const int bc_parse_token_exprs[] = {
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
+ 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1,
+ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1,
+ 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0,
+};
+
+// This is an array of data for operators that correspond to token types.
+const BcOp bc_parse_ops[] = {
+ { 0, 0 }, { 0, 0 },
+ { 1, 0 },
+ { 2, 0 },
+ { 3, 1 }, { 3, 1 }, { 3, 1 },
+ { 4, 1 }, { 4, 1 },
+ { 6, 1 }, { 6, 1 }, { 6, 1 }, { 6, 1 }, { 6, 1 }, { 6, 1 },
+ { 7, 0 },
+ { 8, 1 }, { 8, 1 },
+ { 5, 0 }, { 5, 0 }, { 5, 0 }, { 5, 0 }, { 5, 0 }, { 5, 0 }, { 5, 0 },
+};
+
+const char bc_program_byte_fmt[] = "%02x";
+
+const BcNumBinaryFunc bc_program_math_ops[] = {
+ bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub,
+};
+
+const char bc_program_stdin_name[] = "<stdin>";
+const char bc_program_ready_prompt[] = "ready for more input\n";
+const char *bc_lib_name = "lib.bc";
+
+const char bc_lib[] = {
+ 115,99,97,108,101,61,50,48,10,100,101,102,105,110,101,32,101,40,120,41,123,
+ 10,9,97,117,116,111,32,98,44,115,44,110,44,114,44,100,44,105,44,112,44,102,
+ 44,118,10,9,98,61,105,98,97,115,101,10,9,105,98,97,115,101,61,65,10,9,105,102,
+ 40,120,60,48,41,123,10,9,9,110,61,49,10,9,9,120,61,45,120,10,9,125,10,9,115,
+ 61,115,99,97,108,101,10,9,114,61,115,43,55,43,48,46,52,53,42,120,10,9,115,99,
+ 97,108,101,61,115,99,97,108,101,40,120,41,43,49,10,9,119,104,105,108,101,40,
+ 120,62,49,41,123,10,9,9,100,43,61,49,10,9,9,120,47,61,50,10,9,9,115,99,97,108,
+ 101,43,61,49,10,9,125,10,9,115,99,97,108,101,61,114,10,9,114,61,120,43,49,10,
+ 9,112,61,120,10,9,102,61,118,61,49,10,9,102,111,114,40,105,61,50,59,118,33,
+ 61,48,59,43,43,105,41,123,10,9,9,112,42,61,120,59,10,9,9,102,42,61,105,10,9,
+ 9,118,61,112,47,102,10,9,9,114,43,61,118,10,9,125,10,9,119,104,105,108,101,
+ 40,40,102,45,45,41,33,61,48,41,114,42,61,114,10,9,115,99,97,108,101,61,115,
+ 10,9,105,98,97,115,101,61,98,10,9,105,102,40,109,33,61,48,41,114,101,116,117,
+ 114,110,40,49,47,114,41,10,9,114,101,116,117,114,110,40,114,47,49,41,10,125,
+ 10,100,101,102,105,110,101,32,108,40,120,41,123,10,9,97,117,116,111,32,98,44,
+ 115,44,114,44,112,44,97,44,113,44,105,44,118,10,9,98,61,105,98,97,115,101,10,
+ 9,105,98,97,115,101,61,65,10,9,105,102,40,120,60,61,48,41,123,10,9,9,114,61,
+ 40,49,45,49,48,94,115,99,97,108,101,41,47,49,10,9,9,105,98,97,115,101,61,98,
+ 10,9,9,114,101,116,117,114,110,40,114,41,10,9,125,10,9,115,61,115,99,97,108,
+ 101,10,9,115,99,97,108,101,43,61,55,10,9,112,61,50,10,9,119,104,105,108,101,
+ 40,120,62,61,50,41,123,10,9,9,112,42,61,50,10,9,9,120,61,115,113,114,116,40,
+ 120,41,10,9,125,10,9,119,104,105,108,101,40,120,60,61,48,46,53,41,123,10,9,
+ 9,112,42,61,50,10,9,9,120,61,115,113,114,116,40,120,41,10,9,125,10,9,114,61,
+ 97,61,40,120,45,49,41,47,40,120,43,49,41,10,9,113,61,97,42,97,10,9,118,61,49,
+ 10,9,102,111,114,40,105,61,51,59,118,33,61,48,59,105,43,61,50,41,123,10,9,9,
+ 110,42,61,109,10,9,9,118,61,110,47,105,10,9,9,114,43,61,118,10,9,125,10,9,114,
+ 42,61,112,10,9,115,99,97,108,101,61,115,10,9,105,98,97,115,101,61,98,10,9,114,
+ 101,116,117,114,110,40,114,47,49,41,10,125,10,100,101,102,105,110,101,32,115,
+ 40,120,41,123,10,9,97,117,116,111,32,98,44,115,44,114,44,110,44,97,44,113,44,
+ 105,10,9,98,61,105,98,97,115,101,10,9,105,98,97,115,101,61,65,10,9,115,61,115,
+ 99,97,108,101,10,9,115,99,97,108,101,61,49,46,51,42,115,43,50,10,9,97,61,97,
+ 40,49,41,10,9,105,102,40,120,60,48,41,123,10,9,9,110,61,49,10,9,9,120,61,45,
+ 120,10,9,125,10,9,115,99,97,108,101,61,48,10,9,113,61,40,120,47,97,43,50,41,
+ 47,52,10,9,120,45,61,52,42,113,42,97,10,9,105,102,40,113,37,50,33,61,48,41,
+ 120,61,45,120,10,9,115,99,97,108,101,61,115,43,50,10,9,114,61,97,61,120,10,
+ 9,113,61,45,120,42,120,10,9,102,111,114,40,105,61,51,59,97,33,61,48,59,105,
+ 43,61,50,41,123,10,9,9,97,42,61,113,47,40,105,42,40,105,45,49,41,41,10,9,9,
+ 114,43,61,97,10,9,125,10,9,115,99,97,108,101,61,115,10,9,105,98,97,115,101,
+ 61,98,10,9,105,102,40,110,33,61,48,41,114,101,116,117,114,110,40,45,114,47,
+ 49,41,10,9,114,101,116,117,114,110,40,114,47,49,41,10,125,10,100,101,102,105,
+ 110,101,32,99,40,120,41,123,10,9,97,117,116,111,32,98,44,115,10,9,98,61,105,
+ 98,97,115,101,10,9,105,98,97,115,101,61,65,10,9,115,61,115,99,97,108,101,10,
+ 9,115,99,97,108,101,43,61,49,10,9,120,61,115,40,50,42,97,40,49,41,43,120,41,
+ 10,9,115,99,97,108,101,61,115,10,9,105,98,97,115,101,61,98,10,9,114,101,116,
+ 117,114,110,40,120,47,49,41,10,125,10,100,101,102,105,110,101,32,97,40,120,
+ 41,123,10,9,97,117,116,111,32,98,44,115,44,114,44,110,44,97,44,109,44,116,44,
+ 102,44,105,44,117,10,9,98,61,105,98,97,115,101,10,9,105,98,97,115,101,61,65,
+ 10,9,110,61,49,10,9,105,102,40,120,60,48,41,123,10,9,9,110,61,45,49,10,9,9,
+ 120,61,45,120,10,9,125,10,9,105,102,40,120,61,61,49,41,123,10,9,9,105,102,40,
+ 115,99,97,108,101,60,61,54,52,41,123,10,9,9,9,114,101,116,117,114,110,40,46,
+ 55,56,53,51,57,56,49,54,51,51,57,55,52,52,56,51,48,57,54,49,53,54,54,48,56,
+ 52,53,56,49,57,56,55,53,55,50,49,48,52,57,50,57,50,51,52,57,56,52,51,55,55,
+ 54,52,53,53,50,52,51,55,51,54,49,52,56,48,47,110,41,10,9,9,125,10,9,125,10,
+ 9,105,102,40,120,61,61,46,50,54,55,41,123,10,9,9,105,102,40,115,99,97,108,101,
+ 60,61,54,52,41,123,10,9,9,9,114,101,116,117,114,110,40,46,50,54,48,57,49,51,
+ 53,54,57,50,51,50,57,52,48,53,55,57,53,57,54,55,56,53,50,54,55,55,55,55,57,
+ 56,54,53,54,51,57,55,55,52,55,52,48,50,51,57,56,56,50,52,52,53,56,50,50,51,
+ 50,57,56,56,50,57,49,55,47,110,41,10,9,9,125,10,9,125,10,9,115,61,115,99,97,
+ 108,101,10,9,105,102,40,120,62,46,50,54,55,41,123,10,9,9,115,99,97,108,101,
+ 43,61,53,10,9,9,97,61,97,40,46,50,54,55,41,10,9,125,10,9,115,99,97,108,101,
+ 61,115,43,51,10,9,119,104,105,108,101,40,120,62,46,50,54,55,41,123,10,9,9,109,
+ 43,61,49,10,9,9,120,61,40,120,45,46,50,54,55,41,47,40,49,43,46,50,54,55,42,
+ 120,41,10,9,125,10,9,114,61,117,61,120,10,9,102,61,45,120,42,120,10,9,116,61,
+ 49,10,9,102,111,114,40,105,61,51,59,116,33,61,48,59,105,43,61,50,41,123,10,
+ 9,9,117,42,61,102,10,9,9,116,61,117,47,105,10,9,9,114,43,61,116,10,9,125,10,
+ 9,115,99,97,108,101,61,115,10,9,105,98,97,115,101,61,98,10,9,114,101,116,117,
+ 114,110,40,40,109,42,97,43,114,41,47,110,41,10,125,10,100,101,102,105,110,101,
+ 32,106,40,110,44,120,41,123,10,9,97,117,116,111,32,98,44,115,44,111,44,97,44,
+ 105,44,118,44,102,10,9,98,61,105,98,97,115,101,10,9,105,98,97,115,101,61,65,
+ 10,9,115,61,115,99,97,108,101,10,9,115,99,97,108,101,61,48,10,9,110,47,61,49,
+ 10,9,105,102,40,110,60,48,41,123,10,9,9,110,61,45,110,10,9,9,105,102,40,110,
+ 37,50,61,61,49,41,111,61,49,10,9,125,10,9,97,61,49,10,9,102,111,114,40,105,
+ 61,50,59,105,60,61,110,59,43,43,105,41,102,42,61,105,10,9,115,99,97,108,101,
+ 61,49,46,53,42,115,10,9,97,61,40,120,94,110,41,47,40,50,94,110,42,97,41,10,
+ 9,114,61,118,61,49,10,9,102,61,45,120,42,120,47,52,10,9,115,99,97,108,101,43,
+ 61,108,101,110,103,116,104,40,97,41,45,115,99,97,108,101,40,97,41,10,9,102,
+ 111,114,40,105,61,49,59,118,33,61,48,59,43,43,105,41,123,10,9,9,118,61,118,
+ 42,115,47,105,47,40,110,43,105,41,10,9,9,114,43,61,118,10,9,125,10,9,115,99,
+ 97,108,101,61,115,10,9,105,98,97,115,101,61,98,10,9,105,102,40,111,33,61,48,
+ 41,114,101,116,117,114,110,40,45,97,42,114,47,49,41,10,9,114,101,116,117,114,
+ 110,40,97,42,114,47,49,41,10,125,10,0
+};
+
+BcStatus bc_vec_double(BcVec *vec) {
+
+ uint8_t *ptr = realloc(vec->array, vec->size * (vec->cap * 2));
+ if (!ptr) return BC_STATUS_MALLOC_FAIL;
+
+ vec->array = ptr;
+ vec->cap *= 2;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_vec_init(BcVec *vec, size_t esize, BcVecFreeFunc dtor) {
+
+ vec->size = esize;
+ vec->cap = BC_VEC_INITIAL_CAP;
+ vec->len = 0;
+ vec->dtor = dtor;
+
+ vec->array = malloc(esize * BC_VEC_INITIAL_CAP);
+ if (!vec->array) return BC_STATUS_MALLOC_FAIL;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_vec_expand(BcVec *vec, size_t request) {
+
+ uint8_t *ptr;
+
+ if (vec->cap >= request) return BC_STATUS_SUCCESS;
+
+ ptr = realloc(vec->array, vec->size * request);
+ if (!ptr) return BC_STATUS_MALLOC_FAIL;
+
+ vec->array = ptr;
+ vec->cap = request;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_vec_push(BcVec *vec, void *data) {
+
+ BcStatus status;
+ size_t size;
+
+ if (vec->len == vec->cap && (status = bc_vec_double(vec))) return status;
+
+ size = vec->size;
+ memmove(vec->array + (size * vec->len++), data, size);
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_vec_pushByte(BcVec *vec, uint8_t data) {
+
+ BcStatus status;
+
+ if (vec->len == vec->cap && (status = bc_vec_double(vec))) return status;
+
+ vec->array[vec->len++] = data;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_vec_pushAt(BcVec *vec, void *data, size_t idx) {
+
+ BcStatus status;
+ uint8_t *ptr;
+
+ if (idx == vec->len) return bc_vec_push(vec, data);
+ if (vec->len == vec->cap && (status = bc_vec_double(vec))) return status;
+
+ ptr = vec->array + vec->size * idx;
+
+ memmove(ptr + vec->size, ptr, vec->size * (vec->len++ - idx));
+ memmove(ptr, data, vec->size);
+
+ return BC_STATUS_SUCCESS;
+}
+
+void* bc_vec_top(const BcVec *vec) {
+ return vec->array + vec->size * (vec->len - 1);
+}
+
+void* bc_vec_item(const BcVec *vec, size_t idx) {
+ return vec->array + vec->size * idx;
+}
+
+void* bc_vec_item_rev(const BcVec *vec, size_t idx) {
+ return vec->array + vec->size * (vec->len - idx - 1);
+}
+
+void bc_vec_pop(BcVec *vec) {
+ vec->len -= 1;
+ if (vec->dtor) vec->dtor(vec->array + (vec->size * vec->len));
+}
+
+void bc_vec_npop(BcVec *vec, size_t n) {
+ if (!vec->dtor) vec->len -= n;
+ else {
+ size_t len = vec->len - n;
+ while (vec->len > len) bc_vec_pop(vec);
+ }
+}
+
+void bc_vec_free(void *vec) {
+
+ size_t i;
+ BcVec *s = (BcVec*) vec;
+
+ if (!s) return;
+
+ if (s->dtor) {
+ for (i = 0; i < s->len; ++i) s->dtor(s->array + (i * s->size));
+ }
+
+ free(s->array);
+ memset(s, 0, sizeof(BcVec));
+}
+
+size_t bc_veco_find(const BcVecO* vec, void *data) {
+
+ size_t low, high;
+
+ low = 0;
+ high = vec->vec.len;
+
+ while (low < high) {
+
+ size_t mid = (low + high) / 2;
+ uint8_t *ptr = bc_vec_item(&vec->vec, mid);
+ int result = vec->cmp(data, ptr);
+
+ if (!result) return mid;
+
+ if (result < 0) high = mid;
+ else low = mid + 1;
+ }
+
+ return low;
+}
+
+BcStatus bc_veco_init(BcVecO* vec, size_t esize,
+ BcVecFreeFunc dtor, BcVecCmpFunc cmp)
+{
+ vec->cmp = cmp;
+ return bc_vec_init(&vec->vec, esize, dtor);
+}
+
+BcStatus bc_veco_insert(BcVecO* vec, void *data, size_t *idx) {
+
+ BcStatus status;
+
+ *idx = bc_veco_find(vec, data);
+
+ if (*idx > vec->vec.len) return BC_STATUS_VEC_OUT_OF_BOUNDS;
+ if (*idx != vec->vec.len && !vec->cmp(data, bc_vec_item(&vec->vec, *idx)))
+ return BC_STATUS_VEC_ITEM_EXISTS;
+
+ if (*idx >= vec->vec.len) {
+ *idx = vec->vec.len;
+ status = bc_vec_push(&vec->vec, data);
+ }
+ else status = bc_vec_pushAt(&vec->vec, data, *idx);
+
+ return status;
+}
+
+size_t bc_veco_index(const BcVecO* v, void *data) {
+ size_t i;
+ i = bc_veco_find(v, data);
+ if (i >= v->vec.len || v->cmp(data, bc_vec_item(&v->vec, i))) return -1;
+ return i;
+}
+
+BcStatus bc_io_getline(char **buf, size_t *n) {
+
+ char *temp;
+ int c;
+ size_t size, i;
+
+ if (TT.tty && fputs(">>> ", stdout) == EOF) return BC_STATUS_IO_ERR;
+
+ for (i = 0, c = 0; c != '\n'; ++i) {
+
+ if (i == *n) {
+
+ size = *n * 2;
+
+ if (size > (1 << 20) || !(temp = realloc(*buf, size + 1)))
+ return BC_STATUS_MALLOC_FAIL;
+
+ *buf = temp;
+ *n = size;
+ }
+
+ if ((c = fgetc(stdin)) == EOF) {
+
+ if (errno == EINTR) {
+
+ TT.sigc = TT.sig;
+ TT.signe = 0;
+ --i;
+
+ fprintf(stderr, "%s", bc_program_ready_prompt);
+ fflush(stderr);
+
+ if (TT.tty && fputs(">>> ", stdout) == EOF) return BC_STATUS_IO_ERR;
+
+ continue;
+ }
+ else return BC_STATUS_IO_ERR;
+ }
+ else if (!c || (iscntrl(c) && !isspace(c)) || c > SCHAR_MAX)
+ return BC_STATUS_BINARY_FILE;
+
+ (*buf)[i] = c;
+ }
+
+ (*buf)[i] = '\0';
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_io_fread(const char *path, char **buf) {
+
+ BcStatus st;
+ FILE *f;
+ size_t size, read;
+
+ if (!(f = fopen(path, "r"))) return BC_STATUS_EXEC_FILE_ERR;
+
+ fseek(f, 0, SEEK_END);
+ size = ftell(f);
+ fseek(f, 0, SEEK_SET);
+
+ if (!(*buf = malloc(size + 1))) {
+ st = BC_STATUS_MALLOC_FAIL;
+ goto malloc_err;
+ }
+
+ if ((read = fread(*buf, 1, size, f)) != size) {
+ st = BC_STATUS_IO_ERR;
+ goto read_err;
+ }
+
+ (*buf)[size] = '\0';
+ fclose(f);
+
+ return BC_STATUS_SUCCESS;
+
+read_err:
+ free(*buf);
+malloc_err:
+ fclose(f);
+ return st;
+}
+
+BcStatus bc_num_subArrays(BcDigit *n1, BcDigit *n2, size_t len) {
+ size_t i, j;
+ for (i = 0; !TT.signe && i < len; ++i) {
+ for (n1[i] -= n2[i], j = 0; !TT.signe && n1[i + j] < 0;) {
+ n1[i + j++] += 10;
+ n1[i + j] -= 1;
+ }
+ }
+ return TT.signe ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
+}
+
+ssize_t bc_num_compare(BcDigit *n1, BcDigit *n2, size_t len) {
+ size_t i;
+ BcDigit c;
+ for (c = 0, i = len - 1; !TT.signe && !(c = n1[i] - n2[i]) && i < len; --i);
+ return (c < 0 ? -1 : 1) * (ssize_t) (i + 1);
+}
+
+ssize_t bc_num_cmp(BcNum *a, BcNum *b) {
+
+ size_t i, min, a_int, b_int, diff;
+ BcDigit *max_num, *min_num;
+ int a_max;
+ int cmp, neg;
+
+ if (!a) return !b ? 0 : !b->neg * -2 + 1;
+ else if (!b) return a->neg * -2 + 1;
+
+ neg = 1;
+
+ if (a->neg) {
+ if (b->neg) neg = -1;
+ else return -1;
+ }
+ else if (b->neg) return 1;
+
+ if (!a->len) return (!b->neg * -2 + 1) * !!b->len;
+ else if (!b->len) return a->neg * -2 + 1;
+
+ a_int = a->len - a->rdx;
+ b_int = b->len - b->rdx;
+ a_int -= b_int;
+
+ if (a_int) return a_int;
+
+ a_max = a->rdx > b->rdx;
+
+ if (a_max) {
+ min = b->rdx;
+ diff = a->rdx - b->rdx;
+ max_num = a->num + diff;
+ min_num = b->num;
+ }
+ else {
+ min = a->rdx;
+ diff = b->rdx - a->rdx;
+ max_num = b->num + diff;
+ min_num = a->num;
+ }
+
+ cmp = bc_num_compare(max_num, min_num, b_int + min);
+ if (cmp) return cmp * (!a_max * -2 + 1) * neg;
+
+ for (max_num -= diff, i = diff - 1; !TT.signe && i < diff; --i) {
+ if (max_num[i]) return neg * (!a_max * -2 + 1);
+ }
+
+ return 0;
+}
+
+void bc_num_truncate(BcNum *n, size_t places) {
+
+ BcDigit *ptr;
+
+ if (!places) return;
+
+ ptr = n->num + places;
+ n->len -= places;
+ n->rdx -= places;
+
+ memmove(n->num, ptr, n->len * sizeof(BcDigit));
+ memset(n->num + n->len, 0, sizeof(BcDigit) * (n->cap - n->len));
+}
+
+BcStatus bc_num_extend(BcNum *n, size_t places) {
+
+ BcStatus status;
+ BcDigit *ptr;
+ size_t len;
+
+ if (!places) return BC_STATUS_SUCCESS;
+
+ len = n->len + places;
+ if (n->cap < len && (status = bc_num_expand(n, len))) return status;
+
+ ptr = n->num + places;
+ memmove(ptr, n->num, sizeof(BcDigit) * n->len);
+ memset(n->num, 0, sizeof(BcDigit) * places);
+
+ n->len += places;
+ n->rdx += places;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
+
+ BcStatus status;
+ BcNum one;
+
+ if ((status = bc_num_init(&one, BC_NUM_DEF_SIZE))) return status;
+
+ bc_num_one(&one);
+ status = bc_num_div(&one, a, b, scale);
+ bc_num_free(&one);
+
+ return status;
+}
+
+BcStatus bc_num_alg_a(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+
+ BcDigit *ptr, *ptr_a, *ptr_b, *ptr_c;
+ size_t i, max, min_rdx, min_int, diff, a_int, b_int;
+ BcDigit carry;
+
+ (void) scale;
+
+ if (!a->len) return bc_num_copy(c, b);
+ else if (!b->len) return bc_num_copy(c, a);
+
+ c->neg = a->neg;
+ memset(c->num, 0, c->cap * sizeof(BcDigit));
+
+ c->rdx = maxof(a->rdx, b->rdx);
+ min_rdx = minof(a->rdx, b->rdx);
+ c->len = 0;
+
+ if (a->rdx > b->rdx) {
+ diff = a->rdx - b->rdx;
+ ptr = a->num;
+ ptr_a = a->num + diff;
+ ptr_b = b->num;
+ }
+ else {
+ diff = b->rdx - a->rdx;
+ ptr = b->num;
+ ptr_a = a->num;
+ ptr_b = b->num + diff;
+ }
+
+ for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
+
+ ptr_c += diff;
+ a_int = a->len - a->rdx;
+ b_int = b->len - b->rdx;
+
+ if (a_int > b_int) {
+ min_int = b_int;
+ max = a_int;
+ ptr = ptr_a;
+ }
+ else {
+ min_int = a_int;
+ max = b_int;
+ ptr = ptr_b;
+ }
+
+ for (carry = 0, i = 0; !TT.signe && i < min_rdx + min_int; ++i, ++c->len) {
+ ptr_c[i] = ptr_a[i] + ptr_b[i] + carry;
+ carry = ptr_c[i] / 10;
+ ptr_c[i] %= 10;
+ }
+
+ for (; !TT.signe && i < max + min_rdx; ++i, ++c->len) {
+ ptr_c[i] += ptr[i] + carry;
+ carry = ptr_c[i] / 10;
+ ptr_c[i] %= 10;
+ }
+
+ if (TT.signe) return BC_STATUS_EXEC_SIGNAL;
+
+ if (carry) c->num[c->len++] = carry;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_num_alg_s(BcNum *a, BcNum *b, BcNum *c, size_t sub) {
+
+ BcStatus status;
+ int cmp;
+ BcNum *minuend, *subtrahend;
+ size_t start;
+ int aneg, bneg, neg;
+
+ // Because this function doesn't need to use scale (per the bc spec),
+ // I am hijacking it to tell this function whether it is doing an add
+ // or a subtract.
+
+ if (!a->len) {
+ status = bc_num_copy(c, b);
+ c->neg = !b->neg;
+ return status;
+ }
+ else if (!b->len) return bc_num_copy(c, a);
+
+ aneg = a->neg;
+ bneg = b->neg;
+ a->neg = b->neg = 0;
+
+ cmp = bc_num_cmp(a, b);
+
+ a->neg = aneg;
+ b->neg = bneg;
+
+ if (!cmp) {
+ bc_num_zero(c);
+ return BC_STATUS_SUCCESS;
+ }
+ else if (cmp > 0) {
+ neg = sub && a->neg;
+ minuend = a;
+ subtrahend = b;
+ }
+ else {
+ neg = sub && !b->neg;
+ minuend = b;
+ subtrahend = a;
+ }
+
+ if ((status = bc_num_copy(c, minuend))) return status;
+ c->neg = neg;
+
+ if (c->rdx < subtrahend->rdx) {
+ if ((status = bc_num_extend(c, subtrahend->rdx - c->rdx))) return status;
+ start = 0;
+ }
+ else start = c->rdx - subtrahend->rdx;
+
+ status = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
+
+ while (c->len > c->rdx && !c->num[c->len - 1]) --c->len;
+
+ return status;
+}
+
+BcStatus bc_num_alg_m(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+
+ BcStatus status;
+ BcDigit carry;
+ size_t i, j, len;
+
+ if (!a->len || !b->len) {
+ bc_num_zero(c);
+ return BC_STATUS_SUCCESS;
+ }
+ else if (BC_NUM_ONE(a)) {
+ status = bc_num_copy(c, b);
+ if (a->neg) c->neg = !c->neg;
+ return status;
+ }
+ else if (BC_NUM_ONE(b)) {
+ status = bc_num_copy(c, a);
+ if (b->neg) c->neg = !c->neg;
+ return status;
+ }
+
+ scale = maxof(scale, a->rdx);
+ scale = maxof(scale, b->rdx);
+ c->rdx = a->rdx + b->rdx;
+
+ memset(c->num, 0, sizeof(BcDigit) * c->cap);
+ c->len = carry = len = 0;
+
+ for (i = 0; !TT.signe && i < b->len; ++i) {
+
+ for (j = 0; !TT.signe && j < a->len; ++j) {
+ c->num[i + j] += a->num[j] * b->num[i] + carry;
+ carry = c->num[i + j] / 10;
+ c->num[i + j] %= 10;
+ }
+
+ if (TT.signe) return BC_STATUS_EXEC_SIGNAL;
+
+ if (carry) {
+ c->num[i + j] += carry;
+ carry = 0;
+ len = maxof(len, i + j + 1);
+ }
+ else len = maxof(len, i + j);
+ }
+
+ if (TT.signe) return BC_STATUS_EXEC_SIGNAL;
+
+ c->len = maxof(len, c->rdx);
+ c->neg = !a->neg != !b->neg;
+
+ if (scale < c->rdx) bc_num_truncate(c, c->rdx - scale);
+ while (c->len > c->rdx && !c->num[c->len - 1]) --c->len;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_num_alg_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+
+ BcStatus status;
+ BcDigit *ptr, *bptr, q;
+ size_t len, end, i;
+ BcNum copy;
+ int zero;
+
+ if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
+ else if (!a->len) {
+ bc_num_zero(c);
+ return BC_STATUS_SUCCESS;
+ }
+ else if (BC_NUM_ONE(b)) {
+
+ if ((status = bc_num_copy(c, a))) return status;
+ if (b->neg) c->neg = !c->neg;
+
+ if (c->rdx < scale) status = bc_num_extend(c, scale - c->rdx);
+ else bc_num_truncate(c, c->rdx - scale);
+
+ return status;
+ }
+
+ if ((status = bc_num_init(&copy, a->len + b->rdx + scale + 1))) return status;
+ if ((status = bc_num_copy(&copy, a))) goto err;
+
+ if ((len = b->len) > copy.len) {
+ if ((status = bc_num_expand(&copy, len + 2))) goto err;
+ if ((status = bc_num_extend(&copy, len - copy.len))) goto err;
+ }
+
+ if (b->rdx > copy.rdx && (status = bc_num_extend(&copy, b->rdx - copy.rdx)))
+ goto err;
+
+ copy.rdx -= b->rdx;
+
+ if (scale > copy.rdx && (status = bc_num_extend(&copy, scale - copy.rdx)))
+ goto err;
+
+ if (b->rdx == b->len) {
+ int zero;
+ for (zero = 1, i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
+ if (i == len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
+ len -= i - 1;
+ }
+
+ if (copy.cap == copy.len && (status = bc_num_expand(&copy, copy.len + 1)))
+ goto err;
+
+ // We want an extra zero in front to make things simpler.
+ copy.num[copy.len++] = 0;
+ end = copy.len - len;
+
+ if ((status = bc_num_expand(c, copy.len))) goto err;
+
+ bc_num_zero(c);
+ c->rdx = copy.rdx;
+ c->len = copy.len;
+ bptr = b->num;
+
+ for (i = end - 1; !TT.signe && i < end; --i) {
+
+ ptr = copy.num + i;
+
+ q = 0;
+ for (; (!status && ptr[len]) || bc_num_compare(ptr, bptr, len) >= 0; ++q)
+ status = bc_num_subArrays(ptr, bptr, len);
+
+ c->num[i] = q;
+ }
+
+ if (status) goto err;
+
+ c->neg = !a->neg != !b->neg;
+ while (c->len > c->rdx && !c->num[c->len - 1]) --c->len;
+ if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
+
+ for (i = 0, zero = 1; zero && i < c->len; ++i) zero = !c->num[i];
+ if (zero) bc_num_zero(c);
+
+err:
+ bc_num_free(&copy);
+ return status;
+}
+
+BcStatus bc_num_alg_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+
+ BcStatus status;
+ BcNum c1, c2;
+ size_t len;
+
+ if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
+
+ if (!a->len) {
+ bc_num_zero(c);
+ return BC_STATUS_SUCCESS;
+ }
+
+ len = a->len + b->len + scale;
+
+ if ((status = bc_num_init(&c1, len))) return status;
+ if ((status = bc_num_init(&c2, len))) goto c2_err;
+ if ((status = bc_num_div(a, b, &c1, scale))) goto err;
+
+ c->rdx = maxof(scale + b->rdx, a->rdx);
+ if ((status = bc_num_mul(&c1, b, &c2, scale))) goto err;
+ status = bc_num_sub(a, &c2, c, scale);
+
+err:
+ bc_num_free(&c2);
+c2_err:
+ bc_num_free(&c1);
+ return status;
+}
+
+BcStatus bc_num_alg_p(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
+
+ BcStatus status;
+ BcNum copy;
+ unsigned long pow;
+ size_t i, powrdx, resrdx;
+ int neg, zero;
+
+ if (b->rdx) return BC_STATUS_MATH_NON_INTEGER;
+
+ if (!b->len) {
+ bc_num_one(c);
+ return BC_STATUS_SUCCESS;
+ }
+ else if (!a->len) {
+ bc_num_zero(c);
+ return BC_STATUS_SUCCESS;
+ }
+ else if (BC_NUM_ONE(b)) {
+
+ if (!b->neg) status = bc_num_copy(c, a);
+ else status = bc_num_inv(a, c, scale);
+
+ return status;
+ }
+
+ neg = b->neg;
+ b->neg = 0;
+
+ if ((status = bc_num_ulong(b, &pow))) return status;
+ if ((status = bc_num_init(&copy, a->len))) return status;
+ if ((status = bc_num_copy(&copy, a))) goto err;
+
+ if (!neg) scale = minof(a->rdx * pow, maxof(scale, a->rdx));
+
+ b->neg = neg;
+
+ for (powrdx = a->rdx; !TT.signe && !(pow & 1); pow >>= 1) {
+ powrdx <<= 1;
+ if ((status = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
+ }
+
+ if ((status = bc_num_copy(c, &copy))) goto err;
+ if (TT.signe) {
+ status = BC_STATUS_EXEC_SIGNAL;
+ goto err;
+ }
+
+ resrdx = powrdx;
+
+ for (pow >>= 1; !TT.signe && pow != 0; pow >>= 1) {
+
+ powrdx <<= 1;
+
+ if ((status = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
+
+ if (pow & 1) {
+ resrdx += powrdx;
+ if ((status = bc_num_mul(c, &copy, c, resrdx))) goto err;
+ }
+ }
+
+ if (neg && (status = bc_num_inv(c, c, scale))) goto err;
+ if (TT.signe) {
+ status = BC_STATUS_EXEC_SIGNAL;
+ goto err;
+ }
+
+ if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
+
+ for (zero = 1, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
+ if (zero) bc_num_zero(c);
+
+err:
+ bc_num_free(&copy);
+ return status;
+}
+
+BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
+ BcNumBinaryFunc op, size_t req)
+{
+ BcStatus status;
+ BcNum num2, *ptr_a, *ptr_b;
+ int init = 0;
+
+ if (c == a) {
+ memcpy(&num2, c, sizeof(BcNum));
+ ptr_a = &num2;
+ init = 1;
+ }
+ else ptr_a = a;
+
+ if (c == b) {
+
+ if (c == a) {
+ ptr_b = ptr_a;
+ }
+ else {
+ memcpy(&num2, c, sizeof(BcNum));
+ ptr_b = &num2;
+ init = 1;
+ }
+ }
+ else ptr_b = b;
+
+ if (init) status = bc_num_init(c, req);
+ else status = bc_num_expand(c, req);
+
+ if (status) goto err;
+ status = op(ptr_a, ptr_b, c, scale);
+
+err:
+ if (c == a || c == b) bc_num_free(&num2);
+ return status;
+}
+
+int bc_num_strValid(const char *val, size_t base) {
+
+ size_t len, i;
+ BcDigit c, b;
+ int small, radix;
+
+ radix = 0;
+ len = strlen(val);
+
+ if (!len) return 1;
+
+ small = base <= 10;
+ b = small ? base + '0' : base - 9 + 'A';
+
+ for (i = 0; i < len; ++i) {
+
+ if ((c = val[i]) == '.') {
+
+ if (radix) return 0;
+
+ radix = 1;
+ continue;
+ }
+
+ if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b)))
+ return 0;
+ }
+
+ return 1;
+}
+
+BcStatus bc_num_parseDecimal(BcNum *n, const char *val) {
+
+ BcStatus status;
+ size_t len, i;
+ const char *ptr;
+ int zero = 1;
+
+ for (i = 0; val[i] == '0'; ++i);
+
+ val += i;
+ len = strlen(val);
+ bc_num_zero(n);
+
+ if (len) {
+ for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.';
+ if ((status = bc_num_expand(n, len))) return status;
+ }
+
+ if (zero) {
+ memset(n->num, 0, sizeof(BcDigit) * n->cap);
+ n->neg = 0;
+ return BC_STATUS_SUCCESS;
+ }
+
+ ptr = strchr(val, '.');
+
+ // Explicitly test for NULL here to produce either a 0 or 1.
+ n->rdx = (ptr != NULL) * ((val + len) - (ptr + 1));
+
+ for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.'))
+ n->num[n->len] = val[i] - '0';
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) {
+
+ BcStatus status;
+ BcNum temp, mult, result;
+ size_t i, len, digits;
+ BcDigit c;
+ int zero;
+ unsigned long v;
+
+ len = strlen(val);
+ bc_num_zero(n);
+
+ for (zero = 1, i = 0; zero && i < len; ++i)
+ zero = (val[i] == '.' || val[i] == '0');
+ if (zero) return BC_STATUS_SUCCESS;
+
+ if ((status = bc_num_init(&temp, BC_NUM_DEF_SIZE))) return status;
+ if ((status = bc_num_init(&mult, BC_NUM_DEF_SIZE))) goto mult_err;
+
+ for (i = 0; i < len && (c = val[i]) != '.'; ++i) {
+
+ v = c <= '9' ? c - '0' : c - 'A' + 10;
+
+ if ((status = bc_num_mul(n, base, &mult, 0))) goto int_err;
+ if ((status = bc_num_ulong2num(&temp, v))) goto int_err;
+ if ((status = bc_num_add(&mult, &temp, n, 0))) goto int_err;
+ }
+
+ if (i == len && !(c = val[i])) goto int_err;
+ if ((status = bc_num_init(&result, base->len))) goto int_err;
+
+ bc_num_zero(&result);
+ bc_num_one(&mult);
+
+ for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
+
+ v = c <= '9' ? c - '0' : c - 'A' + 10;
+
+ if ((status = bc_num_mul(&result, base, &result, 0))) goto err;
+ if ((status = bc_num_ulong2num(&temp, v))) goto err;
+ if ((status = bc_num_add(&result, &temp, &result, 0))) goto err;
+ if ((status = bc_num_mul(&mult, base, &mult, 0))) goto err;
+ }
+
+ if ((status = bc_num_div(&result, &mult, &result, digits))) goto err;
+ if ((status = bc_num_add(n, &result, n, digits))) goto err;
+
+ if (n->len) {
+ if (n->rdx < digits && n->len) status = bc_num_extend(n, digits - n->rdx);
+ }
+ else bc_num_zero(n);
+
+err:
+ bc_num_free(&result);
+int_err:
+ bc_num_free(&mult);
+mult_err:
+ bc_num_free(&temp);
+ return status;
+}
+
+BcStatus bc_num_printDigits(size_t num, size_t width, int radix,
+ size_t *nchars, size_t line_len)
+{
+ size_t exp, pow, div;
+
+ if (*nchars == line_len - 1) {
+ if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
+ if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
+ *nchars = 0;
+ }
+
+ if (*nchars || radix) {
+ if (putchar(radix ? '.' : ' ') == EOF) return BC_STATUS_IO_ERR;
+ ++(*nchars);
+ }
+
+ for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10);
+
+ for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) {
+
+ if (*nchars == line_len - 1) {
+ if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
+ if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
+ *nchars = 0;
+ }
+
+ div = num / pow;
+ num -= div * pow;
+
+ if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR;
+ }
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_num_printHex(size_t num, size_t width, int radix,
+ size_t *nchars, size_t line_len)
+{
+ width += !!radix;
+ if (*nchars + width >= line_len) {
+ if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
+ if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
+ *nchars = 0;
+ }
+
+ if (radix && putchar('.') == EOF) return BC_STATUS_IO_ERR;
+ if (putchar(bc_num_hex_digits[num]) == EOF) return BC_STATUS_IO_ERR;
+
+ *nchars = *nchars + width;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_num_printDecimal(BcNum *n, size_t *nchars, size_t line_len) {
+
+ BcStatus status;
+ size_t i;
+
+ if (n->neg) {
+ if (putchar('-') == EOF) return BC_STATUS_IO_ERR;
+ ++(*nchars);
+ }
+
+ status = BC_STATUS_SUCCESS;
+
+ for (i = n->len - 1; !status && i < n->len; --i)
+ status = bc_num_printHex(n->num[i], 1, i == n->rdx - 1, nchars, line_len);
+
+ return status;
+}
+
+BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t,
+ size_t *nchars, size_t line_len)
+{
+ BcStatus status;
+ BcVec stack;
+ BcNum intp, fracp, digit, frac_len;
+ size_t width, i;
+ BcNumDigitFunc print;
+ unsigned long dig, *ptr;
+ int neg, radix;
+
+ neg = n->neg;
+ n->neg = 0;
+
+ if (neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
+ nchars += neg;
+
+ if (base_t <= BC_NUM_MAX_INPUT_BASE) {
+ width = 1;
+ print = bc_num_printHex;
+ }
+ else {
+ width = (size_t) floor(log10((double) (base_t - 1)) + 1.0);
+ print = bc_num_printDigits;
+ }
+
+ if ((status = bc_vec_init(&stack, sizeof(long), NULL))) return status;
+ if ((status = bc_num_init(&intp, n->len))) goto int_err;
+ if ((status = bc_num_init(&fracp, n->rdx))) goto frac_err;
+ if ((status = bc_num_init(&digit, width))) goto digit_err;
+ if ((status = bc_num_copy(&intp, n))) goto frac_len_err;
+
+ bc_num_truncate(&intp, intp.rdx);
+ if ((status = bc_num_sub(n, &intp, &fracp, 0))) goto frac_len_err;
+
+ while (intp.len) {
+ if ((status = bc_num_mod(&intp, base, &digit, 0))) goto frac_len_err;
+ if ((status = bc_num_ulong(&digit, &dig))) goto frac_len_err;
+ if ((status = bc_vec_push(&stack, &dig))) goto frac_len_err;
+ if ((status = bc_num_div(&intp, base, &intp, 0))) goto frac_len_err;
+ }
+
+ for (i = 0; i < stack.len; ++i) {
+ ptr = bc_vec_item_rev(&stack, i);
+ status = print(*ptr, width, 0, nchars, line_len);
+ if (status) goto frac_len_err;
+ }
+
+ if (!n->rdx || (status = bc_num_init(&frac_len, n->len - n->rdx)))
+ goto frac_len_err;
+
+ bc_num_one(&frac_len);
+
+ for (radix = 1; frac_len.len <= n->rdx; radix = 0) {
+ if ((status = bc_num_mul(&fracp, base, &fracp, n->rdx))) goto err;
+ if ((status = bc_num_ulong(&fracp, &dig))) goto err;
+ if ((status = bc_num_ulong2num(&intp, dig))) goto err;
+ if ((status = bc_num_sub(&fracp, &intp, &fracp, 0))) goto err;
+ if ((status = print(dig, width, radix, nchars, line_len))) goto err;
+ if ((status = bc_num_mul(&frac_len, base, &frac_len, 0))) goto err;
+ }
+
+err:
+ n->neg = neg;
+ bc_num_free(&frac_len);
+frac_len_err:
+ bc_num_free(&digit);
+digit_err:
+ bc_num_free(&fracp);
+frac_err:
+ bc_num_free(&intp);
+int_err:
+ bc_vec_free(&stack);
+ return status;
+}
+
+BcStatus bc_num_init(BcNum *n, size_t request) {
+
+ memset(n, 0, sizeof(BcNum));
+
+ request = request >= BC_NUM_DEF_SIZE ? request : BC_NUM_DEF_SIZE;
+ if (!(n->num = malloc(request))) return BC_STATUS_MALLOC_FAIL;
+
+ n->cap = request;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_num_expand(BcNum *n, size_t request) {
+
+ BcDigit *temp;
+
+ if (request <= n->cap) return BC_STATUS_SUCCESS;
+ if (!(temp = realloc(n->num, request))) return BC_STATUS_MALLOC_FAIL;
+
+ memset(temp + n->cap, 0, sizeof(char) * (request - n->cap));
+ n->num = temp;
+ n->cap = request;
+
+ return BC_STATUS_SUCCESS;
+}
+
+void bc_num_free(void *num) {
+ BcNum *n = (BcNum*) num;
+ if (n && n->num) free(n->num);
+}
+
+BcStatus bc_num_copy(BcNum *d, BcNum *s) {
+
+ BcStatus status;
+
+ if (d == s) return BC_STATUS_SUCCESS;
+ if ((status = bc_num_expand(d, s->cap))) return status;
+
+ d->len = s->len;
+ d->neg = s->neg;
+ d->rdx = s->rdx;
+
+ memcpy(d->num, s->num, sizeof(BcDigit) * d->len);
+ memset(d->num + d->len, 0, sizeof(BcDigit) * (d->cap - d->len));
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t) {
+
+ BcStatus status;
+
+ if (!bc_num_strValid(val, base_t)) return BC_STATUS_MATH_BAD_STRING;
+
+ if (base_t == 10) status = bc_num_parseDecimal(n, val);
+ else status = bc_num_parseBase(n, val, base);
+
+ return status;
+}
+
+BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, int newline,
+ size_t *nchars, size_t line_len)
+{
+ BcStatus status;
+
+ if (*nchars >= line_len) {
+ if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
+ if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
+ *nchars = 0;
+ }
+
+ if (!n->len) {
+ if (putchar('0') == EOF) return BC_STATUS_IO_ERR;
+ ++(*nchars);
+ status = BC_STATUS_SUCCESS;
+ }
+ else if (base_t == 10) status = bc_num_printDecimal(n, nchars, line_len);
+ else status = bc_num_printBase(n, base, base_t, nchars, line_len);
+
+ if (status) return status;
+
+ if (newline) {
+ if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
+ *nchars = 0;
+ }
+
+ return status;
+}
+
+BcStatus bc_num_ulong(BcNum *n, unsigned long *result) {
+
+ size_t i;
+ unsigned long prev, pow;
+
+ if (n->neg) return BC_STATUS_MATH_NEGATIVE;
+
+ for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
+
+ prev = *result;
+ *result += n->num[i] * pow;
+ pow *= 10;
+
+ if (*result < prev) return BC_STATUS_MATH_OVERFLOW;
+ }
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_num_ulong2num(BcNum *n, unsigned long val) {
+
+ BcStatus status;
+ size_t len, i;
+ BcDigit *ptr;
+
+ bc_num_zero(n);
+
+ if (!val) {
+ memset(n->num, 0, sizeof(char) * n->cap);
+ return BC_STATUS_SUCCESS;
+ }
+
+ len = (size_t) ceil(log10(((double) ULONG_MAX) + 1.0f));
+
+ if ((status = bc_num_expand(n, len))) return status;
+
+ for (ptr = n->num, i = 0; val; ++i, ++n->len) {
+ ptr[i] = (char) (val % 10);
+ val /= 10;
+ }
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *result, size_t scale) {
+ (void) scale;
+ BcNumBinaryFunc op = (!a->neg == !b->neg) ? bc_num_alg_a : bc_num_alg_s;
+ return bc_num_binary(a, b, result, 0, op, a->len + b->len + 1);
+}
+
+BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *result, size_t scale) {
+ (void) scale;
+ BcNumBinaryFunc op = (!a->neg == !b->neg) ? bc_num_alg_s : bc_num_alg_a;
+ return bc_num_binary(a, b, result, 1, op, a->len + b->len + 1);
+}
+
+BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *result, size_t scale) {
+ return bc_num_binary(a, b, result, scale, bc_num_alg_m,
+ a->len + b->len + scale + 1);
+}
+
+BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *result, size_t scale) {
+ return bc_num_binary(a, b, result, scale, bc_num_alg_d,
+ a->len + b->len + scale + 1);
+}
+
+BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *result, size_t scale) {
+ return bc_num_binary(a, b, result, scale, bc_num_alg_mod,
+ a->len + b->len + scale + 1);
+}
+
+BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *result, size_t scale) {
+ return bc_num_binary(a, b, result, scale, bc_num_alg_p,
+ (a->len + 1) * (b->len + 1));
+}
+
+BcStatus bc_num_sqrt(BcNum *a, BcNum *result, size_t scale) {
+
+ BcStatus status;
+ BcNum a2, *ptr_a, num1, num2, two, f, fprime, *x0, *x1, *temp;
+ size_t pow, len, digits, resrdx, req;
+ int cmp;
+
+ req = a->rdx + (a->len - a->rdx) * 2 + 1;
+
+ if (result == a) {
+ memcpy(&a2, result, sizeof(BcNum));
+ ptr_a = &a2;
+ status = bc_num_init(result, req);
+ }
+ else {
+ ptr_a = a;
+ status = bc_num_expand(result, req);
+ }
+
+ if (status) goto init_err;
+
+ if (!ptr_a->len) {
+ bc_num_zero(result);
+ return BC_STATUS_SUCCESS;
+ }
+ else if (ptr_a->neg) return BC_STATUS_MATH_NEG_SQRT;
+ else if (BC_NUM_ONE(a)) {
+ bc_num_one(result);
+ return bc_num_extend(result, scale);
+ }
+
+ memset(result->num, 0, result->cap * sizeof(BcDigit));
+ len = ptr_a->len;
+
+ scale = maxof(scale, ptr_a->rdx) + 1;
+
+ if ((status = bc_num_init(&num1, len))) return status;
+ if ((status = bc_num_init(&num2, num1.len))) goto num2_err;
+ if ((status = bc_num_init(&two, BC_NUM_DEF_SIZE))) goto two_err;
+
+ bc_num_one(&two);
+ two.num[0] = 2;
+
+ len += scale;
+
+ if ((status = bc_num_init(&f, len))) goto f_err;
+ if ((status = bc_num_init(&fprime, len + scale))) goto fprime_err;
+
+ x0 = &num1;
+ x1 = &num2;
+
+ bc_num_one(x0);
+
+ pow = ptr_a->len - ptr_a->rdx;
+
+ if (pow) {
+
+ if (pow & 1) {
+ x0->num[0] = 2;
+ pow -= 1;
+ }
+ else {
+ x0->num[0] = 6;
+ pow -= 2;
+ }
+
+ if ((status = bc_num_extend(x0, pow))) goto err;
+ }
+
+ cmp = 1;
+ x0->rdx = digits = 0;
+ resrdx = scale + 1;
+ len = (x0->len - x0->rdx) + resrdx;
+
+ while (!TT.signe && cmp && digits <= len) {
+
+ if ((status = bc_num_mul(x0, x0, &f, resrdx))) goto err;
+ if ((status = bc_num_sub(&f, a, &f, resrdx))) goto err;
+ if ((status = bc_num_mul(x0, &two, &fprime, resrdx))) goto err;
+ if ((status = bc_num_div(&f, &fprime, &f, resrdx))) goto err;
+ if ((status = bc_num_sub(x0, &f, x1, resrdx))) goto err;
+
+ cmp = bc_num_cmp(x1, x0);
+ digits = x1->len - llabs(cmp);
+
+ temp = x0;
+ x0 = x1;
+ x1 = temp;
+ }
+
+ if (TT.signe) {
+ status = BC_STATUS_EXEC_SIGNAL;
+ goto err;
+ }
+
+ if ((status = bc_num_copy(result, x0))) goto err;
+
+ if (result->rdx > --scale) bc_num_truncate(result, result->rdx - scale);
+ else if (result->rdx < scale)
+ status = bc_num_extend(result, scale - result->rdx);
+
+err:
+ bc_num_free(&fprime);
+fprime_err:
+ bc_num_free(&f);
+f_err:
+ bc_num_free(&two);
+two_err:
+ bc_num_free(&num2);
+num2_err:
+ bc_num_free(&num1);
+init_err:
+ if (result == a) bc_num_free(&a2);
+ return status;
+}
+
+void bc_num_zero(BcNum *n) {
+ if (!n) return;
+ memset(n->num, 0, n->cap * sizeof(char));
+ n->neg = 0;
+ n->len = 0;
+ n->rdx = 0;
+}
+
+void bc_num_one(BcNum *n) {
+ if (!n) return;
+ bc_num_zero(n);
+ n->len = 1;
+ n->num[0] = 1;
+}
+
+void bc_num_ten(BcNum *n) {
+ if (!n) return;
+ bc_num_zero(n);
+ n->len = 2;
+ n->num[0] = 0;
+ n->num[1] = 1;
+}
+
+BcStatus bc_func_insert(BcFunc *f, char *name, int var) {
+
+ BcAuto a;
+ size_t i;
+
+ for (i = 0; i < f->autos.len; ++i) {
+ if (!strcmp(name, ((BcAuto*) bc_vec_item(&f->autos, i))->name))
+ return BC_STATUS_PARSE_DUPLICATE_LOCAL;
+ }
+
+ a.var = var;
+ a.name = name;
+
+ return bc_vec_push(&f->autos, &a);
+}
+
+BcStatus bc_func_init(BcFunc *f) {
+
+ BcStatus status;
+
+ if ((status = bc_vec_init(&f->code, sizeof(uint8_t), NULL))) return status;
+ if ((status = bc_vec_init(&f->autos, sizeof(BcAuto), bc_auto_free))) goto err;
+ if ((status = bc_vec_init(&f->labels, sizeof(size_t), NULL))) goto label_err;
+
+ f->nparams = 0;
+
+ return BC_STATUS_SUCCESS;
+
+label_err:
+ bc_vec_free(&f->autos);
+err:
+ bc_vec_free(&f->code);
+ return status;
+}
+
+void bc_func_free(void *func) {
+
+ BcFunc *f = (BcFunc*) func;
+
+ if (!f) return;
+
+ bc_vec_free(&f->code);
+ bc_vec_free(&f->autos);
+ bc_vec_free(&f->labels);
+}
+
+BcStatus bc_array_copy(BcVec *d, BcVec *s) {
+
+ BcStatus status;
+ size_t i;
+ BcNum *dnum, *snum;
+
+ bc_vec_npop(d, d->len);
+
+ if ((status = bc_vec_expand(d, s->cap))) return status;
+
+ d->len = s->len;
+
+ for (i = 0; !status && i < s->len; ++i) {
+
+ dnum = bc_vec_item(d, i);
+ snum = bc_vec_item(s, i);
+
+ if ((status = bc_num_init(dnum, snum->len))) return status;
+ if ((status = bc_num_copy(dnum, snum))) bc_num_free(dnum);
+ }
+
+ return status;
+}
+
+BcStatus bc_array_expand(BcVec *a, size_t len) {
+
+ BcStatus status = BC_STATUS_SUCCESS;
+ BcNum num;
+
+ while (!status && len > a->len) {
+ if ((status = bc_num_init(&num, BC_NUM_DEF_SIZE))) return status;
+ bc_num_zero(&num);
+ if ((status = bc_vec_push(a, &num))) bc_num_free(&num);
+ }
+
+ return status;
+}
+
+void bc_string_free(void *string) {
+ char **s = string;
+ if (s) free(*s);
+}
+
+int bc_entry_cmp(void *entry1, void *entry2) {
+ return strcmp(((BcEntry*) entry1)->name, ((BcEntry*) entry2)->name);
+}
+
+void bc_entry_free(void *entry) {
+ BcEntry *e = entry;
+ if (e) free(e->name);
+}
+
+void bc_auto_free(void *auto1) {
+ BcAuto *a = (BcAuto*) auto1;
+ if (a && a->name) free(a->name);
+}
+
+void bc_result_free(void *result) {
+
+ BcResult *r = (BcResult*) result;
+
+ if (!r) return;
+
+ switch (r->type) {
+
+ case BC_RESULT_TEMP:
+ case BC_RESULT_SCALE:
+ case BC_RESULT_VAR_AUTO:
+ {
+ bc_num_free(&r->data.num);
+ break;
+ }
+
+ case BC_RESULT_ARRAY_AUTO:
+ {
+ bc_vec_free(&r->data.array);
+ break;
+ }
+
+ case BC_RESULT_VAR:
+ case BC_RESULT_ARRAY:
+ {
+ if (r->data.id.name) free(r->data.id.name);
+ break;
+ }
+
+ default:
+ {
+ // Do nothing.
+ break;
+ }
+ }
+}
+
+BcStatus bc_lex_string(BcLex *lex) {
+
+ const char *start;
+ size_t newlines, len, i, j;
+ char c;
+
+ newlines = 0;
+ lex->token.type = BC_LEX_STRING;
+ i = lex->idx;
+
+ for (c = lex->buffer[i]; c != '"' && c != '\0'; c = lex->buffer[++i]) {
+ if (c == '\n') ++newlines;
+ }
+
+ if (c == '\0') {
+ lex->idx = i;
+ return BC_STATUS_LEX_NO_STRING_END;
+ }
+
+ len = i - lex->idx;
+ if (!(lex->token.string = malloc(len + 1))) return BC_STATUS_MALLOC_FAIL;
+
+ start = lex->buffer + lex->idx;
+
+ for (j = 0; j < len; ++j) lex->token.string[j] = start[j];
+
+ lex->token.string[len] = '\0';
+ lex->idx = i + 1;
+ lex->line += newlines;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_lex_comment(BcLex *lex) {
+
+ size_t newlines, i;
+ const char *buffer;
+ char c;
+ int end;
+
+ newlines = 0;
+ lex->token.type = BC_LEX_WHITESPACE;
+ end = 0;
+
+ buffer = lex->buffer;
+
+ for (i = ++lex->idx; !end; i += !end) {
+
+ while ((c = buffer[i]) != '*' && c != '\0') {
+ if (c == '\n') ++newlines;
+ c = buffer[++i];
+ }
+
+ if (c == '\0' || buffer[i + 1] == '\0') {
+ lex->idx = i;
+ return BC_STATUS_LEX_NO_COMMENT_END;
+ }
+
+ end = buffer[i + 1] == '/';
+ }
+
+ lex->idx = i + 2;
+ lex->line += newlines;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_lex_number(BcLex *lex, char start) {
+
+ const char *buffer, *buf;
+ size_t backslashes, len, hits, i, j;
+ char c;
+ int point;
+
+ lex->token.type = BC_LEX_NUMBER;
+ point = start == '.';
+ buffer = lex->buffer + lex->idx;
+ backslashes = 0;
+ i = 0;
+ c = buffer[i];
+
+ while (c && ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') ||
+ (c == '.' && !point) || (c == '\\' && buffer[i + 1] == '\n')))
+ {
+ if (c == '\\') {
+ ++i;
+ backslashes += 1;
+ }
+
+ c = buffer[++i];
+ }
+
+ len = i + 1 * (*(buffer + i - 1) != '.');
+
+ lex->token.string = malloc(len - backslashes * 2 + 1);
+ if (!lex->token.string) return BC_STATUS_MALLOC_FAIL;
+
+ lex->token.string[0] = start;
+ buf = buffer - 1;
+ hits = 0;
+
+ for (j = 1; j < len; ++j) {
+
+ c = buf[j];
+
+ // If we have hit a backslash, skip it.
+ // We don't have to check for a newline
+ // because it's guaranteed.
+ if (hits < backslashes && c == '\\') {
+ ++hits;
+ ++j;
+ continue;
+ }
+
+ lex->token.string[j - (hits * 2)] = c;
+ }
+
+ lex->token.string[j - (hits * 2)] = '\0';
+ lex->idx += i;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_lex_name(BcLex *lex) {
+
+ BcStatus status;
+ const char *buffer;
+ size_t i;
+ char c;
+
+ buffer = lex->buffer + lex->idx - 1;
+
+ for (i = 0; i < sizeof(bc_lex_keywords) / sizeof(bc_lex_keywords[0]); ++i) {
+
+ if (!strncmp(buffer, bc_lex_keywords[i].name, bc_lex_keywords[i].len)) {
+
+ lex->token.type = BC_LEX_KEY_AUTO + i;
+
+ if (!bc_lex_keywords[i].posix &&
+ (status = bc_posix_error(BC_STATUS_POSIX_BAD_KEYWORD, lex->file,
+ lex->line, bc_lex_keywords[i].name)))
+ {
+ return status;
+ }
+
+ // We need to minus one because the
+ // index has already been incremented.
+ lex->idx += bc_lex_keywords[i].len - 1;
+
+ return BC_STATUS_SUCCESS;
+ }
+ }
+
+ lex->token.type = BC_LEX_NAME;
+
+ i = 0;
+ c = buffer[i];
+
+ while ((c >= 'a' && c<= 'z') || (c >= '0' && c <= '9') || c == '_')
+ c = buffer[++i];
+
+ if (i > 1 && (status = bc_posix_error(BC_STATUS_POSIX_NAME_LEN,
+ lex->file, lex->line, buffer)))
+ {
+ return status;
+ }
+
+ if (!(lex->token.string = malloc(i + 1))) return BC_STATUS_MALLOC_FAIL;
+
+ strncpy(lex->token.string, buffer, i);
+ lex->token.string[i] = '\0';
+
+ // Increment the index. It is minus one
+ // because it has already been incremented.
+ lex->idx += i - 1;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_lex_token(BcLex *lex) {
+
+ BcStatus status = BC_STATUS_SUCCESS;
+ char c, c2;
+
+ // This is the workhorse of the lexer.
+ switch ((c = lex->buffer[lex->idx++])) {
+
+ case '\0':
+ case '\n':
+ {
+ lex->newline = 1;
+ lex->token.type = BC_LEX_NEWLINE + (!c) * (BC_LEX_EOF - BC_LEX_NEWLINE);
+ break;
+ }
+
+ case '\t':
+ case '\v':
+ case '\f':
+ case '\r':
+ case ' ':
+ case '\\':
+ {
+ lex->token.type = BC_LEX_WHITESPACE;
+ c = lex->buffer[lex->idx];
+
+ while ((isspace(c) && c != '\n') || c == '\\')
+ c = lex->buffer[++lex->idx];
+
+ break;
+ }
+
+ case '!':
+ {
+ c2 = lex->buffer[lex->idx];
+
+ if (c2 == '=') {
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_REL_NOT_EQ;
+ }
+ else {
+
+ if ((status = bc_posix_error(BC_STATUS_POSIX_BOOL_OPS,
+ lex->file, lex->line, "!")))
+ {
+ return status;
+ }
+
+ lex->token.type = BC_LEX_OP_BOOL_NOT;
+ }
+
+ break;
+ }
+
+ case '"':
+ {
+ status = bc_lex_string(lex);
+ break;
+ }
+
+ case '#':
+ {
+ if ((status = bc_posix_error(BC_STATUS_POSIX_SCRIPT_COMMENT,
+ lex->file, lex->line, NULL)))
+ {
+ return status;
+ }
+
+ lex->token.type = BC_LEX_WHITESPACE;
+ while (++lex->idx < lex->len && lex->buffer[lex->idx] != '\n');
+
+ break;
+ }
+
+ case '%':
+ {
+ if ((c2 = lex->buffer[lex->idx]) == '=') {
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_ASSIGN_MODULUS;
+ }
+ else lex->token.type = BC_LEX_OP_MODULUS;
+ break;
+ }
+
+ case '&':
+ {
+ if ((c2 = lex->buffer[lex->idx]) == '&') {
+
+ if ((status = bc_posix_error(BC_STATUS_POSIX_BOOL_OPS,
+ lex->file, lex->line, "&&")))
+ {
+ return status;
+ }
+
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_BOOL_AND;
+ }
+ else {
+ lex->token.type = BC_LEX_INVALID;
+ status = BC_STATUS_LEX_BAD_CHARACTER;
+ }
+
+ break;
+ }
+
+ case '(':
+ case ')':
+ {
+ lex->token.type = c - '(' + BC_LEX_LEFT_PAREN;
+ break;
+ }
+
+ case '*':
+ {
+ if ((c2 = lex->buffer[lex->idx]) == '=') {
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_ASSIGN_MULTIPLY;
+ }
+ else lex->token.type = BC_LEX_OP_MULTIPLY;
+ break;
+ }
+
+ case '+':
+ {
+ if ((c2 = lex->buffer[lex->idx]) == '=') {
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_ASSIGN_PLUS;
+ }
+ else if (c2 == '+') {
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_INC;
+ }
+ else lex->token.type = BC_LEX_OP_PLUS;
+ break;
+ }
+
+ case ',':
+ {
+ lex->token.type = BC_LEX_COMMA;
+ break;
+ }
+
+ case '-':
+ {
+ if ((c2 = lex->buffer[lex->idx]) == '=') {
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_ASSIGN_MINUS;
+ }
+ else if (c2 == '-') {
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_DEC;
+ }
+ else lex->token.type = BC_LEX_OP_MINUS;
+ break;
+ }
+
+ case '.':
+ {
+ c2 = lex->buffer[lex->idx];
+ if (isdigit(c2)) status = bc_lex_number(lex, c);
+ else {
+ status = bc_posix_error(BC_STATUS_POSIX_DOT_LAST,
+ lex->file, lex->line, NULL);
+ lex->token.type = BC_LEX_KEY_LAST;
+ }
+ break;
+ }
+
+ case '/':
+ {
+ if ((c2 = lex->buffer[lex->idx]) == '=') {
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_ASSIGN_DIVIDE;
+ }
+ else if (c2 == '*') status = bc_lex_comment(lex);
+ else lex->token.type = BC_LEX_OP_DIVIDE;
+ break;
+ }
+
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ {
+ status = bc_lex_number(lex, c);
+ break;
+ }
+
+ case ';':
+ {
+ lex->token.type = BC_LEX_SEMICOLON;
+ break;
+ }
+
+ case '<':
+ {
+ if ((c2 = lex->buffer[lex->idx]) == '=') {
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_REL_LESS_EQ;
+ }
+ else lex->token.type = BC_LEX_OP_REL_LESS;
+ break;
+ }
+
+ case '=':
+ {
+ if ((c2 = lex->buffer[lex->idx]) == '=') {
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_REL_EQUAL;
+ }
+ else lex->token.type = BC_LEX_OP_ASSIGN;
+ break;
+ }
+
+ case '>':
+ {
+ if ((c2 = lex->buffer[lex->idx]) == '=') {
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_REL_GREATER_EQ;
+ }
+ else lex->token.type = BC_LEX_OP_REL_GREATER;
+ break;
+ }
+
+ case 'A':
+ case 'B':
+ case 'C':
+ case 'D':
+ case 'E':
+ case 'F':
+ {
+ status = bc_lex_number(lex, c);
+ break;
+ }
+
+ case '[':
+ case ']':
+ {
+ lex->token.type = c - '[' + BC_LEX_LEFT_BRACKET;
+ break;
+ }
+
+ case '^':
+ {
+ if ((c2 = lex->buffer[lex->idx]) == '=') {
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_ASSIGN_POWER;
+ }
+ else lex->token.type = BC_LEX_OP_POWER;
+ break;
+ }
+
+ case 'a':
+ case 'b':
+ case 'c':
+ case 'd':
+ case 'e':
+ case 'f':
+ case 'g':
+ case 'h':
+ case 'i':
+ case 'j':
+ case 'k':
+ case 'l':
+ case 'm':
+ case 'n':
+ case 'o':
+ case 'p':
+ case 'q':
+ case 'r':
+ case 's':
+ case 't':
+ case 'u':
+ case 'v':
+ case 'w':
+ case 'x':
+ case 'y':
+ case 'z':
+ {
+ status = bc_lex_name(lex);
+ break;
+ }
+
+ case '{':
+ case '}':
+ {
+ lex->token.type = c - '{' + BC_LEX_LEFT_BRACE;
+ break;
+ }
+
+ case '|':
+ {
+ if ((c2 = lex->buffer[lex->idx]) == '|') {
+
+ if ((status = bc_posix_error(BC_STATUS_POSIX_BOOL_OPS,
+ lex->file, lex->line, "||")))
+ {
+ return status;
+ }
+
+ ++lex->idx;
+ lex->token.type = BC_LEX_OP_BOOL_OR;
+ }
+ else {
+ lex->token.type = BC_LEX_INVALID;
+ status = BC_STATUS_LEX_BAD_CHARACTER;
+ }
+
+ break;
+ }
+
+ default:
+ {
+ lex->token.type = BC_LEX_INVALID;
+ status = BC_STATUS_LEX_BAD_CHARACTER;
+ break;
+ }
+ }
+
+ return status;
+}
+
+void bc_lex_init(BcLex *lex, const char *file) {
+ lex->line = 1;
+ lex->newline = 0;
+ lex->file = file;
+}
+
+BcStatus bc_lex_next(BcLex *lex) {
+
+ BcStatus status;
+
+ if (lex->token.type == BC_LEX_EOF) return BC_STATUS_LEX_EOF;
+
+ if (lex->idx == lex->len) {
+ lex->newline = 1;
+ lex->token.type = BC_LEX_EOF;
+ return BC_STATUS_SUCCESS;
+ }
+
+ if (lex->newline) {
+ ++lex->line;
+ lex->newline = 0;
+ }
+
+ // Loop until failure or we don't have whitespace. This
+ // is so the parser doesn't get inundated with whitespace.
+ do {
+ lex->token.string = NULL;
+ status = bc_lex_token(lex);
+ } while (!status && lex->token.type == BC_LEX_WHITESPACE);
+
+ return status;
+}
+
+BcStatus bc_lex_text(BcLex *lex, const char *text) {
+ lex->buffer = text;
+ lex->idx = 0;
+ lex->len = strlen(text);
+ lex->token.type = BC_LEX_INVALID;
+ return bc_lex_next(lex);
+}
+
+BcStatus bc_parse_else(BcParse *p, BcVec *code);
+BcStatus bc_parse_stmt(BcParse *p, BcVec *code);
+
+BcStatus bc_parse_pushName(BcVec *code, char *name) {
+
+ BcStatus status;
+ size_t len, i;
+
+ status = BC_STATUS_SUCCESS;
+ len = strlen(name);
+
+ for (i = 0; !status && i < len; ++i)
+ status = bc_vec_pushByte(code, (uint8_t) name[i]);
+
+ if (status) return status;
+
+ free(name);
+
+ return bc_vec_pushByte(code, (uint8_t) ':');
+}
+
+BcStatus bc_parse_pushIndex(BcVec *code, size_t idx) {
+
+ BcStatus status;
+ uint8_t amt, i, nums[sizeof(size_t)];
+
+ for (amt = 0; idx; ++amt) {
+ nums[amt] = (uint8_t) idx;
+ idx = (idx & ~(UINT8_MAX)) >> sizeof(uint8_t) * CHAR_BIT;
+ }
+
+ if ((status = bc_vec_pushByte(code, amt))) return status;
+ for (i = 0; !status && i < amt; ++i) status = bc_vec_pushByte(code, nums[i]);
+
+ return status;
+}
+
+BcStatus bc_parse_operator(BcParse *p, BcVec *code, BcVec *ops, BcLexToken t,
+ uint32_t *num_exprs, int next)
+{
+ BcStatus status;
+ BcLexToken top;
+ uint8_t lp, rp;
+ int rleft;
+
+ rp = bc_parse_ops[t].prec;
+ rleft = bc_parse_ops[t].left;
+
+ while (ops->len &&
+ (top = *((BcLexToken*) bc_vec_top(ops))) != BC_LEX_LEFT_PAREN &&
+ ((lp = bc_parse_ops[top].prec) < rp || (lp == rp && rleft)))
+ {
+ status = bc_vec_pushByte(code, BC_PARSE_TOKEN_TO_INST(top));
+ if (status) return status;
+
+ bc_vec_pop(ops);
+
+ *num_exprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_OP_NEG;
+ }
+
+ if ((status = bc_vec_push(ops, &t))) return status;
+ if (next && (status = bc_lex_next(&p->lex)) && p->lex.token.string) {
+ free(p->lex.token.string);
+ p->lex.token.string = NULL;
+ }
+
+ return status;
+}
+
+BcStatus bc_parse_rightParen(BcParse *p, BcVec *code,
+ BcVec *ops, uint32_t *nexs)
+{
+ BcStatus status;
+ BcLexToken top;
+
+ if (!ops->len) return BC_STATUS_PARSE_BAD_EXPR;
+
+ while ((top = *((BcLexToken*) bc_vec_top(ops))) != BC_LEX_LEFT_PAREN) {
+
+ status = bc_vec_pushByte(code, BC_PARSE_TOKEN_TO_INST(top));
+ if (status) return status;
+
+ bc_vec_pop(ops);
+ *nexs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_OP_NEG;
+
+ if (!ops->len) return BC_STATUS_PARSE_BAD_EXPR;
+ }
+
+ bc_vec_pop(ops);
+
+ return bc_lex_next(&p->lex);
+}
+
+BcStatus bc_parse_params(BcParse *p, BcVec *code, uint8_t flags) {
+
+ BcStatus status;
+ int comma = 0;
+ size_t nparams;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ for (nparams = 0; p->lex.token.type != BC_LEX_RIGHT_PAREN; ++nparams) {
+
+ status = bc_parse_expr(p, code, flags & ~(BC_PARSE_EXPR_PRINT));
+ if (status) return status;
+
+ if (p->lex.token.type == BC_LEX_COMMA) {
+ comma = 1;
+ if ((status = bc_lex_next(&p->lex))) return status;
+ }
+ else comma = 0;
+ }
+
+ if (comma) return BC_STATUS_PARSE_BAD_TOKEN;
+ if ((status = bc_vec_pushByte(code, BC_INST_CALL))) return status;
+
+ return bc_parse_pushIndex(code, nparams);
+}
+
+BcStatus bc_parse_call(BcParse *p, BcVec *code, char *name, uint8_t flags) {
+
+ BcStatus status;
+ BcEntry entry, *entry_ptr;
+ size_t idx;
+
+ entry.name = name;
+
+ if ((status = bc_parse_params(p, code, flags))) goto err;
+
+ if (p->lex.token.type != BC_LEX_RIGHT_PAREN) {
+ status = BC_STATUS_PARSE_BAD_TOKEN;
+ goto err;
+ }
+
+ idx = bc_veco_index(&p->prog->func_map, &entry);
+
+ if (idx == -1) {
+ if ((status = bc_program_addFunc(p->prog, name, &idx))) return status;
+ name = NULL;
+ }
+ else free(name);
+
+ entry_ptr = bc_veco_item(&p->prog->func_map, idx);
+ if ((status = bc_parse_pushIndex(code, entry_ptr->idx))) return status;
+
+ return bc_lex_next(&p->lex);
+
+err:
+ if (name) free(name);
+ return status;
+}
+
+BcStatus bc_parse_name(BcParse *p, BcVec *code, BcInst *type, uint8_t flags)
+{
+ BcStatus status;
+ char *name;
+
+ name = p->lex.token.string;
+
+ if ((status = bc_lex_next(&p->lex))) goto err;
+
+ if (p->lex.token.type == BC_LEX_LEFT_BRACKET) {
+
+ *type = BC_INST_PUSH_ARRAY_ELEM;
+
+ if ((status = bc_lex_next(&p->lex))) goto err;
+ if ((status = bc_parse_expr(p, code, flags))) goto err;
+
+ if (p->lex.token.type != BC_LEX_RIGHT_BRACKET) {
+ status = BC_STATUS_PARSE_BAD_TOKEN;
+ goto err;
+ }
+
+ if ((status = bc_vec_pushByte(code, BC_INST_PUSH_ARRAY_ELEM))) goto err;
+
+ status = bc_parse_pushName(code, name);
+ }
+ else if (p->lex.token.type == BC_LEX_LEFT_PAREN) {
+
+ if (flags & BC_PARSE_EXPR_NOCALL) {
+ status = BC_STATUS_PARSE_BAD_TOKEN;
+ goto err;
+ }
+
+ *type = BC_INST_CALL;
+ status = bc_parse_call(p, code, name, flags);
+ }
+ else {
+ *type = BC_INST_PUSH_VAR;
+ if ((status = bc_vec_pushByte(code, BC_INST_PUSH_VAR))) goto err;
+ status = bc_parse_pushName(code, name);
+ }
+
+ return status;
+
+err:
+ free(name);
+ return status;
+}
+
+BcStatus bc_parse_read(BcParse *p, BcVec *code) {
+
+ BcStatus status;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ if (p->lex.token.type != BC_LEX_LEFT_PAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ if (p->lex.token.type != BC_LEX_RIGHT_PAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ if ((status = bc_vec_pushByte(code, BC_INST_READ))) return status;
+
+ return bc_lex_next(&p->lex);
+}
+
+BcStatus bc_parse_builtin(BcParse *p, BcVec *code,
+ BcLexToken type, uint8_t flags)
+{
+ BcStatus status;
+ uint8_t inst;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ if (p->lex.token.type != BC_LEX_LEFT_PAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ status = bc_parse_expr(p, code, flags & ~(BC_PARSE_EXPR_PRINT));
+ if (status) return status;
+ if (p->lex.token.type != BC_LEX_RIGHT_PAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ inst = type == BC_LEX_KEY_LENGTH ? BC_INST_LENGTH : BC_INST_SQRT;
+ if ((status = bc_vec_pushByte(code, inst))) return status;
+
+ return bc_lex_next(&p->lex);
+}
+
+BcStatus bc_parse_scale(BcParse *p, BcVec *code, BcInst *type, uint8_t flags) {
+
+ BcStatus status;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ if (p->lex.token.type != BC_LEX_LEFT_PAREN) {
+ *type = BC_INST_PUSH_SCALE;
+ return bc_vec_pushByte(code, BC_INST_PUSH_SCALE);
+ }
+
+ *type = BC_INST_SCALE_FUNC;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ if ((status = bc_parse_expr(p, code, flags))) return status;
+ if (p->lex.token.type != BC_LEX_RIGHT_PAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+ if ((status = bc_vec_pushByte(code, BC_INST_SCALE_FUNC))) return status;
+
+ return bc_lex_next(&p->lex);
+}
+
+BcStatus bc_parse_incdec(BcParse *p, BcVec *code, BcInst *prev,
+ uint32_t *nexprs, uint8_t flags)
+{
+ BcStatus status;
+ BcLexToken type;
+ BcInst etype;
+ uint8_t inst;
+
+ etype = *prev;
+
+ if (etype == BC_INST_PUSH_VAR || etype == BC_INST_PUSH_ARRAY_ELEM ||
+ etype == BC_INST_PUSH_SCALE || etype == BC_INST_PUSH_LAST ||
+ etype == BC_INST_PUSH_IBASE || etype == BC_INST_PUSH_OBASE)
+ {
+ *prev = inst = BC_INST_INC_POST + (p->lex.token.type != BC_LEX_OP_INC);
+ if ((status = bc_vec_pushByte(code, inst))) return status;
+ status = bc_lex_next(&p->lex);
+ }
+ else {
+
+ *prev = inst = BC_INST_INC_PRE + (p->lex.token.type != BC_LEX_OP_INC);
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ type = p->lex.token.type;
+
+ // Because we parse the next part of the expression
+ // right here, we need to increment this.
+ *nexprs = *nexprs + 1;
+
+ switch (type) {
+
+ case BC_LEX_NAME:
+ {
+ status = bc_parse_name(p, code, prev, flags | BC_PARSE_EXPR_NOCALL);
+ break;
+ }
+
+ case BC_LEX_KEY_IBASE:
+ {
+ if ((status = bc_vec_pushByte(code, BC_INST_PUSH_IBASE))) return status;
+ status = bc_lex_next(&p->lex);
+ break;
+ }
+
+ case BC_LEX_KEY_LAST:
+ {
+ if ((status = bc_vec_pushByte(code, BC_INST_PUSH_LAST))) return status;
+ status = bc_lex_next(&p->lex);
+ break;
+ }
+
+ case BC_LEX_KEY_OBASE:
+ {
+ if ((status = bc_vec_pushByte(code, BC_INST_PUSH_OBASE))) return status;
+ status = bc_lex_next(&p->lex);
+ break;
+ }
+
+ case BC_LEX_KEY_SCALE:
+ {
+ if ((status = bc_lex_next(&p->lex))) return status;
+ if (p->lex.token.type == BC_LEX_LEFT_PAREN)
+ return BC_STATUS_PARSE_BAD_TOKEN;
+
+ status = bc_vec_pushByte(code, BC_INST_PUSH_SCALE);
+
+ break;
+ }
+
+ default:
+ {
+ return BC_STATUS_PARSE_BAD_TOKEN;
+ }
+ }
+
+ if (status) return status;
+ status = bc_vec_pushByte(code, inst);
+ }
+
+ return status;
+}
+
+BcStatus bc_parse_minus(BcParse *p, BcVec *exs, BcVec *ops, BcInst *prev,
+ int rparen, uint32_t *nexprs)
+{
+ BcStatus status;
+ BcLexToken type;
+ BcInst etype;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ etype = *prev;
+ type = p->lex.token.type;
+
+ if (type != BC_LEX_NAME && type != BC_LEX_NUMBER &&
+ type != BC_LEX_KEY_SCALE && type != BC_LEX_KEY_LAST &&
+ type != BC_LEX_KEY_IBASE && type != BC_LEX_KEY_OBASE &&
+ type != BC_LEX_LEFT_PAREN && type != BC_LEX_OP_MINUS &&
+ type != BC_LEX_OP_INC && type != BC_LEX_OP_DEC &&
+ type != BC_LEX_OP_BOOL_NOT)
+ {
+ return BC_STATUS_PARSE_BAD_TOKEN;
+ }
+
+ type = rparen || etype == BC_INST_INC_POST || etype == BC_INST_DEC_POST ||
+ (etype >= BC_INST_PUSH_NUM && etype <= BC_INST_SQRT) ?
+ BC_LEX_OP_MINUS : BC_LEX_OP_NEG;
+ *prev = BC_PARSE_TOKEN_TO_INST(type);
+
+ if (type == BC_LEX_OP_MINUS)
+ status = bc_parse_operator(p, exs, ops, type, nexprs, 0);
+ else
+ // We can just push onto the op stack because this is the largest
+ // precedence operator that gets pushed. Inc/dec does not.
+ status = bc_vec_push(ops, &type);
+
+ return status;
+}
+
+BcStatus bc_parse_string(BcParse *p, BcVec *code) {
+
+ BcStatus status;
+ size_t len;
+
+ if (strlen(p->lex.token.string) > (unsigned long) maxof_STRING) {
+ status = BC_STATUS_EXEC_STRING_LEN;
+ goto err;
+ }
+
+ len = p->prog->strings.len;
+
+ if ((status = bc_vec_push(&p->prog->strings, &p->lex.token.string))) goto err;
+ if ((status = bc_vec_pushByte(code, BC_INST_STR))) return status;
+ if ((status = bc_parse_pushIndex(code, len))) return status;
+
+ return bc_lex_next(&p->lex);
+
+err:
+ free(p->lex.token.string);
+ return status;
+}
+
+BcStatus bc_parse_print(BcParse *p, BcVec *code) {
+
+ BcStatus status;
+ BcLexToken type;
+ int comma;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ type = p->lex.token.type;
+
+ if (type == BC_LEX_SEMICOLON || type == BC_LEX_NEWLINE)
+ return BC_STATUS_PARSE_BAD_PRINT;
+
+ comma = 0;
+
+ while (!status && type != BC_LEX_SEMICOLON && type != BC_LEX_NEWLINE) {
+
+ if (type == BC_LEX_STRING) {
+
+ size_t len = p->prog->strings.len;
+
+ status = bc_vec_push(&p->prog->strings, &p->lex.token.string);
+ if (status) {
+ free(p->lex.token.string);
+ return status;
+ }
+
+ if ((status = bc_vec_pushByte(code, BC_INST_PRINT_STR))) return status;
+ status = bc_parse_pushIndex(code, len);
+ }
+ else {
+ if ((status = bc_parse_expr(p, code, 0))) return status;
+ status = bc_vec_pushByte(code, BC_INST_PRINT_EXPR);
+ }
+
+ if (status) return status;
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ if (p->lex.token.type == BC_LEX_COMMA) {
+ comma = 1;
+ status = bc_lex_next(&p->lex);
+ }
+ else comma = 0;
+
+ type = p->lex.token.type;
+ }
+
+ if (status) return status;
+ if (comma) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ return bc_lex_next(&p->lex);
+}
+
+BcStatus bc_parse_return(BcParse *p, BcVec *code) {
+
+ BcStatus status;
+
+ if (!BC_PARSE_FUNC(p)) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ if (p->lex.token.type != BC_LEX_NEWLINE &&
+ p->lex.token.type != BC_LEX_SEMICOLON &&
+ p->lex.token.type != BC_LEX_LEFT_PAREN &&
+ (status = bc_posix_error(BC_STATUS_POSIX_RETURN_PARENS,
+ p->lex.file, p->lex.line, NULL)))
+ {
+ return status;
+ }
+
+ if (p->lex.token.type == BC_LEX_NEWLINE ||
+ p->lex.token.type == BC_LEX_SEMICOLON)
+ {
+ status = bc_vec_pushByte(code, BC_INST_RETURN_ZERO);
+ }
+ else {
+ if ((status = bc_parse_expr(p, code, 0))) return status;
+ status = bc_vec_pushByte(code, BC_INST_RETURN);
+ }
+
+ return status;
+}
+
+BcStatus bc_parse_endBody(BcParse *p, BcVec *code, int brace) {
+
+ BcStatus status = BC_STATUS_SUCCESS;
+ uint8_t *flag_ptr;
+
+ if (p->flags.len <= 1 || p->num_braces == 0) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ if (brace) {
+
+ if (p->lex.token.type == BC_LEX_RIGHT_BRACE) {
+
+ if (!p->num_braces) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ --p->num_braces;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ }
+ else return BC_STATUS_PARSE_BAD_TOKEN;
+ }
+
+ if (BC_PARSE_IF(p)) {
+
+ while (p->lex.token.type == BC_LEX_NEWLINE) {
+ if ((status = bc_lex_next(&p->lex))) return status;
+ }
+
+ bc_vec_pop(&p->flags);
+
+ flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
+ *flag_ptr = (*flag_ptr | BC_PARSE_FLAG_IF_END);
+
+ if (p->lex.token.type == BC_LEX_KEY_ELSE) status = bc_parse_else(p, code);
+ }
+ else if (BC_PARSE_ELSE(p)) {
+
+ BcInstPtr *ip;
+ BcFunc *func;
+ size_t *label;
+
+ bc_vec_pop(&p->flags);
+
+ ip = bc_vec_top(&p->exits);
+ func = bc_vec_item(&p->prog->funcs, p->func);
+ label = bc_vec_item(&func->labels, ip->idx);
+ *label = code->len;
+
+ bc_vec_pop(&p->exits);
+ }
+ else if (BC_PARSE_FUNC_INNER(p)) {
+ p->func = 0;
+ if ((status = bc_vec_pushByte(code, BC_INST_RETURN_ZERO))) return status;
+ bc_vec_pop(&p->flags);
+ }
+ else {
+
+ BcInstPtr *ip;
+ BcFunc *func;
+ size_t *label;
+
+ if ((status = bc_vec_pushByte(code, BC_INST_JUMP))) return status;
+
+ ip = bc_vec_top(&p->exits);
+ label = bc_vec_top(&p->conds);
+
+ if ((status = bc_parse_pushIndex(code, *label))) return status;
+
+ func = bc_vec_item(&p->prog->funcs, p->func);
+ label = bc_vec_item(&func->labels, ip->idx);
+ *label = code->len;
+
+ bc_vec_pop(&p->flags);
+ bc_vec_pop(&p->exits);
+ bc_vec_pop(&p->conds);
+ }
+
+ return status;
+}
+
+BcStatus bc_parse_startBody(BcParse *p, uint8_t flags) {
+ uint8_t *flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
+ flags |= (*flag_ptr & (BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_LOOP));
+ flags |= BC_PARSE_FLAG_BODY;
+ return bc_vec_push(&p->flags, &flags);
+}
+
+void bc_parse_noElse(BcParse *p, BcVec *code) {
+
+ uint8_t *flag_ptr;
+ BcInstPtr *ip;
+ BcFunc *func;
+ size_t *label;
+
+ flag_ptr = BC_PARSE_TOP_FLAG_PTR(p);
+ *flag_ptr = (*flag_ptr & ~(BC_PARSE_FLAG_IF_END));
+
+ ip = bc_vec_top(&p->exits);
+ func = bc_vec_item(&p->prog->funcs, p->func);
+ label = bc_vec_item(&func->labels, ip->idx);
+ *label = code->len;
+
+ bc_vec_pop(&p->exits);
+}
+
+BcStatus bc_parse_if(BcParse *p, BcVec *code) {
+
+ BcStatus status;
+ BcInstPtr ip;
+ BcFunc *func;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ if (p->lex.token.type != BC_LEX_LEFT_PAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ status = bc_parse_expr(p, code, BC_PARSE_EXPR_POSIX_REL);
+ if (status) return status;
+ if (p->lex.token.type != BC_LEX_RIGHT_PAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ if ((status = bc_vec_pushByte(code, BC_INST_JUMP_ZERO))) return status;
+
+ func = bc_vec_item(&p->prog->funcs, p->func);
+
+ ip.idx = func->labels.len;
+ ip.func = 0;
+ ip.len = 0;
+
+ if ((status = bc_parse_pushIndex(code, ip.idx))) return status;
+ if ((status = bc_vec_push(&p->exits, &ip))) return status;
+ if ((status = bc_vec_push(&func->labels, &ip.idx))) return status;
+
+ return bc_parse_startBody(p, BC_PARSE_FLAG_IF);
+}
+
+BcStatus bc_parse_else(BcParse *p, BcVec *code) {
+
+ BcStatus status;
+ BcInstPtr ip;
+ BcFunc *func;
+
+ if (!BC_PARSE_IF_END(p)) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ func = bc_vec_item(&p->prog->funcs, p->func);
+
+ ip.idx = func->labels.len;
+ ip.func = 0;
+ ip.len = 0;
+
+ if ((status = bc_vec_pushByte(code, BC_INST_JUMP))) return status;
+ if ((status = bc_parse_pushIndex(code, ip.idx))) return status;
+
+ bc_parse_noElse(p, code);
+
+ if ((status = bc_vec_push(&p->exits, &ip))) return status;
+ if ((status = bc_vec_push(&func->labels, &ip.idx))) return status;
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ return bc_parse_startBody(p, BC_PARSE_FLAG_ELSE);
+}
+
+BcStatus bc_parse_while(BcParse *p, BcVec *code) {
+
+ BcStatus status;
+ BcFunc *func;
+ BcInstPtr ip;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ if (p->lex.token.type != BC_LEX_LEFT_PAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ func = bc_vec_item(&p->prog->funcs, p->func);
+ ip.idx = func->labels.len;
+
+ if ((status = bc_vec_push(&func->labels, &code->len))) return status;
+ if ((status = bc_vec_push(&p->conds, &ip.idx))) return status;
+
+ ip.idx = func->labels.len;
+ ip.func = 1;
+ ip.len = 0;
+
+ if ((status = bc_vec_push(&p->exits, &ip))) return status;
+ if ((status = bc_vec_push(&func->labels, &ip.idx))) return status;
+
+ if ((status = bc_parse_expr(p, code, BC_PARSE_EXPR_POSIX_REL))) return status;
+ if (p->lex.token.type != BC_LEX_RIGHT_PAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ if ((status = bc_vec_pushByte(code, BC_INST_JUMP_ZERO))) return status;
+ if ((status = bc_parse_pushIndex(code, ip.idx))) return status;
+
+ return bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
+}
+
+BcStatus bc_parse_for(BcParse *p, BcVec *code) {
+
+ BcStatus status;
+ BcFunc *func;
+ BcInstPtr ip;
+ size_t cond_idx, exit_idx, body_idx, update_idx;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ if (p->lex.token.type != BC_LEX_LEFT_PAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ if (p->lex.token.type != BC_LEX_SEMICOLON)
+ status = bc_parse_expr(p, code, 0);
+ else
+ status = bc_posix_error(BC_STATUS_POSIX_NO_FOR_INIT,
+ p->lex.file, p->lex.line, NULL);
+
+ if (status) return status;
+ if (p->lex.token.type != BC_LEX_SEMICOLON) return BC_STATUS_PARSE_BAD_TOKEN;
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ func = bc_vec_item(&p->prog->funcs, p->func);
+
+ cond_idx = func->labels.len;
+ update_idx = cond_idx + 1;
+ body_idx = update_idx + 1;
+ exit_idx = body_idx + 1;
+
+ if ((status = bc_vec_push(&func->labels, &code->len))) return status;
+
+ if (p->lex.token.type != BC_LEX_SEMICOLON)
+ status = bc_parse_expr(p, code, BC_PARSE_EXPR_POSIX_REL);
+ else status = bc_posix_error(BC_STATUS_POSIX_NO_FOR_COND,
+ p->lex.file, p->lex.line, NULL);
+
+ if (status) return status;
+ if (p->lex.token.type != BC_LEX_SEMICOLON) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ if ((status = bc_vec_pushByte(code, BC_INST_JUMP_ZERO))) return status;
+ if ((status = bc_parse_pushIndex(code, exit_idx))) return status;
+ if ((status = bc_vec_pushByte(code, BC_INST_JUMP))) return status;
+ if ((status = bc_parse_pushIndex(code, body_idx))) return status;
+
+ ip.idx = func->labels.len;
+
+ if ((status = bc_vec_push(&p->conds, &update_idx))) return status;
+ if ((status = bc_vec_push(&func->labels, &code->len))) return status;
+
+ if (p->lex.token.type != BC_LEX_RIGHT_PAREN)
+ status = bc_parse_expr(p, code, 0);
+ else
+ status = bc_posix_error(BC_STATUS_POSIX_NO_FOR_UPDATE,
+ p->lex.file, p->lex.line, NULL);
+
+ if (status) return status;
+
+ if (p->lex.token.type != BC_LEX_RIGHT_PAREN) {
+ status = bc_parse_expr(p, code, BC_PARSE_EXPR_POSIX_REL);
+ if (status) return status;
+ }
+
+ if (p->lex.token.type != BC_LEX_RIGHT_PAREN) return BC_STATUS_PARSE_BAD_TOKEN;
+ if ((status = bc_vec_pushByte(code, BC_INST_JUMP))) return status;
+ if ((status = bc_parse_pushIndex(code, cond_idx))) return status;
+ if ((status = bc_vec_push(&func->labels, &code->len))) return status;
+
+ ip.idx = exit_idx;
+ ip.func = 1;
+ ip.len = 0;
+
+ if ((status = bc_vec_push(&p->exits, &ip))) return status;
+ if ((status = bc_vec_push(&func->labels, &ip.idx))) return status;
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ return bc_parse_startBody(p, BC_PARSE_FLAG_LOOP | BC_PARSE_FLAG_LOOP_INNER);
+}
+
+BcStatus bc_parse_loopExit(BcParse *p, BcVec *code, BcLexToken type) {
+
+ BcStatus status;
+ size_t idx, top;
+ BcInstPtr *ip;
+
+ if (!BC_PARSE_LOOP(p)) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ if (type == BC_LEX_KEY_BREAK) {
+
+ if (!p->exits.len) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ top = p->exits.len - 1;
+ ip = bc_vec_item(&p->exits, top);
+
+ while (top < p->exits.len && ip && !ip->func)
+ ip = bc_vec_item(&p->exits, top--);
+
+ if (top >= p->exits.len || !ip) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ idx = ip->idx;
+ }
+ else idx = *((size_t*) bc_vec_top(&p->conds));
+
+ if ((status = bc_vec_pushByte(code, BC_INST_JUMP))) return status;
+ if ((status = bc_parse_pushIndex(code, idx))) return status;
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ if (p->lex.token.type != BC_LEX_SEMICOLON &&
+ p->lex.token.type != BC_LEX_NEWLINE)
+ {
+ return BC_STATUS_PARSE_BAD_TOKEN;
+ }
+
+ return bc_lex_next(&p->lex);
+}
+
+BcStatus bc_parse_func(BcParse *p) {
+
+ BcStatus status;
+ BcFunc *fptr;
+ int var, comma = 0;
+ uint8_t flags;
+ char *name;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ name = p->lex.token.string;
+
+ if (p->lex.token.type != BC_LEX_NAME) {
+ status = BC_STATUS_PARSE_BAD_FUNC;
+ goto err;
+ }
+
+ status = bc_program_addFunc(p->prog, name, &p->func);
+ if (status) goto err;
+
+ fptr = bc_vec_item(&p->prog->funcs, p->func);
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+ if (p->lex.token.type != BC_LEX_LEFT_PAREN) return BC_STATUS_PARSE_BAD_FUNC;
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ while (!status && p->lex.token.type != BC_LEX_RIGHT_PAREN) {
+
+ if (p->lex.token.type != BC_LEX_NAME) {
+ status = BC_STATUS_PARSE_BAD_FUNC;
+ goto err;
+ }
+
+ ++fptr->nparams;
+ name = p->lex.token.string;
+
+ if ((status = bc_lex_next(&p->lex))) goto err;
+
+ var = p->lex.token.type != BC_LEX_LEFT_BRACKET;
+
+ if (!var) {
+ if ((status = bc_lex_next(&p->lex))) goto err;
+ if (p->lex.token.type != BC_LEX_RIGHT_BRACKET)
+ return BC_STATUS_PARSE_BAD_FUNC;
+ if ((status = bc_lex_next(&p->lex))) goto err;
+ }
+
+ comma = p->lex.token.type == BC_LEX_COMMA;
+ if (comma && (status = bc_lex_next(&p->lex))) goto err;
+
+ if ((status = bc_func_insert(fptr, name, var))) goto err;
+ }
+
+ if (comma) return BC_STATUS_PARSE_BAD_FUNC;
+
+ flags = BC_PARSE_FLAG_FUNC | BC_PARSE_FLAG_FUNC_INNER | BC_PARSE_FLAG_BODY;
+
+ if ((status = bc_parse_startBody(p, flags))) return status;
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ if (p->lex.token.type != BC_LEX_LEFT_BRACE)
+ return bc_posix_error(BC_STATUS_POSIX_HEADER_BRACE,
+ p->lex.file, p->lex.line, NULL);
+
+ return status;
+
+err:
+ free(name);
+ return status;
+}
+
+BcStatus bc_parse_auto(BcParse *p) {
+
+ BcStatus status;
+ int comma, var, one;
+ char *name;
+ BcFunc *func;
+
+ if (!p->auto_part) return BC_STATUS_PARSE_BAD_TOKEN;
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ p->auto_part = comma = one = 0;
+ func = bc_vec_item(&p->prog->funcs, p->func);
+
+ while (!status && p->lex.token.type == BC_LEX_NAME) {
+
+ name = p->lex.token.string;
+
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ one = 1;
+
+ var = p->lex.token.type != BC_LEX_LEFT_BRACKET;
+
+ if (!var) {
+
+ if ((status = bc_lex_next(&p->lex))) goto err;
+ if (p->lex.token.type != BC_LEX_RIGHT_BRACKET)
+ return BC_STATUS_PARSE_BAD_FUNC;
+
+ if ((status = bc_lex_next(&p->lex))) goto err;
+ }
+
+ comma = p->lex.token.type == BC_LEX_COMMA;
+ if (comma && (status = bc_lex_next(&p->lex))) goto err;
+
+ if ((status = bc_func_insert(func, name, var))) goto err;
+ }
+
+ if (status) return status;
+ if (comma) return BC_STATUS_PARSE_BAD_FUNC;
+ if (!one) return BC_STATUS_PARSE_NO_AUTO;
+
+ if (p->lex.token.type != BC_LEX_NEWLINE &&
+ p->lex.token.type != BC_LEX_SEMICOLON)
+ {
+ return BC_STATUS_PARSE_BAD_TOKEN;
+ }
+
+ return bc_lex_next(&p->lex);
+
+err:
+ free(name);
+ return status;
+}
+
+BcStatus bc_parse_body(BcParse *p, BcVec *code, int brace) {
+
+ BcStatus status;
+ uint8_t *flag_ptr;
+
+ flag_ptr = bc_vec_top(&p->flags);
+ *flag_ptr &= ~(BC_PARSE_FLAG_BODY);
+
+ if (*flag_ptr & BC_PARSE_FLAG_FUNC_INNER) {
+ if (!brace) return BC_STATUS_PARSE_BAD_TOKEN;
+ p->auto_part = 1;
+ status = bc_lex_next(&p->lex);
+ }
+ else {
+ if ((status = bc_parse_stmt(p, code))) return status;
+ if (!brace) status = bc_parse_endBody(p, code, 0);
+ }
+
+ return status;
+}
+
+BcStatus bc_parse_stmt(BcParse *p, BcVec *code) {
+
+ BcStatus status;
+
+ switch (p->lex.token.type) {
+
+ case BC_LEX_NEWLINE:
+ {
+ return bc_lex_next(&p->lex);
+ }
+
+ case BC_LEX_KEY_ELSE:
+ {
+ p->auto_part = 0;
+ break;
+ }
+
+ case BC_LEX_LEFT_BRACE:
+ {
+ if (!BC_PARSE_BODY(p)) return BC_STATUS_PARSE_BAD_TOKEN;
+
+ ++p->num_braces;
+ if ((status = bc_lex_next(&p->lex))) return status;
+
+ return bc_parse_body(p, code, 1);
+ }
+
+ case BC_LEX_KEY_AUTO:
+ {
+ return bc_parse_auto(p);
+ }
+
+ default:
+ {
+ p->auto_part = 0;
+
+ if (BC_PARSE_IF_END(p)) {
+ bc_parse_noElse(p, code);
+ return BC_STATUS_SUCCESS;
+ }
+ else if (BC_PARSE_BODY(p)) return bc_parse_body(p, code, 0);
+
+ break;
+ }
+ }
+
+ switch (p->lex.token.type) {
+
+ case BC_LEX_OP_INC:
+ case BC_LEX_OP_DEC:
+ case BC_LEX_OP_MINUS:
+ case BC_LEX_OP_BOOL_NOT:
+ case BC_LEX_LEFT_PAREN:
+ case BC_LEX_NAME:
+ case BC_LEX_NUMBER:
+ case BC_LEX_KEY_IBASE:
+ case BC_LEX_KEY_LAST:
+ case BC_LEX_KEY_LENGTH:
+ case BC_LEX_KEY_OBASE:
+ case BC_LEX_KEY_READ:
+ case BC_LEX_KEY_SCALE:
+ case BC_LEX_KEY_SQRT:
+ {
+ status = bc_parse_expr(p, code, BC_PARSE_EXPR_PRINT);
+ break;
+ }
+
+ case BC_LEX_KEY_ELSE:
+ {
+ status = bc_parse_else(p, code);
+ break;
+ }
+
+ case BC_LEX_SEMICOLON:
+ {
+ status = BC_STATUS_SUCCESS;
+
+ while (!status && p->lex.token.type == BC_LEX_SEMICOLON)
+ status = bc_lex_next(&p->lex);
+
+ break;
+ }
+
+ case BC_LEX_RIGHT_BRACE:
+ {
+ status = bc_parse_endBody(p, code, 1);
+ break;
+ }
+
+ case BC_LEX_STRING:
+ {
+ status = bc_parse_string(p, code);
+ break;
+ }
+
+ case BC_LEX_KEY_BREAK:
+ case BC_LEX_KEY_CONTINUE:
+ {
+ status = bc_parse_loopExit(p, code, p->lex.token.type);
+ break;
+ }
+
+ case BC_LEX_KEY_FOR:
+ {
+ status = bc_parse_for(p, code);
+ break;
+ }
+
+ case BC_LEX_KEY_HALT:
+ {
+ if ((status = bc_vec_pushByte(code, BC_INST_HALT))) return status;
+ status = bc_lex_next(&p->lex);
+ break;
+ }
+
+ case BC_LEX_KEY_IF:
+ {
+ status = bc_parse_if(p, code);
+ break;
+ }
+
+ case BC_LEX_KEY_LIMITS:
+ {
+ if ((status = bc_lex_next(&p->lex))) return status;
+ status = BC_STATUS_LIMITS;
+ break;
+ }
+
+ case BC_LEX_KEY_PRINT:
+ {
+ status = bc_parse_print(p, code);
+ break;
+ }
+
+ case BC_LEX_KEY_QUIT:
+ {
+ // Quit is a compile-time command, so we send an exit command. We don't
+ // exit directly, so the vm can clean up. Limits do the same thing.
+ status = BC_STATUS_QUIT;
+ break;
+ }
+
+ case BC_LEX_KEY_RETURN:
+ {
+ if ((status = bc_parse_return(p, code))) return status;
+ break;
+ }
+
+ case BC_LEX_KEY_WHILE:
+ {
+ status = bc_parse_while(p, code);
+ break;
+ }
+
+ case BC_LEX_EOF:
+ {
+ status = (p->flags.len > 0) * BC_STATUS_LEX_BAD_CHARACTER;
+ break;
+ }
+
+ default:
+ {
+ status = BC_STATUS_PARSE_BAD_TOKEN;
+ break;
+ }
+ }
+
+ return status;
+}
+
+BcStatus bc_parse_init(BcParse *p, BcProgram *program) {
+
+ BcStatus status;
+ uint8_t flags = 0;
+
+ if ((status = bc_vec_init(&p->flags, sizeof(uint8_t), NULL))) return status;
+ if ((status = bc_vec_init(&p->exits, sizeof(BcInstPtr), NULL))) goto exit_err;
+ if ((status = bc_vec_init(&p->conds, sizeof(size_t), NULL))) goto cond_err;
+ if ((status = bc_vec_push(&p->flags, &flags))) goto push_err;
+ if ((status = bc_vec_init(&p->ops, sizeof(BcLexToken), NULL))) goto push_err;
+
+ p->prog = program;
+ p->func = p->num_braces = 0;
+ p->auto_part = 0;
+
+ return status;
+
+push_err:
+ bc_vec_free(&p->conds);
+cond_err:
+ bc_vec_free(&p->exits);
+exit_err:
+ bc_vec_free(&p->flags);
+ return status;
+}
+
+BcStatus bc_parse_parse(BcParse *p) {
+
+ BcStatus status;
+
+ if (p->lex.token.type == BC_LEX_EOF) status = BC_STATUS_LEX_EOF;
+ else if (p->lex.token.type == BC_LEX_KEY_DEFINE) {
+ if (!BC_PARSE_CAN_EXEC(p)) return BC_STATUS_PARSE_BAD_TOKEN;
+ status = bc_parse_func(p);
+ }
+ else {
+ BcFunc *func = bc_vec_item(&p->prog->funcs, p->func);
+ status = bc_parse_stmt(p, &func->code);
+ }
+
+ if (status || TT.signe) {
+
+ if (p->func) {
+
+ BcFunc *func = bc_vec_item(&p->prog->funcs, p->func);
+
+ func->nparams = 0;
+ bc_vec_npop(&func->code, func->code.len);
+ bc_vec_npop(&func->autos, func->autos.len);
+ bc_vec_npop(&func->labels, func->labels.len);
+
+ p->func = 0;
+ }
+
+ p->lex.idx = p->lex.len;
+ p->lex.token.type = BC_LEX_EOF;
+ p->auto_part = 0;
+ p->num_braces = 0;
+
+ bc_vec_npop(&p->flags, p->flags.len - 1);
+ bc_vec_npop(&p->exits, p->exits.len);
+ bc_vec_npop(&p->conds, p->conds.len);
+ bc_vec_npop(&p->ops, p->ops.len);
+
+ status = bc_program_reset(p->prog, status);
+ }
+
+ return status;
+}
+
+void bc_parse_free(BcParse *p) {
+
+ if (!p) return;
+
+ bc_vec_free(&p->flags);
+ bc_vec_free(&p->exits);
+ bc_vec_free(&p->conds);
+ bc_vec_free(&p->ops);
+
+ if ((p->lex.token.type == BC_LEX_STRING || p->lex.token.type == BC_LEX_NAME ||
+ p->lex.token.type == BC_LEX_NUMBER) && p->lex.token.string)
+ {
+ free(p->lex.token.string);
+ }
+}
+
+BcStatus bc_parse_expr(BcParse *p, BcVec *code, uint8_t flags) {
+
+ BcStatus status;
+ uint32_t nexprs, nparens, ops_start, nrelops;
+ int paren_first, paren_expr, rparen, done, get_token, assign;
+ BcInst prev;
+ BcLexToken type, top;
+
+ status = BC_STATUS_SUCCESS;
+ prev = BC_INST_PRINT;
+
+ ops_start = p->ops.len;
+ paren_first = p->lex.token.type == BC_LEX_LEFT_PAREN;
+ nexprs = nparens = nrelops = 0;
+ paren_expr = rparen = done = get_token = assign = 0;
+
+ type = p->lex.token.type;
+
+ while (!TT.signe && !status && !done && bc_parse_token_exprs[type]) {
+
+ switch (type) {
+
+ case BC_LEX_OP_INC:
+ case BC_LEX_OP_DEC:
+ {
+ status = bc_parse_incdec(p, code, &prev, &nexprs, flags);
+ rparen = get_token = 0;
+ break;
+ }
+
+ case BC_LEX_OP_MINUS:
+ {
+ status = bc_parse_minus(p, code, &p->ops, &prev,
+ rparen, &nexprs);
+ rparen = get_token = 0;
+ break;
+ }
+
+ case BC_LEX_OP_ASSIGN_POWER:
+ case BC_LEX_OP_ASSIGN_MULTIPLY:
+ case BC_LEX_OP_ASSIGN_DIVIDE:
+ case BC_LEX_OP_ASSIGN_MODULUS:
+ case BC_LEX_OP_ASSIGN_PLUS:
+ case BC_LEX_OP_ASSIGN_MINUS:
+ case BC_LEX_OP_ASSIGN:
+ if (prev != BC_INST_PUSH_VAR && prev != BC_INST_PUSH_ARRAY_ELEM &&
+ prev != BC_INST_PUSH_SCALE && prev != BC_INST_PUSH_IBASE &&
+ prev != BC_INST_PUSH_OBASE && prev != BC_INST_PUSH_LAST)
+ {
+ status = BC_STATUS_PARSE_BAD_ASSIGN;
+ break;
+ }
+ // Fallthrough.
+ case BC_LEX_OP_POWER:
+ case BC_LEX_OP_MULTIPLY:
+ case BC_LEX_OP_DIVIDE:
+ case BC_LEX_OP_MODULUS:
+ case BC_LEX_OP_PLUS:
+ case BC_LEX_OP_REL_EQUAL:
+ case BC_LEX_OP_REL_LESS_EQ:
+ case BC_LEX_OP_REL_GREATER_EQ:
+ case BC_LEX_OP_REL_NOT_EQ:
+ case BC_LEX_OP_REL_LESS:
+ case BC_LEX_OP_REL_GREATER:
+ case BC_LEX_OP_BOOL_NOT:
+ case BC_LEX_OP_BOOL_OR:
+ case BC_LEX_OP_BOOL_AND:
+ {
+ if (type >= BC_LEX_OP_REL_EQUAL && type <= BC_LEX_OP_REL_GREATER)
+ nrelops += 1;
+
+ prev = BC_PARSE_TOKEN_TO_INST(type);
+ status = bc_parse_operator(p, code, &p->ops, type, &nexprs, 1);
+ rparen = get_token = 0;
+
+ break;
+ }
+
+ case BC_LEX_LEFT_PAREN:
+ {
+ ++nparens;
+ paren_expr = rparen = 0;
+ get_token = 1;
+ status = bc_vec_push(&p->ops, &type);
+ break;
+ }
+
+ case BC_LEX_RIGHT_PAREN:
+ {
+ if (nparens == 0) {
+ status = BC_STATUS_SUCCESS;
+ done = 1;
+ get_token = 0;
+ break;
+ }
+ else if (!paren_expr) {
+ status = BC_STATUS_PARSE_BAD_EXPR;
+ goto err;
+ }
+
+ --nparens;
+ paren_expr = rparen = 1;
+ get_token = 0;
+
+ status = bc_parse_rightParen(p, code, &p->ops, &nexprs);
+
+ break;
+ }
+
+ case BC_LEX_NAME:
+ {
+ paren_expr = 1;
+ rparen = get_token = 0;
+ status = bc_parse_name(p, code, &prev, flags & ~(BC_PARSE_EXPR_NOCALL));
+ ++nexprs;
+ break;
+ }
+
+ case BC_LEX_NUMBER:
+ {
+ size_t idx = p->prog->constants.len;
+
+ status = bc_vec_push(&p->prog->constants, &p->lex.token.string);
+ if (status) goto err;
+
+ if ((status = bc_vec_pushByte(code, BC_INST_PUSH_NUM))) return status;
+ if ((status = bc_parse_pushIndex(code, idx))) return status;
+
+ paren_expr = get_token = 1;
+ rparen = 0;
+ ++nexprs;
+ prev = BC_INST_PUSH_NUM;
+
+ break;
+ }
+
+ case BC_LEX_KEY_IBASE:
+ case BC_LEX_KEY_LAST:
+ case BC_LEX_KEY_OBASE:
+ {
+ uint8_t inst = type - BC_LEX_KEY_IBASE + BC_INST_PUSH_IBASE;
+ status = bc_vec_pushByte(code, inst);
+
+ paren_expr = get_token = 1;
+ rparen = 0;
+ ++nexprs;
+ prev = BC_INST_PUSH_OBASE;
+
+ break;
+ }
+
+ case BC_LEX_KEY_LENGTH:
+ case BC_LEX_KEY_SQRT:
+ {
+ status = bc_parse_builtin(p, code, type, flags);
+ paren_expr = 1;
+ rparen = get_token = 0;
+ ++nexprs;
+ prev = type == BC_LEX_KEY_LENGTH ? BC_INST_LENGTH : BC_INST_SQRT;
+ break;
+ }
+
+ case BC_LEX_KEY_READ:
+ {
+ if (flags & BC_PARSE_EXPR_NOREAD) status = BC_STATUS_EXEC_NESTED_READ;
+ else status = bc_parse_read(p, code);
+
+ paren_expr = 1;
+ rparen = get_token = 0;
+ ++nexprs;
+ prev = BC_INST_READ;
+
+ break;
+ }
+
+ case BC_LEX_KEY_SCALE:
+ {
+ status = bc_parse_scale(p, code, &prev, flags);
+ paren_expr = 1;
+ rparen = get_token = 0;
+ ++nexprs;
+ prev = BC_INST_PUSH_SCALE;
+ break;
+ }
+
+ default:
+ {
+ status = BC_STATUS_PARSE_BAD_TOKEN;
+ break;
+ }
+ }
+
+ if (status) goto err;
+ if (get_token) status = bc_lex_next(&p->lex);
+
+ type = p->lex.token.type;
+ }
+
+ if (status) goto err;
+ if (TT.signe) {
+ status = BC_STATUS_EXEC_SIGNAL;
+ goto err;
+ }
+
+ status = BC_STATUS_SUCCESS;
+
+ while (!status && p->ops.len > ops_start) {
+
+ top = *((BcLexToken*) bc_vec_top(&p->ops));
+ assign = top >= BC_LEX_OP_ASSIGN_POWER && top <= BC_LEX_OP_ASSIGN;
+
+ if (top == BC_LEX_LEFT_PAREN || top == BC_LEX_RIGHT_PAREN) {
+ status = BC_STATUS_PARSE_BAD_EXPR;
+ goto err;
+ }
+
+ if ((status = bc_vec_pushByte(code, BC_PARSE_TOKEN_TO_INST(top)))) goto err;
+
+ nexprs -= top != BC_LEX_OP_BOOL_NOT && top != BC_LEX_OP_NEG;
+ bc_vec_pop(&p->ops);
+ }
+
+ if (nexprs != 1) {
+ status = BC_STATUS_PARSE_BAD_EXPR;
+ goto err;
+ }
+
+ if (!(flags & BC_PARSE_EXPR_POSIX_REL) && nrelops &&
+ (status = bc_posix_error(BC_STATUS_POSIX_REL_OUTSIDE,
+ p->lex.file, p->lex.line, NULL)))
+ {
+ goto err;
+ }
+ else if ((flags & BC_PARSE_EXPR_POSIX_REL) && nrelops != 1 &&
+ (status = bc_posix_error(BC_STATUS_POSIX_MULTIPLE_REL,
+ p->lex.file, p->lex.line, NULL)))
+ {
+ goto err;
+ }
+
+ if (flags & BC_PARSE_EXPR_PRINT) {
+ if (paren_first || !assign) status = bc_vec_pushByte(code, BC_INST_PRINT);
+ else status = bc_vec_pushByte(code, BC_INST_POP);
+ }
+
+ return status;
+
+err:
+
+ if (p->lex.token.string) {
+ free(p->lex.token.string);
+ p->lex.token.string = NULL;
+ }
+
+ return status;
+}
+
+BcStatus bc_program_search(BcProgram *p, BcResult *result,
+ BcNum **ret, uint8_t flags)
+{
+ BcStatus status;
+ BcEntry entry, *entry_ptr;
+ BcVec *vec;
+ BcVecO *veco;
+ size_t idx, ip_idx;
+ BcAuto *a;
+ int var;
+
+ for (ip_idx = 0; ip_idx < p->stack.len - 1; ++ip_idx) {
+
+ BcFunc *func;
+ BcInstPtr *ip;
+
+ ip = bc_vec_item_rev(&p->stack, ip_idx);
+ if (ip->func == BC_PROGRAM_READ || ip->func == BC_PROGRAM_MAIN) continue;
+
+ func = bc_vec_item(&p->funcs, ip->func);
+
+ for (idx = 0; idx < func->autos.len; ++idx) {
+
+ a = bc_vec_item(&func->autos, idx);
+
+ if (!strcmp(a->name, result->data.id.name)) {
+
+ BcResult *r;
+ uint8_t cond;
+
+ cond = flags & BC_PROGRAM_SEARCH_VAR;
+
+ if (!a->var != !cond) return BC_STATUS_EXEC_BAD_TYPE;
+
+ r = bc_vec_item(&p->results, ip->len + idx);
+
+ if (cond || flags & BC_PROGRAM_SEARCH_ARRAY) *ret = &r->data.num;
+ else {
+ status = bc_array_expand(&r->data.array, result->data.id.idx + 1);
+ if (status) return status;
+ *ret = bc_vec_item(&r->data.array, result->data.id.idx);
+ }
+
+ return BC_STATUS_SUCCESS;
+ }
+ }
+ }
+
+ var = flags & BC_PROGRAM_SEARCH_VAR;
+ vec = var ? &p->vars : &p->arrays;
+ veco = var ? &p->var_map : &p->array_map;
+
+ entry.name = result->data.id.name;
+ entry.idx = vec->len;
+
+ status = bc_veco_insert(veco, &entry, &idx);
+
+ if (status != BC_STATUS_VEC_ITEM_EXISTS) {
+
+ // We use this because it has a union of BcNum and BcVec.
+ BcResult data;
+ size_t len;
+
+ if (status) return status;
+
+ len = strlen(entry.name) + 1;
+
+ if (!(result->data.id.name = malloc(len))) return BC_STATUS_MALLOC_FAIL;
+
+ strcpy(result->data.id.name, entry.name);
+
+ if (flags & BC_PROGRAM_SEARCH_VAR)
+ status = bc_num_init(&data.data.num, BC_NUM_DEF_SIZE);
+ else status = bc_vec_init(&data.data.array, sizeof(BcNum), bc_num_free);
+
+ if (status) return status;
+ if ((status = bc_vec_push(vec, &data.data))) return status;
+ }
+
+ entry_ptr = bc_veco_item(veco, idx);
+
+ if (var) *ret = bc_vec_item(vec, entry_ptr->idx);
+ else {
+
+ BcVec *ptr = bc_vec_item(vec, entry_ptr->idx);
+
+ if (flags & BC_PROGRAM_SEARCH_ARRAY) {
+ *ret = (BcNum*) ptr;
+ return BC_STATUS_SUCCESS;
+ }
+
+ if ((status = bc_array_expand(ptr, result->data.id.idx + 1))) return status;
+
+ *ret = bc_vec_item(ptr, result->data.id.idx);
+ }
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_program_num(BcProgram *p, BcResult *result, BcNum** num, int hex) {
+
+ BcStatus status = BC_STATUS_SUCCESS;
+
+ switch (result->type) {
+
+ case BC_RESULT_TEMP:
+ case BC_RESULT_SCALE:
+ {
+ *num = &result->data.num;
+ break;
+ }
+
+ case BC_RESULT_CONSTANT:
+ {
+ char** s;
+ size_t len, base;
+
+ s = bc_vec_item(&p->constants, result->data.id.idx);
+ len = strlen(*s);
+
+ if ((status = bc_num_init(&result->data.num, len))) return status;
+
+ base = hex && len == 1 ? BC_NUM_MAX_INPUT_BASE : p->ibase_t;
+
+ if ((status = bc_num_parse(&result->data.num, *s, &p->ibase, base))) {
+ bc_num_free(&result->data.num);
+ return status;
+ }
+
+ *num = &result->data.num;
+ result->type = BC_RESULT_TEMP;
+
+ break;
+ }
+
+ case BC_RESULT_VAR:
+ case BC_RESULT_ARRAY:
+ {
+ uint8_t flags = result->type == BC_RESULT_VAR ? BC_PROGRAM_SEARCH_VAR : 0;
+ status = bc_program_search(p, result, num, flags);
+ break;
+ }
+
+ case BC_RESULT_LAST:
+ {
+ *num = &p->last;
+ break;
+ }
+
+ case BC_RESULT_IBASE:
+ {
+ *num = &p->ibase;
+ break;
+ }
+
+ case BC_RESULT_OBASE:
+ {
+ *num = &p->obase;
+ break;
+ }
+
+ case BC_RESULT_ONE:
+ {
+ *num = &p->one;
+ break;
+ }
+
+ default:
+ {
+ // This is here to prevent compiler warnings in release mode.
+ *num = &result->data.num;
+ break;
+ }
+ }
+
+ return status;
+}
+
+BcStatus bc_program_binaryOpPrep(BcProgram *p, BcResult **left, BcNum **lval,
+ BcResult **right, BcNum **rval)
+{
+ BcStatus status;
+ BcResult *l, *r;
+ int hex;
+
+ r = bc_vec_item_rev(&p->results, 0);
+ l = bc_vec_item_rev(&p->results, 1);
+
+ *left = l;
+ *right = r;
+
+ hex = l->type == BC_RESULT_IBASE || l->type == BC_RESULT_OBASE;
+
+ if ((status = bc_program_num(p, l, lval, 0))) return status;
+ if ((status = bc_program_num(p, r, rval, hex))) return status;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_program_binaryOpRetire(BcProgram *p, BcResult *result,
+ BcResultType type)
+{
+ result->type = type;
+ bc_vec_pop(&p->results);
+ bc_vec_pop(&p->results);
+ return bc_vec_push(&p->results, result);
+}
+
+BcStatus bc_program_unaryOpPrep(BcProgram *p, BcResult **result, BcNum **val) {
+
+ BcStatus status;
+ BcResult *r;
+
+ r = bc_vec_item_rev(&p->results, 0);
+
+ if ((status = bc_program_num(p, r, val, 0))) return status;
+
+ *result = r;
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_program_unaryOpRetire(BcProgram *p, BcResult *result,
+ BcResultType type)
+{
+ result->type = type;
+ bc_vec_pop(&p->results);
+ return bc_vec_push(&p->results, result);
+}
+
+BcStatus bc_program_op(BcProgram *p, uint8_t inst) {
+
+ BcStatus status;
+ BcResult *operand1, *operand2, res;
+ BcNum *num1, *num2;
+ BcNumBinaryFunc op;
+
+ status = bc_program_binaryOpPrep(p, &operand1, &num1, &operand2, &num2);
+ if (status) return status;
+
+ if ((status = bc_num_init(&res.data.num, BC_NUM_DEF_SIZE))) return status;
+
+ op = bc_program_math_ops[inst - BC_INST_POWER];
+ if ((status = op(num1, num2, &res.data.num, p->scale))) goto err;
+ if ((status = bc_program_binaryOpRetire(p, &res, BC_RESULT_TEMP))) goto err;
+
+ return status;
+
+err:
+ bc_num_free(&res.data.num);
+ return status;
+}
+
+BcStatus bc_program_read(BcProgram *p) {
+
+ BcStatus status;
+ BcParse parse;
+ char *buffer;
+ size_t size;
+ BcFunc *func;
+ BcInstPtr ip;
+
+ func = bc_vec_item(&p->funcs, BC_PROGRAM_READ);
+ func->code.len = 0;
+
+ if (!(buffer = malloc(BC_PROGRAM_BUF_SIZE + 1))) return BC_STATUS_MALLOC_FAIL;
+
+ size = BC_PROGRAM_BUF_SIZE;
+
+ if ((status = bc_io_getline(&buffer, &size)))goto io_err;
+
+ if ((status = bc_parse_init(&parse, p))) goto io_err;
+ bc_lex_init(&parse.lex, "<stdin>");
+ if ((status = bc_lex_text(&parse.lex, buffer))) goto exec_err;
+
+ if ((status = bc_parse_expr(&parse, &func->code, BC_PARSE_EXPR_NOREAD))) return status;
+
+ if (parse.lex.token.type != BC_LEX_NEWLINE &&
+ parse.lex.token.type != BC_LEX_EOF)
+ {
+ status = BC_STATUS_EXEC_BAD_READ_EXPR;
+ goto exec_err;
+ }
+
+ ip.func = BC_PROGRAM_READ;
+ ip.idx = 0;
+ ip.len = p->results.len;
+
+ if ((status = bc_vec_push(&p->stack, &ip))) goto exec_err;
+ if ((status = bc_program_exec(p))) goto exec_err;
+
+ bc_vec_pop(&p->stack);
+
+exec_err:
+ bc_parse_free(&parse);
+io_err:
+ free(buffer);
+ return status;
+}
+
+size_t bc_program_index(uint8_t *code, size_t *start) {
+
+ uint8_t bytes, i;
+ size_t result;
+
+ for (bytes = code[(*start)++], result = 0, i = 0; i < bytes; ++i)
+ result |= (((size_t) code[(*start)++]) << (i * CHAR_BIT));
+
+ return result;
+}
+
+char* bc_program_name(uint8_t *code, size_t *start) {
+
+ char byte, *s, *string, *ptr;
+ size_t len, i;
+
+ string = (char*) (code + *start);
+ ptr = strchr((char*) string, ':');
+
+ if (ptr) len = ((unsigned long) ptr) - ((unsigned long) string);
+ else len = strlen(string);
+
+ if (!(s = malloc(len + 1))) return NULL;
+
+ for (byte = code[(*start)++], i = 0; byte && byte != ':'; ++i) {
+ s[i] = byte;
+ byte = code[(*start)++];
+ }
+
+ s[i] = '\0';
+
+ return s;
+}
+
+BcStatus bc_program_printString(const char *str, size_t *nchars) {
+
+ char c, c2;
+ size_t len, i;
+ int err;
+
+ len = strlen(str);
+
+ for (i = 0; i < len; ++i, ++(*nchars)) {
+
+ if ((c = str[i]) != '\\') err = putchar(c);
+ else {
+
+ ++i;
+ c2 = str[i];
+
+ switch (c2) {
+
+ case 'a':
+ {
+ err = putchar('\a');
+ break;
+ }
+
+ case 'b':
+ {
+ err = putchar('\b');
+ break;
+ }
+
+ case 'e':
+ {
+ err = putchar('\\');
+ break;
+ }
+
+ case 'f':
+ {
+ err = putchar('\f');
+ break;
+ }
+
+ case 'n':
+ {
+ err = putchar('\n');
+ *nchars = SIZE_MAX;
+ break;
+ }
+
+ case 'r':
+ {
+ err = putchar('\r');
+ break;
+ }
+
+ case 'q':
+ {
+ err = putchar('"');
+ break;
+ }
+
+ case 't':
+ {
+ err = putchar('\t');
+ break;
+ }
+
+ default:
+ {
+ // Do nothing.
+ err = 0;
+ break;
+ }
+ }
+ }
+
+ if (err == EOF) return BC_STATUS_IO_ERR;
+ }
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_program_push(BcProgram *p, uint8_t *code, size_t *start, int var) {
+
+ BcStatus status;
+ BcResult result;
+
+ result.data.id.name = bc_program_name(code, start);
+
+ if (var) {
+ result.type = BC_RESULT_VAR;
+ status = bc_vec_push(&p->results, &result);
+ }
+ else {
+
+ BcResult *operand;
+ BcNum *num;
+ unsigned long temp;
+
+ if ((status = bc_program_unaryOpPrep(p, &operand, &num))) goto err;
+ if ((status = bc_num_ulong(num, &temp))) goto err;
+
+ if (temp > (unsigned long) maxof_DIM) {
+ status = BC_STATUS_EXEC_ARRAY_LEN;
+ goto err;
+ }
+
+ result.data.id.idx = (size_t) temp;
+
+ status = bc_program_unaryOpRetire(p, &result, BC_RESULT_ARRAY);
+ }
+
+ if (status) goto err;
+
+ return status;
+
+err:
+ free(result.data.id.name);
+ return status;
+}
+
+BcStatus bc_program_negate(BcProgram *p) {
+
+ BcStatus status;
+ BcResult result, *ptr;
+ BcNum *num;
+
+ if ((status = bc_program_unaryOpPrep(p, &ptr, &num))) return status;
+ if ((status = bc_num_init(&result.data.num, num->len))) return status;
+ if ((status = bc_num_copy(&result.data.num, num))) goto err;
+
+ result.data.num.neg = !result.data.num.neg;
+
+ if ((status = bc_program_unaryOpRetire(p, &result, BC_RESULT_TEMP))) goto err;
+
+ return status;
+
+err:
+ bc_num_free(&result.data.num);
+ return status;
+}
+
+BcStatus bc_program_logical(BcProgram *p, uint8_t inst) {
+
+ BcStatus status;
+ BcResult *operand1, *operand2, res;
+ BcNum *num1, *num2;
+ int cond;
+ int cmp;
+
+ status = bc_program_binaryOpPrep(p, &operand1, &num1, &operand2, &num2);
+ if (status) return status;
+
+ if ((status = bc_num_init(&res.data.num, BC_NUM_DEF_SIZE))) return status;
+
+ if (inst == BC_INST_BOOL_AND)
+ cond = bc_num_cmp(num1, &p->zero) && bc_num_cmp(num2, &p->zero);
+ else if (inst == BC_INST_BOOL_OR)
+ cond = bc_num_cmp(num1, &p->zero) || bc_num_cmp(num2, &p->zero);
+ else {
+
+ cmp = bc_num_cmp(num1, num2);
+
+ switch (inst) {
+ case BC_INST_REL_EQ:
+ {
+ cond = cmp == 0;
+ break;
+ }
+
+ case BC_INST_REL_LE:
+ {
+ cond = cmp <= 0;
+ break;
+ }
+
+ case BC_INST_REL_GE:
+ {
+ cond = cmp >= 0;
+ break;
+ }
+
+ case BC_INST_REL_NE:
+ {
+ cond = cmp != 0;
+ break;
+ }
+
+ case BC_INST_REL_LT:
+ {
+ cond = cmp < 0;
+ break;
+ }
+
+ case BC_INST_REL_GT:
+ {
+ cond = cmp > 0;
+ break;
+ }
+
+ default:
+ {
+ // This is here to silence a compiler warning in release mode.
+ cond = 0;
+ break;
+ }
+ }
+ }
+
+ (cond ? bc_num_one : bc_num_zero)(&res.data.num);
+
+ if ((status = bc_program_binaryOpRetire(p, &res, BC_RESULT_TEMP))) goto err;
+
+ return status;
+
+err:
+ bc_num_free(&res.data.num);
+ return status;
+}
+
+BcStatus bc_program_assign(BcProgram *p, uint8_t inst) {
+
+ BcStatus status;
+ BcResult *left, *right, res;
+ BcNum *l, *r;
+ BcNumBinaryFunc op;
+ unsigned long val, max;
+ size_t *ptr;
+
+ status = bc_program_binaryOpPrep(p, &left, &l, &right, &r);
+ if (status) return status;
+
+ if (left->type == BC_RESULT_CONSTANT || left->type == BC_RESULT_TEMP)
+ return BC_STATUS_PARSE_BAD_ASSIGN;
+
+ if (inst == BC_INST_ASSIGN_DIVIDE && !bc_num_cmp(r, &p->zero))
+ return BC_STATUS_MATH_DIVIDE_BY_ZERO;
+
+ if (inst == BC_INST_ASSIGN) status = bc_num_copy(l, r);
+ else {
+ op = bc_program_math_ops[inst - BC_INST_ASSIGN_POWER];
+ status = op(l, r, l, p->scale);
+ }
+
+ if (status) return status;
+
+ if (left->type == BC_RESULT_IBASE || left->type == BC_RESULT_OBASE) {
+
+ ptr = left->type == BC_RESULT_IBASE ? &p->ibase_t : &p->obase_t;
+ max = left->type == BC_RESULT_IBASE ? BC_NUM_MAX_INPUT_BASE : maxof_BASE;
+
+ if ((status = bc_num_ulong(l, &val))) return status;
+
+ if (val < BC_NUM_MIN_BASE || val > max)
+ return left->type - BC_RESULT_IBASE + BC_STATUS_EXEC_BAD_IBASE;
+
+ *ptr = (size_t) val;
+ }
+ else if (left->type == BC_RESULT_SCALE) {
+
+ if ((status = bc_num_ulong(l, &val))) return status;
+ if (val > (unsigned long) maxof_SCALE) return BC_STATUS_EXEC_BAD_SCALE;
+
+ p->scale = (size_t) val;
+ }
+
+ if ((status = bc_num_init(&res.data.num, l->len))) return status;
+ if ((status = bc_num_copy(&res.data.num, l))) goto err;
+
+ if ((status = bc_program_binaryOpRetire(p, &res, BC_RESULT_TEMP))) goto err;
+
+ return status;
+
+err:
+ bc_num_free(&res.data.num);
+ return status;
+}
+
+BcStatus bc_program_call(BcProgram *p, uint8_t *code, size_t *idx) {
+
+ BcStatus status;
+ BcInstPtr ip;
+ size_t nparams, i;
+ BcFunc *func;
+ BcAuto *auto_ptr;
+ BcResult param, *arg;
+
+ status = BC_STATUS_SUCCESS;
+ nparams = bc_program_index(code, idx);
+
+ ip.idx = 0;
+ ip.len = p->results.len;
+ ip.func = bc_program_index(code, idx);
+
+ func = bc_vec_item(&p->funcs, ip.func);
+
+ if (!func->code.len) return BC_STATUS_EXEC_UNDEFINED_FUNC;
+ if (nparams != func->nparams) return BC_STATUS_EXEC_MISMATCHED_PARAMS;
+
+ for (i = 0; i < nparams; ++i) {
+
+ auto_ptr = bc_vec_item(&func->autos, i);
+ arg = bc_vec_item_rev(&p->results, nparams - 1);
+ param.type = auto_ptr->var + BC_RESULT_ARRAY_AUTO;
+
+ if (auto_ptr->var) {
+
+ BcNum *n;
+
+ if ((status = bc_program_num(p, arg, &n, 0))) return status;
+ if ((status = bc_num_init(&param.data.num, n->len))) return status;
+
+ status = bc_num_copy(&param.data.num, n);
+ }
+ else {
+
+ BcVec *a;
+
+ if (arg->type != BC_RESULT_VAR || arg->type != BC_RESULT_ARRAY)
+ return BC_STATUS_EXEC_BAD_TYPE;
+
+ status = bc_program_search(p, arg, (BcNum**) &a, BC_PROGRAM_SEARCH_ARRAY);
+ if (status) return status;
+
+ status = bc_vec_init(&param.data.array, sizeof(BcNum), bc_num_free);
+ if (status) return status;
+
+ status = bc_array_copy(&param.data.array, a);
+ }
+
+ if (status || (status = bc_vec_push(&p->results, &param))) goto err;
+ }
+
+ for (; !status && i < func->autos.len; ++i) {
+
+ auto_ptr = bc_vec_item_rev(&func->autos, i);
+ param.type = auto_ptr->var + BC_RESULT_ARRAY_AUTO;
+
+ if (auto_ptr->var) status = bc_num_init(&param.data.num, BC_NUM_DEF_SIZE);
+ else status = bc_vec_init(&param.data.array, sizeof(BcNum), bc_num_free);
+
+ if (status) return status;
+
+ status = bc_vec_push(&p->results, &param);
+ }
+
+ if (status) goto err;
+
+ return bc_vec_push(&p->stack, &ip);
+
+err:
+ bc_result_free(&param);
+ return status;
+}
+
+BcStatus bc_program_return(BcProgram *p, uint8_t inst) {
+
+ BcStatus status;
+ BcResult result, *operand;
+ BcInstPtr *ip;
+ BcFunc *func;
+
+ ip = bc_vec_top(&p->stack);
+ func = bc_vec_item(&p->funcs, ip->func);
+
+ result.type = BC_RESULT_TEMP;
+
+ if (inst == BC_INST_RETURN) {
+
+ BcNum *num;
+
+ operand = bc_vec_top(&p->results);
+
+ if ((status = bc_program_num(p, operand, &num, 0))) return status;
+ if ((status = bc_num_init(&result.data.num, num->len))) return status;
+ if ((status = bc_num_copy(&result.data.num, num))) goto err;
+ }
+ else {
+ status = bc_num_init(&result.data.num, BC_NUM_DEF_SIZE);
+ if (status) return status;
+ bc_num_zero(&result.data.num);
+ }
+
+ // We need to pop arguments as well, so this takes that into account.
+ bc_vec_npop(&p->results, p->results.len - (ip->len - func->nparams));
+
+ if ((status = bc_vec_push(&p->results, &result))) goto err;
+ bc_vec_pop(&p->stack);
+
+ return status;
+
+err:
+ bc_num_free(&result.data.num);
+ return status;
+}
+
+unsigned long bc_program_scale(BcNum *n) {
+ return (unsigned long) n->rdx;
+}
+
+unsigned long bc_program_length(BcNum *n) {
+
+ size_t i;
+ unsigned long len = n->len;
+
+ if (n->rdx == n->len) {
+ for (i = n->len - 1; i < n->len && !n->num[i]; --len, --i);
+ }
+
+ return len;
+}
+
+BcStatus bc_program_builtin(BcProgram *p, uint8_t inst) {
+
+ BcStatus status;
+ BcResult *operand;
+ BcNum *num1;
+ BcResult result;
+
+ if ((status = bc_program_unaryOpPrep(p, &operand, &num1))) return status;
+ if ((status = bc_num_init(&result.data.num, BC_NUM_DEF_SIZE))) return status;
+
+ if (inst == BC_INST_SQRT)
+ status = bc_num_sqrt(num1, &result.data.num, p->scale);
+ else {
+
+ BcProgramBuiltInFunc func;
+ unsigned long ans;
+
+ func = inst == BC_INST_LENGTH ? bc_program_length : bc_program_scale;
+ ans = func(num1);
+
+ status = bc_num_ulong2num(&result.data.num, ans);
+ }
+
+ if (status || (status = bc_program_unaryOpRetire(p, &result, BC_RESULT_TEMP)))
+ goto err;
+
+ return status;
+
+err:
+ bc_num_free(&result.data.num);
+ return status;
+}
+
+BcStatus bc_program_pushScale(BcProgram *p) {
+
+ BcStatus status;
+ BcResult result;
+
+ result.type = BC_RESULT_SCALE;
+
+ if ((status = bc_num_init(&result.data.num, BC_NUM_DEF_SIZE))) return status;
+ status = bc_num_ulong2num(&result.data.num, (unsigned long) p->scale);
+ if (status || (status = bc_vec_push(&p->results, &result))) goto err;
+
+ return status;
+
+err:
+ bc_num_free(&result.data.num);
+ return status;
+}
+
+BcStatus bc_program_incdec(BcProgram *p, uint8_t inst) {
+
+ BcStatus status;
+ BcResult *ptr, result, copy;
+ BcNum *num;
+
+ if ((status = bc_program_unaryOpPrep(p, &ptr, &num))) return status;
+
+ if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
+ copy.type = BC_RESULT_TEMP;
+ if ((status = bc_num_init(&copy.data.num, num->len))) return status;
+ }
+
+ result.type = BC_RESULT_ONE;
+ inst = inst == BC_INST_INC_PRE || inst == BC_INST_INC_POST ?
+ BC_INST_ASSIGN_PLUS : BC_INST_ASSIGN_MINUS;
+
+ if ((status = bc_vec_push(&p->results, &result))) goto err;
+ if ((status = bc_program_assign(p, inst))) goto err;
+
+ if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST) {
+ bc_vec_pop(&p->results);
+ if ((status = bc_vec_push(&p->results, &copy))) goto err;
+ }
+
+ return status;
+
+err:
+
+ if (inst == BC_INST_INC_POST || inst == BC_INST_DEC_POST)
+ bc_num_free(&copy.data.num);
+
+ return status;
+}
+
+BcStatus bc_program_init(BcProgram *p, size_t line_len) {
+
+ BcStatus s;
+ size_t idx;
+ char *main_name, *read_name;
+ BcInstPtr ip;
+
+ main_name = read_name = NULL;
+ p->nchars = p->scale = 0;
+ p->line_len = line_len;
+
+ if ((s = bc_num_init(&p->ibase, BC_NUM_DEF_SIZE))) return s;
+ bc_num_ten(&p->ibase);
+ p->ibase_t = 10;
+
+ if ((s = bc_num_init(&p->obase, BC_NUM_DEF_SIZE))) goto obase_err;
+ bc_num_ten(&p->obase);
+ p->obase_t = 10;
+
+ if ((s = bc_num_init(&p->last, BC_NUM_DEF_SIZE))) goto last_err;
+ bc_num_zero(&p->last);
+
+ if ((s = bc_num_init(&p->zero, BC_NUM_DEF_SIZE))) goto zero_err;
+ bc_num_zero(&p->zero);
+
+ if ((s = bc_num_init(&p->one, BC_NUM_DEF_SIZE))) goto one_err;
+ bc_num_one(&p->one);
+
+ if ((s = bc_vec_init(&p->funcs, sizeof(BcFunc), bc_func_free))) goto func_err;
+
+ s = bc_veco_init(&p->func_map, sizeof(BcEntry), bc_entry_free, bc_entry_cmp);
+ if (s) goto func_map_err;
+
+ if (!(main_name = malloc(sizeof(bc_lang_func_main)))) {
+ s = BC_STATUS_MALLOC_FAIL;
+ goto name_err;
+ }
+
+ strcpy(main_name, bc_lang_func_main);
+ s = bc_program_addFunc(p, main_name, &idx);
+ main_name = NULL;
+ if (s || idx != BC_PROGRAM_MAIN) goto read_err;
+
+ if (!(read_name = malloc(sizeof(bc_lang_func_read)))) {
+ s = BC_STATUS_MALLOC_FAIL;
+ goto read_err;
+ }
+
+ strcpy(read_name, bc_lang_func_read);
+ s = bc_program_addFunc(p, read_name, &idx);
+ read_name = NULL;
+ if (s || idx != BC_PROGRAM_READ) goto var_err;
+
+ if ((s = bc_vec_init(&p->vars, sizeof(BcNum), bc_num_free))) goto var_err;
+ s = bc_veco_init(&p->var_map, sizeof(BcEntry), bc_entry_free, bc_entry_cmp);
+ if (s) goto var_map_err;
+
+ if ((s = bc_vec_init(&p->arrays, sizeof(BcVec), bc_vec_free))) goto array_err;
+ s = bc_veco_init(&p->array_map, sizeof(BcEntry), bc_entry_free, bc_entry_cmp);
+ if (s) goto array_map_err;
+
+ s = bc_vec_init(&p->strings, sizeof(char*), bc_string_free);
+ if (s) goto string_err;
+
+ s = bc_vec_init(&p->constants, sizeof(char*), bc_string_free);
+ if (s) goto const_err;
+
+ s = bc_vec_init(&p->results, sizeof(BcResult), bc_result_free);
+ if (s) goto expr_err;
+
+ if ((s = bc_vec_init(&p->stack, sizeof(BcInstPtr), NULL))) goto stack_err;
+
+ memset(&ip, 0, sizeof(BcInstPtr));
+
+ if ((s = bc_vec_push(&p->stack, &ip))) goto push_err;
+
+ return s;
+
+push_err:
+ bc_vec_free(&p->stack);
+stack_err:
+ bc_vec_free(&p->results);
+expr_err:
+ bc_vec_free(&p->constants);
+const_err:
+ bc_vec_free(&p->strings);
+string_err:
+ bc_veco_free(&p->array_map);
+array_map_err:
+ bc_vec_free(&p->arrays);
+array_err:
+ bc_veco_free(&p->var_map);
+var_map_err:
+ bc_vec_free(&p->vars);
+var_err:
+ if (read_name) free(read_name);
+read_err:
+ if (main_name) free(main_name);
+name_err:
+ bc_veco_free(&p->func_map);
+func_map_err:
+ bc_vec_free(&p->funcs);
+func_err:
+ bc_num_free(&p->one);
+one_err:
+ bc_num_free(&p->zero);
+zero_err:
+ bc_num_free(&p->last);
+last_err:
+ bc_num_free(&p->obase);
+obase_err:
+ bc_num_free(&p->ibase);
+ return s;
+}
+
+BcStatus bc_program_addFunc(BcProgram *p, char *name, size_t *idx) {
+
+ BcStatus status;
+ BcEntry entry, *entry_ptr;
+ BcFunc f;
+
+ entry.name = name;
+ entry.idx = p->funcs.len;
+
+ if ((status = bc_veco_insert(&p->func_map, &entry, idx))) {
+ free(name);
+ if (status != BC_STATUS_VEC_ITEM_EXISTS) return status;
+ }
+
+ entry_ptr = bc_veco_item(&p->func_map, *idx);
+ *idx = entry_ptr->idx;
+
+ if (status == BC_STATUS_VEC_ITEM_EXISTS) {
+
+ BcFunc *func = bc_vec_item(&p->funcs, entry_ptr->idx);
+
+ status = BC_STATUS_SUCCESS;
+
+ // We need to reset these, so the function can be repopulated.
+ func->nparams = 0;
+ bc_vec_npop(&func->autos, func->autos.len);
+ bc_vec_npop(&func->code, func->code.len);
+ bc_vec_npop(&func->labels, func->labels.len);
+ }
+ else {
+ if ((status = bc_func_init(&f))) return status;
+ if ((status = bc_vec_push(&p->funcs, &f))) bc_func_free(&f);
+ }
+
+ return status;
+}
+
+BcStatus bc_program_reset(BcProgram *p, BcStatus status) {
+
+ BcFunc *func;
+ BcInstPtr *ip;
+
+ bc_vec_npop(&p->stack, p->stack.len - 1);
+ bc_vec_npop(&p->results, p->results.len);
+
+ func = bc_vec_item(&p->funcs, 0);
+ ip = bc_vec_top(&p->stack);
+ ip->idx = func->code.len;
+
+ if (!status && TT.signe && !TT.tty) return BC_STATUS_QUIT;
+
+ TT.sigc += TT.signe;
+ TT.signe = TT.sig != TT.sigc;
+
+ if ((!status || status == BC_STATUS_EXEC_SIGNAL) && TT.tty) {
+ status = BC_STATUS_SUCCESS;
+ fprintf(stderr, "%s", bc_program_ready_prompt);
+ fflush(stderr);
+ }
+
+ return status;
+}
+
+BcStatus bc_program_exec(BcProgram *p) {
+
+ BcStatus status;
+ uint8_t *code;
+ size_t idx, len, *addr;
+ BcResult result;
+ BcResult *ptr;
+ BcNum *num;
+ BcFunc *func;
+ BcInstPtr *ip;
+ int cond;
+ const char **string, *s;
+
+ status = BC_STATUS_SUCCESS;
+ cond = 0;
+
+ ip = bc_vec_top(&p->stack);
+ func = bc_vec_item(&p->funcs, ip->func);
+ code = func->code.array;
+
+ while (!status && !TT.sig_other && ip->idx < func->code.len) {
+
+ uint8_t inst = code[(ip->idx)++];
+
+ switch (inst) {
+
+ case BC_INST_CALL:
+ {
+ status = bc_program_call(p, code, &ip->idx);
+ break;
+ }
+
+ case BC_INST_RETURN:
+ case BC_INST_RETURN_ZERO:
+ {
+ status = bc_program_return(p, inst);
+ break;
+ }
+
+ case BC_INST_READ:
+ {
+ status = bc_program_read(p);
+ break;
+ }
+
+ case BC_INST_JUMP_ZERO:
+ {
+ if ((status = bc_program_unaryOpPrep(p, &ptr, &num))) return status;
+ cond = !bc_num_cmp(num, &p->zero);
+ bc_vec_pop(&p->results);
+ }
+ // Fallthrough.
+ case BC_INST_JUMP:
+ {
+ idx = bc_program_index(code, &ip->idx);
+ addr = bc_vec_item(&func->labels, idx);
+ if (inst == BC_INST_JUMP || cond) ip->idx = *addr;
+ break;
+ }
+
+ case BC_INST_PUSH_VAR:
+ case BC_INST_PUSH_ARRAY_ELEM:
+ {
+ status = bc_program_push(p, code, &ip->idx, inst == BC_INST_PUSH_VAR);
+ break;
+ }
+
+ case BC_INST_PUSH_LAST:
+ {
+ result.type = BC_RESULT_LAST;
+ status = bc_vec_push(&p->results, &result);
+ break;
+ }
+
+ case BC_INST_PUSH_SCALE:
+ {
+ status = bc_program_pushScale(p);
+ break;
+ }
+
+ case BC_INST_PUSH_IBASE:
+ {
+ result.type = BC_RESULT_IBASE;
+ status = bc_vec_push(&p->results, &result);
+ break;
+ }
+
+ case BC_INST_PUSH_OBASE:
+ {
+ result.type = BC_RESULT_OBASE;
+ status = bc_vec_push(&p->results, &result);
+ break;
+ }
+
+ case BC_INST_SCALE_FUNC:
+ case BC_INST_LENGTH:
+ case BC_INST_SQRT:
+ {
+ status = bc_program_builtin(p, inst);
+ break;
+ }
+
+ case BC_INST_PUSH_NUM:
+ {
+ result.type = BC_RESULT_CONSTANT;
+ result.data.id.idx = bc_program_index(code, &ip->idx);
+ status = bc_vec_push(&p->results, &result);
+ break;
+ }
+
+ case BC_INST_POP:
+ {
+ bc_vec_pop(&p->results);
+ break;
+ }
+
+ case BC_INST_INC_POST:
+ case BC_INST_DEC_POST:
+ case BC_INST_INC_PRE:
+ case BC_INST_DEC_PRE:
+ {
+ status = bc_program_incdec(p, inst);
+ break;
+ }
+
+ case BC_INST_HALT:
+ {
+ status = BC_STATUS_QUIT;
+ break;
+ }
+
+ case BC_INST_PRINT:
+ case BC_INST_PRINT_EXPR:
+ {
+ if ((status = bc_program_unaryOpPrep(p, &ptr, &num))) return status;
+
+ status = bc_num_print(num, &p->obase, p->obase_t, inst == BC_INST_PRINT,
+ &p->nchars, p->line_len);
+ if (status) return status;
+ if ((status = bc_num_copy(&p->last, num))) return status;
+
+ bc_vec_pop(&p->results);
+
+ break;
+ }
+
+ case BC_INST_STR:
+ {
+ idx = bc_program_index(code, &ip->idx);
+ string = bc_vec_item(&p->strings, idx);
+
+ s = *string;
+ len = strlen(s);
+
+ for (idx = 0; idx < len; ++idx) {
+ char c = s[idx];
+ if (putchar(c) == EOF) return BC_STATUS_IO_ERR;
+ if (c == '\n') p->nchars = SIZE_MAX;
+ ++p->nchars;
+ }
+
+ break;
+ }
+
+ case BC_INST_PRINT_STR:
+ {
+ idx = bc_program_index(code, &ip->idx);
+ string = bc_vec_item(&p->strings, idx);
+ status = bc_program_printString(*string, &p->nchars);
+ break;
+ }
+
+ case BC_INST_POWER:
+ case BC_INST_MULTIPLY:
+ case BC_INST_DIVIDE:
+ case BC_INST_MODULUS:
+ case BC_INST_PLUS:
+ case BC_INST_MINUS:
+ {
+ status = bc_program_op(p, inst);
+ break;
+ }
+
+ case BC_INST_REL_EQ:
+ case BC_INST_REL_LE:
+ case BC_INST_REL_GE:
+ case BC_INST_REL_NE:
+ case BC_INST_REL_LT:
+ case BC_INST_REL_GT:
+ {
+ status = bc_program_logical(p, inst);
+ break;
+ }
+
+ case BC_INST_BOOL_NOT:
+ {
+ if ((status = bc_program_unaryOpPrep(p, &ptr, &num))) return status;
+ status = bc_num_init(&result.data.num, BC_NUM_DEF_SIZE);
+ if (status) return status;
+
+ if (bc_num_cmp(num, &p->zero)) bc_num_one(&result.data.num);
+ else bc_num_zero(&result.data.num);
+
+ status = bc_program_unaryOpRetire(p, &result, BC_RESULT_TEMP);
+ if (status) bc_num_free(&result.data.num);
+
+ break;
+ }
+
+ case BC_INST_BOOL_OR:
+ case BC_INST_BOOL_AND:
+ {
+ status = bc_program_logical(p, inst);
+ break;
+ }
+
+ case BC_INST_NEG:
+ {
+ status = bc_program_negate(p);
+ break;
+ }
+
+ case BC_INST_ASSIGN_POWER:
+ case BC_INST_ASSIGN_MULTIPLY:
+ case BC_INST_ASSIGN_DIVIDE:
+ case BC_INST_ASSIGN_MODULUS:
+ case BC_INST_ASSIGN_PLUS:
+ case BC_INST_ASSIGN_MINUS:
+ case BC_INST_ASSIGN:
+ {
+ status = bc_program_assign(p, inst);
+ break;
+ }
+
+ default:
+ {
+ break;
+ }
+ }
+
+ if ((status && status != BC_STATUS_QUIT) || TT.signe)
+ status = bc_program_reset(p, status);
+
+ // We need to update because if the stack changes, pointers may be invalid.
+ ip = bc_vec_top(&p->stack);
+ func = bc_vec_item(&p->funcs, ip->func);
+ code = func->code.array;
+ }
+
+ return status;
+}
+
+void bc_program_free(BcProgram *p) {
+
+ if (!p) return;
+
+ bc_num_free(&p->ibase);
+ bc_num_free(&p->obase);
+
+ bc_vec_free(&p->funcs);
+ bc_veco_free(&p->func_map);
+
+ bc_vec_free(&p->vars);
+ bc_veco_free(&p->var_map);
+
+ bc_vec_free(&p->arrays);
+ bc_veco_free(&p->array_map);
+
+ bc_vec_free(&p->strings);
+ bc_vec_free(&p->constants);
+
+ bc_vec_free(&p->results);
+ bc_vec_free(&p->stack);
+
+ bc_num_free(&p->last);
+ bc_num_free(&p->zero);
+ bc_num_free(&p->one);
+
+ memset(p, 0, sizeof(BcProgram));
+}
+
+void bc_sig(int sig) {
+ if (sig == SIGINT) {
+ if (write(2, bc_sig_msg, sizeof(bc_sig_msg) - 1) >= 0)
+ TT.sig += (TT.signe = TT.sig == TT.sigc);
+ }
+ else TT.sig_other = 1;
+}
+
+BcStatus bc_error(BcStatus s) {
+ if (!s || s >= BC_STATUS_POSIX_NAME_LEN) return BC_STATUS_SUCCESS;
+ fprintf(stderr, bc_err_fmt, bc_errs[bc_err_indices[s]], bc_err_descs[s]);
+ return s * !TT.tty;
+}
+
+BcStatus bc_error_file(BcStatus s, const char *file, size_t line) {
+ if (!s || !file || s >= BC_STATUS_POSIX_NAME_LEN) return BC_STATUS_SUCCESS;
+ fprintf(stderr, bc_err_fmt, bc_errs[bc_err_indices[s]], bc_err_descs[s]);
+ fprintf(stderr, " %s", file);
+ fprintf(stderr, &":%d\n\n"[3 * !line], line);
+ return s * !TT.tty;
+}
+
+BcStatus bc_posix_error(BcStatus s, const char *file,
+ size_t line, const char *msg)
+{
+ int p = (int) (toys.optflags & FLAG_s), w = (int) (toys.optflags & FLAG_w);
+
+ if (!(p || w) || s < BC_STATUS_POSIX_NAME_LEN || !file)
+ return BC_STATUS_SUCCESS;
+
+ fprintf(stderr, "\n%s %s: %s\n", bc_errs[bc_err_indices[s]],
+ p ? "error" : "warning", bc_err_descs[s]);
+
+ if (msg) fprintf(stderr, " %s\n", msg);
+ fprintf(stderr, " %s", file);
+ fprintf(stderr, &":%d\n\n"[3 * !line], line);
+
+ return s * !!p;
+}
+
+BcStatus bc_process(Bc *bc, const char *text) {
+
+ BcStatus s = bc_lex_text(&bc->parse.lex, text);
+
+ if (s && (s = bc_error_file(s, bc->parse.lex.file, bc->parse.lex.line)))
+ return s;
+
+ while (bc->parse.lex.token.type != BC_LEX_EOF) {
+
+ if ((s = bc_parse_parse(&bc->parse)) == BC_STATUS_LIMITS) {
+
+ s = BC_STATUS_IO_ERR;
+
+ if (putchar('\n') == EOF) return s;
+
+ if (printf("BC_BASE_MAX = %zu\n", (size_t) maxof_BASE) < 0 ||
+ printf("BC_DIM_MAX = %zu\n", (size_t) maxof_DIM) < 0 ||
+ printf("BC_SCALE_MAX = %zu\n", (size_t) maxof_SCALE) < 0 ||
+ printf("BC_STRING_MAX = %zu\n", (size_t) maxof_STRING) < 0 ||
+ printf("Max Exponent = %ld\n", (long) LONG_MAX) < 0 ||
+ printf("Number of Vars = %zu\n", (size_t) SIZE_MAX) < 0)
+ {
+ return s;
+ }
+
+ if (putchar('\n') == EOF) return s;
+ }
+ else if (s == BC_STATUS_QUIT || TT.sig_other ||
+ (s && (s = bc_error_file(s, bc->parse.lex.file, bc->parse.lex.line))))
+ {
+ return s;
+ }
+ }
+
+ if (BC_PARSE_CAN_EXEC(&bc->parse)) {
+ s = bc_program_exec(&bc->prog);
+ if (TT.tty) fflush(stdout);
+ if (s && s != BC_STATUS_QUIT) s = bc_error(s);
+ }
+
+ return s;
+}
+
+BcStatus bc_file(Bc *bc, const char *file) {
+
+ BcStatus s;
+ char *data;
+ BcFunc *main_func;
+ BcInstPtr *ip;
+
+ bc->prog.file = file;
+ if ((s = bc_io_fread(file, &data))) return s;
+
+ bc_lex_init(&bc->parse.lex, file);
+ if ((s = bc_process(bc, data))) goto err;
+
+ main_func = bc_vec_item(&bc->prog.funcs, BC_PROGRAM_MAIN);
+ ip = bc_vec_item(&bc->prog.stack, 0);
+
+ if (main_func->code.len > ip->idx) s = BC_STATUS_EXEC_FILE_NOT_EXECUTABLE;
+
+err:
+ free(data);
+ return s;
+}
+
+BcStatus bc_concat(char **buffer, size_t *n, char *buf, size_t total_len) {
+
+ if (total_len > *n) {
+
+ char *temp = realloc(*buffer, total_len + 1);
+ if (!temp) return BC_STATUS_MALLOC_FAIL;
+
+ *buffer = temp;
+ *n = total_len;
+ }
+
+ strcat(*buffer, buf);
+
+ return BC_STATUS_SUCCESS;
+}
+
+BcStatus bc_stdin(Bc *bc) {
+
+ BcStatus s;
+ char *buf, *buffer, c;
+ size_t n, bufn, slen, total_len, len, i;
+ int string, comment, notend;
+
+ bc->prog.file = bc_program_stdin_name;
+ bc_lex_init(&bc->parse.lex, bc_program_stdin_name);
+
+ n = bufn = BC_BUF_SIZE;
+
+ if (!(buffer = malloc(BC_BUF_SIZE + 1))) return BC_STATUS_MALLOC_FAIL;
+
+ if (!(buf = malloc(BC_BUF_SIZE + 1))) {
+ s = BC_STATUS_MALLOC_FAIL;
+ goto buf_err;
+ }
+
+ buffer[0] = '\0';
+ string = comment = 0;
+ s = BC_STATUS_SUCCESS;
+
+ // The following loop is complex because the vm tries not to send any lines
+ // that end with a backslash to the parser. The reason for that is because the
+ // parser treats a backslash+newline combo as whitespace, per the bc spec. In
+ // that case, and for strings and comments, the parser will expect more stuff.
+ while ((!s || s != BC_STATUS_QUIT) &&
+ !((s = bc_io_getline(&buf, &bufn)) && s != BC_STATUS_BINARY_FILE))
+ {
+ if (s == BC_STATUS_BINARY_FILE) {
+ putchar('\a');
+ s = BC_STATUS_SUCCESS;
+ continue;
+ }
+
+ len = strlen(buf);
+ slen = strlen(buffer);
+ total_len = slen + len;
+
+ if (len == 1 && buf[0] == '"') string = !string;
+ else if (len > 1 || comment) {
+
+ for (i = 0; i < len; ++i) {
+
+ notend = len > i + 1;
+
+ if ((c = buf[i]) == '"') string = !string;
+ else if (c == '/' && notend && !comment && buf[i + 1] == '*') {
+ comment = 1;
+ break;
+ }
+ else if (c == '*' && notend && comment && buf[i + 1] == '/')
+ comment = 0;
+ }
+
+ if (string || comment || buf[len - 2] == '\\') {
+ if ((s = bc_concat(&buffer, &n, buf, total_len))) goto exit_err;
+ continue;
+ }
+ }
+
+ if ((s = bc_concat(&buffer, &n, buf, total_len))) goto exit_err;
+
+ s = bc_process(bc, buffer);
+ buffer[0] = '\0';
+ }
+
+ // I/O error will always happen when stdin is
+ // closed. It's not a problem in that case.
+ s = s == BC_STATUS_IO_ERR ? BC_STATUS_SUCCESS : s;
+
+exit_err:
+ free(buf);
+buf_err:
+ free(buffer);
+ return s;
+}
+
+void bc_main(void) {
+
+ BcStatus status;
+ Bc bc;
+ struct sigaction sa;
+ size_t i, len;
+ char *lenv;
+ int num;
+
+ TT.tty = (toys.optflags & FLAG_i) || (isatty(0) && isatty(1));
+
+ if ((lenv = getenv("BC_LINE_LENGTH"))) {
+ len = strlen(lenv);
+ for (num = 1, i = 0; num && i < len; ++i) num = isdigit(lenv[i]);
+ if (!num || (len = (size_t) atoi(lenv) - 1) < 2) len = BC_NUM_PRINT_WIDTH;
+ }
+ else len = BC_NUM_PRINT_WIDTH;
+
+ if ((toys.exitval = bc_program_init(&bc.prog, len))) return;
+ if ((status = bc_parse_init(&bc.parse, &bc.prog))) goto parse_err;
+
+ sigemptyset(&sa.sa_mask);
+ sa.sa_handler = bc_sig;
+ sa.sa_flags = 0;
+
+ if (sigaction(SIGINT, &sa, NULL) < 0 || sigaction(SIGPIPE, &sa, NULL) < 0 ||
+ sigaction(SIGHUP, &sa, NULL) < 0 || sigaction(SIGTERM, &sa, NULL) < 0)
+ {
+ status = BC_STATUS_EXEC_SIGACTION_FAIL;
+ goto err;
+ }
+
+ if (TT.tty && !(toys.optflags & FLAG_q) && printf("%s", bc_header) < 0) {
+ status = BC_STATUS_IO_ERR;
+ goto err;
+ }
+
+ if (toys.optflags & FLAG_l) {
+
+ bc_lex_init(&bc.parse.lex, bc_lib_name);
+ if ((status = bc_lex_text(&bc.parse.lex, bc_lib))) goto err;
+
+ while (!status && bc.parse.lex.token.type != BC_LEX_EOF)
+ status = bc_parse_parse(&bc.parse);
+
+ if (status || (status = bc_program_exec(&bc.prog))) goto err;
+ }
+
+ for (i = 0; !TT.sig_other && !status && i < toys.optc; ++i)
+ status = bc_file(&bc, toys.optargs[i]);
+ if (status || TT.sig_other) goto err;
+
+ status = bc_stdin(&bc);
+
+err:
+ if (CFG_TOYBOX_FREE) bc_parse_free(&bc.parse);
+parse_err:
+ bc_program_free(&bc.prog);
+ toys.exitval = status == BC_STATUS_QUIT ? BC_STATUS_SUCCESS : status;
+}