fig50.py 59 KB

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