BYTENAI

Backends

Python, and the bridge

A space can be emitted as Python, and the two runtimes can be run side by side and compared. The comparison is the point: agree is printed only when both produced the same state.

Emit

$ smurf emit genesis.smurf --lang python --out out.pythonexit 0
wrote python (4155 bytes) to out.python

The emitted file is the compiler's own AST, not a translation of a comment. It does not carry the event log.

Bridge

$ smurf bridge genesis.smurf --do transfer --arg amount=10 --lang python --dir bridgeexit 0
interpreter:
  source.energy = f64:90.0
  source.label = str:source
  target.energy = f64:10.0
  target.label = str:target
python:
  source.energy = f64:90.0
  source.label = str:source
  target.energy = f64:10.0
  target.label = str:target
agree: same state, both runtimes  3df57bde9e2cd77a
  ran: python3 bridge/bridge.py
  receipt 3df57bde9e2cd77ab4bf332c2ac4141ad7021628a7b605e8642c6f007c2796d9

The three hashes are the interpreter's state, Python's state, and the agreement id.

The six targets

Each language below was asked for by this compiler, not described from a specification. The exit code and the first line of output are the recording; a target that refused would say so here.

Emit, one language at a time. Exit 0 means the source was produced.
TargetCommandExitFirst line
Pythonsmurf emit … --lang python0wrote python (4155 bytes) to out.python
Rustsmurf emit … --lang rust0wrote rust (4819 bytes) to out.rust
Csmurf emit … --lang c0wrote c (5656 bytes) to out.c
C++smurf emit … --lang cpp0wrote c++ (5533 bytes) to out.cpp
JavaScriptsmurf emit … --lang javascript0wrote javascript (4674 bytes) to out.javascript
TypeScriptsmurf emit … --lang typescript0wrote typescript (5062 bytes) to out.typescript

The emitted source

emit.pypython
# 
# space Genesis — generated by `smurf emit --lang python`; do not edit by hand.
# The transitions, their parameters, their `require`s, their writes and their `ensure`s
# are the compiler's own AST, so this is the same space, not a translation of a comment.
# Not carried here: the event log, the content root and forks — those are the SMURF runtime.
# 

class Source:
    __slots__ = ("energy", "label")
    def __init__(self):
        self.energy = 100.0
        self.label = "source"

class Target:
    __slots__ = ("energy", "label")
    def __init__(self):
        self.energy = 0.0
        self.label = "target"

class Genesis:
    def __init__(self):
        self.source = Source()
        self.target = Target()


class Refuted(Exception):
    """A transition refused: the graph is left as it was."""

# Integer arithmetic refuses where the interpreter refuses: no wrap, no divide by zero.
def smurf_i_add(a, b):
    r = a + b
    if r < -9223372036854775808 or r > 9223372036854775807:
        raise Refuted('evaluation: i64 overflow')
    return r

def smurf_i_sub(a, b):
    r = a - b
    if r < -9223372036854775808 or r > 9223372036854775807:
        raise Refuted('evaluation: i64 overflow')
    return r

def smurf_i_mul(a, b):
    r = a * b
    if r < -9223372036854775808 or r > 9223372036854775807:
        raise Refuted('evaluation: i64 overflow')
    return r

def smurf_i_div(a, b):
    if b == 0:
        raise Refuted('evaluation: division by zero')
    q = abs(a) // abs(b)
    return -q if (a < 0) != (b < 0) else q

def smurf_i_rem(a, b):
    if b == 0:
        raise Refuted('evaluation: remainder by zero')
    return a - b * smurf_i_div(a, b)

def smurf_u_add(a, b):
    r = a + b
    if r > 18446744073709551615:
        raise Refuted('evaluation: u64 overflow')
    return r

def smurf_u_sub(a, b):
    if a < b:
        raise Refuted('evaluation: u64 underflow')
    return a - b

def smurf_u_mul(a, b):
    r = a * b
    if r > 18446744073709551615:
        raise Refuted('evaluation: u64 overflow')
    return r

def smurf_u_div(a, b):
    if b == 0:
        raise Refuted('evaluation: division by zero')
    return a // b

def smurf_u_rem(a, b):
    if b == 0:
        raise Refuted('evaluation: remainder by zero')
    return a % b
def canon_num(v):
    """Numbers print the way the SMURF runtime prints them: 90 is '90.0'."""
    if float(v).is_integer():
        return str(int(v)) + '.0'
    return str(float(v))
def transfer(space, amount):
    if not (amount >= 0.0):
        raise Refuted("require in `transfer` (line 18): amount >= 0.0")
    if not (space.source.energy >= amount):
        raise Refuted("require in `transfer` (line 18): source.energy >= amount")
    space.source.energy -= amount
    space.target.energy += amount
    if not ((space.source.energy + space.target.energy) == 100.0):
        raise Refuted("ensure in `transfer` (line 18): source.energy + target.energy == 100.0")



def dispatch(space, flag, args):
    if flag == "transfer":
        if len(args) <= 0:
            raise Refuted('`transfer` needs amount on the command line')
        amount = float(args[0])
        return transfer(space, amount)
    raise Refuted("no transition `%s` — this program has: transfer" % flag)

def main(argv):
    space = Genesis()
    i = 0
    while i < len(argv):
        flag = argv[i].lstrip('-')
        i += 1
        args = []
        while i < len(argv) and not argv[i].startswith('--'):
            args.append(argv[i])
            i += 1
        try:
            dispatch(space, flag, args)
        except Refuted as e:
            import sys
            print('refused: ' + str(e), file=sys.stderr)
            return 3
    print('source.energy = f64:' + canon_num(space.source.energy))
    print('source.label = str:' + space.source.label.replace('\\', '\\\\').replace('\t', '\\t').replace('\n', '\\n'))
    print('target.energy = f64:' + canon_num(space.target.energy))
    print('target.label = str:' + space.target.label.replace('\\', '\\\\').replace('\t', '\\t').replace('\n', '\\n'))
    return 0


if __name__ == '__main__':
    import sys
    sys.exit(main(sys.argv[1:]))

Recorded by tools/smurf-fixtures.sh from smurf (../smurf); re-run that script and compare. The recording is 18654 bytes, sha256 d2977e431fde7e2c84d0c8d05aa8d2d8acb1fcc1ef794320b995b05b39bebf15.