XXIIVV

Firth, or 1th, is a postfix calculator for fractions.

Firth is a stack based rpn calculator, created to be an improved version of Unix's dc, written in ANSI C. It supports most basic math operations, variables, and basic branching.

Examples

The following example, will print the mixed fraction 3&1/6.

clr 11 4 div 5 12 div add mix

To print the decimal value of a fraction.

clr 22 7 div 1 791 div sub dec

To get the min() and max() of two numbers.

clr 4 8 < pop end .
clr 4 8 < swp pop end .

To get the floor() of a fraction.

clr 17 4 div dup 1 mod sub .

1th.c

#include <stdio.h>

/* 
Copyright (c) 2020 Devine Lu Linvega

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/

typedef struct Fraction {
	int num, den;
} Fraction;

typedef struct Stack {
	int flag, len;
	Fraction data[256];
} Stack;

typedef struct Query {
	int len;
	char data[64];
} Query;

/* helpers */

int
cinu(char c)
{
	return c >= '0' && c <= '9';
}

int
sinu(char *s)
{
	int i = 0;
	if(!s[0])
		return 0;
	while(s[i])
		if(!cinu(s[i++]))
			return 0;
	return 1;
}

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

int
scmp(char *a, char *b)
{
	int i = 0;
	while(a[i] == b[i])
		if(!a[i++])
			return 1;
	return 0;
}

int
gcd(int a, int b)
{
	if(b == 0)
		return a;
	return gcd(b, a % b);
}

void
init(Query *s)
{
	s->data[0] = '\0';
	s->len = 0;
}

void
append(Query *s, char c)
{
	if(s->len < 64)
		s->data[s->len++] = c;
	s->data[s->len] = '\0';
}

/* rpn */

int
error(char *err, char *msg)
{
	printf("%s Error: %s\n", err, msg);
	return 0;
}

int
trypop(Stack *s, int len)
{
	int err = s->len - len < 0;
	if(err)
		error("Stack", "underflow");
	return !err;
}

int
trypush(Stack *s, int len)
{
	int err = s->len + len >= 256;
	if(err)
		error("Stack", "overflow");
	return !err;
}

int
tryget(Stack *s, int offset)
{
	int err = s->len - offset < 0;
	if(err)
		error("Stack", "overflow");
	return !err;
}

Fraction
pop(Stack *s)
{
	return s->data[--s->len];
}

Fraction *
push(Stack *s, Fraction f)
{
	s->data[s->len] = f;
	return &s->data[s->len++];
}

Fraction *
get(Stack *s, int offset)
{
	return &s->data[s->len - offset - 1];
}

Fraction
Frac(int num, int den)
{
	Fraction f;
	int d = gcd(num, den);
	f.num = num / d;
	f.den = den / d;
	return f;
}

void
print(Stack *s, int format)
{
	int i;
	if(s->len < 1)
		return;
	for(i = 0; i < s->len; ++i) {
		Fraction *f = &s->data[i];
		if(format == 1)
			printf("%f ", f->num / (double)f->den);
		else if(format == 2 && f->num > f->den)
			printf("%d&%d/%d ", f->num / f->den, f->num % f->den, f->den);
		else if(f->den != 1)
			printf("%d/%d ", f->num, f->den);
		else
			printf("%d ", f->num);
	}
	printf("\n");
}

/* ops */

int
request(Stack *s, char *qd)
{
	if((qd[0] == '(' || scmp(qd, "esc")))
		s->flag += 2;
	else if((qd[0] == ')' || scmp(qd, "end")))
		s->flag -= s->flag > 0 ? 2 : 0;
	else if(s->flag)
		return 1;
	else if(!qd[0] || qd[0] == ' ' || qd[0] == '.')
		print(s, 0);
	else if(qd[0] == ',' || scmp(qd, "dec"))
		print(s, 1);
	else if(qd[0] == '_' || scmp(qd, "mix"))
		print(s, 2);
	/* stack */
	else if(sinu(qd) && trypush(s, 1))
		push(s, Frac(sint(qd), 1));
	else if(scmp(qd, "pop") && trypop(s, 1))
		pop(s);
	else if(scmp(qd, "clr"))
		s->len = 0;
	else if(scmp(qd, "swp") && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac(b.num, b.den));
		push(s, Frac(a.num, a.den));
	} else if(scmp(qd, "dup") && trypop(s, 1) && trypush(s, 1)) {
		Fraction *f = get(s, 0);
		push(s, Frac(f->num, f->den));
	} else if(scmp(qd, "ovr") && trypop(s, 2) && trypush(s, 1)) {
		Fraction *f = get(s, 1);
		push(s, Frac(f->num, f->den));
	} else if(scmp(qd, "rot") && trypop(s, 3)) {
		Fraction c = pop(s);
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac(b.num, b.den));
		push(s, Frac(c.num, c.den));
		push(s, Frac(a.num, a.den));
		/* arithmetic */
	} else if((qd[0] == '+' || scmp(qd, "add")) && 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((qd[0] == '-' || scmp(qd, "sub")) && 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((qd[0] == '*' || scmp(qd, "mul")) && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac(a.num * b.num, a.den * b.den));
	} else if((qd[0] == '/' || scmp(qd, "div")) && trypop(s, 2)) {
		Fraction b = pop(s);
		Fraction a = pop(s);
		push(s, Frac(a.num * b.den, a.den * b.num));
	} else if((qd[0] == '%' || scmp(qd, "mod")) && 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(qd, "inv")) && trypop(s, 1)) {
		Fraction f = pop(s);
		push(s, Frac(f.den, f.num));
	} else if(scmp(qd, "vid") && trypop(s, 1) && trypush(s, 1)) {
		Fraction f = pop(s);
		push(s, Frac(f.num, 1));
		push(s, Frac(f.den, 1));
		/* Logic */
	} else if((qd[0] == '=' || scmp(qd, "beq")) && tryget(s, 2)) {
		Fraction *b = get(s, 0);
		Fraction *a = get(s, 1);
		s->flag += (b->den * a->num != a->den * b->num) * 2;
	} else if((qd[0] == '!' || scmp(qd, "bne")) && tryget(s, 2)) {
		Fraction *b = get(s, 0);
		Fraction *a = get(s, 1);
		s->flag += (b->den * a->num == a->den * b->num) * 2;
	} else if((qd[0] == '>' || scmp(qd, "bgt")) && tryget(s, 2)) {
		Fraction *b = get(s, 0);
		Fraction *a = get(s, 1);
		s->flag += (b->den * a->num < a->den * b->num) * 2;
	} else if((qd[0] == '<' || scmp(qd, "blt")) && tryget(s, 2)) {
		Fraction *b = get(s, 0);
		Fraction *a = get(s, 1);
		s->flag += (b->den * a->num > a->den * b->num) * 2;
	} else if(qd)
		return error("Command", qd);
	return 1;
}

int
run(Stack *s)
{
	char c;
	Query q;
	init(&q);
	while((c = fgetc(stdin)) != EOF) {
		if(c == ' ' || c == '\n' || !c) {
			request(s, q.data);
			init(&q);
		} else
			append(&q, c);
	}
	return 0;
}

int
main()
{
	Stack s;
	s.len = 0;
	s.flag = 0;
	while(run(&s))
		;
	print(&s, 0);
	return 0;
}
— Submit an edit to 1th.c.txt(293 lines)

incoming(3): computer forth reverse polish

Last update on 20V07, edited 5 times. +18/21fh-----+