summaryrefslogblamecommitdiff
path: root/src/user/app/kbasic/commands.c
blob: 3e478964bca82fb2bce4908ac838090b3600b0cc (plain) (tree)
1
2
3
4
5
6
7
8
9





                   


                                      




























                                            




                                                           
                            





















































                                                                              
                                          




                                                        
















                                                                                                           
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "basic.h"

static int fg_color = 7, bg_color = 0;


/* Assign a variable a value. */
void assignment()
{
	int *var;
	int value;

	/* get the variable name */
	get_token();
	if(!isalpha(*token)) {
		serror(4);
		return;
	}

	var = find_var(token);

	/* get the equals sign */
	get_token();
	if(*token!='=') {
		serror(3);
		return;
	}

	/* get the value to assign to var */
	get_exp(&value);

	/* assign the value */
	*var = value;
}

/* Execute a simple version of the BASIC PRINT statement */
void exec_print()
{
	int answer;
	int len=0, spaces;
	char last_delim = 0;

	do {
		get_token(); /* get next list item */
		if(tok==EOL || tok==FINISHED) break;
		if(token_type==QUOTE) { /* is string */
			printf("%s", token);
			len += strlen(token);
			get_token();
		}
		else { /* is expression */
			putback();
			get_exp(&answer);
			get_token();
			len += printf("%d", answer);
		}
		last_delim = *token; 

		if(*token==';') {
			/* compute number of spaces to move to next tab */
			spaces = 8 - (len % 8); 
			len += spaces; /* add in the tabbing position */
			while(spaces) { 
				printf(" ");
				spaces--;
			}
		}
		else if(*token==',') /* do nothing */;
		else if(tok!=EOL && tok!=FINISHED) serror(0); 
	} while (*token==';' || *token==',');

	if(tok==EOL || tok==FINISHED) {
		if(last_delim != ';' && last_delim!=',') printf("\n");
	}
	else serror(0); /* error is not , or ; */

}

/* Execute a simple form of the BASIC INPUT command */
void input()
{
	int *var;
	int tmp;

	get_token(); /* see if prompt string is present */
	if(token_type==QUOTE) {
		printf("%s", token); /* if so, print it and check for comma */
		get_token();
		if(*token!=',') serror(1);
		get_token();
	}
	else printf("? "); /* otherwise, prompt with / */

	var = find_var(token); /* get the input var */

	scanf("%d", var); /* read input */

	/* flush input */
	while ((tmp = getchar()) != '\n' && tmp != EOF);
}


/* COLOR statement */
void color() {
	get_exp(&fg_color);

	get_token();
	if (*token == ',') {
		get_exp(&bg_color);
	} else if (token_type != DELIMITER) {
		serror(0);
	}
	set_color();
}

void set_color() {
	printf("\x1b[%dm\x1b[%dm\x1b[%dm", fg_color % 8 + 30, (fg_color > 7 ? 1 : 22), bg_color % 16 + 40);
}