~rabbits/firth

6dbd04b17cdf16b1fa6de9abe00eda7adab1ea7a — neauoire 2 months ago aa6614e
Added bitwise
2 files changed, 54 insertions(+), 13 deletions(-)

M 1th.c
M README.md
M 1th.c => 1th.c +41 -12
@@ 11,7 11,7 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

#define SZ 4
#define SZ 256

typedef struct Fraction {
	int num, den;


@@ 55,10 55,10 @@ sinu(char *s)
int
snum(char *s)
{
	int i = 0, num = 0;
	int i = 0, n = 0;
	while(s[i] && cinu(s[i]))
		num = num * 10 + (s[i++] - '0');
	return num;
		n = n * 10 + (s[i++] - '0');
	return n;
}

int


@@ 79,6 79,16 @@ gcd(int a, int b)
	return gcd(b, a % b);
}

void
pbin(int n, unsigned int l)
{
	while(l) {
		putchar(n & l ? '1' : '0');
		l >>= 1;
	}
	putchar(' ');
}

/* rpn */

int


@@ 145,14 155,9 @@ print(Stack *s)
			printf("%d&%d/%d ", f->num / f->den, f->num % f->den, f->den);
		else if(MODE == HEXADECIMAL)
			printf("%08hX ", f->num);
		else if(MODE == BINARY) {
			unsigned int max = 128;
			while(max) {
				putchar(f->num & max ? '1' : '0');
				max >>= 1;
			}
			putchar(' ');
		} else if(f->den != 1)
		else if(MODE == BINARY)
			pbin(f->num, 1 << 7);
		else if(f->den != 1)
			printf("%d/%d ", f->num, f->den);
		else
			printf("%d ", f->num);


@@ 179,6 184,7 @@ interpret(Stack *s, char *word)
		MODE = BINARY;
	else if(sinu(word) && trypush(s, 1))
		push(s, Frac(snum(word), 1));
	/* stack */
	else if(scmp(word, "pop") && trypop(s, 1))
		pop(s);
	else if(scmp(word, "clr"))


@@ 201,6 207,7 @@ interpret(Stack *s, char *word)
		push(s, Frac(b.num, b.den));
		push(s, Frac(c.num, c.den));
		push(s, Frac(a.num, a.den));
		/* arithmetic */
	} else if((word[0] == '+' || scmp(word, "add")) && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);


@@ 221,6 228,28 @@ interpret(Stack *s, char *word)
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac((a.num * b.den) % (a.den * b.num), a.den * b.den));
		/* bitwise */
	} else if((word[0] == '&' || scmp(word, "and")) && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac((a.num * b.den) & (a.den * b.num), a.den * b.den));
	} else if((word[0] == '|' || scmp(word, "or")) && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac((a.num * b.den) | (a.den * b.num), a.den * b.den));
	} else if((word[0] == '^' || scmp(word, "xor")) && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac((a.num * b.den) ^ (a.den * b.num), a.den * b.den));
	} else if((scmp(word, "<<") || scmp(word, "rol")) && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac((a.num * b.den) << (a.den * b.num), a.den * b.den));
	} else if((scmp(word, ">>") || scmp(word, "ror")) && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac((a.num * b.den) >> (a.den * b.num), a.den * b.den));
		/* special */
	} else if((scmp(word, "inv")) && trypop(s, 1)) {
		Fraction f = pop(s);
		push(s, Frac(f.den, f.num));

M README.md => README.md +13 -1
@@ 14,7 14,8 @@ The supported operations are as follow:

### Print

- `.`(fraction)
- `.` Print fraction
- `std` Standard(default)
- `mix` Mixed
- `dec` Decimal
- `hex` Hexadecimal


@@ 36,6 37,17 @@ The supported operations are as follow:
- `mul`, or `*`
- `div`, or `/`
- `mod`, or `%`

## Bitwise

- `and`, or `&`
- `or `, or `|`
- `xor`, or `^`
- `rol`, or `<<`
- `ror`, or `>>`

### Special

- `inv` Invert numerator and denominator 
- `vid` Convert fraction to two whole numbers