Skip to content

Playground skeleton #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tmptrash opened this issue May 1, 2022 · 0 comments
Open

Playground skeleton #18

tmptrash opened this issue May 1, 2022 · 0 comments
Assignees
Labels
dev Good for newcomers

Comments

@tmptrash
Copy link
Owner

tmptrash commented May 1, 2022

Should work like a container for different applications like:

  • Atoms and molecules editor by mouse and some menu with settings. Should support atom type, bonds, position, atom selection
enum Dirs {
    NO         = -1,
    LEFT_UP    = 0,
    UP         = 1,
    UP_RIGHT   = 2,
    RIGHT      = 3,
    RIGHT_DOWN = 4,
    DOWN       = 5,
    DOWN_LEFT  = 6,
    LEFT       = 7
}
const TYPE_MOV = 0b0010000000000000;
const TYPE_SPL = 0b0110000000000000;
const TYPE_FIX = 0b0100000000000000;
const TYPE_IF  = 0b1000000000000000;
const TYPE_JOB = 0b1010000000000000;

const log = console.log.bind(console);

function hex(atom: number) { return '0b' + atom.toString(2).padStart(16, '0').match(/.{1,4}/g)?.join('_') }
/**
 * Returns standard header (type + vm dir + vm dir bit). This header exists
 * for all atom types. Here are related bits: 0 atom type 3 vmDir 6 vmDir on/off
 *                                            XXX         XXX     X
 * @param  atomType Type of the atom (0 - empty, 1 - mov, 2 - fix,...)
 * @param  vmDir    Direction of near atom, where VM will be moved (0..7)
 * @return 16bit number
 */
function head(atomType: number, vmDir: Dirs = Dirs.NO): number {
    let atom = atomType;                         // atom type
    if (vmDir >= Dirs.LEFT_UP && vmDir <= Dirs.LEFT) {
        atom |= (vmDir << 10);                   // VM dir
        atom |= (1 << 9);                        // VM dir bit
    } else {
        atom &= 0b1111110111111111;              // no VM dir bit
    }
    return atom;
}
/**
 * Returns mov atom as binary u16 number (Rust format)
 */
function mov(movDir: Dirs, vmDir: Dirs = Dirs.NO): string {
    let atom = head(TYPE_MOV, vmDir);            // mov type
    if (movDir < Dirs.LEFT_UP || movDir > Dirs.LEFT) { throw 'Invalid "mov" atom direction' }
    atom |= (movDir << 6);                       // move dir
    return hex(atom);
}
/**
 * Returns fix atom as binary u16 number (Rust format)
 */
function fix(dir1: Dirs, dir2: Dirs, vmDir: Dirs = Dirs.NO): string {
    let atom = head(TYPE_FIX, vmDir);            // fix type
    if (dir1 < Dirs.LEFT_UP || dir1 > Dirs.LEFT) { throw 'Invalid "fix" direction 1' }
    atom |= (dir1 << 6);                         // dir1
    if (dir2 < Dirs.LEFT_UP || dir2 > Dirs.LEFT) { throw 'Invalid "fix" direction 2' }
    atom |= (dir2 << 3);                         // dir2
    return hex(atom);
}
/**
 * Returns spl atom as binary u16 number (Rust format)
 */
function spl(vmDir: Dirs = Dirs.NO, dir1: Dirs = Dirs.LEFT_UP, dir2: Dirs = Dirs.LEFT_UP): string {
    let atom = head(TYPE_SPL, vmDir);            // spl type
    if (dir1 < Dirs.LEFT_UP || dir1 > Dirs.LEFT) { throw 'Invalid "spl" direction 1' }
    atom |= (dir1 << 6);                         // dir1
    if (dir2 < Dirs.LEFT_UP || dir2 > Dirs.LEFT) { throw 'Invalid "spl" direction 2' }
    atom |= (dir2 << 3);                         // dir2
    return hex(atom);
}
/**
 * Returns if atom as binary u16 number (Rust format)
 */
function ifo(vmElseDir: Dirs = Dirs.NO, ifDir: Dirs = Dirs.NO, thenDir: Dirs = Dirs.NO): string {
    let atom = head(TYPE_IF, vmElseDir);         // if type
    if (ifDir >= Dirs.LEFT_UP && ifDir <= Dirs.LEFT) {
        atom |= (ifDir << 6);                    // ifDir
        atom |= (1 << 2);                        // ifDir bit
    } else {
        atom &= 0b1111111111111011;              // ifDir bit
    }
    if (thenDir >= Dirs.LEFT_UP && thenDir <= Dirs.LEFT) {
        atom |= (thenDir << 3);                  // thenDir
        atom |= (1 << 1);                        // thenDir bit
    } else {
        atom &= 0b1111111111111101;              // thenDir bit
    }
    return hex(atom);
}
/**
 * Returns job atom as binary u16 number (Rust format)
 */
function job(jobDir: Dirs = Dirs.NO, vmDir: Dirs = Dirs.NO): string {
    let atom = head(TYPE_JOB, vmDir);            // job type
    if (jobDir < Dirs.LEFT_UP || jobDir > Dirs.LEFT) { throw 'Invalid "job" direction' }
    atom |= (jobDir << 6);
    return hex(atom);
}

log(mov(Dirs.RIGHT));
  • Debugger
  • Save/Load from JSON
@tmptrash tmptrash added the dev Good for newcomers label May 1, 2022
@tmptrash tmptrash added this to the Atomic Playground milestone May 1, 2022
@tmptrash tmptrash self-assigned this May 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant