diff options
author | Alex AUVOLAT <alexis211@gmail.com> | 2013-06-09 14:35:40 +0200 |
---|---|---|
committer | Alex AUVOLAT <alexis211@gmail.com> | 2013-06-09 14:35:40 +0200 |
commit | 9c74cb8c5bb5661d7f71f43b88816e70cc9fc022 (patch) | |
tree | 77a893ff826dcb14e9b8768c23e4970033e934ea /src/user/app/kbasic | |
parent | 4d65fcb9a8b6c7c6fd5a3390c46a96d11b6a80d4 (diff) | |
download | TCE-9c74cb8c5bb5661d7f71f43b88816e70cc9fc022.tar.gz TCE-9c74cb8c5bb5661d7f71f43b88816e70cc9fc022.zip |
Many bug corrections.
Diffstat (limited to 'src/user/app/kbasic')
-rw-r--r-- | src/user/app/kbasic/basic.h | 2 | ||||
-rw-r--r-- | src/user/app/kbasic/commands.c | 24 | ||||
-rw-r--r-- | src/user/app/kbasic/control.c | 45 | ||||
-rw-r--r-- | src/user/app/kbasic/count.bas | 7 | ||||
-rw-r--r-- | src/user/app/kbasic/guess.bas | 14 | ||||
-rw-r--r-- | src/user/app/kbasic/lex.c | 23 | ||||
-rw-r--r-- | src/user/app/kbasic/main.c | 24 | ||||
-rw-r--r-- | src/user/app/kbasic/pppg.bas | 14 | ||||
-rw-r--r-- | src/user/app/kbasic/prime.bas | 20 | ||||
-rw-r--r-- | src/user/app/kbasic/tables.bas | 6 | ||||
-rw-r--r-- | src/user/app/kbasic/var.c | 2 |
11 files changed, 122 insertions, 59 deletions
diff --git a/src/user/app/kbasic/basic.h b/src/user/app/kbasic/basic.h index b3491da..0cc5f0c 100644 --- a/src/user/app/kbasic/basic.h +++ b/src/user/app/kbasic/basic.h @@ -35,6 +35,7 @@ #define LIST 13 #define PRINT 20 // Built-in commands #define INPUT 21 +#define COLOR 22 #define RND 50 // Built-in functions // Buffer size definitions @@ -67,6 +68,7 @@ void serror(int); // BASIC commands void exec_print(), input(); +void color(); void set_color(); void assignment(); void get_exp(int*); diff --git a/src/user/app/kbasic/commands.c b/src/user/app/kbasic/commands.c index f60f32a..b88e57e 100644 --- a/src/user/app/kbasic/commands.c +++ b/src/user/app/kbasic/commands.c @@ -4,6 +4,9 @@ #include "basic.h" +static int fg_color = 7, bg_color = 0; + + /* Assign a variable a value. */ void assignment() { @@ -33,8 +36,6 @@ void assignment() *var = value; } - - /* Execute a simple version of the BASIC PRINT statement */ void exec_print() { @@ -95,9 +96,26 @@ void input() var = find_var(token); /* get the input var */ - scanf("%d\n", var); /* read input */ + 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); +} diff --git a/src/user/app/kbasic/control.c b/src/user/app/kbasic/control.c index 1ab22ff..87731c4 100644 --- a/src/user/app/kbasic/control.c +++ b/src/user/app/kbasic/control.c @@ -1,7 +1,7 @@ +#include <tce/syscall.h> + #include <stdio.h> #include <string.h> -// #include <sys/wait.h> -// #include <unistd.h> #include <ctype.h> #include <stdlib.h> #include <setjmp.h> @@ -94,18 +94,25 @@ void insert_line(char *l, int n) { } } - p = malloc(prog_l + strlen(l) + 3); + p = malloc(prog_l + strlen(l) + 4); strncpy(p, program, (a - program)); // copy [0, a[ - p[a - program] = 0; + + char *p2 = p + (a - program); + if (*(p2-1) != '\n') *(p2++) = '\n'; + *p2 = 0; + // if l is an empty line, do not copy. for (i_n = 0; l[i_n]; i_n++) { if (!isdigit(l[i_n]) && l[i_n] != '\n' && l[i_n] != '\r') { - strcpy(p + (a - program), l); // copy l + strcpy(p2, l); // copy l break; } } - strcat(p + (a - program), b); // copy [b, end[ + if (p2[strlen(p2)-1] != '\n') strcat(p2, "\n"); + + strcat(p2, b); // copy [b, end[ + if (p2[strlen(p2)-1] != '\n') strcat(p2, "\n"); load_program(p); // reparse labels } @@ -140,6 +147,9 @@ int start(char *entry) { case PRINT: exec_print(); break; + case COLOR: + color(); + break; case GOTO: exec_goto(); break; @@ -455,30 +465,25 @@ char *gpop() Forks and runs another instance of the interpreter */ void exec_exec() { - - /* TODO int pid; get_token(); if (token_type==QUOTE) { - pid = fork(); - if (pid < 0) { - printf("Error: could not fork.\n"); + char *args[2]; + args[0] = token; + args[1] = 0; + + pid = run(interp_name, args, term.fd); + if (pid <= 0) { + printf("Error: could not run '%s'.\n", interp_name); } else { - if (pid == 0) { - execlp(interp_name, interp_name, token, 0); - printf("Error: could not exec.\n"); - exit(0); - } else { - wait(0); - } + int ret = waitpid(pid, 1); + printf("[exited: %i]\n", ret); } get_token(); } else { serror(0); } - */ - printf("EXEC not available.\n"); } /* LOAD function diff --git a/src/user/app/kbasic/count.bas b/src/user/app/kbasic/count.bas new file mode 100644 index 0000000..5741a72 --- /dev/null +++ b/src/user/app/kbasic/count.bas @@ -0,0 +1,7 @@ +05 print "Let me count to 100:" +10 a = 0 +20 print a; +30 a = a + 1 +40 if a = 100 then goto 60 +50 goto 20 +60 end diff --git a/src/user/app/kbasic/guess.bas b/src/user/app/kbasic/guess.bas new file mode 100644 index 0000000..f15ba49 --- /dev/null +++ b/src/user/app/kbasic/guess.bas @@ -0,0 +1,14 @@ + +n = RND % 100 +e = 0 + +20 input "Guess the number: ", i +e = e + 1 +if i > n then print "Too big!" +if i < n then print "Too small!" +if i = n then goto 42 +goto 20 + +42 print "That's right! You got it with only ", e, " guesses!" +end + diff --git a/src/user/app/kbasic/lex.c b/src/user/app/kbasic/lex.c index 055b502..5d3487b 100644 --- a/src/user/app/kbasic/lex.c +++ b/src/user/app/kbasic/lex.c @@ -11,6 +11,7 @@ struct commands { /* keyword lookup table */ } table[] = { /* Commands must be entered lowercase */ {"print", PRINT}, /* in this table. */ {"input", INPUT}, + {"color", COLOR}, {"if", IF}, {"then", THEN}, @@ -84,7 +85,8 @@ int get_token() if(*prog_ip=='\0') { /* end of file */ *token=0; tok = FINISHED; - return(token_type=DELIMITER); + token_type=DELIMITER; + goto get_tok_end; } while(iswhite(*prog_ip)) ++prog_ip; /* skip over white space */ @@ -93,12 +95,14 @@ int get_token() ++prog_ip; ++prog_ip; tok = EOL; *token='\r'; token[1]='\n'; token[2]=0; - return (token_type = DELIMITER); + token_type = DELIMITER; + goto get_tok_end; } if (*prog_ip=='\n') { /* lf (unix newline) */ ++prog_ip; tok = EOL; *token='\n'; token[1] = 0; - return (token_type = DELIMITER); + token_type = DELIMITER; + goto get_tok_end; } if(strchr("+-*^/%=;(),><", *prog_ip)){ /* delimiter */ @@ -106,7 +110,8 @@ int get_token() prog_ip++; /* advance to next position */ temp++; *temp=0; - return (token_type=DELIMITER); + token_type=DELIMITER; + goto get_tok_end; } if(*prog_ip=='"') { /* quoted string */ @@ -117,7 +122,8 @@ int get_token() } if(*prog_ip=='\r' || *prog_ip=='\n') serror(1); prog_ip++;*temp=0; - return(token_type=QUOTE); + token_type=QUOTE; + goto get_tok_end; } if(isdigit(*prog_ip)) { /* number */ @@ -126,7 +132,8 @@ int get_token() prog_ip++; } *temp = '\0'; - return(token_type = NUMBER); + token_type = NUMBER; + goto get_tok_end; } if(isalpha(*prog_ip)) { /* var or command */ @@ -145,6 +152,10 @@ int get_token() if(!tok) token_type = VARIABLE; else token_type = COMMAND; /* is a command */ } + +get_tok_end: + //printf("[%d.%d.%s]", token_type, tok, token); + return token_type; } diff --git a/src/user/app/kbasic/main.c b/src/user/app/kbasic/main.c index 1003a7b..c76980c 100644 --- a/src/user/app/kbasic/main.c +++ b/src/user/app/kbasic/main.c @@ -7,10 +7,7 @@ int main(int argc, char **argv) {
char *p_buf;
- srand(0); // not very usefull...
-
- readline_history hist;
- hist.str = 0; hist.max = 10;
+ srand(1); // not very usefull...
interp_name = argv[0];
init();
@@ -22,18 +19,19 @@ int main(int argc, char **argv) { load_program(p_buf);
start(p_buf);
} else if (argc == 1) {
+ readline_history hist;
+ hist.str = 0; hist.max = 10;
+
printf("KBASIC 0.1\n"
"Press ^D to quit interpreter.\n\n");
for (;;) {
- printf("> ");
+ printf("\x1b[0m\x1b[32m> \x1b[1m");
char *l = freadline(stdin, &hist);
- if (l == NULL) break;
+ set_color();
- if (!strcmp(l, "exit")) {
- free(l);
- break;
- }
+ if (l == NULL) break;
+ if (!strcmp(l, "exit")) break;
prog_ip = l;
get_token();
@@ -42,11 +40,11 @@ int main(int argc, char **argv) { } else { // directly execute
start(l);
}
- free(l);
}
} else {
- printf("usage:\n\t%s <filename>\trun BASIC file\n", argv[0]);
- printf("\n\t%s\t\t\tinteractive BASIC interpreter\n", argv[0]);
+ printf("usage:\n");
+ printf(" %s <filename> run BASIC file\n", argv[0]);
+ printf(" %s interactive BASIC interpreter\n", argv[0]);
exit(1);
}
diff --git a/src/user/app/kbasic/pppg.bas b/src/user/app/kbasic/pppg.bas deleted file mode 100644 index 32fc050..0000000 --- a/src/user/app/kbasic/pppg.bas +++ /dev/null @@ -1,14 +0,0 @@ - -n = RND % 100 -e = 0 - -20 input "Devinez le nombre: ", i -e = e + 1 -if i > n then print "Trop grand !" -if i < n then print "Trop petit !" -if i = n then goto 42 -goto 20 - -42 print "Bravo, vous avez trouvé en ", e, " essais!" -end - diff --git a/src/user/app/kbasic/prime.bas b/src/user/app/kbasic/prime.bas new file mode 100644 index 0000000..bbe4bd0 --- /dev/null +++ b/src/user/app/kbasic/prime.bas @@ -0,0 +1,20 @@ +10 i = 2 +20 gosub 100 +30 if p = 1 then print i; +40 i = i + 1 +50 if i > 1000 then goto 300 +60 goto 20 + +100 p = 1 +110 d = 2 +120 if d * d > i then return +130 if i % d = 0 then goto 200 +140 d = d + 1 +150 goto 120 +200 p = 0 +210 return + +300 print +310 print "That's all primes for today!" +320 end + diff --git a/src/user/app/kbasic/tables.bas b/src/user/app/kbasic/tables.bas index 31e9ce3..a6aaf9c 100644 --- a/src/user/app/kbasic/tables.bas +++ b/src/user/app/kbasic/tables.bas @@ -1,12 +1,12 @@ -input "Taille de la table: ", k -print "Table d'addition:" +input "Table size: ", k +print "Addition table:" for i = 0 to k for j = 0 to k print i+j; next print next -print "Table de multiplication:" +print "Multiplication table:" for i = 1 to k for j = 1 to k print i * j; diff --git a/src/user/app/kbasic/var.c b/src/user/app/kbasic/var.c index a49ce61..6ed2255 100644 --- a/src/user/app/kbasic/var.c +++ b/src/user/app/kbasic/var.c @@ -17,3 +17,5 @@ int* find_var(char *s) } return variables + (toupper(*s)-'A'); } + + |