free software resistance
the cost of computing freedom is eternal vigilance
### difig.fig
*original date:* nov 2018
*originally posted:* oct 2024
difig.fig:
```
function decodeline p
python
#### license: creative commons cc0 1.0 (public domain)
#### http://creativecommons.org/publicdomain/zero/1.0/
proginf = "analyze fig 4.0, nov 2016 mn"
import sys
import os
from sys import stdin, stdout
from os import popen
try: from colorama import init ; init()
except: pass
buf = []
cmdhelp = [("timer", "input (shared-line) change main variable to number of seconds past midnight")
,("arrstdin", "input (shared-line) change main variable to array containing lines of stdin")
,("lineinput", "input (shared-line) change main variable to string input from keyboard")
,("flineinput filepath", "input (shared-line) change main var to string of line from open file filepath")
,("time", "input (shared-line) change main variable to string of current time: hh:mm:ss")
,("arropen filepath", "input (shared-line) change main variable to array of file lines in filepath")
,("date", "input (shared-line) change main variable to string of the date: mm/dd/yyyy")
,("arrcurl url", "input (shared-line) like arropen, except downloading url into the array")
,("sleep seconds", "input (shared-line) wait for number of seconds before continuing with program")
,("command", "input (shared-line) change main variable to array of command line parameters")
,("print", "output (shared-line) output main variable to the screen (aka stdout)")
,("prints", "output (shared-line) put main var to screen; like print but (s)tays on line.")
,("fprint filepath", "output (shared-line) write main variable to open file designated by filepath")
,("display", "output (shared-line) 1st time: stop automatic graphx update. 2nd, etc: update.")
,("cls", "output (shared-line) clear the screen. currently only affects text screen")
,("graphics", "output --\\own\\line dont(or stop) suppress(ing) graphics. this is the default.")
,("textmode", "output --\\own\\line suppress graphics; force graphics commands to use text.")
,("colortext colorcode", "output (shared-line) change color of upcoming text to colorcode from 0 - 15")
,("highlight colorcode", "output (shared-line) change background color of upcoming text tocolorcode 0-15")
,("locate row column", "output (shared-line) move to textmode position at row, column")
,("pset x y c", "output (shared-line) draw dot at location (x, y) in colorcode c (0 - 15)")
,("line x1 y1 x2 y2 c", "output (shared-line) draw line from (x1, y1) to (x2, y2) in colorcode c (0-15)")
,("while", "loop --\\own\\line mark the start of a loop (will keep going without break)")
,("break", "loop --\\own\\line put in the middle of a loop to exit (stop looping)")
,("for var strt stop step", "loop --\\own\\line start a for loop, changing var from strt to stop, by step")
,("forin var array", "loop --\\own\\line loop through each item in array; for each, set var to item")
,("iftrue ckvar", "conditional --\\own\\line run lines between iftrue and fig if ckvar is \"non-zero\"")
,("ifequal var1 var2", "conditional --\\own\\line run lines between ifequal and fig if var1 equals var2")
,("ifmore var1 var2", "conditional --\\own\\line run lines between ifmore and fig if var1 is > var2")
,("ifless var1 var2", "conditional --\\own\\line run lines between ifless and fig if var1 is < var2")
,("try", "conditional --\\own\\line put code that might not work between try and except")
,("except", "conditional --\\own\\line if code between try/except fails, run the code after except")
,("resume", "conditional --\\own\\line mark the end of try / except / resume command block")
,("else", "conditional --\\own\\line after if- line, before fig. run lines if condition isnt true")
,("function name p1 p2 …", "function --\\own\\line define function named name with optional params p1,p2, etc")
,("get parametername", "function (shared-line) (no longer required) copy parametername value to main var")
,("python", "function --\\own\\line put inline python code between lines python and fig")
,("fig/next/nextin/wend", "fig (interchangeable) function --\\own\\line finalize a block (started by if/while/function/for/forin")
,("pass", "function --\\own\\line blocks (for/next, etc) require something inside lines; pass works / does nothing")
,("lcase", "function (shared-line) change main variable to all-lower-case copy of own value")
,("ucase", "function (shared-line) change main variable to all-upper-case copy of own value")
,("str", "function (shared-line) convert main variable from number to string")
,("shell", "function (shared-line) run main variable contents in a command shell (os specific)")
,("asc", "function (shared-line) change main variable from string to ascii code of 1st char")
,("val", "function (shared-line) change main variable from string to numeric (int if whole)")
,("len", "function (shared-line) change main variable to numeric length of main var")
,("not", "function (shared-line) change main variable to zero if non-zero; or -1 if zero")
,("ltrim", "function (shared-line) strip whitespace from left side of main variable")
,("rtrim", "function (shared-line) strip whitespace from right side of main variable")
,("chr", "function (shared-line) change main variable from numeric to ascii/uni string")
,("arrshell", "function (shared-line) change main var to array of shell output (from main var)")
,("arreverse", "function (shared-line) change main variable from array to reverse order of array")
,("reverse", "function (shared-line) like arreverse (which might be faster for array) for strings")
,("arrsort", "function (shared-line) change main variable from array to sorted array")
,("#", "comment (can\\share) place at beginning (or end) of line, prior to a comment")
,("():;|=,. ( ) : ; | = , .", "optional (shared-line) use in a shared line (and some others) for aesthetics/notation")
,("left numofcharsoritems", "function (shared-line) change main variable to __ leftmost group of chars/items")
,("right numofchrsoritems", "function (shared-line) change main variable to __ rightmost group of chars/items")
,("arrget array position", "function (shared-line) change main variable to position-th item from array")
,("arrset position setto", "function (shared-line) change item in array in main variable to value of setto")
,("mid position len", "function (shared-line) change main variable to range of len items from position")
,("string len asciiorstr", "function (shared-line) change main variable to len instances of asciiorstr")
,("split string splitby", "function (shared-line) split string by separator splitby into array, to main var")
,("join array usestring", "function (shared-line) change main var to string by joining array using usestring")
,("instr lookin lookfor", "function (shared-line) change main var to numeric position of lookfor in lookin")
,("chdir", "function (shared-line) change current folder to path string from main variable")
,("system", "function (shared-line) put on (usually at the end of) a line to stop the program")
,("close", "function (shared-line) close the open file designated by main variable")
,("end", "function (shared-line) interchangeable with system which ends the program")
,("open mode", "function (shared-line) open file at filepath main variable in mode \"r\" or \"w\"")
,("return var", "function (shared-line) (optional) exit current function, returning value var")
,("swap var1 var2", "function (shared-line) change contents of var1 to contents of var2 and vice-versa")
,("plus numstrarr", "math (shared-line) change main variable to itself plus num or string or arr")
,("minus numeric", "math (shared-line) change main variable to itself minus numeric")
,("divby numeric", "math (shared-line) change main variable to itself divided by numeric")
,("times numeric", "math (shared-line) change main variable to itself times numeric")
,("oct", "math (shared-line) change main variable from numeric decimal to octal")
,("hex", "math (shared-line) change main variable from numeric decimal to hexadecimal")
,("cos", "math (shared-line) change numeric main variable to the cosine of itself")
,("sin", "math (shared-line) change numeric main variable to the sine of itself")
,("tan", "math (shared-line) change numeric main variable to its tangent")
,("atn", "math (shared-line) change numeric main variable to its arctangent")
,("int", "math (shared-line) change main variable from decimal (aka \"float\") to integer")
,("sgn", "math (shared-line) change main variable to 0 if 0, to -1 if < 0, or 1 if > 0.")
,("sqr", "math (shared-line) change main variable to square root of itself")
,("mod denominator", "math (shared-line) change main variable to: main var modulus denominator")
,("topwr n", "math (shared-line) raise numeric main variable to n-th power")
,("randint smallst largst", "input (shared-line) change main var to random number from smallst to largst")
,("arr", "function (shared-line) change main var to array (starting with same contents)") ]
def chelp(f):
ck = 0 ; print ""
for p in cmdhelp:
rcmd = p[0]
if f in rcmd.split()[0]:
ck = 1
rd = p[1].split()
rcat = rd[0] ; rd.remove(rd[0])
rt = rd[0] ; rd.remove(rd[0])
cde = rcmd.split(" ")
print ""
stdout.write(" " + color(14,0)+ cde[0])
cda = cde.remove(cde[0])
for c in cde:
stdout.write(" " + color(0, 7)+ " " + c + " " + color(7,0)+" ") ; stdout.flush()
print ""
print ""
print color(3,0) + " category:", rcat, rt.replace("\\", " ")
print ""
print " " + color(7,0) + " ".join(rd)
print ""
color(7,0);
return ck
def outfilewrite(outb, p):
outb += [p]
#global vrck
#vrck += p.strip()
#if inle: print color(5, 0) + p.rstrip() ; p=raw_input() ; quit()
def color(f, b):
if f == None: f = 0
if b == None: b = 0
n = "0"
if f > 7: n = "1" ; f = f - 8
if f == 1: f = 4 ## switch ansi colors for qb colors
elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
if f == 3: f = 6
elif f == 6: f = 3
if b > 7: b = b - 8
if b == 1: b = 4
elif b == 4: b = 1
if b == 3: b = 6
elif b == 6: b = 3
stdout.write("\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m")
return "\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
def bcolor(b):
f = None
if f == None: f = 0
if b == None: b = 0
n = "0"
if f > 7: n = "1" ; f = f - 8
if f == 1: f = 4 ## switch ansi colors for qb colors
elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
if f == 3: f = 6
elif f == 6: f = 3
if b > 7: b = b - 8
if b == 1: b = 4
elif b == 4: b = 1
if b == 3: b = 6
elif b == 6: b = 3
stdout.write("\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m")
return "\x1b[" + n + str(40+b) + "m"
def sgn(p):
p = float(p)
if p > 0: return 1
if p < 0: return -1
return 0
def left(p, s):
return p[:s]
def right(p, s):
return p[-s:]
def leftfour(p):
#####try:
#####if left(p, 4) == chr(32) * 4: p = right(p, len(p) - 4)
#####except:
pass
#####return p
def atleast(s, p):
if p < s: return s
else: return p
def figfsp(p):
pp = "" ; flg = 0
fsp = figfsplit(p)
for fp in enumerate(fsp):
if flg == 0 and fp[1] in cmds.keys():
pp += color(8,0) + "_" + color(7,0) + " " ; flg = cmds[fp[1]]
if flg < 0: flg = flg * -1
else: flg = flg + 1
pp += fp[1] + " "
if flg > 0:
flg -= 1
if flg == 0 and fp[0] + 1 < len(fsp):
pp += color(8,0) + "_" + color(7,0) + " "
return pp.rstrip().replace(color(8,0) + "_" + color(7,0) + " " + color(8,0) +
"_" + color(7,0), color(8,0) + "__" + color(7,0)).replace(color(8,0) + "_" +
color(7,0),color(8,0) + "__" + color(7,0))
def figfsplit(p):
# return p.split() # that was fine when strings weren't tokens
# we have to make this 3 tokens: variable "hello, world!" #comment not string
px = []
pxc = -1 # could use len(px) -1 instead?
inquotes = 0
remarked = 0
inspc = "" ; vnspc = ""
#print "->", p
for l in p:
if inquotes == 0 and remarked == 0 and l == "#":
remarked = 1
pxc += 1 ; px += [""]
if remarked == 1:
px[pxc] += l
if remarked == 0:
if l == "\"":
if inquotes == 0:
inquotes = 1 ; pxc += 1 ; px += [""]
else: inquotes = 0 #; px[pxc] += l
if inquotes == 1: px[pxc] += l
if remarked == 0 and inquotes == 0:
if vnspc not in "1234567890-" + chr(32) and l[0] == ".": l = " "
vnspc = l
if l[0] in "():;|=,": l = " "
if inspc != " " and l == " ": pxc += 1 ; px += [""]
if l != " ":
if pxc == -1: pxc += 1 ; px += [""]
px[pxc] += l.lower()
inspc = l
#print "->", px[:]
while ('') in px: px.remove('')
while (':') in px: px.remove(':')
for p in range(len(px)):
if px[p][0] != "#":
if right(px[p], 1) == ":":
lenpx = len(px[p]) - 1
if lenpx > 0:
px[p] = left(px[p], lenpx)
return px[:]
def nob(p, s):
r = ""
if s == len(p) - 1:
if len(p):
if p[s].rstrip() != ".": r = p[s].rstrip()
if len(r):
if r[-1:] == ".": r = left(r, len(r) - 1)
pfig = ""
try: pfig = left(p[s], 3)
except: pfig = ""
#if pfig.lower() == "fig" and p[s].lower() != "fig": return "figg"
#####try:
#####if r != "": return r
#####else: return p[s]
#####except: return ""
def snobl(p):
if "\"" in p: return p
else: return p.lower()
def snob(p, s):
r = ""
if s == len(p) - 1:
if len(p):
if p[s].rstrip() != ".": r = p[s].rstrip()
if len(r):
if r[-1:] == ".": r = left(r, len(r) - 1)
pqt = ""
try: pqt = left(p[s], 3)
except: pqt = ""
if pqt.lower() == "fig" and p[s].lower() != "fig": return "figg"
try:
if r != "": return snobl(r)
else: return snobl(p[s])
except: return ""
def lnob(p, s):
r = ""
if s == len(p) - 1:
if len(p):
if p[s].rstrip() != ".": r = p[s].rstrip()
if len(r):
if r[-1:] == ".": r = left(r, len(r) - 1)
pfig = ""
try: pfig = left(p[s], 3)
except: pfig = ""
if pfig.lower() == "fig" and p[s].lower() != "fig": return "figg"
#try:
# if r != "": return r.lower()
# else: return p[s].lower()
#####except: return ""
cmds = {"ltrim":0, "lineinput":0, "len":0, "asc":0, "atn":0, "str":0,
"get":1, "chr":0, "prints":-1, "sleep":-2, "arrsort":-1,
"arreverse":-1, "reverse":0, "display":-1, "system":-1, "end":-1,
"print":-1, "arrset":-3,
"split":2, "left":1, "join":2, "arrget":2, "mid":2, "right":1,
"plus":1, "times":1, "close":-1, "cls":-1, "flineinput":1, "fprint":-2,
"open":-2, "arropen":1, "arrstdin":0, "arrcurl":1, "colortext":-2,
"highlight":-2, "divby":1, "hex":0, "rtrim":0, "string":2, "timer":0, "command":0,
"time":0, "date":0, "tan":0, "oct":0, "val":0, "minus":1, "lcase":0, "ucase":0,
"int":0, "left":1, "swap":-3, "locate":-3, "pset":-4, "line":-6,
"return":-2, "randint":2, "topwr":1, "arr":0, "mod":1, "cos":0,
"not":0, "sin":0, "instr":2, "chdir":-1, "shell":-1, "arrshell":0,
"sgn":0, "sqr":0}
funcs = {"function" : -1, "iftrue" : -2, "ifequal" : -3, "ifless" : -3,
"ifmore" : -3, "try":0, "except":0, "resume":0, "else":0}
ufunc = {}
#####
def figcolortext(x, f):
b = 0
if f == None: f = 0
if b == None: b = 0
n = "0"
if f > 7: n = "1" ; f = f - 8
if f == 1: f = 4 ## switch ansi colors for qb colors
elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
if f == 3: f = 6
elif f == 6: f = 3
if b > 7: b = b - 8
if b == 1: b = 4
elif b == 4: b = 1
if b == 3: b = 6
elif b == 6: b = 3
stdout.write("\\x1b[" + n + ";" + str(30+f) + "m")
return "\\x1b[" + n + ";" + str(30+f) + ";" + str(40+b) + "m"
figcgapal = [(0, 0, 0), (0, 0, 170), (0, 170, 0), (0, 170, 170),
(170, 0, 0), (170, 0, 170), (170, 85, 0), (170, 170, 170),
(85, 85, 85), (85, 85, 255), (85, 255, 85), (85, 255, 255),
(255, 85, 85), (255, 85, 255), (255, 255, 85), (255, 255, 255)]
def figget(p, s): return s
def fighighlight(x, b):
f = None
if f == None: f = 0
if b == None: b = 0
n = "0"
if f > 7: n = "1" ; f = f - 8
if f == 1: f = 4 ## switch ansi colors for qb colors
elif f == 4: f = 1 ## 1 = blue not red, 4 = red not blue, etc.
if f == 3: f = 6
elif f == 6: f = 3
if b > 7: b = b - 8
if b == 1: b = 4
elif b == 4: b = 1
if b == 3: b = 6
elif b == 6: b = 3
stdout.write("\\x1b[" + n + str(40+b) + "m")
return "\\x1b[" + n + str(40+b) + "m"
def figinstr(x, p, e):
try: return p.index(e) + 1
except: return 0
def figchdir(p):
try: figoch(p)
except: print "no such file or directory: " + str(p) ; figend(1)
def figshell(p):
global figsysteme
try: figsysteme = figsh(p)
except:
print "error running shell command: " + chr(34) + str(p) + chr(34)
figend(1)
def figarrshell(c):
global figsysteme
#try:
# figsysteme = 0
# sh = figpo(c)
# ps = sh.read().replace(chr(13) + chr(10),
# #####chr(10)).replace(chr(13), chr(10)).split(chr(10))
# figsysteme = sh.close()
##except:
# print "error running arrshell command: " + chr(34) + str(c) + chr(34)
# figend(1)
return ps[:]
def figsgn(p):
p = float(p)
if p > 0: return 1
if p < 0: return -1
return 0
def figstr(p): return str(p)
def figprint(p): print p
def figchr(p):
if type(p) == str:
if len(p) > 0:
return p[0]
return chr(p)
def figprints(p): stdout.write(str(p)) ; sys.stdout.flush()
def figleft(p, s): return p[:s]
def figmid(p, s, x):
arr = 0
if type(p) == list or type(p) == tuple: arr = 1
rt = p[s - 1:x + s - 1]
if arr and len(rt) == 1: rt = rt[0]
return rt
def figright(p, s): return p[-s:]
def figrandint(x, s, f):
return randint(s, f)
def figlcase(p): return p.lower()
def figucase(p): return p.upper()
def figint(p): return int(p)
def figarrset(x, p, s):
if 1:
#if type(p) == str: p = p + s # str(s) if you want it easier
if 1: #type(p) == list:
if type(s) == tuple:
if len(s) == 1: fas = s[0]
elif type(s) == list:
if len(s) == 1: fas = s[0]
else:
fas = s
x[p - 1] = s
def figopen(x, s):
import fileinput
if s.lower() == "w":
if (x) not in figfilehandles.keys():
figfilehandles[x] = open(x[:], s.lower())
elif s.lower() == "r":
if (x) not in figfilehandles.keys():
figfilehandles[x] = fileinput.input(x[:])
figfilecounters[x] = 0
else:
if (x) not in figfilehandles.keys(): figfilehandles[x] = open(x[:], s[:])
def figfprint(x, s):
fon = figosname
sep = chr(10)
if fon == "nt": sep = chr(13) + chr(10)
figfilehandles[s].write(str(x) + sep)
def figclose(x):
if (x) in figfilehandles.keys():
figfilehandles[x].close() ; del figfilehandles[x]
try: del figfilecounters[x]
except: pass
def figcls(x):
if figosname == "nt": cls = figsh("cls")
else: stdout.write("\x1b[2J\x1b[1;1H") ; sys.stdout.flush()
# p = ""
# try: p = right(sys.argv, 1)[0]
# except: pass
# if not ".fig" in p.lower():
# if p.lower() == "help":
# stdout.write("\n type (any) part of the command you want help on." +
# "\n\n fig will show all matches.\n\n\n ")
# helpf = chelp(raw_input())
# if not helpf: print(color(14,0)+"\n no commands match your search.") ; print("")
# color(7,0)
# #try: inputfile = stdin.read().replace(chr(13), "").split("\n")
# #except:
# #print "need an input file to do anything..."; print ; quit()
# quit()
# else:
# #print "using built-in demo source, translating to demo.fig.py..." ; print
# p = "demo.fig"
# #inputfile = demo.replace(chr(13), "").split("\n")
# #else:
# # try:
# # inputfile = open(p).read().replace(chr(13) + chr(10), chr(10)).replace(chr(13), chr(10)).split(chr(10))
# # except: print "couldn't open \"" + p + "\", exiting." ; print ; quit()
# ##try: outfile = open(p + ".py", "w")
# ##except: print "couldn't write to \"" + p + ".py" "\", exiting." ; print ; quit()
# outname = p + ".py"
#flen = len(str(len(inputfile)))
#linecount = 0
#indent = 0
#inlinep = 0
#inle = 0
#errorin = 0
#errorsrc = ""
#error = ""
#mode = 0
######figraphics = -1 # -1 = uninitialized, 0 = textmode, 1 = initialized
#vrs = []
#vr = ""
#outb = []
#ingfx = 0
#linesoutc = 0
# for p in inputfile:
# linecount += 1 ; vrop = 0 ; vrcl = 0
# if linecount == 1:
# outfile.write("#!/usr/bin/env python" + "\n# encoding: utf-8\n")
# if "," in proginf:
# outfile.write("# fig translator version: " + proginf.split(",")[0] + "\n")
# if inlinep:
# if p.lower().strip() == "fig":
# inlinep = 0
# #####print lc() + p
# indent = atleast(0, indent - 4)
# else:
# #####print lc() + color(2, None) + p + color(7, None)
# #% write copied lines of inline python
# outfilewrite(outb, chr(32) * atleast(0, indent - 4) +
# leftfour(p) + "\n")
# elif mode == "output the following:":
# if p.lower().strip() == "display":
# mode = 0
# #####print lc() + p
# else:
# wr(chr(32) * atleast(0, indent) + "print \"" + p.replace(chr(34) #,
# #"\" + chr(34) + \"").replace(chr(92), "\" + chr(92) + \"") + "\"")
# #print lc() + p.replace(chr(34), "\" + chr(34) + \"").replace(chr(92),
# #"\" + chr(92) + \"")
fig
now "hello" print
now return p
fig
now decodeline ""
```
license: 0-clause bsd
```
# 2018 mn
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
```
=> https://freesoftwareresistance.neocities.org