fig49.py 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  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.9, 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. try:
  476. stdout.write(unichr(9608))
  477. except:
  478. stdout.write('M')
  479. sys.stdout.flush()
  480. # if figraphics == 1:
  481. # if x > -1 and y > -1:
  482. # x y figcgapal[c]
  483. # if figrupd: # update
  484. # process window close
  485. # if
  486. # figraphics = 0
  487. # stopgraphics()
  488. def figline(z, x, y, x2, y2, c):
  489. global figraphics, figrupd
  490. global yourscreen
  491. global figcgapal
  492. global figcgapalcopy
  493. if figraphics == -1:
  494. # actual init -- this line was already disabled before removing pygame
  495. try:
  496. # yourscreen = # 800, 600
  497. # caption # "fig graphics screen"
  498. figraphics = 1
  499. except:
  500. stopgraphics()
  501. figraphics = 0
  502. if figraphics == 0:
  503. if x > -1 and y > -1 and x2 > -1 and y2 > -1:
  504. figcolourtext(c, c)
  505. if x2 < x: x, y, x2, y2 = x2, y2, x, y
  506. figliney = [y, y2]
  507. figlinec = 0
  508. figlinestep = int(y2 - y)
  509. if figlinestep < 0:
  510. figlinestep = int(y - y2)
  511. figlinec = 0
  512. if figlinestep < 1:
  513. figlinestep = 1
  514. figlinestep = float(1) / figlinestep
  515. figlinex = x
  516. while 1:
  517. if figlinex > x2: break
  518. if y2 - y == 0:
  519. figlocate(0, int(y) + 1, int(figlinex) + 1)
  520. try:
  521. stdout.write(unichr(9608))
  522. except:
  523. stdout.write('M')
  524. elif y2 < y:
  525. figlinec -= figlinestep
  526. figlocate(0, int(y + int(float(y - y2) / fignonz(x2 - x,.1) *
  527. fignonz(figlinec,.1) ) ) + 1, int(figlinex) + 1)
  528. try:
  529. stdout.write(unichr(9608))
  530. except:
  531. stdout.write('M')
  532. else:
  533. figlocate(0, int(y + int(float(y2 - y) / fignonz(x2 - x,.1) *
  534. fignonz ( figlinec,.1) ) ) + 1, int(figlinex) + 1)
  535. try:
  536. stdout.write(unichr(9608))
  537. except:
  538. stdout.write('M')
  539. figlinec += figlinestep
  540. #[0] = figliney[0]+float(figliney[1] - figliney[0]) / (x2 - x)
  541. figlinex += figlinestep
  542. figlocate(0, int(y) + 1, int(x) + 1)
  543. try:
  544. stdout.write(unichr(9608))
  545. except:
  546. stdout.write('M')
  547. figlocate(0, int(y2) + 1, int(x2) + 1)
  548. try:
  549. stdout.write(unichr(9608))
  550. except:
  551. stdout.write('M')
  552. sys.stdout.flush()
  553. # if figraphics == 1:
  554. # if x > -1 and y > -1 and x2 > -1 and y2 > -1:
  555. # x y figcgapal[c]
  556. # line figcgapal[c] x y x2 y2
  557. # if figrupd:
  558. # update
  559. # process window close
  560. # if
  561. # figraphics = 0
  562. # stopgraphics()
  563. """
  564. addtoout += [0]
  565. addto += [0]
  566. # -2: print(variable, etc)
  567. # -1: print(variable), 0: variable = int(variable), 1: variable=left(variable, etc)
  568. cmds = {"ltrim":0, "lineinput":0, "len":0, "asc":0, "atn":0,
  569. "str":0, "get":1, "chr":0, "prints":-1, "sleep":-2,
  570. "arrsort":-1, "arreverse":-1, "reverse":0, "display":-1, "system":-1,
  571. "end":-1, "print":-1, "arrset":-3, "split":2, "left":1,
  572. "join":2, "arrget":2, "mid":2, "right":1, "plus":1,
  573. "times":1, "close":-1, "cls":-1, "flineinput":1, "fprint":-2,
  574. "open":-2, "arropen":1, "arrstdin":0, "arrcurl":1, "colourtext":-2,
  575. "highlight":-2, "divby":1, "hex":0, "rtrim":0, "string":2,
  576. "timer":0, "command":0, "time":0, "date":0, "tan":0,
  577. "oct":0, "val":0, "minus":1, "lcase":0, "ucase":0,
  578. "int":0, "left":1, "swap":-3, "locate":-3, "pset":-4,
  579. "line":-6, "return":-2, "randint":2, "topwr":1, "arr":0,
  580. "mod":1, "cos":0, "not":0, "sin":0, "instr":2,
  581. "chdir":-1, "shell":-1, "arrshell":0, "colortext":-2, "sgn":0,
  582. "sqr":0}
  583. funcs = {"function" : -1, "iftrue" : -2, "ifequal" : -3, "ifless" : -3,
  584. "ifmore" : -3, "try" : 0, "except" : 0, "resume" : 0,
  585. "else" : 0}
  586. ufunc = {}
  587. #addfuncs = addtoout[0] + addtoout[1] + addtoout[3] + """
  588. addfuncs = """
  589. def figcolortext(x, f):
  590. b = 0
  591. if f == None: f = 0
  592. if b == None: b = 0
  593. n = "0"
  594. if f > 7:
  595. n = "1"
  596. f = f - 8
  597. if f == 1: f = 4 ## switch ansi colours for qb colours
  598. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  599. if f == 3: f = 6
  600. elif f == 6: f = 3
  601. if b > 7: b = b - 8
  602. if b == 1: b = 4
  603. elif b == 4: b = 1
  604. if b == 3: b = 6
  605. elif b == 6: b = 3
  606. stdout.write("\\x1b[" + n + ";" + str(30+f) + "m")
  607. return "\\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
  608. def figcolourtext(x, f):
  609. global figcgapal
  610. global figcgapalcopy
  611. f24 = f
  612. b = 0
  613. if f == None: f = 0
  614. if b == None: b = 0
  615. n = "0"
  616. if f > 7:
  617. n = "1"
  618. f = f - 8
  619. if f == 1: f = 4 ## switch ansi colours for qb colours
  620. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  621. if f == 3: f = 6
  622. elif f == 6: f = 3
  623. if b > 7: b = b - 8
  624. if b == 1: b = 4
  625. elif b == 4: b = 1
  626. if b == 3: b = 6
  627. elif b == 6: b = 3
  628. if figcgapal == figcgapalcopy:
  629. stdout.write("\\x1b[" + n + ";" + str(30+f) + "m")
  630. return "\\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
  631. else:
  632. stdout.write("\\x1b[38;2;" + str(figcgapal[f24][0]) + ";" +
  633. str(figcgapal[f24][1]) + ";" + str(figcgapal[f24][2]) + "m")
  634. return "\\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
  635. figcgapal = [(0, 0, 0), (0, 0, 170), (0, 170, 0), (0, 170, 170),
  636. (170, 0, 0), (170, 0, 170), (170, 85, 0), (170, 170, 170),
  637. (85, 85, 85), (85, 85, 255), (85, 255, 85), (85, 255, 255),
  638. (255, 85, 85), (255, 85, 255), (255, 255, 85), (255, 255, 255)]
  639. figcgapalcopy = figcgapal[:] # support 24bit colour hack
  640. def figget(p, s):
  641. return s
  642. def fighighlight(x, b):
  643. f = None
  644. if f == None: f = 0
  645. if b == None: b = 0
  646. n = "0"
  647. if f > 7:
  648. n = "1"
  649. f = f - 8
  650. if f == 1: f = 4 ## switch ansi colours for qb colours
  651. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  652. if f == 3: f = 6
  653. elif f == 6: f = 3
  654. if b > 7: b = b - 8
  655. if b == 1: b = 4
  656. elif b == 4: b = 1
  657. if b == 3: b = 6
  658. elif b == 6: b = 3
  659. stdout.write("\\x1b[" + n + str(40+b) + "m")
  660. return "\\x1b[" + n + str(40+b) + "m"
  661. def figinstr(x, p, e):
  662. try:
  663. return p.index(e) + 1
  664. except:
  665. return 0
  666. def figchdir(p):
  667. try:
  668. figoch(p)
  669. except:
  670. print "no such file or directory: " + str(p)
  671. figend(1)
  672. def figshell(p):
  673. global figsysteme
  674. try:
  675. figsysteme = figsh(p)
  676. except:
  677. print "error running shell command: " + chr(34) + str(p) + chr(34)
  678. figend(1)
  679. def figarrshell(c):
  680. global figsysteme
  681. try:
  682. figsysteme = 0
  683. sh = figpo(c)
  684. ps = sh.read().replace(chr(13) + chr(10),
  685. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  686. figsysteme = sh.close()
  687. except:
  688. print "error running arrshell command: " + chr(34) + str(c) + chr(34)
  689. figend(1)
  690. return ps[:]
  691. def figsgn(p):
  692. p = float(p)
  693. if p > 0:
  694. return 1
  695. if p < 0:
  696. return -1
  697. return 0
  698. def figstr(p):
  699. return str(p)
  700. def figprint(p):
  701. print p
  702. def figchr(p):
  703. if type(p) == str:
  704. if len(p) > 0:
  705. return p[0]
  706. return chr(p)
  707. def figprints(p):
  708. stdout.write(str(p))
  709. sys.stdout.flush()
  710. def figleft(p, s):
  711. return p[:s]
  712. def figmid(p, s, x):
  713. arr = 0
  714. if type(p) == list or type(p) == tuple:
  715. arr = 1
  716. rt = p[s - 1:
  717. x + s - 1]
  718. if arr and len(rt) == 1:
  719. rt = rt[0]
  720. return rt
  721. def figright(p, s):
  722. return p[-s:]
  723. def figrandint(x, s, f):
  724. return randint(s, f)
  725. def figlcase(p):
  726. return p.lower()
  727. def figucase(p):
  728. return p.upper()
  729. def figint(p):
  730. return int(p)
  731. def figarrset(x, p, s):
  732. if type(s) == tuple:
  733. if len(s) == 1: fas = s[0]
  734. elif type(s) == list:
  735. if len(s) == 1: fas = s[0]
  736. else:
  737. fas = s
  738. x[p - 1] = s
  739. def figopen(x, s):
  740. import fileinput
  741. if s.lower() == "w":
  742. if (x) not in figfilehandles.keys():
  743. figfilehandles[x] = open(x[:], s.lower())
  744. elif s.lower() == "r":
  745. if (x) not in figfilehandles.keys():
  746. figfilehandles[x] = fileinput.input(x[:])
  747. figfilecounters[x] = 0
  748. else:
  749. if (x) not in figfilehandles.keys(): figfilehandles[x] = open(x[:], s[:])
  750. def figfprint(x, s):
  751. fon = figosname
  752. sep = chr(10)
  753. if fon == "nt":
  754. sep = chr(13) + chr(10)
  755. figfilehandles[s].write(str(x) + sep)
  756. def figflineinput(x, s):
  757. try:
  758. p = figfilehandles[s][figfilecounters[s]].replace(chr(13),
  759. "").replace(chr(10), "")
  760. figfilecounters[s] += 1
  761. except:
  762. p = chr(10)
  763. return p
  764. def figclose(x):
  765. if (x) in figfilehandles.keys():
  766. figfilehandles[x].close()
  767. del figfilehandles[x]
  768. try:
  769. del figfilecounters[x]
  770. except:
  771. pass
  772. def figcls(x):
  773. if figosname == "nt":
  774. cls = figsh("cls")
  775. else:
  776. stdout.write("\x1b[2J\x1b[1;1H")
  777. sys.stdout.flush()
  778. def figarropen(x, s):
  779. p = open(s)
  780. f = p.read().replace(chr(13) + chr(10), chr(10)).replace(chr(13),
  781. chr(10)).split(chr(10))
  782. p.close()
  783. return f[:]
  784. def figarrcurl(x, s):
  785. from urllib import urlopen
  786. x = str(urlopen(s).read())
  787. x = x.replace(chr(13) + chr(10),
  788. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  789. return x[:]
  790. def figarrstdin(x):
  791. ps = []
  792. for p in stdin:
  793. ps += [p[:-1]]
  794. return ps[:]
  795. def figarrget(x, p, s):
  796. if 1:
  797. return p[s - 1]
  798. def figplus(p, s):
  799. if type(p) in (float, int):
  800. if type(s) in (float, int):
  801. p = p + s
  802. else:
  803. p = p + s # float(s) if you want it easier
  804. if p == float(int(p)):
  805. p = int(p)
  806. else:
  807. if type(p) == str: p = p + s # str(s) if you want it easier
  808. if type(p) == list:
  809. if type(s) == tuple:
  810. p = p + list(s)
  811. elif type(s) == list:
  812. p = p + s[:]
  813. else:
  814. p = p + [s]
  815. if type(p) == tuple:
  816. if type(s) == tuple:
  817. p = tuple(list(p) + list(s))
  818. elif type(s) == list:
  819. p = tuple(list(p) + s[:])
  820. else:
  821. p = tuple(list(p) + [s])
  822. return p
  823. def figjoin(p, x, s):
  824. t = ""
  825. if len(x):
  826. t = str(x[0])
  827. for c in range(len(x)):
  828. if c > 0: t += str(s) + str(x[c])
  829. return t # s.join(x)
  830. def figarr(p):
  831. if type(p) in (float, int, str):
  832. p = [p]
  833. else:
  834. p = list(p)
  835. return p
  836. def figsplit(p, x, s):
  837. return x.split(s)
  838. def figval(n):
  839. n = float(n)
  840. if float(int(n)) == float(n):
  841. n = int(n)
  842. return n
  843. def figtimes(p, s):
  844. if type(p) in (float, int):
  845. p = p * s # float(s) if you want it easier
  846. if p == float(int(p)):
  847. p = int(p)
  848. else:
  849. if type(p) == list:
  850. p = p[:] * s # figval(s)
  851. else:
  852. p = p * s # figval(s) if you want it easer
  853. return p
  854. def figdivby(p, s):
  855. p = float(p) / s
  856. if p == float(int(p)):
  857. p = int(p)
  858. return p
  859. def figminus(p, s):
  860. return p - s
  861. def figtopwr(p, s):
  862. p = p ** s
  863. if p == float(int(p)):
  864. p = int(p)
  865. return p
  866. def figmod(p, s):
  867. return p % s
  868. def figcos(p):
  869. from math import cos
  870. p = cos(p)
  871. if p == float(int(p)):
  872. p = int(p)
  873. return p
  874. def figsin(p):
  875. from math import sin
  876. p = sin(p)
  877. if p == float(int(p)):
  878. p = int(p)
  879. return p
  880. def figsqr(p):
  881. from math import sqrt
  882. p = sqrt(p)
  883. if p == float(int(p)):
  884. p = int(p)
  885. return p
  886. def figltrim(p):
  887. return p.lstrip()
  888. def figlineinput(p):
  889. return raw_input()
  890. def figlen(p):
  891. return len(p)
  892. def figasc(p):
  893. return ord(p[0])
  894. def figatn(p):
  895. from math import atan
  896. p = atan(p)
  897. if p == float(int(p)):
  898. p = int(p)
  899. return p
  900. def fighex(p):
  901. return hex(p)
  902. def figrtrim(p):
  903. return p.rstrip()
  904. def figstring(x, p, n):
  905. if type(n) == str:
  906. return n * p
  907. return chr(n) * p
  908. def figtimer(p):
  909. from time import strftime
  910. return int(strftime("%H"))*60*60+int(strftime("%M"))*60+int(strftime("%S"))
  911. def figtime(p):
  912. from time import strftime
  913. return strftime("%H:%M:%S")
  914. def figdate(p):
  915. from time import strftime
  916. return strftime("%m/%d/%Y")
  917. def figcommand(p):
  918. return figargv[1:]
  919. def figtan(p):
  920. from math import tan
  921. p = tan(p)
  922. if p == float(int(p)):
  923. p = int(p)
  924. return p
  925. def figoct(p): return oct(p)
  926. def figsleep(p, s):
  927. sleep(s)
  928. def figarrsort(p):
  929. p.sort()
  930. def figdisplay(x):
  931. global figraphics, figrupd
  932. figrupd = 0
  933. # if figraphics == 1:
  934. # update
  935. def figreverse(p):
  936. if type(p) == list:
  937. p.reverse()
  938. return p
  939. elif type(p) == str:
  940. p = map(str, p)
  941. p.reverse()
  942. p = "".join(p)
  943. return p
  944. def figarreverse(p):
  945. p.reverse()
  946. def figfunction(p, s):
  947. return p
  948. def figend(x):
  949. quit()
  950. def figif(p, s):
  951. return p
  952. def figthen(p, s):
  953. return p
  954. def figsystem(x):
  955. quit()
  956. """
  957. demo = """
  958. p 7 arr times 5
  959. x "hello, world!"
  960. x 5
  961. x 5 times 7
  962. x 3 plus 5 times 7
  963. abs
  964. z x abs
  965. x z str asc abs int
  966. function hello p
  967. x "hello, "
  968. x prints return 5
  969. fig
  970. x hello x
  971. c command print
  972. function add5 r
  973. x get r plus 5 return x
  974. fig
  975. p print
  976. p print arrset 2 8 print
  977. z join p "(_)" print
  978. x z print
  979. now add5 p print
  980. p print end
  981. function ppp
  982. z 32 chr print
  983. for p 1 100 1
  984. x randint 0 3
  985. y randint 0 3
  986. c randint 1 9
  987. c colourtext 7
  988. next
  989. fig
  990. z ppp
  991. z sleep 1
  992. z ppp sleep 2 z ppp
  993. """
  994. p = ""
  995. try:
  996. p = right(sys.argv, 1)[0]
  997. except:
  998. pass
  999. if not ".fig" in p.lower():
  1000. if p.lower() == "help":
  1001. stdout.write(nl + " type (any) part of the command you want help on." +
  1002. nl + nl + " fig will show all matches." + nl * 3 + " " * 4)
  1003. helpf = chelp(raw_input())
  1004. if not helpf:
  1005. print(colour(14,0) + nl + " no commands match your search.")
  1006. print("")
  1007. colour(7,0)
  1008. quit()
  1009. else:
  1010. print "using built-in demo source, translating to demo.fig.py..."
  1011. print
  1012. p = "demo.fig"
  1013. inputfile = demo.replace(chr(13), "").split(nl)
  1014. else:
  1015. try:
  1016. inputfile = open(p).read().replace(chr(13) + chr(10),
  1017. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  1018. except:
  1019. print "couldn't open \"" + p + "\", exiting."
  1020. print
  1021. quit()
  1022. try:
  1023. outfile = open(p + ".py", "w")
  1024. except:
  1025. print "couldn't write to \"" + p + ".py" "\", exiting."
  1026. print
  1027. quit()
  1028. outname = p + ".py"
  1029. flen = len(str(len(inputfile)))
  1030. linecount = 0
  1031. indent = 0
  1032. inlinep = 0
  1033. errorin = 0
  1034. errorsrc = ""
  1035. error = ""
  1036. mode = 0
  1037. figraphics = -1 # -1 = uninitialised, 0 = textmode, 1 = initialised
  1038. vrs = []
  1039. vr = ""
  1040. outb = []
  1041. ingfx = 0
  1042. linesoutc = 0
  1043. for p in inputfile:
  1044. linecount += 1
  1045. vrop = 0
  1046. vrcl = 0
  1047. if linecount == 1:
  1048. outfile.write("#!/usr/bin/env pypy2" + nl + "# encoding: utf-8" + nl)
  1049. if "," in proginf:
  1050. outfile.write("# fig translator version: " + proginf.split(",")[0] + nl)
  1051. if inlinep:
  1052. if p.lower().strip() == "fig":
  1053. inlinep = 0
  1054. print lc() + p
  1055. indent = atleast(0, indent - 4)
  1056. else:
  1057. print lc() + colour(2, None) + p + colour(7, None)
  1058. #% write copied lines of inline python
  1059. outfilewrite(outb, chr(32) * atleast(0, indent - 4) +
  1060. leftfour(p) + nl)
  1061. elif mode == "output the following:":
  1062. if p.lower().strip() == "display":
  1063. mode = 0
  1064. print lc() + p
  1065. else:
  1066. wr(chr(32) * atleast(0, indent) + "print \"" + p.replace(chr(34),
  1067. "\" + chr(34) + \"").replace(chr(92), "\" + chr(92) + \"") + "\"")
  1068. print lc() + p.replace(chr(34), "\" + chr(34) + \"").replace(chr(92),
  1069. "\" + chr(92) + \"", nl)
  1070. elif mode == 0:
  1071. x = figfsplit(p.lstrip())
  1072. lp = p.lower()
  1073. if not len(p):
  1074. print lc() + ""
  1075. #% write copied blank lines from inline python
  1076. outfilewrite(outb, nl)
  1077. if len(p.lstrip()):
  1078. e = 0
  1079. if p.lstrip()[0] == "#":
  1080. if linecount == 1:
  1081. es = 0
  1082. try:
  1083. if p.lstrip()[1] == "!":
  1084. es = 1
  1085. except: es = 0
  1086. if not es:
  1087. wr(p, nl)
  1088. print lc(), figfsp(p)
  1089. else: print lc() + "[this first comment isn't copied over]"
  1090. es = 0
  1091. else:
  1092. #% write comments
  1093. #print colour(14, 0) + p + colour(7,0) ; znul = raw_input() #$
  1094. outfilewrite(outb, chr(32) * atleast(0, indent) + p + nl)
  1095. print lc(), figfsp(p)
  1096. elif lnob(x, 0) == "figg":
  1097. e = 2
  1098. elif lp.rstrip() == "python":
  1099. indent += 4
  1100. inlinep = 1
  1101. print lc() + p
  1102. else:
  1103. if not lnob(x, 0) == "figg":
  1104. if lnob(x, 0) != "fig" and not lnob(x,
  1105. 0) in cmds.keys() and not lnob(x,
  1106. 0) in funcs.keys() + ["forin", "for", "function", "nextin",
  1107. "next", "while", "wend"] + ["break", "pass"]:
  1108. if not lnob(x, 0) in vrs:
  1109. vrs += [lnob(x, 0)[:]] # main vars, also func params, etc
  1110. #% write variable
  1111. #var: print colour(14, 0) + "variable:" + lnob(x, 0) + colour(7,0) ; znul = raw_input() #$
  1112. outfilewrite(outb, nl)
  1113. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1114. "figlist = 0" + nl)
  1115. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1116. "try: figlist = int(type(" + lnob(x, 0) + ") == list)" + nl)
  1117. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1118. "except NameError: pass" + nl)
  1119. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1120. "if not figlist: " + lnob(x, 0) + " = 0" + nl)
  1121. if lnob(x, 0) == "fig":
  1122. #print lc () + p
  1123. #% write? its whitespace
  1124. #$
  1125. indent = atleast(0, indent - 4)
  1126. if lnob(x, 0) == "wend":
  1127. #print lc () + p
  1128. #% write? its whitespace
  1129. #$
  1130. indent = atleast(0, indent - 4)
  1131. if lnob(x, 0) == "next":
  1132. #print lc () + p
  1133. #% write? its whitespace
  1134. #$
  1135. indent = atleast(0, indent - 4)
  1136. if lnob(x, 0) == "nextin":
  1137. #print lc () + p
  1138. #% write? its whitespace
  1139. #$
  1140. indent = atleast(0, indent - 4)
  1141. if lnob(x, 0) == "try":
  1142. #print lc () + p
  1143. #% write try line
  1144. #$
  1145. outfilewrite(outb, chr(32) * atleast(0, indent) + "try:" + nl)
  1146. indent = atleast(0, indent + 4)
  1147. if lnob(x, 0) == "else":
  1148. #print lc () + p
  1149. #% write else line
  1150. #$
  1151. outfilewrite(outb, chr(32) * atleast(0, indent - 4) +
  1152. "else:" + nl)
  1153. if lnob(x, 0) == "except":
  1154. #print lc () + p
  1155. indent = atleast(0, indent - 4)
  1156. #% write except line
  1157. #$
  1158. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1159. "except:" + nl)
  1160. indent = atleast(0, indent + 4)
  1161. if lnob(x, 0) == "resume":
  1162. #print lc () + p
  1163. #% write? its whitespace
  1164. #$
  1165. indent = atleast(0, indent - 4)
  1166. if lnob(x, 0) == "while":
  1167. #print lc () + p
  1168. #% write simple loop
  1169. #$
  1170. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1171. "while 1:" + nl)
  1172. indent += 4
  1173. if lnob(x, 0) == "function" and len(getmore(x, 1)) > 0:
  1174. #print lc () + p
  1175. mkf = []
  1176. funcname = getlmore(x, 1)[0]
  1177. prm = 1
  1178. while 1:
  1179. try:
  1180. aprm = getlmore(x, 1)[prm]
  1181. if len(aprm):
  1182. if aprm[0] != "#":
  1183. mkf += [aprm]
  1184. if aprm not in vrs: vrs += [aprm[:]]
  1185. prm += 1
  1186. except: break
  1187. ufunc[funcname] = mkf[:] #; print ufunc # #
  1188. #print ufunc
  1189. #print len(ufunc[funcname])
  1190. #% write func def
  1191. #$ print colour(14,0)+ "def " + funcname + "(" + ", ".join(mkf) + "):" + colour(7,0)
  1192. outfilewrite(outb, chr(32) * atleast(0, indent) + "def " +
  1193. funcname + "(" + ", ".join(mkf) + "):" + nl)
  1194. indent += 4
  1195. if lnob(x, 0) == "for" and len(getmore(x, 1)) == 4:
  1196. #print lc () + p
  1197. gmro = getlmore(x, 1)[0]
  1198. gmrt = getlmore(x, 1)[1]
  1199. gmrh = getlmore(x, 1)[2]
  1200. gmrf = getlmore(x, 1)[3]
  1201. if gmro not in vrs: vrs += [gmro[:]]
  1202. if "." not in gmrf and (gmrf.strip()) not in ("0",
  1203. "0.0", "-0") and "." not in gmrt and "." not in gmrh:
  1204. #% write standard for loop
  1205. #$
  1206. outfilewrite(outb, chr(32) * atleast(0, indent)
  1207. + "for "
  1208. + gmro + " in range(int(float(" + gmrt +
  1209. ")), int(float(" + gmrh + ")) + figsgn(" + gmrf +
  1210. "), fignonz(int(float(" + gmrf + ")))):" + nl)
  1211. else:
  1212. #% write for loop that allows floating step
  1213. #$
  1214. outfilewrite(outb, chr(32) * atleast(0, indent) + gmro
  1215. + " = float(" + gmrt + ") - float(" + gmrf + ")" + nl +
  1216. chr(32) * atleast(0, indent) + "while 1:" + nl + chr(32) *
  1217. atleast(0, indent + 4) + gmro + " += float(" + gmrf +
  1218. ")" + nl + chr(32) * atleast(0, indent + 4) + "if " +
  1219. gmrf +
  1220. " > 0 and " + gmro + " > float(" + gmrh + "): break" + nl
  1221. + chr(32) * atleast(0, indent + 4) + "elif " + gmrf +
  1222. " <= 0 and " + gmro +
  1223. " < float(" + gmrh + "): break" + nl)
  1224. indent += 4
  1225. if lnob(x, 0) == "forin" and len(getmore(x, 1)) == 2:
  1226. #print lc () + p
  1227. gmro = getlmore(x, 1)[0]
  1228. gmrt = getlmore(x, 1)[1]
  1229. if gmro not in vrs: vrs += [gmro[:]]
  1230. #% write forin command with params
  1231. #$
  1232. outfilewrite(outb, chr(32) * atleast(0, indent) + "for " +
  1233. gmro + " in " + gmrt + ":" + nl)
  1234. indent += 4
  1235. if lnob(x, 0) == "break":
  1236. #print lc () + p
  1237. #% write break command
  1238. #$ print
  1239. outfilewrite(outb, chr(32) *
  1240. atleast(0, indent) + "break" + nl)
  1241. if lp.rstrip() == "graphics":
  1242. ingfx = 1
  1243. #% write change to default mode (dont suppress gfx)
  1244. #$
  1245. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1246. "figraphics = -1" + nl)
  1247. figraphics = -1
  1248. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1249. "figpset(0, -1, -1, 0)" + nl)
  1250. #print lc () + p
  1251. if lnob(x, 0) == "textmode":
  1252. #print lc () + p
  1253. addto[3] = 1
  1254. #% write change to text mode (suppress graphics)
  1255. #$
  1256. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1257. "figraphics = 0" + nl)
  1258. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1259. "stopgraphics()" + nl)
  1260. figraphics = 0
  1261. if lnob(x, 0) == "pass":
  1262. #print lc () + p
  1263. #% write pass command
  1264. #$ print
  1265. outfilewrite(outb, chr(32) *
  1266. atleast(0, indent) + "pass" + nl)
  1267. if lnob(x, 0) == "iftrue":
  1268. #print lc () + p
  1269. #% write iftrue
  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) + ":" + nl)
  1273. indent += 4
  1274. if lnob(x, 0) == "ifequal" and len(getmore(x, 1)) == 2:
  1275. #print lc () + p
  1276. #% write ifequal
  1277. #$ print colour(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +colour(7,0)
  1278. outfilewrite(outb, chr(32) * atleast(0, indent) + "if " +
  1279. snob(x, 1) + " == " + snob(x, 2) + ":" + nl)
  1280. indent += 4
  1281. if lnob(x, 0) == "ifless" and len(getmore(x, 1)) == 2:
  1282. #print lc () + p
  1283. #% write ifless
  1284. #$ print colour(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +colour(7,0)
  1285. outfilewrite(outb, chr(32) * atleast(0, indent) + "if " +
  1286. snob(x, 1) + " < " + snob(x, 2) + ":" + nl)
  1287. indent += 4
  1288. if lnob(x, 0) == "ifmore" and len(getmore(x, 1)) == 2:
  1289. #print lc () + p
  1290. #% write ifmore
  1291. #$ print colour(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +colour(7,0)
  1292. outfilewrite(outb, chr(32) * atleast(0, indent) + "if " +
  1293. snob(x, 1) + " > " + snob(x, 2) + ":" + nl)
  1294. indent += 4
  1295. if lnob(x, 0) in cmds.keys(): # + ufunc.keys():
  1296. e = 4
  1297. shln = lnob(x, 0)
  1298. if lnob(x, 0) != "fig" and lnob(x,
  1299. 0) not in funcs.keys() + ["forin", "for", "function",
  1300. "nextin", "next", "while", "wend"] + ["break", "pass"]:
  1301. #print lc () + p
  1302. vr = lnob(x, 0)
  1303. #print vr, type(vr)
  1304. #print getlmore(x, 1)
  1305. prsc = 0
  1306. cstrctr = 0
  1307. csbuf = []
  1308. vrcs = ""
  1309. for prs in getlmore(x, 1):
  1310. #$ print prs
  1311. if "fig" in prs:
  1312. if prs[:3] == "fig":
  1313. e = 2
  1314. break
  1315. break
  1316. if prs in funcs.keys():
  1317. e = 3
  1318. shln = prs
  1319. prsc += 1
  1320. if cstrctr > 0:
  1321. vrcs += prs
  1322. cstrctr -= 1
  1323. if cstrctr == 0:
  1324. if lnob(x, prsc - 1) == "return":
  1325. #% write return command
  1326. #$ print colour(14,0) +vrcs + " ; " +colour(7,0)
  1327. outfilewrite(outb, chr(32) * atleast(0,
  1328. indent) + vrcs)
  1329. elif lnob(x, prsc - 2) == "swap":
  1330. vrcs = lnob(x, prsc - 1) + ", " + lnob(x,
  1331. prsc - 0) + " = " + lnob(x,
  1332. prsc - 0) + ", " + lnob(x, prsc - 1)
  1333. #% write swap of 2 vars in python syntax
  1334. #$ print colour(14,0) +vrcs + " ; " +colour(7,0)
  1335. outfilewrite(outb, chr(32) * atleast(0,
  1336. indent) + vrcs + " ; ")
  1337. else:
  1338. if figprsbac in ufunc.keys():
  1339. #% write parametered func call
  1340. #$ print colour(14,0)+ vrcs + ") ; " + lnob(x, 0) + " = fignone(" + lnob(x, 0) + ", figbac) ; " +colour(7,0)
  1341. outfilewrite(outb, chr(32) * atleast(0,
  1342. indent) + vrcs + ") ; " + lnob(x, 0) +
  1343. " = fignone(" + lnob(x, 0) +
  1344. ", figbac) ; ")
  1345. vrcl += 1
  1346. else:
  1347. #% write builtin func call assignment
  1348. #$ print colour(14,0)+ vr + " = " + vrcs + ") ; " +colour(7,0)
  1349. outfilewrite(outb, chr(32) *
  1350. atleast(0, indent) + vrcs + ")" + nl)
  1351. vrcl += 1
  1352. else:
  1353. vrcs += ", " #; print "*"
  1354. #if
  1355. continue
  1356. if prs.lower() in funcs.keys() + ["forin", "for",
  1357. "function", "nextin", "next", "while",
  1358. "wend"] + ["break", "pass"]:
  1359. e = 3
  1360. figprsbac = None
  1361. if cstrctr == 0:
  1362. if not prs.lower() in vrs:
  1363. if prs.lower()[0] in "abcdefghijklmnopqrstuvwxyz":
  1364. if not prs.lower() in ufunc.keys():
  1365. if not prs.lower() in funcs.keys():
  1366. if not prs.lower() in cmds.keys():
  1367. e = 5
  1368. shln = prs
  1369. if prs.lower() in vrs and cstrctr == 0:
  1370. #and len(getmore(x, 1)) == 1:
  1371. #% write lefthand variable assignment
  1372. #$ print colour(14,0)+ vr + " = " + prs.lower() +colour(7,0)
  1373. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1374. vr + " = " + prs.lower() + nl)
  1375. if prs[0] == "\"":
  1376. #% write string assignment (any place in shared line)
  1377. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1378. vr + " = " + prs + nl)
  1379. if prs[0] in ".1234567890-":
  1380. #% write numerics
  1381. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1382. vr + " = " + prs + nl)
  1383. if prs[0] == "#":
  1384. #% write trailing comments #$ print colour(14, 0) + prs + colour(7,0)
  1385. outfilewrite(outb, prs + "" + nl)
  1386. break
  1387. if prs.lower() in ufunc.keys():
  1388. #% write pre-func-call var backup for sub-style behavior #$ print colour(14, 0) + "figbac = " + lnob(x,0) + " ; " + colour(7,0)
  1389. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1390. "figbac = " + lnob(x,0) + nl) # ##
  1391. figprsbac = prs.lower()
  1392. cstrctr = len(ufunc[prs])
  1393. #print cstrctr
  1394. if cstrctr == 0:
  1395. #% write zero-param func/?sub call
  1396. #$ print colour(14, 0) + vr + " = " + prs.lower() + "() ; " + lnob(x, 0) + " = fignone(" + lnob(x, 0) + ", figbac) ; " + colour(7,0)
  1397. outfilewrite(outb,
  1398. chr(32) * atleast(0, indent) +
  1399. vr + " = " + prs.lower() + "()" + nl + chr(32) * atleast(0, indent) +
  1400. lnob(x, 0) + " = fignone(" + lnob(x, 0) + ", figbac)" + nl) # #
  1401. else:
  1402. #print "y"
  1403. vrop += 1
  1404. vrcs = vr + " = " + prs.lower() + "("
  1405. #$ print colour(4, 0) + vr + " = " + prs.lower() + "(" + colour(7,0) #$
  1406. #multiparameter
  1407. if prs.lower() in cmds.keys():
  1408. if prs.lower() in ["display", "pset", "line"]:
  1409. ingfx = 1
  1410. ##print prs
  1411. cstrctr = cmds[prs]
  1412. ##print cstrctr
  1413. if cstrctr == -1:
  1414. #% write zero-param subs
  1415. #print colour(14, 0) + "fig" + prs.lower() + "(" + vr
  1416. #+ ") ; " + colour(7,0) ; #znul = raw_input() #$
  1417. outfilewrite(outb, chr(32) *
  1418. atleast(0, indent) + "fig" +
  1419. prs.lower() + "(" + vr + ")" + nl)
  1420. vrcl += 1
  1421. if cstrctr == 0:
  1422. #% write zero-param functions
  1423. #print colour(14, 0) + vr + " = fig" + prs.lower()
  1424. #+ "(" + vr + ") ; "+ colour(7,0) ; #znul = raw_input() #$
  1425. outfilewrite(outb, chr(32) * atleast(0,
  1426. indent) + vr +
  1427. " = fig" + prs.lower() + "(" + vr + ")" + nl)
  1428. vrcl += 1
  1429. if cstrctr < -1:
  1430. if prs == "return":
  1431. cstrctr = abs(cstrctr) - 1
  1432. vrcs = "return " #parameter
  1433. else:
  1434. cstrctr = abs(cstrctr) - 1
  1435. if prs == "swap":
  1436. vrcs = "swap "
  1437. else:
  1438. vrop += 1
  1439. vrcs = "fig" + prs.lower() + "(" + vr
  1440. vrcs += ", " #multiparameter
  1441. else:
  1442. vrop += 1
  1443. vrcs = vr + " = fig" + prs.lower() + "(" + vr
  1444. vrcs += ", " #multiparameter
  1445. if vrop == vrcl and e == 0:
  1446. print lc(), figfsp(p)
  1447. #% finish each line with lf
  1448. outfilewrite(outb, "" + nl)
  1449. else:
  1450. print lc() + p
  1451. else:
  1452. e = 2
  1453. if e == 1:
  1454. e = 0
  1455. if not len(error):
  1456. error = "error: problem in command structure or details."
  1457. errorin = linecount
  1458. errorsrc = p
  1459. print lc() + colour(14, 0) + str(p) + colour(7, 0)
  1460. break
  1461. if e == 2:
  1462. e = 0
  1463. if not len(error):
  1464. error = "error: cannot create variable or function beginning"
  1465. error += " with \"fig\""
  1466. errorin = linecount
  1467. errorsrc = p
  1468. print lc() + colour(14, 0) + p + colour(7, 0)
  1469. break
  1470. if e == 3:
  1471. e = 0
  1472. if not len(error):
  1473. error = "error: single-line command \"" + shln + "\" not on own line"
  1474. errorin = linecount
  1475. errorsrc = p
  1476. print lc() + colour(14, 0) + p + colour(7, 0)
  1477. break
  1478. if e == 4:
  1479. e = 0
  1480. if not len(error):
  1481. error = "error: shared-line function \""
  1482. error += shln + "\" cannot be used to start a line"
  1483. errorin = linecount
  1484. errorsrc = p
  1485. print lc() + colour(14, 0) + p + colour(7, 0)
  1486. break
  1487. if e == 5:
  1488. e = 0
  1489. if not len(error):
  1490. error = "error: variable or function not created, but referenced... \""
  1491. error += shln + "\" needs to be set before first use"
  1492. errorin = linecount
  1493. errorsrc = p
  1494. print lc() + colour(14, 0) + p + colour(7, 0)
  1495. break
  1496. if vrcl != vrop:
  1497. e = 0
  1498. if not len(error):
  1499. error = "error: a command has the wrong number of parameters."
  1500. errorin = linecount
  1501. errorsrc = p
  1502. print lc() + colour(14, 0) + str(p) + colour(7, 0)
  1503. break
  1504. if ingfx == 0:
  1505. addtoout[3] = ""
  1506. outfile.write(addtoout[0] + addtoout[1] + addtoout[3] + addfuncs)
  1507. for outsb in outb:
  1508. outfile.write(outsb)
  1509. outfile.close()
  1510. print
  1511. if errorin:
  1512. print error
  1513. colour(14, None)
  1514. print "error in line " + str(errorin) + ":"
  1515. colour(7, None)
  1516. print errorsrc
  1517. else:
  1518. try:
  1519. os.system("chmod +x \"" + outname + "\"")
  1520. except:
  1521. pass
  1522. colour (11, None)
  1523. print "translation complete. ",
  1524. colour(7, None)
  1525. print "here's a python script you can run: ",
  1526. print colour(11, None) + outname + colour(7, None)
  1527. print