Advertisement
Guest User

fig 2.9 (python2, cc0, public domain)

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