Пример #1
0
int main(int argc, char *argv[])
{
	int ecode;

	bfcc_options bfopts = {0};
	if (parse_arguments(argc, argv, &bfopts) != 0)
	{
		return -2;
	}

	FILE *f = stdin;
	if (bfopts.input_file != 0)
	{
		f = fopen(bfopts.input_file, "r");
		if (!f)
		{
			fprintf(stderr, "Unknown file.\n");
			return ERROR_FILE_NOT_FOUND;
		}
	}

	c99_options opts;
	c99_options_default(&opts);
	
	backend back = create_c99_backend(&opts);

	ecode = back.begin(&back, stdout);
	FATAL_IF_ERROR(ecode, "Backend preamble generation");

	tokeniser *t = tokeniser_setup(f);
	CHECK_ALLOCATION(t, "Tokeniser setup");

	while (1)
	{
		token tok;
		int error = tokeniser_next(t, &tok);

		if (IS_ERROR(error))
		{
			fprintf(stderr,  "Tokenisation error detected: %d.\n", error);
			return ERROR_TOKENISATION;
		}
		if (tok == token_eof)
			break;
		
		if (IS_ERROR(back.emit(&back, stdout, (token) tok)))
		{
			fprintf(stderr, "Failure encountered when translating token: %s\n", token_name((token) tok));
		}
	}

	ecode = back.end(&back, stdout);
	FATAL_IF_ERROR(ecode, "Backend could not finish")
	return 0;
}
Пример #2
0
int runline(char* line, int* pc) {
	tokeniser_setup(line);
	token = tokeniser_next();
	switch (token) {
		case TOK_PRINT:// STRING|EXPR (, STRING|EXPR)
			do {
				token = tokeniser_next();
				if (token == TOK_EOL || token == TOK_ERR) {
					break;
				}
				if (token == TOK_STRING) {
					printf("%s ", tokeniser_string());
					token = tokeniser_next();
				} else {
					printf("%d ", parseexpression());
				}
				if (token != TOK_COMMA && token != TOK_EOL) {
					printf("expected ','\n");
					return 0;
				}
			}
			while (token != TOK_EOL && token != TOK_ERR);
			printf("\n");
			break;
		case TOK_LET:// VAR = EXPR
			token = tokeniser_next();
			if (token != TOK_VAR) {
				printf("expected variable name\n");
				return 0;
			}
			char* varName = tokeniser_string();
			token = tokeniser_next();
			if (token != TOK_EQ) {
				printf("expected = after variable name\n");
				return 0;
			}
			token = tokeniser_next();
			int val = parseexpression();
			if (token == TOK_ERR) {
				return 0;
			}
			setvariable(varName, token);
			break;
	}
	(*pc)++;
	return 1;
}