summaryrefslogtreecommitdiff
path: root/src/user/app/kbasic/commands.c
blob: 3e478964bca82fb2bce4908ac838090b3600b0cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#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);
}