I'm trying a fun little experiment where I try to build a Tierra-like program with a few lines of Julia code.
I thought that the metaprogramming thing would be really suitable for this.
Basically I have an expression object that converts itself to a string, mutates it by flipping some bits, and then tries to parse it and evaluate it. If it fails, then it discards the changes.
I have a couple questions:
- to avoid huge amounts of syntax errors I would like to recombine on an expression level rather than on the bitstring level. Are there any facilities to allow the manipulation of Expr objects? For example, drilling down into sub-expressions?
- if not, how can I convert a string into a BitArray, then back?
e = quote
s = string(e)
o = s # backup
# mutate the string
# somehow convert string to BitArray
b = BitArray(10000) # plus extra buffer to allow program to expand in size
f = rand(len(b)) < 1/len(b)
flipbits!(b[f])
#somehow convert back to string
try:
# convert back to string
e = parse(s)
eval(e)
catch:
print('something broke, reverting')
e = parse(o)
eval(e)
end