fig48.py 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  1. #!/usr/bin/env pypy2
  2. # coding: utf-8
  3. #### license: creative commons cc0 1.0 (public domain)
  4. #### http://creativecommons.org/publicdomain/zero/1.0/
  5. proginf = "fig 4.8, apr 2020 mn"
  6. # what happened to 4.7? 4.7 added a few lines to try to suppress the text pygame
  7. # devs thought would be alright to display over stdout, never caring that
  8. # someone might create a text+graphics python application they would be spamming
  9. # on every run. it didnt work properly, it was taken down the same day. it only
  10. # used an implementation that someone else offered online.
  11. import sys
  12. import os
  13. from sys import stdin, stdout
  14. from os import popen
  15. # try:
  16. # import graphics
  17. # except: pass
  18. try: from sys import exit as quit
  19. except: pass
  20. buf = []
  21. cmdhelp = [("timer", "input (shared-line) change main variable to number of seconds past midnight")
  22. ,("arrstdin", "input (shared-line) change main variable to array containing lines of stdin")
  23. ,("lineinput", "input (shared-line) change main variable to string input from keyboard")
  24. ,("flineinput filepath", "input (shared-line) change main var to string of line from open file filepath")
  25. ,("time", "input (shared-line) change main variable to string of current time: hh:mm:ss")
  26. ,("arropen filepath", "input (shared-line) change main variable to array of file lines in filepath")
  27. ,("date", "input (shared-line) change main variable to string of the date: mm/dd/yyyy")
  28. ,("arrcurl url", "input (shared-line) like arropen, except downloading url into the array")
  29. ,("sleep seconds", "input (shared-line) wait for number of seconds before continuing with program")
  30. ,("command", "input (shared-line) change main variable to array of command line parameters")
  31. ,("print", "output (shared-line) output main variable to the screen (aka stdout)")
  32. ,("prints", "output (shared-line) put main var to screen; like print but (s)tays on line.")
  33. ,("fprint filepath", "output (shared-line) write main variable to open file designated by filepath")
  34. ,("display", "output (shared-line) 1st time: stop automatic graphx update. 2nd, etc: update.")
  35. ,("cls", "output (shared-line) clear the screen. currently only affects text screen")
  36. ,("graphics", "output --\\own\\line dont(or stop) suppress(ing) graphics. this is the default.")
  37. ,("textmode", "output --\\own\\line suppress graphics; force graphics commands to use text.")
  38. ,("colourtext colourcode", "output (shared-line) change colour of upcoming text to colourcode from 0 - 15")
  39. ,("colortext colorcode", "output (shared-line) change color of upcoming text to colorcode from 0 - 15")
  40. ,("highlight colourcode", "output (shared-line) change background colour of upcoming text tocolourcode 0-15")
  41. ,("locate row column", "output (shared-line) move to textmode position at row, column")
  42. ,("pset x y c", "output (shared-line) draw dot at location (x, y) in colourcode c (0 - 15)")
  43. ,("line x1 y1 x2 y2 c", "output (shared-line) draw line from (x1, y1) to (x2, y2) in colourcode c (0-15)")
  44. ,("while", "loop --\\own\\line mark the start of a loop (will keep going without break)")
  45. ,("break", "loop --\\own\\line put in the middle of a loop to exit (stop looping)")
  46. ,("for var strt stop step", "loop --\\own\\line start a for loop, changing var from strt to stop, by step")
  47. ,("forin var array", "loop --\\own\\line loop through each item in array; for each, set var to item")
  48. ,("iftrue ckvar", "conditional --\\own\\line run lines between iftrue and fig if ckvar is \"non-zero\"")
  49. ,("ifequal var1 var2", "conditional --\\own\\line run lines between ifequal and fig if var1 equals var2")
  50. ,("ifmore var1 var2", "conditional --\\own\\line run lines between ifmore and fig if var1 is > var2")
  51. ,("ifless var1 var2", "conditional --\\own\\line run lines between ifless and fig if var1 is < var2")
  52. ,("try", "conditional --\\own\\line put code that might not work between try and except")
  53. ,("except", "conditional --\\own\\line if code between try/except fails, run the code after except")
  54. ,("resume", "conditional --\\own\\line mark the end of try / except / resume command block")
  55. ,("else", "conditional --\\own\\line after if- line, before fig. run lines if condition isnt true")
  56. ,("function name p1 p2 …", "function --\\own\\line define function named name with optional params p1,p2, etc")
  57. ,("get parametername", "function (shared-line) (no longer required) copy parametername value to main var")
  58. ,("python", "function --\\own\\line put inline python code between lines python and fig")
  59. ,("fig/next/nextin/wend", "fig (interchangeable) function --\\own\\line finalise a block (started by if/while/function/for/forin")
  60. ,("pass", "function --\\own\\line blocks (for/next, etc) require something inside lines; pass works / does nothing")
  61. ,("lcase", "function (shared-line) change main variable to all-lower-case copy of own value")
  62. ,("ucase", "function (shared-line) change main variable to all-upper-case copy of own value")
  63. ,("str", "function (shared-line) convert main variable from number to string")
  64. ,("shell", "function (shared-line) run main variable contents in a command shell (os specific)")
  65. ,("asc", "function (shared-line) change main variable from string to ascii code of 1st char")
  66. ,("val", "function (shared-line) change main variable from string to numeric (int if whole)")
  67. ,("len", "function (shared-line) change main variable to numeric length of main var")
  68. ,("not", "function (shared-line) change main variable to zero if non-zero; or -1 if zero")
  69. ,("ltrim", "function (shared-line) strip whitespace from left side of main variable")
  70. ,("rtrim", "function (shared-line) strip whitespace from right side of main variable")
  71. ,("chr", "function (shared-line) change main variable from numeric to ascii/uni string")
  72. ,("arrshell", "function (shared-line) change main var to array of shell output (from main var)")
  73. ,("arreverse", "function (shared-line) change main variable from array to reverse order of array")
  74. ,("reverse", "function (shared-line) like arreverse (which might be faster for array) for strings")
  75. ,("arrsort", "function (shared-line) change main variable from array to sorted array")
  76. ,("#", "comment (can\\share) place at beginning (or end) of line, prior to a comment")
  77. ,("():;|=,. ( ) : ; | = , .", "optional (shared-line) use in a shared line (and some others) for aesthetics/notation")
  78. ,("left numofcharsoritems", "function (shared-line) change main variable to __ leftmost group of chars/items")
  79. ,("right numofchrsoritems", "function (shared-line) change main variable to __ rightmost group of chars/items")
  80. ,("arrget array position", "function (shared-line) change main variable to position-th item from array")
  81. ,("arrset position setto", "function (shared-line) change item in array in main variable to value of setto")
  82. ,("mid position len", "function (shared-line) change main variable to range of len items from position")
  83. ,("string len asciiorstr", "function (shared-line) change main variable to len instances of asciiorstr")
  84. ,("split string splitby", "function (shared-line) split string by separator splitby into array, to main var")
  85. ,("join array usestring", "function (shared-line) change main var to string by joining array using usestring")
  86. ,("instr lookin lookfor", "function (shared-line) change main var to numeric position of lookfor in lookin")
  87. ,("chdir", "function (shared-line) change current folder to path string from main variable")
  88. ,("system", "function (shared-line) put on (usually at the end of) a line to stop the program")
  89. ,("close", "function (shared-line) close the open file designated by main variable")
  90. ,("end", "function (shared-line) interchangeable with system which ends the program")
  91. ,("open mode", "function (shared-line) open file at filepath main variable in mode \"r\" or \"w\"")
  92. ,("return var", "function (shared-line) (optional) exit current function, returning value var")
  93. ,("swap var1 var2", "function (shared-line) change contents of var1 to contents of var2 and vice-versa")
  94. ,("plus numstrarr", "math (shared-line) change main variable to itself plus num or string or arr")
  95. ,("minus numeric", "math (shared-line) change main variable to itself minus numeric")
  96. ,("divby numeric", "math (shared-line) change main variable to itself divided by numeric")
  97. ,("times numeric", "math (shared-line) change main variable to itself times numeric")
  98. ,("oct", "math (shared-line) change main variable from numeric decimal to octal")
  99. ,("hex", "math (shared-line) change main variable from numeric decimal to hexadecimal")
  100. ,("cos", "math (shared-line) change numeric main variable to the cosine of itself")
  101. ,("sin", "math (shared-line) change numeric main variable to the sine of itself")
  102. ,("tan", "math (shared-line) change numeric main variable to its tangent")
  103. ,("atn", "math (shared-line) change numeric main variable to its arctangent")
  104. ,("int", "math (shared-line) change main variable from decimal (aka \"float\") to integer")
  105. ,("sgn", "math (shared-line) change main variable to 0 if 0, to -1 if < 0, or 1 if > 0.")
  106. ,("sqr", "math (shared-line) change main variable to square root of itself")
  107. ,("mod denominator", "math (shared-line) change main variable to: main var modulus denominator")
  108. ,("topwr n", "math (shared-line) raise numeric main variable to n-th power")
  109. ,("randint smallst largst", "input (shared-line) change main var to random number from smallst to largst")
  110. ,("arr", "function (shared-line) change main var to array (starting with same contents)") ]
  111. def chelp(f):
  112. ck = 0
  113. print ""
  114. for p in cmdhelp:
  115. rcmd = p[0]
  116. if f in rcmd.split()[0]:
  117. ck = 1
  118. rd = p[1].split()
  119. rcat = rd[0]
  120. rd.remove(rd[0])
  121. rt = rd[0]
  122. rd.remove(rd[0])
  123. cde = rcmd.split(" ")
  124. print ""
  125. stdout.write(" " + colour(14,0)+ cde[0])
  126. cda = cde.remove(cde[0])
  127. for c in cde:
  128. stdout.write(" " + colour(0, 7)+ " " + c + " " + colour(7,0)+" ")
  129. stdout.flush()
  130. print ""
  131. print ""
  132. print colour(3,0) + " category:", rcat, rt.replace("\\", " ")
  133. print ""
  134. print " " + colour(7,0) + " ".join(rd)
  135. print ""
  136. colour(7,0);
  137. return ck
  138. def outfilewrite(outb, p):
  139. outb += [p]
  140. #global vrck
  141. #vrck += p.strip()
  142. def colour(f, b):
  143. if f == None:
  144. f = 0
  145. if b == None:
  146. b = 0
  147. n = "0"
  148. if f > 7:
  149. n = "1"
  150. f = f - 8
  151. if f == 1: f = 4 ## switch ansi colours for qb colours
  152. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  153. if f == 3: f = 6
  154. elif f == 6: f = 3
  155. if b > 7: b = b - 8
  156. if b == 1: b = 4
  157. elif b == 4: b = 1
  158. if b == 3: b = 6
  159. elif b == 6: b = 3
  160. stdout.write("\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m")
  161. return "\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
  162. def bcolour(b):
  163. f = None
  164. if f == None: f = 0
  165. if b == None: b = 0
  166. n = "0"
  167. if f > 7:
  168. n = "1"
  169. f = f - 8
  170. if f == 1: f = 4 ## switch ansi colours for qb colours
  171. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  172. if f == 3: f = 6
  173. elif f == 6: f = 3
  174. if b > 7: b = b - 8
  175. if b == 1: b = 4
  176. elif b == 4: b = 1
  177. if b == 3: b = 6
  178. elif b == 6: b = 3
  179. stdout.write("\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m")
  180. return "\x1b[" + n + str(40+b) + "m"
  181. def sgn(p):
  182. p = float(p)
  183. if p > 0:
  184. return 1
  185. if p < 0:
  186. return -1
  187. return 0
  188. def left(p, s):
  189. return p[:s]
  190. def right(p, s):
  191. return p[-s:]
  192. def leftfour(p):
  193. try:
  194. if left(p, 4) == chr(32) * 4:
  195. p = right(p, len(p) - 4)
  196. except:
  197. pass
  198. return p
  199. def atleast(s, p):
  200. if p < s:
  201. return s
  202. else:
  203. return p
  204. def figfsp(p):
  205. pp = ""
  206. flg = 0
  207. fsp = figfsplit(p)
  208. for fp in enumerate(fsp):
  209. if flg == 0 and fp[1] in cmds.keys():
  210. pp += colour(8,0) + "_" + colour(7,0) + " "
  211. flg = cmds[fp[1]]
  212. if flg < 0:
  213. flg = flg * -1
  214. else:
  215. flg = flg + 1
  216. pp += fp[1] + " "
  217. if flg > 0:
  218. flg -= 1
  219. if flg == 0 and fp[0] + 1 < len(fsp):
  220. pp += colour(8,0) + "_" + colour(7,0) + " "
  221. return pp.rstrip().replace(colour(8,0) + "_" + colour(7,0) + " " + colour(8,0) +
  222. "_" + colour(7,0), colour(8,0) + "__" + colour(7,0)).replace(colour(8,0) + "_" +
  223. colour(7,0),colour(8,0) + "__" + colour(7,0))
  224. def figfsplit(p):
  225. # return p.split() # that was fine when strings weren't tokens
  226. # we have to make this 3 tokens: variable "hello, world!" #comment not string
  227. px = []
  228. pxc = -1 # could use len(px) -1 instead?
  229. inquotes = 0
  230. remarked = 0
  231. inspc = ""
  232. vnspc = ""
  233. for l in p:
  234. if inquotes == 0 and remarked == 0 and l == "#":
  235. remarked = 1
  236. pxc += 1
  237. px += [""]
  238. if remarked == 1:
  239. px[pxc] += l
  240. if remarked == 0:
  241. if l == "\"":
  242. if inquotes == 0:
  243. inquotes = 1
  244. pxc += 1
  245. px += [""]
  246. else: inquotes = 0
  247. if inquotes == 1:
  248. px[pxc] += l
  249. if remarked == 0 and inquotes == 0:
  250. if vnspc not in "1234567890-" + chr(32) and l[0] == ".":
  251. l = " "
  252. vnspc = l
  253. if l[0] in "():;|=,":
  254. l = " "
  255. if inspc != " " and l == " ":
  256. pxc += 1
  257. px += [""]
  258. if l != " ":
  259. if pxc == -1:
  260. pxc += 1
  261. px += [""]
  262. px[pxc] += l.lower()
  263. inspc = l
  264. #print "->", px[:]
  265. while ('') in px:
  266. px.remove('')
  267. while (':') in px:
  268. px.remove(':')
  269. for p in range(len(px)):
  270. if px[p][0] != "#":
  271. if right(px[p], 1) == ":":
  272. lenpx = len(px[p]) - 1
  273. if lenpx > 0:
  274. px[p] = left(px[p], lenpx)
  275. return px[:]
  276. def nob(p, s):
  277. r = ""
  278. if s == len(p) - 1:
  279. if len(p):
  280. if p[s].rstrip() != ".":
  281. r = p[s].rstrip()
  282. if len(r):
  283. if r[-1:] == ".":
  284. r = left(r, len(r) - 1)
  285. pfig = ""
  286. try:
  287. pfig = left(p[s], 3)
  288. except:
  289. pfig = ""
  290. if pfig.lower() == "fig" and p[s].lower() != "fig":
  291. return "figg"
  292. try:
  293. if r != "":
  294. return r
  295. else:
  296. return p[s]
  297. except:
  298. return ""
  299. def snobl(p):
  300. if "\"" in p:
  301. return p
  302. else:
  303. return p.lower()
  304. def snob(p, s):
  305. r = ""
  306. if s == len(p) - 1:
  307. if len(p):
  308. if p[s].rstrip() != ".": r = p[s].rstrip()
  309. if len(r):
  310. if r[-1:] == ".": r = left(r, len(r) - 1)
  311. pqt = ""
  312. try: pqt = left(p[s], 3)
  313. except: pqt = ""
  314. if pqt.lower() == "fig" and p[s].lower() != "fig": return "figg"
  315. try:
  316. if r != "": return snobl(r)
  317. else: return snobl(p[s])
  318. except: return ""
  319. def lnob(p, s):
  320. r = ""
  321. if s == len(p) - 1:
  322. if len(p):
  323. if p[s].rstrip() != ".": r = p[s].rstrip()
  324. if len(r):
  325. if r[-1:] == ".": r = left(r, len(r) - 1)
  326. pfig = ""
  327. try: pfig = left(p[s], 3)
  328. except: pfig = ""
  329. if pfig.lower() == "fig" and p[s].lower() != "fig": return "figg"
  330. try:
  331. if r != "": return r.lower()
  332. else: return p[s].lower()
  333. except: return ""
  334. def stripcoords(p):
  335. ps = ""
  336. for s in str(p):
  337. if s in "1234567890.": ps += s
  338. return ps
  339. def getmore(p, s):
  340. try:
  341. for t in range(1, s + 1):
  342. if len(p) == 1: p = []
  343. p = right(p, len(p) - 1)
  344. while "" in p: p.remove("")
  345. for prx in range(len(p)):
  346. if p[prx][0] == "#":
  347. p.remove(p[prx])
  348. return p
  349. except: return []
  350. def getlmore(p, s):
  351. try:
  352. for t in range(1, s + 1):
  353. if len(p) == 1: p = []
  354. p = right(p, len(p) - 1)
  355. while "" in p: p.remove("")
  356. return p
  357. except: return []
  358. def getpairs(p, s):
  359. r = ""
  360. if len(p):
  361. if p[len(p) - 1].rstrip() != ".": r = p[len(p) - 1].rstrip()
  362. if len(r):
  363. if r[-1:] == ".":
  364. r = left(r, len(r) - 1)
  365. p[len(p) - 1] = r
  366. try:
  367. p = right(p, len(p) - s)
  368. if not len(p) % 2: return p
  369. else: return []
  370. except: return []
  371. def lc():
  372. global linecount
  373. global flen
  374. es = " \x1b[0;37;40m"
  375. return "\x1b[0;37;44m" + right(chr(32) * flen + str(linecount), flen) + es
  376. def wr(p, nl):
  377. global buf
  378. buf += [p + nl]
  379. colour(11, None)
  380. print proginf; colour(7, None)
  381. print
  382. nl = chr(10)
  383. addtoout = [0]
  384. addto = [0]
  385. addtoout[0] = """import sys, os
  386. from sys import stdin, stdout
  387. from sys import argv as figargv
  388. try:
  389. from colorama import init
  390. init()
  391. except:
  392. pass # (only) windows users want colorama installed or ansi.sys enabled
  393. try:
  394. from sys import exit as quit
  395. except:
  396. pass
  397. from random import randint
  398. from time import sleep
  399. from os import chdir as figoch
  400. from os import popen as figpo
  401. from os import system as figsh
  402. from os import name as figosname
  403. figsysteme = 0
  404. figfilehandles = {}
  405. figfilecounters = {}
  406. """
  407. addtoout += [0]
  408. addto += [0]
  409. addtoout[1] = """from sys import stdout
  410. def figlocate(x, l = "ignore", c = "ignore"):
  411. import sys
  412. if l == "ignore" and c == "ignore": pass
  413. # do nothing. want it to return an error?
  414. elif l < 1 and c != "ignore":
  415. sys.stdout.write("\x1b[" + str(c) + "G") # not ansi.sys compatible
  416. elif l != "ignore" and c == "ignore":
  417. sys.stdout.write("\x1b[" + str(l) + ";" + str(1) + "H")
  418. else: sys.stdout.write("\x1b[" + str(l) + ";" + str(c) + "H")
  419. import time
  420. def fignonz(p, n=None):
  421. if n==None:
  422. if p == 0: return 1
  423. else:
  424. if p == 0: return n
  425. return p
  426. def fignot(p):
  427. if p: return 0
  428. return -1
  429. figbac = None
  430. figprsbac = None
  431. sub = None
  432. def fignone(p, figbac):
  433. if p == None: return figbac
  434. return p
  435. return -1
  436. def stopgraphics():
  437. global yourscreen
  438. global figraphics
  439. figraphics = 0
  440. # try:
  441. # except: pass
  442. """
  443. addtoout += [0]
  444. addto += [0]
  445. addtoout[2] = ""
  446. addtoout += [0]
  447. addto += [0]
  448. addtoout[3] = """figraphics = -1
  449. figrupd = 1
  450. # try:
  451. # import graphics
  452. # except:
  453. figraphics = 0
  454. yourscreen = ""
  455. # try: init graphics
  456. # except: figraphics = 0 # unable to init, just use text
  457. def figpset(z, x, y, c):
  458. global figraphics, figrupd
  459. global yourscreen
  460. global figcgapal
  461. global figcgapalcopy
  462. if figraphics == -1:
  463. # actual init -- this line was already disabled before removing pygame
  464. try:
  465. # yourscreen = # 800, 600
  466. # caption # "fig graphics screen"
  467. figraphics = 1
  468. except:
  469. stopgraphics()
  470. figraphics = 0
  471. if figraphics == 0:
  472. if x > -1 and y > -1:
  473. figcolourtext(c, c)
  474. figlocate(0, int(y) + 1, int(x) + 1)
  475. stdout.write(unichr(9608))
  476. sys.stdout.flush()
  477. # if figraphics == 1:
  478. # if x > -1 and y > -1:
  479. # x y figcgapal[c]
  480. # if figrupd: # update
  481. # process window close
  482. # if
  483. # figraphics = 0
  484. # stopgraphics()
  485. def figline(z, x, y, x2, y2, c):
  486. global figraphics, figrupd
  487. global yourscreen
  488. global figcgapal
  489. global figcgapalcopy
  490. if figraphics == -1:
  491. # actual init -- this line was already disabled before removing pygame
  492. try:
  493. # yourscreen = # 800, 600
  494. # caption # "fig graphics screen"
  495. figraphics = 1
  496. except:
  497. stopgraphics()
  498. figraphics = 0
  499. if figraphics == 0:
  500. if x > -1 and y > -1 and x2 > -1 and y2 > -1:
  501. figcolourtext(c, c)
  502. if x2 < x: x, y, x2, y2 = x2, y2, x, y
  503. figliney = [y, y2]
  504. figlinec = 0
  505. figlinestep = int(y2 - y)
  506. if figlinestep < 0:
  507. figlinestep = int(y - y2)
  508. figlinec = 0
  509. if figlinestep < 1:
  510. figlinestep = 1
  511. figlinestep = float(1) / figlinestep
  512. figlinex = x
  513. while 1:
  514. if figlinex > x2: break
  515. if y2 - y == 0:
  516. figlocate(0, int(y) + 1, int(figlinex) + 1)
  517. stdout.write(unichr(9608))
  518. elif y2 < y:
  519. figlinec -= figlinestep
  520. figlocate(0, int(y + int(float(y - y2) / fignonz(x2 - x,.1) *
  521. fignonz(figlinec,.1) ) ) + 1, int(figlinex) + 1)
  522. stdout.write(unichr(9608))
  523. else:
  524. figlocate(0, int(y + int(float(y2 - y) / fignonz(x2 - x,.1) *
  525. fignonz ( figlinec,.1) ) ) + 1, int(figlinex) + 1)
  526. stdout.write(unichr(9608))
  527. figlinec += figlinestep
  528. #[0] = figliney[0]+float(figliney[1] - figliney[0]) / (x2 - x)
  529. figlinex += figlinestep
  530. figlocate(0, int(y) + 1, int(x) + 1)
  531. stdout.write(unichr(9608))
  532. figlocate(0, int(y2) + 1, int(x2) + 1)
  533. stdout.write(unichr(9608))
  534. sys.stdout.flush()
  535. # if figraphics == 1:
  536. # if x > -1 and y > -1 and x2 > -1 and y2 > -1:
  537. # x y figcgapal[c]
  538. # line figcgapal[c] x y x2 y2
  539. # if figrupd:
  540. # update
  541. # process window close
  542. # if
  543. # figraphics = 0
  544. # stopgraphics()
  545. """
  546. addtoout += [0]
  547. addto += [0]
  548. # -2: print(variable, etc)
  549. # -1: print(variable), 0: variable = int(variable), 1: variable=left(variable, etc)
  550. cmds = {"ltrim":0, "lineinput":0, "len":0, "asc":0, "atn":0,
  551. "str":0, "get":1, "chr":0, "prints":-1, "sleep":-2,
  552. "arrsort":-1, "arreverse":-1, "reverse":0, "display":-1, "system":-1,
  553. "end":-1, "print":-1, "arrset":-3, "split":2, "left":1,
  554. "join":2, "arrget":2, "mid":2, "right":1, "plus":1,
  555. "times":1, "close":-1, "cls":-1, "flineinput":1, "fprint":-2,
  556. "open":-2, "arropen":1, "arrstdin":0, "arrcurl":1, "colourtext":-2,
  557. "highlight":-2, "divby":1, "hex":0, "rtrim":0, "string":2,
  558. "timer":0, "command":0, "time":0, "date":0, "tan":0,
  559. "oct":0, "val":0, "minus":1, "lcase":0, "ucase":0,
  560. "int":0, "left":1, "swap":-3, "locate":-3, "pset":-4,
  561. "line":-6, "return":-2, "randint":2, "topwr":1, "arr":0,
  562. "mod":1, "cos":0, "not":0, "sin":0, "instr":2,
  563. "chdir":-1, "shell":-1, "arrshell":0, "colortext":-2, "sgn":0,
  564. "sqr":0}
  565. funcs = {"function" : -1, "iftrue" : -2, "ifequal" : -3, "ifless" : -3,
  566. "ifmore" : -3, "try" : 0, "except" : 0, "resume" : 0,
  567. "else" : 0}
  568. ufunc = {}
  569. #addfuncs = addtoout[0] + addtoout[1] + addtoout[3] + """
  570. addfuncs = """
  571. def figcolortext(x, f):
  572. b = 0
  573. if f == None: f = 0
  574. if b == None: b = 0
  575. n = "0"
  576. if f > 7:
  577. n = "1"
  578. f = f - 8
  579. if f == 1: f = 4 ## switch ansi colours for qb colours
  580. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  581. if f == 3: f = 6
  582. elif f == 6: f = 3
  583. if b > 7: b = b - 8
  584. if b == 1: b = 4
  585. elif b == 4: b = 1
  586. if b == 3: b = 6
  587. elif b == 6: b = 3
  588. stdout.write("\\x1b[" + n + ";" + str(30+f) + "m")
  589. return "\\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
  590. def figcolourtext(x, f):
  591. global figcgapal
  592. global figcgapalcopy
  593. b = 0
  594. if f == None: f = 0
  595. if b == None: b = 0
  596. n = "0"
  597. if f > 7:
  598. n = "1"
  599. f = f - 8
  600. if f == 1: f = 4 ## switch ansi colours for qb colours
  601. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  602. if f == 3: f = 6
  603. elif f == 6: f = 3
  604. if b > 7: b = b - 8
  605. if b == 1: b = 4
  606. elif b == 4: b = 1
  607. if b == 3: b = 6
  608. elif b == 6: b = 3
  609. if figcgapal == figcgapalcopy:
  610. stdout.write("\\x1b[" + n + ";" + str(30+f) + "m")
  611. return "\\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
  612. else:
  613. stdout.write("\\x1b[38;2;" + str(figcgapal[c][0]) + ";" +
  614. str(figcgapal[c][1]) + ";" + str(figcgapal[c][2]) + "m")
  615. return "\\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
  616. figcgapal = [(0, 0, 0), (0, 0, 170), (0, 170, 0), (0, 170, 170),
  617. (170, 0, 0), (170, 0, 170), (170, 85, 0), (170, 170, 170),
  618. (85, 85, 85), (85, 85, 255), (85, 255, 85), (85, 255, 255),
  619. (255, 85, 85), (255, 85, 255), (255, 255, 85), (255, 255, 255)]
  620. figcgapalcopy = figcgapal[:] # support 24bit colour hack
  621. def figget(p, s):
  622. return s
  623. def fighighlight(x, b):
  624. f = None
  625. if f == None: f = 0
  626. if b == None: b = 0
  627. n = "0"
  628. if f > 7:
  629. n = "1"
  630. f = f - 8
  631. if f == 1: f = 4 ## switch ansi colours for qb colours
  632. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  633. if f == 3: f = 6
  634. elif f == 6: f = 3
  635. if b > 7: b = b - 8
  636. if b == 1: b = 4
  637. elif b == 4: b = 1
  638. if b == 3: b = 6
  639. elif b == 6: b = 3
  640. stdout.write("\\x1b[" + n + str(40+b) + "m")
  641. return "\\x1b[" + n + str(40+b) + "m"
  642. def figinstr(x, p, e):
  643. try:
  644. return p.index(e) + 1
  645. except:
  646. return 0
  647. def figchdir(p):
  648. try:
  649. figoch(p)
  650. except:
  651. print "no such file or directory: " + str(p)
  652. figend(1)
  653. def figshell(p):
  654. global figsysteme
  655. try:
  656. figsysteme = figsh(p)
  657. except:
  658. print "error running shell command: " + chr(34) + str(p) + chr(34)
  659. figend(1)
  660. def figarrshell(c):
  661. global figsysteme
  662. try:
  663. figsysteme = 0
  664. sh = figpo(c)
  665. ps = sh.read().replace(chr(13) + chr(10),
  666. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  667. figsysteme = sh.close()
  668. except:
  669. print "error running arrshell command: " + chr(34) + str(c) + chr(34)
  670. figend(1)
  671. return ps[:]
  672. def figsgn(p):
  673. p = float(p)
  674. if p > 0:
  675. return 1
  676. if p < 0:
  677. return -1
  678. return 0
  679. def figstr(p):
  680. return str(p)
  681. def figprint(p):
  682. print p
  683. def figchr(p):
  684. if type(p) == str:
  685. if len(p) > 0:
  686. return p[0]
  687. return chr(p)
  688. def figprints(p):
  689. stdout.write(str(p))
  690. sys.stdout.flush()
  691. def figleft(p, s):
  692. return p[:s]
  693. def figmid(p, s, x):
  694. arr = 0
  695. if type(p) == list or type(p) == tuple:
  696. arr = 1
  697. rt = p[s - 1:
  698. x + s - 1]
  699. if arr and len(rt) == 1:
  700. rt = rt[0]
  701. return rt
  702. def figright(p, s):
  703. return p[-s:]
  704. def figrandint(x, s, f):
  705. return randint(s, f)
  706. def figlcase(p):
  707. return p.lower()
  708. def figucase(p):
  709. return p.upper()
  710. def figint(p):
  711. return int(p)
  712. def figarrset(x, p, s):
  713. if type(s) == tuple:
  714. if len(s) == 1: fas = s[0]
  715. elif type(s) == list:
  716. if len(s) == 1: fas = s[0]
  717. else:
  718. fas = s
  719. x[p - 1] = s
  720. def figopen(x, s):
  721. import fileinput
  722. if s.lower() == "w":
  723. if (x) not in figfilehandles.keys():
  724. figfilehandles[x] = open(x[:], s.lower())
  725. elif s.lower() == "r":
  726. if (x) not in figfilehandles.keys():
  727. figfilehandles[x] = fileinput.input(x[:])
  728. figfilecounters[x] = 0
  729. else:
  730. if (x) not in figfilehandles.keys(): figfilehandles[x] = open(x[:], s[:])
  731. def figfprint(x, s):
  732. fon = figosname
  733. sep = chr(10)
  734. if fon == "nt":
  735. sep = chr(13) + chr(10)
  736. figfilehandles[s].write(str(x) + sep)
  737. def figflineinput(x, s):
  738. try:
  739. p = figfilehandles[s][figfilecounters[s]].replace(chr(13),
  740. "").replace(chr(10), "")
  741. figfilecounters[s] += 1
  742. except:
  743. p = chr(10)
  744. return p
  745. def figclose(x):
  746. if (x) in figfilehandles.keys():
  747. figfilehandles[x].close()
  748. del figfilehandles[x]
  749. try:
  750. del figfilecounters[x]
  751. except:
  752. pass
  753. def figcls(x):
  754. if figosname == "nt":
  755. cls = figsh("cls")
  756. else:
  757. stdout.write("\x1b[2J\x1b[1;1H")
  758. sys.stdout.flush()
  759. def figarropen(x, s):
  760. x = open(s).read().replace(chr(13) + chr(10), chr(10)).replace(chr(13),
  761. chr(10)).split(chr(10))
  762. return x[:]
  763. def figarrcurl(x, s):
  764. from urllib import urlopen
  765. x = str(urlopen(s).read())
  766. x = x.replace(chr(13) + chr(10),
  767. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  768. return x[:]
  769. def figarrstdin(x):
  770. ps = []
  771. for p in stdin:
  772. ps += [p[:-1]]
  773. return ps[:]
  774. def figarrget(x, p, s):
  775. if 1:
  776. return p[s - 1]
  777. def figplus(p, s):
  778. if type(p) in (float, int):
  779. if type(s) in (float, int):
  780. p = p + s
  781. else:
  782. p = p + s # float(s) if you want it easier
  783. if p == float(int(p)):
  784. p = int(p)
  785. else:
  786. if type(p) == str: p = p + s # str(s) if you want it easier
  787. if type(p) == list:
  788. if type(s) == tuple:
  789. p = p + list(s)
  790. elif type(s) == list:
  791. p = p + s[:]
  792. else:
  793. p = p + [s]
  794. if type(p) == tuple:
  795. if type(s) == tuple:
  796. p = tuple(list(p) + list(s))
  797. elif type(s) == list:
  798. p = tuple(list(p) + s[:])
  799. else:
  800. p = tuple(list(p) + [s])
  801. return p
  802. def figjoin(p, x, s):
  803. t = ""
  804. if len(x):
  805. t = str(x[0])
  806. for c in range(len(x)):
  807. if c > 0: t += str(s) + str(x[c])
  808. return t # s.join(x)
  809. def figarr(p):
  810. if type(p) in (float, int, str):
  811. p = [p]
  812. else:
  813. p = list(p)
  814. return p
  815. def figsplit(p, x, s):
  816. return x.split(s)
  817. def figval(n):
  818. n = float(n)
  819. if float(int(n)) == float(n):
  820. n = int(n)
  821. return n
  822. def figtimes(p, s):
  823. if type(p) in (float, int):
  824. p = p * s # float(s) if you want it easier
  825. if p == float(int(p)):
  826. p = int(p)
  827. else:
  828. if type(p) == list:
  829. p = p[:] * s # figval(s)
  830. else:
  831. p = p * s # figval(s) if you want it easer
  832. return p
  833. def figdivby(p, s):
  834. p = float(p) / s
  835. if p == float(int(p)):
  836. p = int(p)
  837. return p
  838. def figminus(p, s):
  839. return p - s
  840. def figtopwr(p, s):
  841. p = p ** s
  842. if p == float(int(p)):
  843. p = int(p)
  844. return p
  845. def figmod(p, s):
  846. return p % s
  847. def figcos(p):
  848. from math import cos
  849. p = cos(p)
  850. if p == float(int(p)):
  851. p = int(p)
  852. return p
  853. def figsin(p):
  854. from math import sin
  855. p = sin(p)
  856. if p == float(int(p)):
  857. p = int(p)
  858. return p
  859. def figsqr(p):
  860. from math import sqrt
  861. p = sqrt(p)
  862. if p == float(int(p)):
  863. p = int(p)
  864. return p
  865. def figltrim(p):
  866. return p.lstrip()
  867. def figlineinput(p):
  868. return raw_input()
  869. def figlen(p):
  870. return len(p)
  871. def figasc(p):
  872. return ord(p[0])
  873. def figatn(p):
  874. from math import atan
  875. p = atan(p)
  876. if p == float(int(p)):
  877. p = int(p)
  878. return p
  879. def fighex(p):
  880. return hex(p)
  881. def figrtrim(p):
  882. return p.rstrip()
  883. def figstring(x, p, n):
  884. if type(n) == str:
  885. return n * p
  886. return chr(n) * p
  887. def figtimer(p):
  888. from time import strftime
  889. return int(strftime("%H"))*60*60+int(strftime("%M"))*60+int(strftime("%S"))
  890. def figtime(p):
  891. from time import strftime
  892. return strftime("%H:%M:%S")
  893. def figdate(p):
  894. from time import strftime
  895. return strftime("%m/%d/%Y")
  896. def figcommand(p):
  897. return figargv[1:]
  898. def figtan(p):
  899. from math import tan
  900. p = tan(p)
  901. if p == float(int(p)):
  902. p = int(p)
  903. return p
  904. def figoct(p): return oct(p)
  905. def figsleep(p, s):
  906. sleep(s)
  907. def figarrsort(p):
  908. p.sort()
  909. def figdisplay(x):
  910. global figraphics, figrupd
  911. figrupd = 0
  912. # if figraphics == 1:
  913. # update
  914. def figreverse(p):
  915. if type(p) == list:
  916. p.reverse()
  917. return p
  918. elif type(p) == str:
  919. p = map(str, p)
  920. p.reverse()
  921. p = "".join(p)
  922. return p
  923. def figarreverse(p):
  924. p.reverse()
  925. def figfunction(p, s):
  926. return p
  927. def figend(x):
  928. quit()
  929. def figif(p, s):
  930. return p
  931. def figthen(p, s):
  932. return p
  933. def figsystem(x):
  934. quit()
  935. """
  936. demo = """
  937. p 7 arr times 5
  938. x "hello, world!"
  939. x 5
  940. x 5 times 7
  941. x 3 plus 5 times 7
  942. abs
  943. z x abs
  944. x z str asc abs int
  945. function hello p
  946. x "hello, "
  947. x prints return 5
  948. fig
  949. x hello x
  950. c command print
  951. function add5 r
  952. x get r plus 5 return x
  953. fig
  954. p print
  955. p print arrset 2 8 print
  956. z join p "(_)" print
  957. x z print
  958. now add5 p print
  959. p print end
  960. function ppp
  961. z 32 chr print
  962. for p 1 100 1
  963. x randint 0 3
  964. y randint 0 3
  965. c randint 1 9
  966. c colourtext 7
  967. next
  968. fig
  969. z ppp
  970. z sleep 1
  971. z ppp sleep 2 z ppp
  972. """
  973. p = ""
  974. try:
  975. p = right(sys.argv, 1)[0]
  976. except:
  977. pass
  978. if not ".fig" in p.lower():
  979. if p.lower() == "help":
  980. stdout.write(nl + " type (any) part of the command you want help on." +
  981. nl + nl + " fig will show all matches." + nl * 3 + " " * 4)
  982. helpf = chelp(raw_input())
  983. if not helpf:
  984. print(colour(14,0) + nl + " no commands match your search.")
  985. print("")
  986. colour(7,0)
  987. quit()
  988. else:
  989. print "using built-in demo source, translating to demo.fig.py..."
  990. print
  991. p = "demo.fig"
  992. inputfile = demo.replace(chr(13), "").split(nl)
  993. else:
  994. try:
  995. inputfile = open(p).read().replace(chr(13) + chr(10),
  996. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  997. except:
  998. print "couldn't open \"" + p + "\", exiting."
  999. print
  1000. quit()
  1001. try:
  1002. outfile = open(p + ".py", "w")
  1003. except:
  1004. print "couldn't write to \"" + p + ".py" "\", exiting."
  1005. print
  1006. quit()
  1007. outname = p + ".py"
  1008. flen = len(str(len(inputfile)))
  1009. linecount = 0
  1010. indent = 0
  1011. inlinep = 0
  1012. errorin = 0
  1013. errorsrc = ""
  1014. error = ""
  1015. mode = 0
  1016. figraphics = -1 # -1 = uninitialised, 0 = textmode, 1 = initialised
  1017. vrs = []
  1018. vr = ""
  1019. outb = []
  1020. ingfx = 0
  1021. linesoutc = 0
  1022. for p in inputfile:
  1023. linecount += 1
  1024. vrop = 0
  1025. vrcl = 0
  1026. if linecount == 1:
  1027. outfile.write("#!/usr/bin/env pypy2" + nl + "# encoding: utf-8" + nl)
  1028. if "," in proginf:
  1029. outfile.write("# fig translator version: " + proginf.split(",")[0] + nl)
  1030. if inlinep:
  1031. if p.lower().strip() == "fig":
  1032. inlinep = 0
  1033. print lc() + p
  1034. indent = atleast(0, indent - 4)
  1035. else:
  1036. print lc() + colour(2, None) + p + colour(7, None)
  1037. #% write copied lines of inline python
  1038. outfilewrite(outb, chr(32) * atleast(0, indent - 4) +
  1039. leftfour(p) + nl)
  1040. elif mode == "output the following:":
  1041. if p.lower().strip() == "display":
  1042. mode = 0
  1043. print lc() + p
  1044. else:
  1045. wr(chr(32) * atleast(0, indent) + "print \"" + p.replace(chr(34),
  1046. "\" + chr(34) + \"").replace(chr(92), "\" + chr(92) + \"") + "\"")
  1047. print lc() + p.replace(chr(34), "\" + chr(34) + \"").replace(chr(92),
  1048. "\" + chr(92) + \"", nl)
  1049. elif mode == 0:
  1050. x = figfsplit(p.lstrip())
  1051. lp = p.lower()
  1052. if not len(p):
  1053. print lc() + ""
  1054. #% write copied blank lines from inline python
  1055. outfilewrite(outb, nl)
  1056. if len(p.lstrip()):
  1057. e = 0
  1058. if p.lstrip()[0] == "#":
  1059. if linecount == 1:
  1060. es = 0
  1061. try:
  1062. if p.lstrip()[1] == "!":
  1063. es = 1
  1064. except: es = 0
  1065. if not es:
  1066. wr(p, nl)
  1067. print lc(), figfsp(p)
  1068. else: print lc() + "[this first comment isn't copied over]"
  1069. es = 0
  1070. else:
  1071. #% write comments
  1072. #print colour(14, 0) + p + colour(7,0) ; znul = raw_input() #$
  1073. outfilewrite(outb, chr(32) * atleast(0, indent) + p + nl)
  1074. print lc(), figfsp(p)
  1075. elif lnob(x, 0) == "figg":
  1076. e = 2
  1077. elif lp.rstrip() == "python":
  1078. indent += 4
  1079. inlinep = 1
  1080. print lc() + p
  1081. else:
  1082. if not lnob(x, 0) == "figg":
  1083. if lnob(x, 0) != "fig" and not lnob(x,
  1084. 0) in cmds.keys() and not lnob(x,
  1085. 0) in funcs.keys() + ["forin", "for", "function", "nextin",
  1086. "next", "while", "wend"] + ["break", "pass"]:
  1087. if not lnob(x, 0) in vrs:
  1088. vrs += [lnob(x, 0)[:]] # main vars, also func params, etc
  1089. #% write variable
  1090. #var: print colour(14, 0) + "variable:" + lnob(x, 0) + colour(7,0) ; znul = raw_input() #$
  1091. outfilewrite(outb, nl)
  1092. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1093. "figlist = 0" + nl)
  1094. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1095. "try: figlist = int(type(" + lnob(x, 0) + ") == list)" + nl)
  1096. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1097. "except NameError: pass" + nl)
  1098. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1099. "if not figlist: " + lnob(x, 0) + " = 0" + nl)
  1100. if lnob(x, 0) == "fig":
  1101. #print lc () + p
  1102. #% write? its whitespace
  1103. #$
  1104. indent = atleast(0, indent - 4)
  1105. if lnob(x, 0) == "wend":
  1106. #print lc () + p
  1107. #% write? its whitespace
  1108. #$
  1109. indent = atleast(0, indent - 4)
  1110. if lnob(x, 0) == "next":
  1111. #print lc () + p
  1112. #% write? its whitespace
  1113. #$
  1114. indent = atleast(0, indent - 4)
  1115. if lnob(x, 0) == "nextin":
  1116. #print lc () + p
  1117. #% write? its whitespace
  1118. #$
  1119. indent = atleast(0, indent - 4)
  1120. if lnob(x, 0) == "try":
  1121. #print lc () + p
  1122. #% write try line
  1123. #$
  1124. outfilewrite(outb, chr(32) * atleast(0, indent) + "try:" + nl)
  1125. indent = atleast(0, indent + 4)
  1126. if lnob(x, 0) == "else":
  1127. #print lc () + p
  1128. #% write else line
  1129. #$
  1130. outfilewrite(outb, chr(32) * atleast(0, indent - 4) +
  1131. "else:" + nl)
  1132. if lnob(x, 0) == "except":
  1133. #print lc () + p
  1134. indent = atleast(0, indent - 4)
  1135. #% write except line
  1136. #$
  1137. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1138. "except:" + nl)
  1139. indent = atleast(0, indent + 4)
  1140. if lnob(x, 0) == "resume":
  1141. #print lc () + p
  1142. #% write? its whitespace
  1143. #$
  1144. indent = atleast(0, indent - 4)
  1145. if lnob(x, 0) == "while":
  1146. #print lc () + p
  1147. #% write simple loop
  1148. #$
  1149. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1150. "while 1:" + nl)
  1151. indent += 4
  1152. if lnob(x, 0) == "function" and len(getmore(x, 1)) > 0:
  1153. #print lc () + p
  1154. mkf = []
  1155. funcname = getlmore(x, 1)[0]
  1156. prm = 1
  1157. while 1:
  1158. try:
  1159. aprm = getlmore(x, 1)[prm]
  1160. if len(aprm):
  1161. if aprm[0] != "#":
  1162. mkf += [aprm]
  1163. if aprm not in vrs: vrs += [aprm[:]]
  1164. prm += 1
  1165. except: break
  1166. ufunc[funcname] = mkf[:] #; print ufunc # #
  1167. #print ufunc
  1168. #print len(ufunc[funcname])
  1169. #% write func def
  1170. #$ print colour(14,0)+ "def " + funcname + "(" + ", ".join(mkf) + "):" + colour(7,0)
  1171. outfilewrite(outb, chr(32) * atleast(0, indent) + "def " +
  1172. funcname + "(" + ", ".join(mkf) + "):" + nl)
  1173. indent += 4
  1174. if lnob(x, 0) == "for" and len(getmore(x, 1)) == 4:
  1175. #print lc () + p
  1176. gmro = getlmore(x, 1)[0]
  1177. gmrt = getlmore(x, 1)[1]
  1178. gmrh = getlmore(x, 1)[2]
  1179. gmrf = getlmore(x, 1)[3]
  1180. if gmro not in vrs: vrs += [gmro[:]]
  1181. if "." not in gmrf and (gmrf.strip()) not in ("0",
  1182. "0.0", "-0") and "." not in gmrt and "." not in gmrh:
  1183. #% write standard for loop
  1184. #$
  1185. outfilewrite(outb, chr(32) * atleast(0, indent)
  1186. + "for "
  1187. + gmro + " in range(int(float(" + gmrt +
  1188. ")), int(float(" + gmrh + ")) + figsgn(" + gmrf +
  1189. "), fignonz(int(float(" + gmrf + ")))):" + nl)
  1190. else:
  1191. #% write for loop that allows floating step
  1192. #$
  1193. outfilewrite(outb, chr(32) * atleast(0, indent) + gmro
  1194. + " = float(" + gmrt + ") - float(" + gmrf + ")" + nl +
  1195. chr(32) * atleast(0, indent) + "while 1:" + nl + chr(32) *
  1196. atleast(0, indent + 4) + gmro + " += float(" + gmrf +
  1197. ")" + nl + chr(32) * atleast(0, indent + 4) + "if " +
  1198. gmrf +
  1199. " > 0 and " + gmro + " > float(" + gmrh + "): break" + nl
  1200. + chr(32) * atleast(0, indent + 4) + "elif " + gmrf +
  1201. " <= 0 and " + gmro +
  1202. " < float(" + gmrh + "): break" + nl)
  1203. indent += 4
  1204. if lnob(x, 0) == "forin" and len(getmore(x, 1)) == 2:
  1205. #print lc () + p
  1206. gmro = getlmore(x, 1)[0]
  1207. gmrt = getlmore(x, 1)[1]
  1208. if gmro not in vrs: vrs += [gmro[:]]
  1209. #% write forin command with params
  1210. #$
  1211. outfilewrite(outb, chr(32) * atleast(0, indent) + "for " +
  1212. gmro + " in " + gmrt + ":" + nl)
  1213. indent += 4
  1214. if lnob(x, 0) == "break":
  1215. #print lc () + p
  1216. #% write break command
  1217. #$ print
  1218. outfilewrite(outb, chr(32) *
  1219. atleast(0, indent) + "break" + nl)
  1220. if lp.rstrip() == "graphics":
  1221. ingfx = 1
  1222. #% write change to default mode (dont suppress gfx)
  1223. #$
  1224. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1225. "figraphics = -1" + nl)
  1226. figraphics = -1
  1227. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1228. "figpset(0, -1, -1, 0)" + nl)
  1229. #print lc () + p
  1230. if lnob(x, 0) == "textmode":
  1231. #print lc () + p
  1232. addto[3] = 1
  1233. #% write change to text mode (suppress graphics)
  1234. #$
  1235. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1236. "figraphics = 0" + nl)
  1237. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1238. "stopgraphics()" + nl)
  1239. figraphics = 0
  1240. if lnob(x, 0) == "pass":
  1241. #print lc () + p
  1242. #% write pass command
  1243. #$ print
  1244. outfilewrite(outb, chr(32) *
  1245. atleast(0, indent) + "pass" + nl)
  1246. if lnob(x, 0) == "iftrue":
  1247. #print lc () + p
  1248. #% write iftrue
  1249. #$ print colour(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +colour(7,0)
  1250. outfilewrite(outb, chr(32) * atleast(0, indent) + "if " +
  1251. snob(x, 1) + ":" + nl)
  1252. indent += 4
  1253. if lnob(x, 0) == "ifequal" and len(getmore(x, 1)) == 2:
  1254. #print lc () + p
  1255. #% write ifequal
  1256. #$ print colour(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +colour(7,0)
  1257. outfilewrite(outb, chr(32) * atleast(0, indent) + "if " +
  1258. snob(x, 1) + " == " + snob(x, 2) + ":" + nl)
  1259. indent += 4
  1260. if lnob(x, 0) == "ifless" and len(getmore(x, 1)) == 2:
  1261. #print lc () + p
  1262. #% write ifless
  1263. #$ print colour(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +colour(7,0)
  1264. outfilewrite(outb, chr(32) * atleast(0, indent) + "if " +
  1265. snob(x, 1) + " < " + snob(x, 2) + ":" + nl)
  1266. indent += 4
  1267. if lnob(x, 0) == "ifmore" and len(getmore(x, 1)) == 2:
  1268. #print lc () + p
  1269. #% write ifmore
  1270. #$ print colour(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +colour(7,0)
  1271. outfilewrite(outb, chr(32) * atleast(0, indent) + "if " +
  1272. snob(x, 1) + " > " + snob(x, 2) + ":" + nl)
  1273. indent += 4
  1274. if lnob(x, 0) in cmds.keys(): # + ufunc.keys():
  1275. e = 4
  1276. shln = lnob(x, 0)
  1277. if lnob(x, 0) != "fig" and lnob(x,
  1278. 0) not in funcs.keys() + ["forin", "for", "function",
  1279. "nextin", "next", "while", "wend"] + ["break", "pass"]:
  1280. #print lc () + p
  1281. vr = lnob(x, 0)
  1282. #print vr, type(vr)
  1283. #print getlmore(x, 1)
  1284. prsc = 0
  1285. cstrctr = 0
  1286. csbuf = []
  1287. vrcs = ""
  1288. for prs in getlmore(x, 1):
  1289. #$ print prs
  1290. if "fig" in prs:
  1291. if prs[:3] == "fig":
  1292. e = 2
  1293. break
  1294. break
  1295. if prs in funcs.keys():
  1296. e = 3
  1297. shln = prs
  1298. prsc += 1
  1299. if cstrctr > 0:
  1300. vrcs += prs
  1301. cstrctr -= 1
  1302. if cstrctr == 0:
  1303. if lnob(x, prsc - 1) == "return":
  1304. #% write return command
  1305. #$ print colour(14,0) +vrcs + " ; " +colour(7,0)
  1306. outfilewrite(outb, chr(32) * atleast(0,
  1307. indent) + vrcs)
  1308. elif lnob(x, prsc - 2) == "swap":
  1309. vrcs = lnob(x, prsc - 1) + ", " + lnob(x,
  1310. prsc - 0) + " = " + lnob(x,
  1311. prsc - 0) + ", " + lnob(x, prsc - 1)
  1312. #% write swap of 2 vars in python syntax
  1313. #$ print colour(14,0) +vrcs + " ; " +colour(7,0)
  1314. outfilewrite(outb, chr(32) * atleast(0,
  1315. indent) + vrcs + " ; ")
  1316. else:
  1317. if figprsbac in ufunc.keys():
  1318. #% write parametered func call
  1319. #$ print colour(14,0)+ vrcs + ") ; " + lnob(x, 0) + " = fignone(" + lnob(x, 0) + ", figbac) ; " +colour(7,0)
  1320. outfilewrite(outb, chr(32) * atleast(0,
  1321. indent) + vrcs + ") ; " + lnob(x, 0) +
  1322. " = fignone(" + lnob(x, 0) +
  1323. ", figbac) ; ")
  1324. vrcl += 1
  1325. else:
  1326. #% write builtin func call assignment
  1327. #$ print colour(14,0)+ vr + " = " + vrcs + ") ; " +colour(7,0)
  1328. outfilewrite(outb, chr(32) *
  1329. atleast(0, indent) + vrcs + ")" + nl)
  1330. vrcl += 1
  1331. else:
  1332. vrcs += ", " #; print "*"
  1333. #if
  1334. continue
  1335. if prs.lower() in funcs.keys() + ["forin", "for",
  1336. "function", "nextin", "next", "while",
  1337. "wend"] + ["break", "pass"]:
  1338. e = 3
  1339. figprsbac = None
  1340. if cstrctr == 0:
  1341. if not prs.lower() in vrs:
  1342. if prs.lower()[0] in "abcdefghijklmnopqrstuvwxyz":
  1343. if not prs.lower() in ufunc.keys():
  1344. if not prs.lower() in funcs.keys():
  1345. if not prs.lower() in cmds.keys():
  1346. e = 5
  1347. shln = prs
  1348. if prs.lower() in vrs and cstrctr == 0:
  1349. #and len(getmore(x, 1)) == 1:
  1350. #% write lefthand variable assignment
  1351. #$ print colour(14,0)+ vr + " = " + prs.lower() +colour(7,0)
  1352. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1353. vr + " = " + prs.lower() + nl)
  1354. if prs[0] == "\"":
  1355. #% write string assignment (any place in shared line)
  1356. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1357. vr + " = " + prs + nl)
  1358. if prs[0] in ".1234567890-":
  1359. #% write numerics
  1360. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1361. vr + " = " + prs + nl)
  1362. if prs[0] == "#":
  1363. #% write trailing comments #$ print colour(14, 0) + prs + colour(7,0)
  1364. outfilewrite(outb, prs + "" + nl)
  1365. break
  1366. if prs.lower() in ufunc.keys():
  1367. #% write pre-func-call var backup for sub-style behavior #$ print colour(14, 0) + "figbac = " + lnob(x,0) + " ; " + colour(7,0)
  1368. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1369. "figbac = " + lnob(x,0) + nl) # ##
  1370. figprsbac = prs.lower()
  1371. cstrctr = len(ufunc[prs])
  1372. #print cstrctr
  1373. if cstrctr == 0:
  1374. #% write zero-param func/?sub call
  1375. #$ print colour(14, 0) + vr + " = " + prs.lower() + "() ; " + lnob(x, 0) + " = fignone(" + lnob(x, 0) + ", figbac) ; " + colour(7,0)
  1376. outfilewrite(outb,
  1377. chr(32) * atleast(0, indent) +
  1378. vr + " = " + prs.lower() + "()" + nl + chr(32) * atleast(0, indent) +
  1379. lnob(x, 0) + " = fignone(" + lnob(x, 0) + ", figbac)" + nl) # #
  1380. else:
  1381. #print "y"
  1382. vrop += 1
  1383. vrcs = vr + " = " + prs.lower() + "("
  1384. #$ print colour(4, 0) + vr + " = " + prs.lower() + "(" + colour(7,0) #$
  1385. #multiparameter
  1386. if prs.lower() in cmds.keys():
  1387. if prs.lower() in ["display", "pset", "line"]:
  1388. ingfx = 1
  1389. ##print prs
  1390. cstrctr = cmds[prs]
  1391. ##print cstrctr
  1392. if cstrctr == -1:
  1393. #% write zero-param subs
  1394. #print colour(14, 0) + "fig" + prs.lower() + "(" + vr
  1395. #+ ") ; " + colour(7,0) ; #znul = raw_input() #$
  1396. outfilewrite(outb, chr(32) *
  1397. atleast(0, indent) + "fig" +
  1398. prs.lower() + "(" + vr + ")" + nl)
  1399. vrcl += 1
  1400. if cstrctr == 0:
  1401. #% write zero-param functions
  1402. #print colour(14, 0) + vr + " = fig" + prs.lower()
  1403. #+ "(" + vr + ") ; "+ colour(7,0) ; #znul = raw_input() #$
  1404. outfilewrite(outb, chr(32) * atleast(0,
  1405. indent) + vr +
  1406. " = fig" + prs.lower() + "(" + vr + ")" + nl)
  1407. vrcl += 1
  1408. if cstrctr < -1:
  1409. if prs == "return":
  1410. cstrctr = abs(cstrctr) - 1
  1411. vrcs = "return " #parameter
  1412. else:
  1413. cstrctr = abs(cstrctr) - 1
  1414. if prs == "swap":
  1415. vrcs = "swap "
  1416. else:
  1417. vrop += 1
  1418. vrcs = "fig" + prs.lower() + "(" + vr
  1419. vrcs += ", " #multiparameter
  1420. else:
  1421. vrop += 1
  1422. vrcs = vr + " = fig" + prs.lower() + "(" + vr
  1423. vrcs += ", " #multiparameter
  1424. if vrop == vrcl and e == 0:
  1425. print lc(), figfsp(p)
  1426. #% finish each line with lf
  1427. outfilewrite(outb, "" + nl)
  1428. else:
  1429. print lc() + p
  1430. else:
  1431. e = 2
  1432. if e == 1:
  1433. e = 0
  1434. if not len(error):
  1435. error = "error: problem in command structure or details."
  1436. errorin = linecount
  1437. errorsrc = p
  1438. print lc() + colour(14, 0) + str(p) + colour(7, 0)
  1439. break
  1440. if e == 2:
  1441. e = 0
  1442. if not len(error):
  1443. error = "error: cannot create variable or function beginning"
  1444. error += " with \"fig\""
  1445. errorin = linecount
  1446. errorsrc = p
  1447. print lc() + colour(14, 0) + p + colour(7, 0)
  1448. break
  1449. if e == 3:
  1450. e = 0
  1451. if not len(error):
  1452. error = "error: single-line command \"" + shln + "\" not on own line"
  1453. errorin = linecount
  1454. errorsrc = p
  1455. print lc() + colour(14, 0) + p + colour(7, 0)
  1456. break
  1457. if e == 4:
  1458. e = 0
  1459. if not len(error):
  1460. error = "error: shared-line function \""
  1461. error += shln + "\" cannot be used to start a line"
  1462. errorin = linecount
  1463. errorsrc = p
  1464. print lc() + colour(14, 0) + p + colour(7, 0)
  1465. break
  1466. if e == 5:
  1467. e = 0
  1468. if not len(error):
  1469. error = "error: variable or function not created, but referenced... \""
  1470. error += shln + "\" needs to be set before first use"
  1471. errorin = linecount
  1472. errorsrc = p
  1473. print lc() + colour(14, 0) + p + colour(7, 0)
  1474. break
  1475. if vrcl != vrop:
  1476. e = 0
  1477. if not len(error):
  1478. error = "error: a command has the wrong number of parameters."
  1479. errorin = linecount
  1480. errorsrc = p
  1481. print lc() + colour(14, 0) + str(p) + colour(7, 0)
  1482. break
  1483. if ingfx == 0:
  1484. addtoout[3] = ""
  1485. outfile.write(addtoout[0] + addtoout[1] + addtoout[3] + addfuncs)
  1486. for outsb in outb:
  1487. outfile.write(outsb)
  1488. outfile.close()
  1489. print
  1490. if errorin:
  1491. print error
  1492. colour(14, None)
  1493. print "error in line " + str(errorin) + ":"
  1494. colour(7, None)
  1495. print errorsrc
  1496. else:
  1497. try:
  1498. os.system("chmod +x \"" + outname + "\"")
  1499. except:
  1500. pass
  1501. colour (11, None)
  1502. print "translation complete. ",
  1503. colour(7, None)
  1504. print "here's a python script you can run: ",
  1505. print colour(11, None) + outname + colour(7, None)
  1506. print