add blog posts: dollcode, python-goto

This commit is contained in:
2025-05-12 10:05:59 -07:00
parent e0e86e964c
commit 8a2343335c
14 changed files with 445 additions and 1 deletions

View File

@ -0,0 +1,39 @@
~ λ dollcode -a "$(for i in {0..65536}; do echo -ne a; done)" >/dev/null & pid=$!; while kill -0 $pid 2>/dev/null; do pmap -x $pid | tail -n 1 | awk '{print ""$3/1024/1024 " GB"}'; sleep .5; done; wait $pid
[1] 11113
0.0247803 GB
0.261642 GB
0.625462 GB
0.982468 GB
1.34475 GB
1.69653 GB
2.07035 GB
2.43603 GB
2.80609 GB
3.16292 GB
3.53598 GB
3.90202 GB
4.27491 GB
4.63856 GB
5.02195 GB
5.37629 GB
5.75089 GB
6.13502 GB
6.48058 GB
6.85527 GB
7.24199 GB
7.6222 GB
7.96089 GB
8.33753 GB
8.719 GB
9.0616 GB
9.44017 GB
9.82001 GB
10.1397 GB
10.526 GB
10.9109 GB
11.0703 GB
10.956 GB
10.8826 GB
10.8184 GB
0 GB
[1] + done dollcode -a "$(for i in {0..65536}; do echo -ne a; done)" > /dev/null

View File

@ -0,0 +1,2 @@
~ λ time dollcode -a "$(for i in {0..65536}; do echo -ne a; done)" >/dev/null
dollcode -a "$(for i in {0..65536}; do echo -ne a; done)" > /dev/null 14.31s user 7.79s system 99% cpu 22.202 total

View File

@ -0,0 +1,29 @@
from goto_label import *
def fizzbuzz(n):
count = 0
#LABEL fizz_start
count += 1
string = ''
if count == n+1: goto &fizz_end;
goto &label(output.line + int(count%3==0)*4+int(count%5==0)*8);
#LABEL output
print(string if string else count)
goto &fizz_start
string += 'fizz'
goto &output;
string += 'buzz'
goto &output;
string += 'fizzbuzz'
goto &output;
#LABEL fizz_end
...
fizzbuzz(100)

View File

@ -0,0 +1,15 @@
from goto_label import *
def fib(n):
depth = 0
a = 1
b = 0
#LABEL fib_start
print(a)
a, b, depth = b, b + a, depth + 1
if depth > n+1: goto &fib_end
goto &fib_start
#LABEL fib_end
Ellipsis
fib(30)

View File

@ -0,0 +1,25 @@
from goto_label import *
def fizzbuzz(n):
count = 0
#LABEL fizz_start
str = ''
if n == count: goto &fizz_end;
count += 1
if count % 3 == 0: goto &fizz;
#LABEL buzz_check
if count % 5 == 0: goto &buzz;
goto &output;
#LABEL fizz
str += 'fizz'
goto &buzz_check;
#LABEL buzz
str += 'buzz'
#LABEL output
print(str if str else count)
goto &fizz_start;
#LABEL fizz_end
...
fizzbuzz(100)

View File

@ -0,0 +1,27 @@
_goto = type('goto', (object,), dict(__and__=lambda _, other: (_goto(other.line))))
goto = _goto()
def _goto(lineno):
frame = sys._getframe().f_back.f_back
called_from = frame
def hook(frame, event, _):
if event == 'line' and frame == called_from:
try:
frame.f_lineno = lineno
except ValueError as e:
print("jump failed:", e)
while frame:
frame.f_trace = None
frame = frame.f_back
return None
return hook
# it doesn't think we actually need to set the hook for each frame
# while frame:
# frame.f_trace = hook
# frame = frame.f_back
called_from.f_trace = hook
sys.settrace(hook)

View File

@ -0,0 +1,20 @@
from goto_label import *
count = 0
#LABEL lol
print('this prints twice')
count += 1
goto &lol if (count <= 1) else goto &goto_statements_are_a_perfectly_reasonable_thing_to_have_in_python
#LABEL last_print
print('this prints last')
goto &stop
#LABEL ellipsis
print('at least Ellipsis() has some use now, for normal reasons that make sense')
goto &last_print
#LABEL goto_statements_are_a_perfectly_reasonable_thing_to_have_in_python
print('it is sure this is how the variety of language features it is abusing were intended to be used')
goto &ellipsis
#LABEL stop
...

View File

@ -0,0 +1,46 @@
import sys, inspect
label = lambda lineno: type(
'label',
(object,),
dict(line=lineno)
)
def prepare_labels(fp, scope):
with open(fp, 'r') as f:
lines = f.readlines()
for lineno, line in enumerate(lines):
if line.startswith('#LABEL'):
# 1 + 1, first to account for 0 index then to account for comment line
scope.__setitem__(line.split(' ')[1].strip(), label(lineno+2))
_goto = type('goto', (object,), dict(__and__=lambda _, other: (_goto(other.line))))
goto = _goto()
def _goto(lineno):
frame = sys._getframe().f_back.f_back
called_from = frame
def hook(frame, event, _):
if event == 'line' and frame == called_from:
try:
frame.f_lineno = lineno
except ValueError as e:
print("jump failed:", e)
while frame:
frame.f_trace = None
frame = frame.f_back
return None
return hook
# it doesn't think we actually need to set the hook for each frame
# while frame:
# frame.f_trace = hook
# frame = frame.f_back
called_from.f_trace = hook
sys.settrace(hook)
prepare_labels(inspect.stack()[-1].frame.f_code.co_filename, inspect.stack()[-1].frame.f_globals)

View File

@ -0,0 +1,14 @@
label = lambda lineno: type(
'label',
(object,),
dict(line=lineno)
)
def prepare_labels(fp, scope):
with open(fp, 'r') as f:
lines = f.readlines()
for lineno, line in enumerate(lines):
if line.startswith('#LABEL'):
# 1 + 1, first to account for 0 index then to account for comment line
scope.__setitem__(line.split(' ')[1].strip(), label(lineno+2))

View File

@ -0,0 +1,49 @@
from goto_label import *
gaurd = 0
#LABEL say_hi
print('hi')
#LABEL loop_start
gaurd += 1
if gaurd > 2: goto &call_b
def a():
global gaurd
goto &a_skip
#LABEL a_label
print('inside a_label')
goto &loop_start
if gaurd:
return
#LABEL a_skip
print('a')
if gaurd < 2:
goto &say_hi
goto &a_label
...
def b():
global gaurd
goto &b_skip
#LABEL b_label
print('inside b_label')
goto &say_hi
goto &a_label
if gaurd > 2:
return
#LABEL b_skip
print('b')
goto &a_label
a()
goto &b_label
...
a()
print('done with a()')
goto &say_hi
...
#LABEL call_b
b()