Пример #1
0
int main(int argc, char *argv[]) {
	int i;
	Table_T identifiers = Table_new(10000, textcmp, texthash);
	Text_save_T mark = Text_save();

	Fmt_register('T', Text_fmt);
	Fmt_register('D', Integer_fmt);
	first = Text_cat(Text_cat(Text_ucase, Text_lcase), Text_box("_", 1));
	rest  = Text_cat(first, Text_digits);
	for (i = 1; i < argc; i++) {
		FILE *fp = fopen(argv[i], "r");
		if (fp == NULL)
		fprintf(stderr, "%s: can't open '%s' (%s)\n", argv[0], argv[i], strerror(errno));
		else {
			cref(argv[i], fp, identifiers);
			fclose(fp);
		}
	}
	if (argc == 1)
		cref(NULL, stdin, identifiers);
	{
		int i;
		void **array = Table_toArray(identifiers, NULL);
		qsort(array, Table_length(identifiers), 2*sizeof (*array), compare);
		for (i = 0; array[i]; i += 2) {
			Fmt_print("%T", array[i]);
			print(array[i+1]);
			FREE(array[i]);
		}
		FREE(array);
		Table_free(&identifiers);
	}
	Text_restore(&mark);
	return EXIT_SUCCESS;
}
Пример #2
0
int main(int argc, char *argv[]) {
	int i;
	Table_T identifiers = Table_new(5000,
		(int (*)(const void *, const void *))strcmp, strhash);

	Fmt_register('S', Str_fmt);
	first = Str_catv("abcdefghijklmnopqrstuvwxyz", 1, 0,
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1, 0, "_", 1, 0, NULL);
	rest  = Str_cat(first, 1, 0, "0123456789", 1, 0);
	for (i = 1; i < argc; i++) {
		FILE *fp = fopen(argv[i], "r");
		if (fp == NULL)
			fprintf(stderr, "%s: can't open '%s' (%s)\n", argv[0], argv[i], strerror(errno));
		else {
			kref(argv[i], fp, identifiers);
			fclose(fp);
		}
	}
	if (argc == 1)
		kref(NULL, stdin, identifiers);
	{
		int i;
		void **array = Table_toArray(identifiers, NULL);
		qsort(array, Table_length(identifiers), 2*sizeof (*array), compare);
		for (i = 0; array[i]; i += 2) {
			Fmt_print("%S", array[i], 1, 0);
			print(array[i+1]);
			FREE(array[i]);
		}
		FREE(array);
		Table_free(&identifiers);
	}
	return EXIT_SUCCESS;
}
Пример #3
0
Text_T File_reader_reader(const char *pathname, const char *filter)
{
	int c, i, max = BUFFSIZE;
	char *buf = ALLOC(max);
	Text_T doc;
	FILE *in;
 
	// debug
	Fmt_register('T', Text_fmt);
	Fmt_fprint(stderr, "%s\n", pathname);
	Fmt_fprint(stderr, "BUFFSIZE: %d\n", BUFFSIZE);

	if ((in = fopen(pathname, "r")) == NULL)
		err(1, "%s", pathname);

	for (c = fgetc(in), i = 0; c != EOF; c = fgetc(in), i++) {
		if (i == max) { 		// max buf
			RESIZE(buf, max <<= 1);
		}

		buf[i] = c;
	}

	doc = Text_put(buf);

	Fmt_fprint(stderr, "%T\n", &doc);

	return doc;
}
Пример #4
0
int main(int argc, char* argv[]) {
	char line[512];
	static char set[] = "0123456789_"
		"abcdefghijklmnopqrstuvwxyz"
		"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	Fmt_register('S', Str_fmt);
	while (fgets(line, sizeof line, stdin) != NULL) {
		int i = 1, j;
		while ((i = Str_upto(line, i, 0, &set[10])) > 0) {
			j = Str_many(line, i, 0, set);
			Fmt_print("%S\n", line, i, j);
			i = j;
		}
	}
	return EXIT_SUCCESS;
}
Пример #5
0
int main(int argc, char *argv[]) {
	int c;
	sp = Stack_new();
	Fmt_register('D', AP_fmt);
	while ((c = getchar()) != EOF)
		switch (c) {
		case ' ': case '\t': case '\n': case '\f': case '\r':
			break;
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9': {
			char buf[512];
			{
				int i = 0;
				for ( ; c != EOF && isdigit(c); c = getchar(), i++)
					if (i < (int)sizeof (buf) - 1)
						buf[i] = c;
				if (i > (int)sizeof (buf) - 1) {
					i = (int)sizeof (buf) - 1;
					Fmt_fprint(stderr,
						"?integer constant exceeds %d digits\n", i);
				}
				buf[i] = 0;
				if (c != EOF)
					ungetc(c, stdin);
			}
			Stack_push(sp, AP_fromstr(buf, 10, NULL));
			break;
		}
		case '+': {
			AP_T y = pop(), x = pop();
			Stack_push(sp, AP_add(x, y));
			AP_free(&x);
			AP_free(&y);
			break;
		}
		case '-': {
			AP_T y = pop(), x = pop();
			Stack_push(sp, AP_sub(x, y));
			AP_free(&x);
			AP_free(&y);
			break;
		}
		case '*': {
			AP_T y = pop(), x = pop();
			Stack_push(sp, AP_mul(x, y));
			AP_free(&x);
			AP_free(&y);
			break;
		}
		case '/': {
			AP_T y = pop(), x = pop();
			if (AP_cmpi(y, 0) == 0) {
				Fmt_fprint(stderr, "?/ by 0\n");
				Stack_push(sp, AP_new(0));
			} else
				Stack_push(sp, AP_div(x, y));
			AP_free(&x);
			AP_free(&y);
			break;
		}
		case '%': {
			AP_T y = pop(), x = pop();
			if (AP_cmpi(y, 0) == 0) {
				Fmt_fprint(stderr, "?%% by 0\n");
				Stack_push(sp, AP_new(0));
			} else
				Stack_push(sp, AP_mod(x, y));
			AP_free(&x);
			AP_free(&y);
			break;
		}
		case '^': {
			AP_T y = pop(), x = pop();
			if (AP_cmpi(y, 0) <= 0) {
				Fmt_fprint(stderr, "?nonpositive power\n");
				Stack_push(sp, AP_new(0));
			} else
				Stack_push(sp, AP_pow(x, y, NULL));
			AP_free(&x);
			AP_free(&y);
			break;
		}
		case 'd': {
			AP_T x = pop();
			Stack_push(sp, x);
			Stack_push(sp, AP_addi(x, 0));
			break;
		}
		case 'p': {
			AP_T x = pop();
			Fmt_print("%D\n", x);
			Stack_push(sp, x);
			break;
		}
		case 'f':
			if (!Stack_empty(sp)) {
				Stack_T tmp = Stack_new();
				while (!Stack_empty(sp)) {
					AP_T x = pop();
					Fmt_print("%D\n", x);
					Stack_push(tmp, x);
				}
				while (!Stack_empty(tmp))
					Stack_push(sp, Stack_pop(tmp));
				Stack_free(&tmp);
			}
			break;
		case '~': {
			AP_T x = pop();
			Stack_push(sp, AP_neg(x));
			AP_free(&x);
			break;
		}
		case 'c': while (!Stack_empty(sp)) {
			  	AP_T x = Stack_pop(sp);
			  	AP_free(&x);
			  } break;
		case 'q': while (!Stack_empty(sp)) {
			  	AP_T x = Stack_pop(sp);
			  	AP_free(&x);
			  }
			  Stack_free(&sp);
			  return EXIT_SUCCESS;
		default:
			if (isprint(c))
				Fmt_fprint(stderr, "?'%c'", c);
			else
				Fmt_fprint(stderr, "?'\\%03o'", c);
			Fmt_fprint(stderr, " is unimplemented\n");
			break;
		}
	while (!Stack_empty(sp)) {
		AP_T x = Stack_pop(sp);
		AP_free(&x);
	}
	Stack_free(&sp);
	return EXIT_SUCCESS;
}
Пример #6
0
int main(int argc, char *argv[]) {
	int c;
	sp = Seq_new(0);
	Fmt_register('D', MP_fmt);
	Fmt_register('U', MP_fmtu);
	while ((c = getchar()) != EOF) {
		MP_T x = NULL, y = NULL, z = NULL;
		TRY
 			switch (c) {
				default:
					if (isprint(c))
						Fmt_fprint(stderr, "?'%c'", c);
					else
						Fmt_fprint(stderr, "?'\\%03o'", c);
					Fmt_fprint(stderr, " is unimplemented\n");
					break;
				case ' ': case '\t': case '\n': case '\f': case '\r':
					break;
				case 'c': while (Seq_length(sp) > 0) {
					  	MP_T x = Seq_remhi(sp);
					  	FREE(x);
					  } break;
				case 'q': while (Seq_length(sp) > 0) {
					  	MP_T x = Seq_remhi(sp);
					  	FREE(x);
					  }
					  Seq_free(&sp);
					  return EXIT_SUCCESS;
				case '0': case '1': case '2': case '3': case '4':
				case '5': case '6': case '7': case '8': case '9': {
					char buf[512];
					z = MP_new(0);
					{
						int i = 0;
						for ( ; strchr(&"zyxwvutsrqponmlkjihgfedcba9876543210"[36-ibase], tolower(c)); c = getchar(), i++)
							if (i < (int)sizeof (buf) - 1)
								buf[i] = c;
						if (i > (int)sizeof (buf) - 1) {
							i = (int)sizeof (buf) - 1;
							Fmt_fprint(stderr, "?integer constant exceeds %d digits\n", i);
						}
						buf[i] = '\0';
						if (c != EOF)
							ungetc(c, stdin);
					}
					MP_fromstr(z, buf, ibase, NULL);
					break;
				}
				case '+': y = pop(); x = pop();
					  z = MP_new(0); (*f->add)(z, x, y); break;
				case '-': y = pop(); x = pop();
					  z = MP_new(0); (*f->sub)(z, x, y); break;
				case '*': y = pop(); x = pop();
					  z = MP_new(0); (*f->mul)(z, x, y); break;
				case '/': y = pop(); x = pop();
					  z = MP_new(0); (*f->div)(z, x, y); break;
				case '%': y = pop(); x = pop();
					  z = MP_new(0); (*f->mod)(z, x, y); break;
				case '&': y = pop(); x = pop();
					  z = MP_new(0);    MP_and(z, x, y); break;
				case '|': y = pop(); x = pop();
					  z = MP_new(0);    MP_or (z, x, y); break;
				case '^': y = pop(); x = pop();
					  z = MP_new(0);    MP_xor(z, x, y); break;
				case '!': z = pop(); MP_not(z, z); break;
				case '~': z = pop(); MP_neg(z, z); break;
				case 'i': case 'o': {
					long n;
					x = pop();
					n = MP_toint(x);
					if (n < 2 || n > 36)
						Fmt_fprint(stderr, "?%d is an illegal base\n",n);
					else if (c == 'i')
						ibase = n;
					else
						obase = n;
					if (obase == 2 || obase == 8 || obase == 16)
						f = &u;
					else
						f = &s;
					break;
				}
				case 'p':
					Fmt_print(f->fmt, z = pop(), obase);
					break;
				case 'f': {
					int n = Seq_length(sp);
					while (--n > 0)
						Fmt_print(f->fmt, Seq_get(sp, n), obase);
					break;
				}
				case '<': { long s;
					    y = pop();
					    z = pop();
					    s = MP_toint(y);
					    if (s < 0 || s > INT_MAX) {
					    	Fmt_fprint(stderr,
					    		"?%d is an illegal shift amount\n", s);
					    	break;
					    }; MP_lshift(z, z, s); break; }
				case '>': { long s;
					    y = pop();
					    z = pop();
					    s = MP_toint(y);
					    if (s < 0 || s > INT_MAX) {
					    	Fmt_fprint(stderr,
					    		"?%d is an illegal shift amount\n", s);
					    	break;
					    }; MP_rshift(z, z, s); break; }
				case 'k': {
					long n;
					x = pop();
					n = MP_toint(x);
					if (n < 2 || n > INT_MAX)
						Fmt_fprint(stderr,
							"?%d is an illegal precision\n", n);
					else if (Seq_length(sp) > 0)
						Fmt_fprint(stderr, "?nonempty stack\n");
					else
						MP_set(n);
					break;
				}
				case 'd': {
					MP_T x = pop();
					z = MP_new(0);
					Seq_addhi(sp, x);
					MP_addui(z, x, 0);
					break;
				}
			}
		EXCEPT(MP_Overflow)
			Fmt_fprint(stderr, "?overflow\n");
		EXCEPT(MP_Dividebyzero)
			Fmt_fprint(stderr, "?divide by 0\n");
		END_TRY;
		if (z)
			Seq_addhi(sp, z);
		FREE(x);
		FREE(y);
	}
	while (Seq_length(sp) > 0) {
		MP_T x = Seq_remhi(sp);
		FREE(x);
	}
	Seq_free(&sp);
	return EXIT_SUCCESS;
}