7. Operations
7.1. Num
Creates a 7-bit number in a single instruction.
<<funcdefs>>=
int bitlang_num(bitlang_state *st, int num);
<<funcs>>=
int bitlang_num(bitlang_state *st, int num)
{
num &= 0x7f;
if (st->len >= st->sz) return 1;
st->bytes[st->len] = 0x80 | num;
st->len++;
return 0;
}
7.2. Add
<<opcodes>>=
BITLANG_ADD,
<<funcdefs>>=
int bitlang_add(bitlang_state *st);
<<funcs>>=
int bitlang_add(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_ADD;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_ADD: {
int x, y;
rc = bitlang_pop(vm, &y);
if (rc) return rc;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, x + y);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "+", 1)) {
return bitlang_add(st);
}
7.3. Sub
<<opcodes>>=
BITLANG_SUB,
<<funcdefs>>=
int bitlang_sub(bitlang_state *st);
<<funcs>>=
int bitlang_sub(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_SUB;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_SUB: {
int x, y;
rc = bitlang_pop(vm, &y);
if (rc) return rc;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, x - y);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "-", 1)) {
return bitlang_sub(st);
}
7.4. Mul
<<opcodes>>=
BITLANG_MUL,
<<funcdefs>>=
int bitlang_mul(bitlang_state *st);
<<funcs>>=
int bitlang_mul(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_MUL;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_MUL: {
int x, y;
rc = bitlang_pop(vm, &y);
if (rc) return rc;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, x * y);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "*", 1)) {
return bitlang_mul(st);
}
7.5. Div
<<opcodes>>=
BITLANG_DIV,
<<funcdefs>>=
int bitlang_div(bitlang_state *st);
<<funcs>>=
int bitlang_div(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_DIV;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_DIV: {
int x, y;
rc = bitlang_pop(vm, &y);
if (rc) return rc;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
if (y == 0) return 1;
rc = bitlang_push(vm, x / y);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "/", 1)) {
return bitlang_div(st);
}
7.6. Get
Gets a value from a register and pushes it onto the stack.
<<opcodes>>=
BITLANG_GET,
<<funcdefs>>=
int bitlang_get(bitlang_state *st);
<<funcs>>=
int bitlang_get(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_GET;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_GET: {
int rp;
rc = bitlang_pop(vm, &rp);
if (rc) return rc;
if (rp < 0 || rp >= 8) return 1;
rc = bitlang_push(vm, vm->reg[rp]);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "get", 3)) {
return bitlang_get(st);
}
7.7. X, Y, W, H, T
These are all links to getters 0-4.
<<search>>=
else if (match(str, len, "x", 1)) {
int rc;
rc = bitlang_num(st, 0);
if (rc) return rc;
return bitlang_get(st);
}
<<search>>=
else if (match(str, len, "y", 1)) {
int rc;
rc = bitlang_num(st, 1);
if (rc) return rc;
return bitlang_get(st);
}
<<search>>=
else if (match(str, len, "w", 1)) {
int rc;
rc = bitlang_num(st, 2);
if (rc) return rc;
return bitlang_get(st);
}
<<search>>=
else if (match(str, len, "h", 1)) {
int rc;
rc = bitlang_num(st, 3);
if (rc) return rc;
return bitlang_get(st);
}
<<search>>=
else if (match(str, len, "t", 1)) {
int rc;
rc = bitlang_num(st, 4);
if (rc) return rc;
return bitlang_get(st);
}
7.8. Mod
<<opcodes>>=
BITLANG_MOD,
<<funcdefs>>=
int bitlang_mod(bitlang_state *st);
<<funcs>>=
int bitlang_mod(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_MOD;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_MOD: {
int x, y;
rc = bitlang_pop(vm, &y);
if (rc) return rc;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
if (y == 0) rc = bitlang_push(vm, 0);
else rc = bitlang_push(vm, x % y);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "%", 1)) {
return bitlang_mod(st);
}
7.9. Equ
<<opcodes>>=
BITLANG_EQ,
<<funcdefs>>=
int bitlang_eq(bitlang_state *st);
<<funcs>>=
int bitlang_eq(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_EQ;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_EQ: {
int x, y;
rc = bitlang_pop(vm, &y);
if (rc) return rc;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, x == y);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "=", 1)) {
return bitlang_eq(st);
}
7.10. LShift
<<opcodes>>=
BITLANG_LSHIFT,
<<funcdefs>>=
int bitlang_lshift(bitlang_state *st);
<<funcs>>=
int bitlang_lshift(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_LSHIFT;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_LSHIFT: {
int x, y;
rc = bitlang_pop(vm, &y);
if (rc) return rc;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, x << y);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "<<", 2)) {
return bitlang_lshift(st);
}
7.11. RShift
<<opcodes>>=
BITLANG_RSHIFT,
<<funcdefs>>=
int bitlang_rshift(bitlang_state *st);
<<funcs>>=
int bitlang_rshift(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_RSHIFT;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_RSHIFT: {
int x, y;
rc = bitlang_pop(vm, &y);
if (rc) return rc;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, x >> y);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, ">>", 2)) {
return bitlang_rshift(st);
}
7.12. Logical OR
<<opcodes>>=
BITLANG_LOR,
<<funcdefs>>=
int bitlang_lor(bitlang_state *st);
<<funcs>>=
int bitlang_lor(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_LOR;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_LOR: {
int x, y;
rc = bitlang_pop(vm, &y);
if (rc) return rc;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, x || y);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "||", 2)) {
return bitlang_lor(st);
}
7.13. Bitwise OR
<<opcodes>>=
BITLANG_BOR,
<<funcdefs>>=
int bitlang_bor(bitlang_state *st);
<<funcs>>=
int bitlang_bor(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_BOR;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_BOR: {
int x, y;
rc = bitlang_pop(vm, &y);
if (rc) return rc;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, x | y);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "|", 1)) {
return bitlang_bor(st);
}
7.14. Bitwise AND
<<opcodes>>=
BITLANG_BAND,
<<funcdefs>>=
int bitlang_band(bitlang_state *st);
<<funcs>>=
int bitlang_band(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_BAND;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_BAND: {
int x, y;
rc = bitlang_pop(vm, &y);
if (rc) return rc;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, x & y);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "&", 1)) {
return bitlang_band(st);
}
7.15. XOR
<<opcodes>>=
BITLANG_XOR,
<<funcdefs>>=
int bitlang_xor(bitlang_state *st);
<<funcs>>=
int bitlang_xor(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_XOR;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_XOR: {
int x, y;
rc = bitlang_pop(vm, &y);
if (rc) return rc;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, x ^ y);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "^", 1)) {
return bitlang_xor(st);
}
7.16. Bitwise NOT
<<opcodes>>=
BITLANG_BNOT,
<<funcdefs>>=
int bitlang_bnot(bitlang_state *st);
<<funcs>>=
int bitlang_bnot(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_BNOT;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_BNOT: {
int x;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, ~x);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "~", 1)) {
return bitlang_bnot(st);
}
7.17. Logical NOT
<<opcodes>>=
BITLANG_LNOT,
<<funcdefs>>=
int bitlang_lnot(bitlang_state *st);
<<funcs>>=
int bitlang_lnot(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_LNOT;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_LNOT: {
int x;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, !x);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "!", 1)) {
return bitlang_lnot(st);
}
7.18. Abs
<<opcodes>>=
BITLANG_ABS,
<<funcdefs>>=
int bitlang_abs(bitlang_state *st);
<<funcs>>=
int bitlang_abs(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_ABS;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_ABS: {
int x;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, abs(x));
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "abs", 3)) {
return bitlang_abs(st);
}
7.19. Neg
<<opcodes>>=
BITLANG_NEG,
<<funcdefs>>=
int bitlang_neg(bitlang_state *st);
<<funcs>>=
int bitlang_neg(bitlang_state *st)
{
if (st->len >= st->sz) return 1;
st->bytes[st->len] = BITLANG_NEG;
st->len++;
return 0;
}
<<ops>>=
case BITLANG_NEG: {
int x;
rc = bitlang_pop(vm, &x);
if (rc) return rc;
rc = bitlang_push(vm, -x);
if (rc) return rc;
pos++;
break;
}
<<search>>=
else if (match(str, len, "neg", 3)) {
return bitlang_neg(st);
}
prev | home | next