figu29.py 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. #### license: creative commons cc0 1.0 (public domain)
  4. #### http://creativecommons.org/publicdomain/zero/1.0/
  5. proginf = "figueira 2.9, nov 2016 mn"
  6. import sys
  7. import os
  8. from sys import stdin, stdout
  9. from os import popen
  10. try: from colorama import init ; init()
  11. except: pass
  12. buf = []
  13. cmdhelp = [("crono", "input (shared-line) change var principal to number of seconds past midnight")
  14. ,("ordstdin", "input (shared-line) change var principal to array containing lines of stdin")
  15. ,("linhaentra", "input (shared-line) change var principal to string input from keyboard")
  16. ,("alinhaentra arquivo", "input (shared-line) change var principal to string of line from open file arquivo")
  17. ,("hora", "input (shared-line) change var principal to string of current time: hh:mm:ss")
  18. ,("ordaberto arquivo", "input (shared-line) change var principal to array of file lines in arquivo")
  19. ,("data", "input (shared-line) change var principal to string of the date: mm/dd/yyyy")
  20. ,("ordcurl url", "input (shared-line) like ordaberto, except downloading url into the array")
  21. ,("pausa segundos", "input (shared-line) wait for number of seconds before continuing with program")
  22. ,("comando", "input (shared-line) change var principal to array of command line parameters")
  23. ,("impress", "output (shared-line) output var principal to the screen (aka stdout)")
  24. ,("impressha", "output (shared-line) put var principal to screen; like print but stays on the line.")
  25. ,("aimpress arquivo", "output (shared-line) write var principal to open file designated by arquivo")
  26. ,("display", "output (shared-line) 1st time: stop automatic graphx update. 2nd, etc: update.")
  27. ,("lmp", "output (shared-line) clear the screen. currently only affects text screen")
  28. ,("graficos", "output --\\own\\line dont(or stop) suppress(ing) graphics. this is the default.")
  29. ,("textomodo", "output --\\own\\line suppress graphics; force graphics commands to use text.")
  30. ,("cortexto corcodigo", "output (shared-line) change color of upcoming text to colorcode from 0 - 15")
  31. ,("realcar corcodigo", "output (shared-line) change background color of upcoming text to colorcode 0-15")
  32. ,("localiz linha coluna", "output (shared-line) move to textmode position at linha, coluna")
  33. ,("ppus x y c", "output (shared-line) draw dot at location (x, y) in colorcode c (0 - 15)")
  34. ,("linha x1 y1 x2 y2 c", "output (shared-line) draw line from (x1, y1) to (x2, y2) in colorcode c (0-15)")
  35. ,("enquant", "loop --\\own\\line mark the start of a loop (will keep going without saida)")
  36. ,("saida", "loop --\\own\\line put in the middle of a loop to exit (stop looping)")
  37. ,("para vc de para quanto", "loop --\\own\\line start a for loop, changing vc from de to para, by quanto")
  38. ,("paraem itemv ordem", "loop --\\own\\line loop through each item in ordem; for each, set itemv to item")
  39. ,("severd cfvar", "conditional --\\own\\line run lines between severd and fig if cfvar is \"non-zero\"")
  40. ,("seigual var1 var2", "conditional --\\own\\line run lines between seigual and fig if var1 equals var2")
  41. ,("semais var1 var2", "conditional --\\own\\line run lines between semais and fig if var1 is > var2")
  42. ,("semenos var1 var2", "conditional --\\own\\line run lines between semenos and fig if var1 is < var2")
  43. ,("tentar", "conditional --\\own\\line put code that might not work between tentar and exceto")
  44. ,("exceto", "conditional --\\own\\line if code between tentar/exceto fails, run the code after exceto")
  45. ,("retomar", "conditional --\\own\\line mark the end of tentar/exceto/retomar command block")
  46. ,("outra", "conditional --\\own\\line after se- line, before fig. run lines if condition isnt true")
  47. ,("funcao nome p1 p2 …", "function --\\own\\line define function named nome with optional params p1,p2, etc")
  48. ,("obter parametro", "function (shared-line) (no longer required) copy parametro value to var principal")
  49. ,("python", "function --\\own\\line put inline python code between lines python and fig")
  50. ,("fig/seg/segem/efim", "fig (interchangeable) function --\\own\\line finalize a block (started by se/enquant/funcao/para/paraem")
  51. ,("passar", "function --\\own\\line blocks (for/next, etc) require something inside lines; passar works / does nothing")
  52. ,("mincula", "function (shared-line) change var principal to all-lower-case copy of own value")
  53. ,("maicula", "function (shared-line) change var principal to all-upper-case copy of own value")
  54. ,("cad", "function (shared-line) convert var principal from number to string")
  55. ,("shell", "function (shared-line) run var principal contents in a command shell (os specific)")
  56. ,("asc", "function (shared-line) change var principal from string to ascii code of 1st char")
  57. ,("val", "function (shared-line) change var principal from string to numeric (int if whole)")
  58. ,("comprim", "function (shared-line) change var principal to numeric length of var principal")
  59. ,("estanao", "function (shared-line) change var principal to zero if non-zero; or -1 if zero")
  60. ,("ecorta", "function (shared-line) strip whitespace from left side of var principal")
  61. ,("dcorta", "function (shared-line) strip whitespace from right side of var principal")
  62. ,("car", "function (shared-line) change var principal from numeric to ascii/uni string")
  63. ,("ordshell", "function (shared-line) change var principal to array of shell output (from var principal)")
  64. ,("ordreverso", "function (shared-line) change var principal from array to reverse order of array")
  65. ,("reverso", "function (shared-line) like ordreverso (which might be faster for array) for strings")
  66. ,("ordordena", "function (shared-line) change var principal from array to sorted array")
  67. ,("#", "comment (can\\share) place at beginning (or end) of line, prior to a comment")
  68. ,("():;|=,. ( ) : ; | = , .", "optional (shared-line) use in a shared line (and some others) for aesthetics/notation")
  69. ,("esquerdo numdecaras", "function (shared-line) change var principal to __ leftmost group of caras/items")
  70. ,("direito numdecaras", "function (shared-line) change var principal to __ rightmost group of caras/items")
  71. ,("ordobter ordem posicao", "function (shared-line) change var principal to posicao-nth item from ordem")
  72. ,("ordpus posicao que", "function (shared-line) change item in array in var principal to value of que")
  73. ,("meio posicao comprimento", "function (shared-line) change var principal to range of comprimento items from posicao")
  74. ,("cadeia n asciiorcad", "function (shared-line) change var principal to n instances of asciiorcad")
  75. ,("separar cadeia comcadeia", "function (shared-line) split cadeia by separator comcadeia into array, to var principal")
  76. ,("juntar ordem comcadeia", "function (shared-line) change var principal to string by joining ordem using comcadeia")
  77. ,("incad cadeia olharpara", "function (shared-line) change var principal to numeric position of olharpara in cadeia")
  78. ,("mudar", "function (shared-line) change current folder to path string from var principal")
  79. ,("sistema", "function (shared-line) put on (usually at the end of) a line to stop the program")
  80. ,("fechar", "function (shared-line) close the open file designated by var principal")
  81. ,("fim", "function (shared-line) interchangeable with sistema which ends the program")
  82. ,("aberto modo", "function (shared-line) open file at arquivo var principal in modo \"r\" or \"w\"")
  83. ,("retorno var", "function (shared-line) (optional) exit current function, returning value var")
  84. ,("troca var1 var2", "function (shared-line) change contents of var1 to contents of var2 and vice-versa")
  85. ,("mais numcadord", "math (shared-line) change var principal to itself plus num or cadeia or ord")
  86. ,("menos numerico", "math (shared-line) change var principal to itself minus numerico")
  87. ,("divipor numerico", "math (shared-line) change var principal to itself divided by numerico")
  88. ,("multipor numerico", "math (shared-line) change var principal to itself times numerico")
  89. ,("oct", "math (shared-line) change var principal from numeric decimal to octal")
  90. ,("hex", "math (shared-line) change var principal from numeric decimal to hexadecimal")
  91. ,("cosseno", "math (shared-line) change numeric var principal to the cosine of itself")
  92. ,("seno", "math (shared-line) change numeric var principal to the sine of itself")
  93. ,("tan", "math (shared-line) change numeric var principal to its tangent")
  94. ,("atan", "math (shared-line) change numeric var principal to its arctangent")
  95. ,("int", "math (shared-line) change var principal from decimal (aka \"float\") to integer")
  96. ,("smb", "math (shared-line) change var principal to 0 if 0, to -1 if < 0, or 1 if > 0.")
  97. ,("raizquadr", "math (shared-line) change var principal to square root of itself")
  98. ,("mod denominador", "math (shared-line) change var principal to: var principal módulo denominador")
  99. ,("apoten n", "math (shared-line) raise numeric var principal to n-th power")
  100. ,("aleatint menor maior", "input (shared-line) change var principal to random number from menor to maior")
  101. ,("ordem", "function (shared-line) change var principal to array (starting with same contents)") ]
  102. def chelp(f):
  103. ck = 0 ; print ""
  104. for p in cmdhelp:
  105. rcmd = p[0]
  106. if f in rcmd.split()[0]:
  107. ck = 1
  108. rd = p[1].split()
  109. rcat = rd[0] ; rd.remove(rd[0])
  110. rt = rd[0] ; rd.remove(rd[0])
  111. cde = rcmd.split(" ")
  112. print ""
  113. stdout.write(" " + color(14,0)+ cde[0])
  114. cda = cde.remove(cde[0])
  115. for c in cde:
  116. stdout.write(" " + color(0, 7)+ " " + c + " " + color(7,0)+" ") ; stdout.flush()
  117. print ""
  118. print ""
  119. print color(3,0) + " category:", rcat, rt.replace("\\", " ")
  120. print ""
  121. print " " + color(7,0) + " ".join(rd)
  122. print ""
  123. color(7,0);
  124. return ck
  125. def outfilewrite(outb, p):
  126. outb += [p]
  127. #global vrck
  128. #vrck += p.strip()
  129. #if inle: print color(5, 0) + p.rstrip() ; p=raw_input() ; quit()
  130. def color(f, b):
  131. if f == None: f = 0
  132. if b == None: b = 0
  133. n = "0"
  134. if f > 7: n = "1" ; f = f - 8
  135. if f == 1: f = 4 ## switch ansi colors for qb colors
  136. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  137. if f == 3: f = 6
  138. elif f == 6: f = 3
  139. if b > 7: b = b - 8
  140. if b == 1: b = 4
  141. elif b == 4: b = 1
  142. if b == 3: b = 6
  143. elif b == 6: b = 3
  144. stdout.write("\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m")
  145. return "\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
  146. def bcolor(b):
  147. f = None
  148. if f == None: f = 0
  149. if b == None: b = 0
  150. n = "0"
  151. if f > 7: n = "1" ; f = f - 8
  152. if f == 1: f = 4 ## switch ansi colors for qb colors
  153. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  154. if f == 3: f = 6
  155. elif f == 6: f = 3
  156. if b > 7: b = b - 8
  157. if b == 1: b = 4
  158. elif b == 4: b = 1
  159. if b == 3: b = 6
  160. elif b == 6: b = 3
  161. stdout.write("\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m")
  162. return "\x1b[" + n + str(40+b) + "m"
  163. def sgn(p):
  164. p = float(p)
  165. if p > 0: return 1
  166. if p < 0: return -1
  167. return 0
  168. def left(p, s):
  169. return p[:s]
  170. def right(p, s):
  171. return p[-s:]
  172. def leftfour(p):
  173. try:
  174. if left(p, 4) == chr(32) * 4: p = right(p, len(p) - 4)
  175. except:
  176. pass
  177. return p
  178. def atleast(s, p):
  179. if p < s: return s
  180. else: return p
  181. def figfsp(p):
  182. pp = "" ; flg = 0
  183. fsp = figfsplit(p)
  184. for fp in enumerate(fsp):
  185. if flg == 0 and fp[1] in cmds.keys():
  186. pp += color(8,0) + "_" + color(7,0) + " " ; flg = cmds[fp[1]]
  187. if flg < 0: flg = flg * -1
  188. else: flg = flg + 1
  189. pp += fp[1] + " "
  190. if flg > 0:
  191. flg -= 1
  192. if flg == 0 and fp[0] + 1 < len(fsp):
  193. pp += color(8,0) + "_" + color(7,0) + " "
  194. return pp.rstrip().replace(color(8,0) + "_" + color(7,0) + " " + color(8,0) +
  195. "_" + color(7,0), color(8,0) + "__" + color(7,0)).replace(color(8,0) + "_" +
  196. color(7,0),color(8,0) + "__" + color(7,0))
  197. def figfsplit(p):
  198. # return p.split() # that was fine when strings weren't tokens
  199. # we have to make this 3 tokens: variable "hello, world!" #comment not string
  200. px = []
  201. pxc = -1 # could use len(px) -1 instead?
  202. inquotes = 0
  203. remarked = 0
  204. inspc = "" ; vnspc = ""
  205. #print "->", p
  206. for l in p:
  207. if inquotes == 0 and remarked == 0 and l == "#":
  208. remarked = 1
  209. pxc += 1 ; px += [""]
  210. if remarked == 1:
  211. px[pxc] += l
  212. if remarked == 0:
  213. if l == "\"":
  214. if inquotes == 0:
  215. inquotes = 1 ; pxc += 1 ; px += [""]
  216. else: inquotes = 0 #; px[pxc] += l
  217. if inquotes == 1: px[pxc] += l
  218. if remarked == 0 and inquotes == 0:
  219. if vnspc not in "1234567890-" + chr(32) and l[0] == ".": l = " "
  220. vnspc = l
  221. if l[0] in "():;|=,": l = " "
  222. if inspc != " " and l == " ": pxc += 1 ; px += [""]
  223. if l != " ":
  224. if pxc == -1: pxc += 1 ; px += [""]
  225. px[pxc] += l.lower()
  226. inspc = l
  227. #print "->", px[:]
  228. while ('') in px: px.remove('')
  229. while (':') in px: px.remove(':')
  230. for p in range(len(px)):
  231. if px[p][0] != "#":
  232. if right(px[p], 1) == ":":
  233. lenpx = len(px[p]) - 1
  234. if lenpx > 0:
  235. px[p] = left(px[p], lenpx)
  236. return px[:]
  237. def nob(p, s):
  238. r = ""
  239. if s == len(p) - 1:
  240. if len(p):
  241. if p[s].rstrip() != ".": r = p[s].rstrip()
  242. if len(r):
  243. if r[-1:] == ".": r = left(r, len(r) - 1)
  244. pfig = ""
  245. try: pfig = left(p[s], 3)
  246. except: pfig = ""
  247. if pfig.lower() == "fig" and p[s].lower() != "fig": return "figg"
  248. try:
  249. if r != "": return r
  250. else: return p[s]
  251. except: return ""
  252. def snobl(p):
  253. if "\"" in p: return p
  254. else: return p.lower()
  255. def snob(p, s):
  256. r = ""
  257. if s == len(p) - 1:
  258. if len(p):
  259. if p[s].rstrip() != ".": r = p[s].rstrip()
  260. if len(r):
  261. if r[-1:] == ".": r = left(r, len(r) - 1)
  262. pqt = ""
  263. try: pqt = left(p[s], 3)
  264. except: pqt = ""
  265. if pqt.lower() == "fig" and p[s].lower() != "fig": return "figg"
  266. try:
  267. if r != "": return snobl(r)
  268. else: return snobl(p[s])
  269. except: return ""
  270. def lnob(p, s):
  271. r = ""
  272. if s == len(p) - 1:
  273. if len(p):
  274. if p[s].rstrip() != ".": r = p[s].rstrip()
  275. if len(r):
  276. if r[-1:] == ".": r = left(r, len(r) - 1)
  277. pfig = ""
  278. try: pfig = left(p[s], 3)
  279. except: pfig = ""
  280. if pfig.lower() == "fig" and p[s].lower() != "fig": return "figg"
  281. try:
  282. if r != "": return r.lower()
  283. else: return p[s].lower()
  284. except: return ""
  285. def stripcoords(p):
  286. ps = ""
  287. for s in str(p):
  288. if s in "1234567890.": ps += s
  289. return ps
  290. def getmore(p, s):
  291. try:
  292. for t in range(1, s + 1):
  293. if len(p) == 1: p = []
  294. p = right(p, len(p) - 1)
  295. while "" in p: p.remove("")
  296. for prx in range(len(p)):
  297. if p[prx][0] == "#":
  298. p.remove(p[prx])
  299. return p
  300. except: return []
  301. def getlmore(p, s):
  302. try:
  303. for t in range(1, s + 1):
  304. if len(p) == 1: p = []
  305. p = right(p, len(p) - 1)
  306. while "" in p: p.remove("")
  307. return p
  308. except: return []
  309. def getpairs(p, s):
  310. r = ""
  311. if len(p):
  312. if p[len(p) - 1].rstrip() != ".": r = p[len(p) - 1].rstrip()
  313. if len(r):
  314. if r[-1:] == ".":
  315. r = left(r, len(r) - 1)
  316. p[len(p) - 1] = r
  317. try:
  318. p = right(p, len(p) - s)
  319. if not len(p) % 2: return p
  320. else: return []
  321. except: return []
  322. def lc():
  323. global linecount
  324. global flen
  325. es = " \x1b[0;37;40m"
  326. return "\x1b[0;37;44m" + right(chr(32) * flen + str(linecount), flen) + es
  327. def wr(p):
  328. global buf
  329. buf += [p + "\n"]
  330. color(11, None) ; print proginf; color(7, None) ; print
  331. addtoout = [0]
  332. addto = [0]
  333. addtoout[0] = """import sys, os
  334. from sys import stdin, stdout
  335. from sys import argv as figargv
  336. try: from colorama import init ; init()
  337. except: pass # (only) windows users want colorama installed or ansi.sys enabled
  338. from random import randint
  339. from time import sleep\n\n
  340. from os import chdir as figoch
  341. from os import popen as figpo
  342. from os import system as figsh
  343. from os import name as figosname
  344. figsysteme = 0
  345. figfilehandles = {}
  346. figfilecounters = {}
  347. """
  348. addtoout += [0] ; addto += [0]
  349. addtoout[1] = """from sys import stdout
  350. def figlocaliz(x, l = "ignore", c = "ignore"):
  351. import sys
  352. if l == "ignore" and c == "ignore": pass
  353. # do nothing. want it to return an error?
  354. elif l < 1 and c != "ignore":
  355. sys.stdout.write("\x1b[" + str(c) + "G") # not ansi.sys compatible
  356. elif l != "ignore" and c == "ignore":
  357. sys.stdout.write("\x1b[" + str(l) + ";" + str(1) + "H")
  358. else: sys.stdout.write("\x1b[" + str(l) + ";" + str(c) + "H")
  359. import time
  360. def fignonz(p, n=None):
  361. if n==None:
  362. if p == 0: return 1
  363. else:
  364. if p == 0: return n
  365. return p
  366. def figestanao(p):
  367. if p: return 0
  368. return -1
  369. figbac = None
  370. figprsbac = None
  371. sub = None
  372. def fignone(p, figbac):
  373. if p == None: return figbac
  374. return p
  375. return -1
  376. def stopgraphics():
  377. global yourscreen
  378. global figraphics
  379. figraphics = 0
  380. try: pygame.quit()
  381. except: pass\n
  382. \n"""
  383. addtoout += [0] ; addto += [0]
  384. addtoout[2] = """palette = {}
  385. palette["black"] = (0, 0, 0)
  386. palette["gray"], palette["grey"] = (0, 0, 0), (85, 85, 85)
  387. palette["blue"], palette["lightblue"] = (0, 0, 170), (85, 85, 255)
  388. palette["green"], palette["lightgreen"] = (0, 170, 0), (85, 255, 85)
  389. palette["cyan"], palette["lightcyan"] = (0, 170, 170), (85, 255, 255)
  390. palette["red"], palette["lightred"] = (170, 0, 0), (255, 85, 85)
  391. palette["magenta"], palette["lightmagenta"] = (170, 0, 170), (255, 85, 255)
  392. palette["brown"], palette["yellow"] = (170, 85, 0), (255, 255, 85)
  393. palette["white"], palette["lightwhite"] = (170, 170, 170), (255, 255, 255)\n"""
  394. addtoout += [0] ; addto += [0]
  395. addtoout[3] = """figraphics = -1
  396. figrupd = 1
  397. try: import pygame
  398. except: figraphics = 0
  399. yourscreen = ""
  400. try: pygame.init()
  401. except: figraphics = 0 # unable to init pygame, just use text
  402. def figppus(z, x, y, c):
  403. global figraphics, figrupd
  404. global yourscreen
  405. global figcgapal
  406. if figraphics == -1:
  407. #pygame.init()
  408. try:
  409. yourscreen = pygame.display.set_mode((800, 600))
  410. pygame.display.set_caption("fig graphics screen")
  411. #pygame.quit()
  412. figraphics = 1
  413. except:
  414. stopgraphics() ; figraphics = 0
  415. if figraphics == 0:
  416. if x > -1 and y > -1:
  417. figcortexto(c, c)
  418. figlocaliz(0, int(y) + 1, int(x) + 1) ; stdout.write(unichr(9608))
  419. sys.stdout.flush()
  420. if figraphics == 1:
  421. if x > -1 and y > -1:
  422. yourscreen.set_at((x, y), figcgapal[c])
  423. #pygame.draw.circle(yourscreen,(255, 255, 255),(int(x), int(y)), 1, 0)
  424. if figrupd: pygame.display.update()
  425. for event in pygame.event.get():
  426. if event.type == pygame.QUIT:
  427. figraphics = 0
  428. stopgraphics()
  429. def figlinha(z, x, y, x2, y2, c):
  430. global figraphics, figrupd
  431. global yourscreen
  432. global figcgapal
  433. if figraphics == -1:
  434. #pygame.init()
  435. try:
  436. yourscreen = pygame.display.set_mode((800, 600))
  437. pygame.display.set_caption("fig graphics screen")
  438. #pygame.quit()
  439. figraphics = 1
  440. except:
  441. stopgraphics() ; figraphics = 0
  442. if figraphics == 0:
  443. if x > -1 and y > -1 and x2 > -1 and y2 > -1:
  444. figcortexto(c, c)
  445. if x2 < x: x, y, x2, y2 = x2, y2, x, y
  446. figliney = [y, y2]
  447. figlinec = 0
  448. figlinestep = int(y2 - y)
  449. if figlinestep < 0: figlinestep = int(y - y2) ; figlinec = 0
  450. if figlinestep < 1: figlinestep = 1
  451. figlinestep = float(1) / figlinestep
  452. figlinex = x
  453. while 1:
  454. if figlinex > x2: break
  455. if y2 - y == 0:
  456. figlocaliz(0, int(y) + 1, int(figlinex) + 1)
  457. stdout.write(unichr(9608))
  458. elif y2 < y:
  459. figlinec -= figlinestep
  460. figlocaliz(0, int(y + int(float(y - y2) / fignonz(x2 - x,.1) *
  461. fignonz(figlinec,.1) ) ) + 1, int(figlinex) + 1)
  462. stdout.write(unichr(9608))
  463. else:
  464. figlocaliz(0, int(y + int(float(y2 - y) / fignonz(x2 - x,.1) *
  465. fignonz ( figlinec,.1) ) ) + 1, int(figlinex) + 1) ;
  466. stdout.write(unichr(9608))
  467. figlinec += figlinestep
  468. #[0] = figliney[0]+float(figliney[1] - figliney[0]) / (x2 - x)
  469. figlinex += figlinestep
  470. figlocaliz(0, int(y) + 1, int(x) + 1) ; stdout.write(unichr(9608))
  471. figlocaliz(0, int(y2) + 1, int(x2) + 1) ; stdout.write(unichr(9608))
  472. sys.stdout.flush()
  473. if figraphics == 1:
  474. if x > -1 and y > -1 and x2 > -1 and y2 > -1:
  475. yourscreen.set_at((x, y), figcgapal[c])
  476. pygame.draw.line(yourscreen, figcgapal[c], (x, y), (x2, y2), 1)
  477. if figrupd: pygame.display.update()
  478. for event in pygame.event.get():
  479. if event.type == pygame.QUIT:
  480. figraphics = 0
  481. stopgraphics()
  482. def anykeypyg():
  483. global yourscreen
  484. global figraphics, figrupd
  485. p = 0
  486. while not p:
  487. if figraphics == 0: break
  488. time.sleep(0.001)
  489. for event in pygame.event.get():
  490. if event.type == pygame.KEYDOWN:
  491. if figrupd: pygame.display.update()
  492. p = 1\n\n"""
  493. addtoout += [0] ; addto += [0]
  494. palette = {}
  495. palette["black"] = (0, 0, 0)
  496. palette["gray"], palette["grey"] = (0, 0, 0), (85, 85, 85)
  497. palette["blue"], palette["lightblue"] = (0, 0, 170), (85, 85, 255)
  498. palette["green"], palette["lightgreen"] = (0, 170, 0), (85, 255, 85)
  499. palette["cyan"], palette["lightcyan"] = (0, 170, 170), (85, 255, 255)
  500. palette["red"], palette["lightred"] = (170, 0, 0), (255, 85, 85)
  501. palette["magenta"], palette["lightmagenta"] = (170, 0, 170), (255, 85, 255)
  502. palette["brown"], palette["yellow"] = (170, 85, 0), (255, 255, 85)
  503. palette["white"], palette["lightwhite"] = (170, 170, 170), (255, 255, 255)
  504. textpalette = {}
  505. textpalette["black"],textpalette["gray"], palette["grey"] = 0, 8, 8
  506. textpalette["blue"], textpalette["lightblue"] = 1, 9
  507. textpalette["green"], textpalette["lightgreen"] = 2, 10
  508. textpalette["cyan"], textpalette["lightcyan"] = 3, 11
  509. textpalette["red"], textpalette["lightred"] = 4, 12
  510. textpalette["magenta"], textpalette["lightmagenta"] = 5, 13
  511. textpalette["brown"], textpalette["yellow"] = 6, 14
  512. textpalette["white"], textpalette["lightwhite"] = 7, 15
  513. # -2: print(variable, etc)
  514. # -1: print(variable), 0: variable = int(variable), 1: variable=left(variable, etc)
  515. cmds = {"ecorta":0, "linhaentra":0, "comprim":0, "asc":0, "aatn":0, "cad":0,
  516. "obter":1, "car":0, "impressha":-1, "pausa":-2, "ordordena":-1,
  517. "ordreverso":-1, "reverso":0, "display":-1, "sistema":-1, "fim":-1,
  518. "impress":-1, "ordpus":-3,
  519. "separar":2, "esquerdo":1, "juntar":2, "ordobter":2, "meio":2, "direito":1,
  520. "mais":1, "multipor":1, "fechar":-1, "lmp":-1, "alinhaentra":1, "aimpress":-2,
  521. "aberto":-2, "ordaberto":1, "ordstdin":0, "ordcurl":1, "cortexto":-2,
  522. "realcar":-2, "divipor":1, "hex":0, "dcorta":0, "cadeia":2, "crono":0, "comando":0,
  523. "hora":0, "data":0, "tan":0, "oct":0, "val":0, "menos":1, "mincula":0, "maicula":0,
  524. "int":0, "esquerdo":1, "troca":-3, "localiz":-3, "ppus":-4, "linha":-6,
  525. "retorno":-2, "aleatint":2, "apoten":1, "ordem":0, "mod":1, "cosseno":0,
  526. "estanao":0, "seno":0, "incad":2, "mudar":-1, "shell":-1, "ordshell":0,
  527. "smb":0, "raizquadr":0}
  528. funcs = {"funcao" : -1, "severd" : -2, "seigual" : -3, "semenos" : -3,
  529. "semais" : -3, "tentar":0, "exceto":0, "retomar":0, "outra":0}
  530. ufunc = {}
  531. #addfuncs = addtoout[0] + addtoout[1] + addtoout[3] + """
  532. addfuncs = """
  533. def figcortexto(x, f):
  534. b = 0
  535. if f == None: f = 0
  536. if b == None: b = 0
  537. n = "0"
  538. if f > 7: n = "1" ; f = f - 8
  539. if f == 1: f = 4 ## switch ansi colors for qb colors
  540. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  541. if f == 3: f = 6
  542. elif f == 6: f = 3
  543. if b > 7: b = b - 8
  544. if b == 1: b = 4
  545. elif b == 4: b = 1
  546. if b == 3: b = 6
  547. elif b == 6: b = 3
  548. stdout.write("\\x1b[" + n + ";" + str(30+f) + "m")
  549. return "\\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
  550. figcgapal = [(0, 0, 0), (0, 0, 170), (0, 170, 0), (0, 170, 170),
  551. (170, 0, 0), (170, 0, 170), (170, 85, 0), (170, 170, 170),
  552. (85, 85, 85), (85, 85, 255), (85, 255, 85), (85, 255, 255),
  553. (255, 85, 85), (255, 85, 255), (255, 255, 85), (255, 255, 255)]
  554. def figobter(p, s): return s
  555. def figrealcar(x, b):
  556. f = None
  557. if f == None: f = 0
  558. if b == None: b = 0
  559. n = "0"
  560. if f > 7: n = "1" ; f = f - 8
  561. if f == 1: f = 4 ## switch ansi colors for qb colors
  562. elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
  563. if f == 3: f = 6
  564. elif f == 6: f = 3
  565. if b > 7: b = b - 8
  566. if b == 1: b = 4
  567. elif b == 4: b = 1
  568. if b == 3: b = 6
  569. elif b == 6: b = 3
  570. stdout.write("\\x1b[" + n + str(40+b) + "m")
  571. return "\\x1b[" + n + str(40+b) + "m"
  572. def figincad(x, p, e):
  573. try: return p.index(e) + 1
  574. except: return 0
  575. def figmudar(p):
  576. try: figoch(p)
  577. except: print "no such file or directory: " + str(p) ; figend(1)
  578. def figshell(p):
  579. global figsysteme
  580. try: figsysteme = figsh(p)
  581. except:
  582. print "error running shell command: " + chr(34) + str(p) + chr(34)
  583. figend(1)
  584. def figordshell(c):
  585. global figsysteme
  586. try:
  587. figsysteme = 0
  588. sh = figpo(c)
  589. ps = sh.read().replace(chr(13) + chr(10),
  590. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  591. figsysteme = sh.close()
  592. except:
  593. print "error running arrshell command: " + chr(34) + str(c) + chr(34)
  594. figend(1)
  595. return ps[:]
  596. def figsmb(p):
  597. p = float(p)
  598. if p > 0: return 1
  599. if p < 0: return -1
  600. return 0
  601. def figcad(p): return str(p)
  602. def figimpress(p): print p
  603. def figcar(p):
  604. if type(p) == str:
  605. if len(p) > 0:
  606. return p[0]
  607. return chr(p)
  608. def figimpressha(p): stdout.write(str(p)) ; sys.stdout.flush()
  609. def figesquerdo(p, s): return p[:s]
  610. def figmeio(p, s, x):
  611. arr = 0
  612. if type(p) == list or type(p) == tuple: arr = 1
  613. rt = p[s - 1:x + s - 1]
  614. if arr and len(rt) == 1: rt = rt[0]
  615. return rt
  616. def figdireito(p, s): return p[-s:]
  617. def figaleatint(x, s, f):
  618. return randint(s, f)
  619. def figmincula(p): return p.lower()
  620. def figmaicula(p): return p.upper()
  621. def figint(p): return int(p)
  622. def figordpus(x, p, s):
  623. if 1:
  624. #if type(p) == str: p = p + s # str(s) if you want it easier
  625. if 1: #type(p) == list:
  626. if type(s) == tuple:
  627. if len(s) == 1: fas = s[0]
  628. elif type(s) == list:
  629. if len(s) == 1: fas = s[0]
  630. else:
  631. fas = s
  632. x[p - 1] = s
  633. #if type(p) == tuple:
  634. # if type(s) == tuple:
  635. # p = tuple(list(p) + list(s))
  636. # elif type(s) == list:
  637. # p = tuple(list(p) + s[:])
  638. # else:
  639. # p = tuple(list(p) + [s])
  640. #if type(p) == str: p = p + s # str(s) if you want it easier
  641. def figaberto(x, s):
  642. import fileinput
  643. if s.lower() == "w":
  644. if (x) not in figfilehandles.keys():
  645. figfilehandles[x] = open(x[:], s.lower())
  646. elif s.lower() == "r":
  647. if (x) not in figfilehandles.keys():
  648. figfilehandles[x] = fileinput.input(x[:])
  649. figfilecounters[x] = 0
  650. else:
  651. if (x) not in figfilehandles.keys(): figfilehandles[x] = open(x[:], s[:])
  652. def figaimpress(x, s):
  653. fon = figosname
  654. sep = chr(10)
  655. if fon == "nt": sep = chr(13) + chr(10)
  656. figfilehandles[s].write(str(x) + sep)
  657. def figalinhaentra(x, s):
  658. try:
  659. p = figfilehandles[s][figfilecounters[s]].replace(chr(13),
  660. "").replace(chr(10), "")
  661. figfilecounters[s] += 1
  662. except:
  663. p = chr(10)
  664. return p
  665. def figfechar(x):
  666. if (x) in figfilehandles.keys():
  667. figfilehandles[x].close() ; del figfilehandles[x]
  668. try: del figfilecounters[x]
  669. except: pass
  670. def figlmp(x):
  671. if figosname == "nt": cls = figsh("cls")
  672. else: stdout.write("\x1b[2J\x1b[1;1H") ; sys.stdout.flush()
  673. def figordaberto(x, s):
  674. x = open(s).read().replace(chr(13) + chr(10), chr(10)).replace(chr(13),
  675. chr(10)).split(chr(10))
  676. return x[:]
  677. def figordcurl(x, s):
  678. from urllib import urlopen
  679. x = str(urlopen(s).read()) ; x = x.replace(chr(13) + chr(10),
  680. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  681. return x[:]
  682. def figordstdin(x):
  683. ps = []
  684. for p in stdin: ps += [p[:-1]]
  685. return ps[:]
  686. def figordobter(x, p, s):
  687. if 1:
  688. return p[s - 1]
  689. def figmais(p, s):
  690. if type(p) in (float, int):
  691. if type(s) in (float, int):
  692. p = p + s
  693. else:
  694. p = p + s # float(s) if you want it easier
  695. if p == float(int(p)): p = int(p)
  696. else:
  697. if type(p) == str: p = p + s # str(s) if you want it easier
  698. if type(p) == list:
  699. if type(s) == tuple:
  700. p = p + list(s)
  701. elif type(s) == list:
  702. p = p + s[:]
  703. else:
  704. p = p + [s]
  705. if type(p) == tuple:
  706. if type(s) == tuple:
  707. p = tuple(list(p) + list(s))
  708. elif type(s) == list:
  709. p = tuple(list(p) + s[:])
  710. else:
  711. p = tuple(list(p) + [s])
  712. return p
  713. def figjuntar(p, x, s):
  714. t = ""
  715. if len(x) : t = str(x[0])
  716. for c in range(len(x)):
  717. if c > 0: t += str(s) + str(x[c])
  718. return t # s.join(x)
  719. def figordem(p):
  720. if type(p) in (float, int, str):
  721. p = [p]
  722. else:
  723. p = list(p)
  724. return p
  725. def figseparar(p, x, s):
  726. return x.split(s)
  727. def figval(n):
  728. n = float(n)
  729. if float(int(n)) == float(n): n = int(n)
  730. return n
  731. def figmultipor(p, s):
  732. if type(p) in (float, int):
  733. p = p * s # float(s) if you want it easier
  734. if p == float(int(p)): p = int(p)
  735. else:
  736. if type(p) == list:
  737. p = p[:] * s # figval(s)
  738. else:
  739. p = p * s # figval(s) if you want it easer
  740. return p
  741. def figdivipor(p, s):
  742. p = float(p) / s
  743. if p == float(int(p)): p = int(p)
  744. return p
  745. def figmenos(p, s): return p - s
  746. def figapoten(p, s):
  747. p = p ** s
  748. if p == float(int(p)): p = int(p)
  749. return p
  750. def figmod(p, s):
  751. return p % s
  752. def figcosseno(p):
  753. from math import cos ; p = cos(p)
  754. if p == float(int(p)): p = int(p)
  755. return p
  756. def figseno(p):
  757. from math import sin ; p = sin(p)
  758. if p == float(int(p)): p = int(p)
  759. return p
  760. def figraizquadr(p):
  761. from math import sqrt ; p = sqrt(p)
  762. if p == float(int(p)): p = int(p)
  763. return p
  764. def figecorta(p): return p.lstrip()
  765. def figlinhaentra(p): return raw_input()
  766. def figcomprim(p): return len(p)
  767. def figasc(p): return ord(p[0])
  768. def figatan(p):
  769. from math import atan ; p = atan(p)
  770. if p == float(int(p)): p = int(p)
  771. return p
  772. def fighex(p): return hex(p)
  773. def figdcorta(p): return p.rstrip()
  774. def figcadeia(x, p, n):
  775. if type(n) == str: return n * p
  776. return chr(n) * p
  777. def figcrono(p):
  778. from time import strftime
  779. return int(strftime("%H"))*60*60+int(strftime("%M"))*60+int(strftime("%S"))
  780. def fighora(p): from time import strftime ; return strftime("%H:%M:%S")
  781. def figdata(p): from time import strftime ; return strftime("%m/%d/%Y")
  782. def figcomando(p): return figargv[1:]
  783. def figtan(p):
  784. from math import tan ; p = tan(p)
  785. if p == float(int(p)): p = int(p)
  786. return p
  787. def figoct(p): return oct(p)
  788. def figpausa(p, s):
  789. #print lc () + p
  790. #addto[0] = 1
  791. sleep(s)
  792. def figordordena(p):
  793. p.sort()
  794. def figdisplay(x):
  795. global figraphics, figrupd
  796. figrupd = 0
  797. if figraphics == 1:
  798. pygame.display.update()
  799. def figreverso(p):
  800. if type(p) == list: p.reverse() ; return p
  801. elif type(p) == str:
  802. p = map(str, p) ; p.reverse()
  803. p = "".join(p)
  804. return p
  805. def figordreverso(p):
  806. p.reverse()
  807. def figfuncao(p, s): return p
  808. def figfim(x): quit()
  809. def figif(p, s): return p
  810. def figthen(p, s): return p
  811. def figsistema(x): quit()
  812. \n"""
  813. demo = """
  814. p 7 ordem multipor 5
  815. x "hello, world!"
  816. #z x impressha
  817. #z x impress
  818. x 5
  819. x 5 multipor 7
  820. x 3 mais 5 multipor 7
  821. z x abs
  822. x z cad asc abs int
  823. funcao oi p | # function hello(p)
  824. x "oi, " | # x = "hello, "
  825. x impressha retorno 5 | # print x; : hello = 5 : exit function
  826. fig | # end function
  827. x oi x | # x = hello(x)
  828. #x if 5```````````````` # if x = 5 then
  829. #p "five" print```````` # p = "five" : print p
  830. #else`````````````````` # else
  831. #p "not 5 or 7" print`` # p = "not 5 or 7" : print p
  832. #fig````````````````````# end if
  833. c comando impress
  834. #y x````````````````````# y = x
  835. #y chr 70 mid y 1 1```` # y = chr(70) : y = mid(y, 1, 1)
  836. #x print sleep 2 cls````# print x : sleep 2 : cls
  837. p impress
  838. p impress ordpus 2 8 impress
  839. z juntar p "(_)" impress
  840. x z impress
  841. p impress fim
  842. funcao pete
  843. p "oi" impress
  844. fig
  845. funcao mais5 r
  846. x obter r mais 5 retorno x
  847. fig
  848. funcao ppp
  849. z 32 car impress
  850. para p 1 100 1
  851. x aleatint 0 3
  852. y aleatint 0 3
  853. c aleatint 1 9
  854. #z ppus x y c
  855. c cortexto 7
  856. seg
  857. fig
  858. z ppp
  859. z pausa 1
  860. #textmode
  861. z ppp pausa 2 z ppp
  862. """
  863. p = ""
  864. try: p = right(sys.argv, 1)[0]
  865. except: pass
  866. if not ".figu" in p.lower():
  867. if p.lower() == "help":
  868. stdout.write("\n type (any) part of the command you want help on." +
  869. "\n\n fig will show all matches.\n\n\n ")
  870. helpf = chelp(raw_input())
  871. if not helpf: print(color(14,0)+"\n no commands match your search.") ; print("")
  872. color(7,0)
  873. #try: inputfile = stdin.read().replace(chr(13), "").split("\n")
  874. #except:
  875. #print "need an input file to do anything..."; print ; quit()
  876. quit()
  877. else:
  878. print "using built-in demo source, translating to demo.figu.py..." ; print
  879. p = "demo.figu"
  880. inputfile = demo.replace(chr(13), "").split("\n")
  881. else:
  882. try:
  883. inputfile = open(p).read().replace(chr(13) + chr(10),
  884. chr(10)).replace(chr(13), chr(10)).split(chr(10))
  885. except: print "couldn't open \"" + p + "\", exiting." ; print ; quit()
  886. try: outfile = open(p + ".py", "w")
  887. except: print "couldn't write to \"" + p + ".py" "\", exiting." ; print ; quit()
  888. outname = p + ".py"
  889. flen = len(str(len(inputfile)))
  890. linecount = 0
  891. indent = 0
  892. inlinep = 0
  893. inle = 0
  894. errorin = 0
  895. errorsrc = ""
  896. error = ""
  897. mode = 0
  898. figraphics = -1 # -1 = uninitialized, 0 = textmode, 1 = initialized
  899. vrs = []
  900. vr = ""
  901. outb = []
  902. ingfx = 0
  903. linesoutc = 0
  904. for p in inputfile:
  905. linecount += 1 ; vrop = 0 ; vrcl = 0
  906. if linecount == 1:
  907. outfile.write("#!/usr/bin/env python" + "\n# encoding: utf-8\n")
  908. if "," in proginf:
  909. outfile.write("# figueira translator version: " + proginf.split(",")[0] + "\n")
  910. if inlinep:
  911. if p.lower().strip() == "fig":
  912. inlinep = 0
  913. print lc() + p
  914. indent = atleast(0, indent - 4)
  915. else:
  916. print lc() + color(2, None) + p + color(7, None)
  917. #% write copied lines of inline python
  918. outfilewrite(outb, chr(32) * atleast(0, indent - 4) +
  919. leftfour(p) + "\n")
  920. elif mode == "output the following:":
  921. if p.lower().strip() == "display":
  922. mode = 0
  923. print lc() + p
  924. else:
  925. wr(chr(32) * atleast(0, indent) + "print \"" + p.replace(chr(34),
  926. "\" + chr(34) + \"").replace(chr(92), "\" + chr(92) + \"") + "\"")
  927. print lc() + p.replace(chr(34), "\" + chr(34) + \"").replace(chr(92),
  928. "\" + chr(92) + \"")
  929. elif mode == 0:
  930. x = figfsplit(p.lstrip())
  931. lp = p.lower()
  932. if not len(p):
  933. print lc() + ""
  934. #% write copied blank lines from inline python
  935. outfilewrite(outb, "\n")
  936. if len(p.lstrip()):
  937. e = 0
  938. if p.lstrip()[0] == "#":
  939. if linecount == 1:
  940. es = 0
  941. try:
  942. if p.lstrip()[1] == "!": es = 1
  943. except: es = 0
  944. if not es:
  945. wr(p)
  946. print lc(), figfsp(p)
  947. else: print lc() + "[this first comment isn't copied over]"
  948. es = 0
  949. else:
  950. #% write comments
  951. #print color(14, 0) + p + color(7,0) ; znul = raw_input() #$
  952. outfilewrite(outb, chr(32) * atleast(0, indent) + p + "\n")
  953. print lc(), figfsp(p)
  954. elif lnob(x, 0) == "figg":
  955. e = 2
  956. elif lp.rstrip() == "python":
  957. indent += 4
  958. inlinep = 1
  959. print lc() + p
  960. else:
  961. if not lnob(x, 0) == "figg":
  962. if lnob(x, 0) != "fig" and not lnob(x,
  963. 0) in cmds.keys() and not lnob(x,
  964. 0) in funcs.keys() + ["paraem", "para", "funcao", "segem",
  965. "seg", "enquant", "efim"] + ["saida", "passar"]:
  966. if not lnob(x, 0) in vrs: vrs += [lnob(x, 0)[:]]
  967. #% write variable
  968. #var: print color(14, 0) + "variable:" + lnob(x, 0) + color(7,0) ; znul = raw_input() #$
  969. outfilewrite(outb, "\n")
  970. outfilewrite(outb, chr(32) * atleast(0, indent) +
  971. "figlist = 0\n")
  972. outfilewrite(outb, chr(32) * atleast(0, indent) +
  973. "try: figlist = int(type(" + lnob(x, 0) + ") == list)\n")
  974. outfilewrite(outb, chr(32) * atleast(0, indent) +
  975. "except NameError: pass\n")
  976. outfilewrite(outb, chr(32) * atleast(0, indent) +
  977. "if not figlist: " + lnob(x, 0) + " = 0 \n")
  978. if lnob(x, 0) == "fig":
  979. #print lc () + p
  980. #% write? its whitespace
  981. #$
  982. indent = atleast(0, indent - 4)
  983. if lnob(x, 0) == "efim":
  984. #print lc () + p
  985. #% write? its whitespace
  986. #$
  987. indent = atleast(0, indent - 4)
  988. if lnob(x, 0) == "seg":
  989. #print lc () + p
  990. #% write? its whitespace
  991. #$
  992. indent = atleast(0, indent - 4)
  993. if lnob(x, 0) == "segem":
  994. #print lc () + p
  995. #% write? its whitespace
  996. #$
  997. indent = atleast(0, indent - 4)
  998. if lnob(x, 0) == "tentar":
  999. #print lc () + p
  1000. #% write try line
  1001. #$
  1002. outfilewrite(outb, chr(32) * atleast(0, indent) + "try:\n")
  1003. indent = atleast(0, indent + 4)
  1004. if lnob(x, 0) == "outra":
  1005. #print lc () + p
  1006. #% write else line
  1007. #$
  1008. outfilewrite(outb, chr(32) * atleast(0, indent - 4) +
  1009. "else:\n")
  1010. if lnob(x, 0) == "exceto":
  1011. #print lc () + p
  1012. indent = atleast(0, indent - 4)
  1013. #% write except line
  1014. #$
  1015. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1016. "except:\n")
  1017. indent = atleast(0, indent + 4)
  1018. if lnob(x, 0) == "retomar":
  1019. #print lc () + p
  1020. #% write? its whitespace
  1021. #$
  1022. indent = atleast(0, indent - 4)
  1023. if lnob(x, 0) == "enquant":
  1024. #print lc () + p
  1025. #% write simple loop
  1026. #$
  1027. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1028. "while 1:\n")
  1029. indent += 4
  1030. if lnob(x, 0) == "funcao" and len(getmore(x, 1)) > 0:
  1031. #print lc () + p
  1032. mkf = []
  1033. funcname = getlmore(x, 1)[0]
  1034. prm = 1
  1035. while 1:
  1036. try:
  1037. aprm = getlmore(x, 1)[prm]
  1038. if len(aprm):
  1039. if aprm[0] != "#":
  1040. mkf += [aprm]
  1041. if aprm not in vrs: vrs += [aprm[:]]
  1042. prm += 1
  1043. except: break
  1044. ufunc[funcname] = mkf[:] #; print ufunc # #
  1045. #print ufunc
  1046. #print len(ufunc[funcname])
  1047. #% write func def
  1048. #$ print color(14,0)+ "def " + funcname + "(" + ", ".join(mkf) + "):" + color(7,0)
  1049. outfilewrite(outb, chr(32) * atleast(0, indent) + "def " +
  1050. funcname + "(" + ", ".join(mkf) + "):\n")
  1051. indent += 4
  1052. if lnob(x, 0) == "para" and len(getmore(x, 1)) == 4:
  1053. #print lc () + p
  1054. gmro = getlmore(x, 1)[0]
  1055. gmrt = getlmore(x, 1)[1]
  1056. gmrh = getlmore(x, 1)[2]
  1057. gmrf = getlmore(x, 1)[3]
  1058. if gmro not in vrs: vrs += [gmro[:]]
  1059. if "." not in gmrf and (gmrf.strip()) not in ("0",
  1060. "0.0", "-0") and "." not in gmrt and "." not in gmrh:
  1061. #% write standard for loop
  1062. #$
  1063. outfilewrite(outb, chr(32) * atleast(0, indent)
  1064. + "for "
  1065. + gmro + " in range(int(float(" + gmrt +
  1066. ")), int(float(" + gmrh + ")) + figsmb(" + gmrf +
  1067. "), fignonz(int(float(" + gmrf + ")))):\n")
  1068. else:
  1069. #% write for loop that allows floating step
  1070. #$
  1071. outfilewrite(outb, chr(32) * atleast(0, indent) + gmro
  1072. + " = float(" + gmrt + ") - float(" + gmrf + ")\n" +
  1073. chr(32) * atleast(0, indent) + "while 1:\n" + chr(32) *
  1074. atleast(0, indent + 4) + gmro + " += float(" + gmrf +
  1075. ")\n" + chr(32) * atleast(0, indent + 4) + "if " +
  1076. gmrf +
  1077. " > 0 and " + gmro + " > float(" + gmrh + "): break\n"
  1078. + chr(32) * atleast(0, indent + 4) + "elif " + gmrf +
  1079. " <= 0 and " + gmro +
  1080. " < float(" + gmrh + "): break\n")
  1081. indent += 4
  1082. if lnob(x, 0) == "paraem" and len(getmore(x, 1)) == 2:
  1083. #print lc () + p
  1084. gmro = getlmore(x, 1)[0]
  1085. gmrt = getlmore(x, 1)[1]
  1086. if gmro not in vrs: vrs += [gmro[:]]
  1087. #% write forin command with params
  1088. #$
  1089. outfilewrite(outb, chr(32) * atleast(0, indent) + "for " +
  1090. gmro + " in " + gmrt + ":\n")
  1091. indent += 4
  1092. if lnob(x, 0) == "saida":
  1093. #print lc () + p
  1094. #% write break command
  1095. #$ print
  1096. outfilewrite(outb, chr(32) *
  1097. atleast(0, indent) + "break\n")
  1098. if lp.rstrip() == "graficos":
  1099. ingfx = 1
  1100. #% write change to default mode (dont suppress gfx)
  1101. #$
  1102. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1103. "figraphics = -1\n")
  1104. figraphics = -1
  1105. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1106. "figpset(0, -1, -1, 0)\n")
  1107. #print lc () + p
  1108. if lnob(x, 0) == "textomodo":
  1109. #print lc () + p
  1110. addto[3] = 1
  1111. #% write change to text mode (suppress graphics)
  1112. #$
  1113. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1114. "figraphics = 0\n")
  1115. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1116. "stopgraphics()\n")
  1117. figraphics = 0
  1118. if lnob(x, 0) == "passar":
  1119. #print lc () + p
  1120. #% write pass command
  1121. #$ print
  1122. outfilewrite(outb, chr(32) *
  1123. atleast(0, indent) + "pass\n")
  1124. if lnob(x, 0) == "severd":
  1125. #print lc () + p
  1126. #% write iftrue
  1127. #$ print color(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +color(7,0)
  1128. outfilewrite(outb, chr(32) * atleast(0, indent) + "if " +
  1129. snob(x, 1) + ":\n") ; indent += 4
  1130. if lnob(x, 0) == "seigual" and len(getmore(x, 1)) == 2:
  1131. #print lc () + p
  1132. #% write ifequal
  1133. #$ print color(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +color(7,0)
  1134. outfilewrite(outb, chr(32) * atleast(0, indent) + "if " +
  1135. snob(x, 1) + " == " + snob(x, 2) + ":\n") ; indent += 4
  1136. if lnob(x, 0) == "semenos" and len(getmore(x, 1)) == 2:
  1137. #print lc () + p
  1138. #% write ifless
  1139. #$ print color(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +color(7,0)
  1140. outfilewrite(outb, chr(32) * atleast(0, indent) + "if " +
  1141. snob(x, 1) + " < " + snob(x, 2) + ":\n") ; indent += 4
  1142. if lnob(x, 0) == "semais" and len(getmore(x, 1)) == 2:
  1143. #print lc () + p
  1144. #% write ifmore
  1145. #$ print color(14,0) + "if " + snob(x, 1) + " > " + snob(x, 2) + ":\n"+ " ; " +color(7,0)
  1146. outfilewrite(outb, chr(32) * atleast(0, indent) + "if " +
  1147. snob(x, 1) + " > " + snob(x, 2) + ":\n") ; indent += 4
  1148. if lnob(x, 0) in cmds.keys(): # + ufunc.keys():
  1149. e = 4 ; shln = lnob(x, 0)
  1150. if lnob(x, 0) != "fig" and lnob(x,
  1151. 0) not in funcs.keys() + ["paraem", "para", "funcao",
  1152. "segem", "seg", "enquant", "efim"] + ["saida", "passar"]:
  1153. #print lc () + p
  1154. vr = lnob(x, 0)
  1155. #print vr, type(vr)
  1156. #print getlmore(x, 1)
  1157. prsc = 0
  1158. cstrctr = 0
  1159. csbuf = []
  1160. vrcs = ""
  1161. for prs in getlmore(x, 1):
  1162. #$ print prs
  1163. if "fig" in prs:
  1164. if prs[:3] == "fig": e = 2 ; break ; break
  1165. if prs in funcs.keys():
  1166. e = 3 ; shln = prs
  1167. prsc += 1
  1168. if cstrctr > 0:
  1169. vrcs += prs
  1170. cstrctr -= 1
  1171. if cstrctr == 0:
  1172. if lnob(x, prsc - 1) == "retorno":
  1173. #% write return command
  1174. #$ print color(14,0) +vrcs + " ; " +color(7,0)
  1175. outfilewrite(outb, chr(32) * atleast(0,
  1176. indent) + vrcs)
  1177. elif lnob(x, prsc - 2) == "troca":
  1178. vrcs = lnob(x, prsc - 1) + ", " + lnob(x,
  1179. prsc - 0) + " = " + lnob(x,
  1180. prsc - 0) + ", " + lnob(x, prsc - 1)
  1181. #% write swap of 2 vars in python syntax
  1182. #$ print color(14,0) +vrcs + " ; " +color(7,0)
  1183. outfilewrite(outb, chr(32) * atleast(0,
  1184. indent) + vrcs + " ; ")
  1185. else:
  1186. if figprsbac in ufunc.keys():
  1187. #% write parametered func call
  1188. #$ print color(14,0)+ vrcs + ") ; " + lnob(x, 0) + " = fignone(" + lnob(x, 0) + ", figbac) ; " +color(7,0)
  1189. outfilewrite(outb, chr(32) * atleast(0,
  1190. indent) + vrcs + ") ; " + lnob(x, 0) +
  1191. " = fignone(" + lnob(x, 0) +
  1192. ", figbac) ; ") ; vrcl += 1
  1193. else:
  1194. #% write builtin func call assignment
  1195. #$ print color(14,0)+ vr + " = " + vrcs + ") ; " +color(7,0)
  1196. outfilewrite(outb, chr(32) *
  1197. atleast(0, indent) + vrcs + ") ; ") ; vrcl += 1
  1198. else:
  1199. vrcs += ", " #; print "*"
  1200. #if
  1201. continue
  1202. if prs.lower() in funcs.keys() + ["paraem", "para",
  1203. "funcao", "segem", "seg", "enquant",
  1204. "efim"] + ["saida", "passar"]:
  1205. e = 3
  1206. figprsbac = None
  1207. if prs.lower() in vrs and cstrctr == 0:
  1208. #and len(getmore(x, 1)) == 1:
  1209. #% write lefthand variable assignment
  1210. #$ print color(14,0)+ vr + " = " + prs.lower() +color(7,0)
  1211. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1212. vr + " = " + prs.lower() + "\n")
  1213. if prs[0] == "\"":
  1214. #% write string assignment (any place in shared line)
  1215. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1216. vr + " = " + prs + " ; ")
  1217. if prs[0] in ".1234567890-":
  1218. #% write numerics
  1219. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1220. vr + " = " + prs + " ; ")
  1221. if prs[0] == "#":
  1222. #% write trailing comments #$ print color(14, 0) + prs + color(7,0)
  1223. outfilewrite(outb, prs + "\n") ; break
  1224. if prs.lower() in ufunc.keys():
  1225. #% write pre-func-call var backup for sub-style behavior #$ print color(14, 0) + "figbac = " + lnob(x,0) + " ; " + color(7,0)
  1226. outfilewrite(outb, chr(32) * atleast(0, indent) +
  1227. "figbac = " + lnob(x,0) + " ; " ) # ##
  1228. figprsbac = prs.lower()
  1229. cstrctr = len(ufunc[prs])
  1230. #print cstrctr
  1231. if cstrctr == 0:
  1232. #% write zero-param func/?sub call
  1233. #$ print color(14, 0) + vr + " = " + prs.lower() + "() ; " + lnob(x, 0) + " = fignone(" + lnob(x, 0) + ", figbac) ; " + color(7,0)
  1234. outfilewrite(outb, chr(32) *
  1235. atleast(0, indent) +
  1236. vr + " = " + prs.lower() + "() ; " + lnob(x, 0) +
  1237. " = fignone(" + lnob(x, 0) + ", figbac) ; ") # #
  1238. else:
  1239. #print "y"
  1240. vrop += 1
  1241. vrcs = vr + " = " + prs.lower() + "("
  1242. #$ print color(4, 0) + vr + " = " + prs.lower() + "(" + color(7,0) #$
  1243. #multiparameter
  1244. if prs.lower() in cmds.keys():
  1245. if prs.lower() in ["display", "ppus", "linha"]:
  1246. ingfx = 1
  1247. ##print prs
  1248. cstrctr = cmds[prs]
  1249. ##print cstrctr
  1250. if cstrctr == -1:
  1251. #% write zero-param subs
  1252. #print color(14, 0) + "fig" + prs.lower() + "(" + vr
  1253. #+ ") ; " + color(7,0) ; #znul = raw_input() #$
  1254. outfilewrite(outb, chr(32) *
  1255. atleast(0, indent) + "fig" +
  1256. prs.lower() + "(" + vr + ") ; " ) ; vrcl += 1
  1257. if cstrctr == 0:
  1258. #% write zero-param functions
  1259. #print color(14, 0) + vr + " = fig" + prs.lower()
  1260. #+ "(" + vr + ") ; "+ color(7,0) ; #znul = raw_input() #$
  1261. outfilewrite(outb, chr(32) * atleast(0,
  1262. indent) + vr +
  1263. " = fig" + prs.lower() + "(" + vr + ") ; " ) ; vrcl += 1
  1264. if cstrctr < -1:
  1265. if prs == "retorno":
  1266. cstrctr = abs(cstrctr) - 1
  1267. vrcs = "return " #parameter
  1268. else:
  1269. cstrctr = abs(cstrctr) - 1
  1270. if prs == "troca": vrcs = "troca "
  1271. else:
  1272. vrop += 1
  1273. vrcs = "fig" + prs.lower() + "(" + vr
  1274. vrcs += ", " #multiparameter
  1275. else:
  1276. vrop += 1
  1277. vrcs = vr + " = fig" + prs.lower() + "(" + vr
  1278. vrcs += ", " #multiparameter
  1279. if vrop == vrcl and e == 0:
  1280. print lc(), figfsp(p)
  1281. #% finish each line with lf
  1282. outfilewrite(outb, "\n")
  1283. else:
  1284. print lc() + p
  1285. else:
  1286. e = 2
  1287. # if not len(error):
  1288. # error = "error: unknown command."
  1289. # errorin = linecount
  1290. # errorsrc = p
  1291. # print lc () + "unknown:", p
  1292. #vrck = len(outb)
  1293. #try:
  1294. # if "(" in vrcs and vrck == 0:
  1295. # e = 1
  1296. #except: pass
  1297. if e == 1:
  1298. e = 0
  1299. if not len(error):
  1300. error = "error: problem in command structure or details."
  1301. errorin = linecount
  1302. errorsrc = p
  1303. print lc() + color(14, 0) + str(p) + color(7, 0)
  1304. break
  1305. if e == 2:
  1306. e = 0
  1307. if not len(error):
  1308. error = "error: cannot create variable or function beginning"
  1309. error += " with \"fig\""
  1310. errorin = linecount
  1311. errorsrc = p
  1312. print lc() + color(14, 0) + p + color(7, 0)
  1313. break
  1314. if e == 3:
  1315. e = 0
  1316. if not len(error):
  1317. error = "error: single-line command \"" + shln + "\" not on own line"
  1318. errorin = linecount
  1319. errorsrc = p
  1320. print lc() + color(14, 0) + p + color(7, 0)
  1321. break
  1322. if e == 4:
  1323. e = 0
  1324. if not len(error):
  1325. error = "error: shared-line function \""
  1326. error += shln + "\" cannot be used to start a line"
  1327. errorin = linecount
  1328. errorsrc = p
  1329. print lc() + color(14, 0) + p + color(7, 0)
  1330. break
  1331. if vrcl != vrop:
  1332. e = 0
  1333. if not len(error):
  1334. error = "error: a command has the wrong number of parameters."
  1335. errorin = linecount
  1336. errorsrc = p
  1337. print lc() + color(14, 0) + str(p) + color(7, 0)
  1338. break
  1339. #for p in range(len(addtoout)):
  1340. # if addto[p]: outfilewrite(outb, addtoout[p])
  1341. #for p in buf:
  1342. # outfilewrite(outb, p)
  1343. if ingfx == 0: addtoout[3] = ""
  1344. outfile.write(addtoout[0] + addtoout[1] + addtoout[3] + addfuncs)
  1345. for outsb in outb: outfile.write(outsb)
  1346. outfile.close()
  1347. print
  1348. if errorin:
  1349. print error ; color(14, None) ; print "error in line " + str(errorin) + ":"
  1350. color(7, None)
  1351. print errorsrc
  1352. #from os import system as stf ; p = stf("touch e") # patched jun 2016
  1353. else:
  1354. try: os.system("chmod +x \"" + outname + "\"")
  1355. except: pass
  1356. color (11, None) ; print "translation complete. ", ; color(7, None)
  1357. print "here's a python script you can run: ",
  1358. print color(11, None) + outname + color(7, None)
  1359. print