apps/interpreters/minibasic: Add an option to enable/disable the test script

This commit is contained in:
Gregory Nutt 2016-08-11 12:52:11 -06:00
parent 37b6303751
commit 6e80d39c91
2 changed files with 31 additions and 11 deletions

View File

@ -51,4 +51,13 @@ config INTERPRETER_MINIBASIC_IOBUFSIZE
---help--- ---help---
Size of the statically allocated I/O buffer. Size of the statically allocated I/O buffer.
config INTERPRETER_MINIBASIC_TESTSCRIPT
bool "Test script"
default n
---help---
By default, the path to a basic program must provided on the command
line. It this option is selected, then a built-in, canned script is
enabled and will be used if no path is provided on the command line.
This canned script can be used for testing purposes.
endif endif

View File

@ -47,6 +47,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h>
#include "interpreters/minibasic.h" #include "interpreters/minibasic.h"
@ -54,6 +55,7 @@
* Private Data * Private Data
****************************************************************************/ ****************************************************************************/
#ifdef CONFIG_INTERPRETER_MINIBASIC_TESTSCRIPT
/* Here is a simple script to play with */ /* Here is a simple script to play with */
static FAR char *script = static FAR char *script =
@ -64,7 +66,9 @@ static FAR char *script =
"40 PRINT INSTR(\"FRED\", \"ED\", 4)\n" "40 PRINT INSTR(\"FRED\", \"ED\", 4)\n"
"50 PRINT VALLEN(\"12a\"), VALLEN(\"xyz\")\n" "50 PRINT VALLEN(\"12a\"), VALLEN(\"xyz\")\n"
"60 LET x = SQRT(3.0) * SQRT(3.0)\n" "60 LET x = SQRT(3.0) * SQRT(3.0)\n"
"65 LET x = INT(x + 0.5)\n" "70 PRINT MID$(\"1234567890\", x, -1)\n"; "65 LET x = INT(x + 0.5)\n"
"70 PRINT MID$(\"1234567890\", x, -1)\n";
#endif
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
@ -91,7 +95,7 @@ static FAR char *loadfile(FAR char *path)
fp = fopen(path, "r"); fp = fopen(path, "r");
if (!fp) if (!fp)
{ {
printf("Can't open %s\n", path); fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, errno);
return 0; return 0;
} }
@ -102,7 +106,7 @@ static FAR char *loadfile(FAR char *path)
answer = malloc(size + 100); answer = malloc(size + 100);
if (!answer) if (!answer)
{ {
printf("Out of memory\n"); fprintf(stderr, "ERROR: Out of memory\n");
fclose(fp); fclose(fp);
return 0; return 0;
} }
@ -128,10 +132,10 @@ static FAR char *loadfile(FAR char *path)
static void usage(void) static void usage(void)
{ {
printf("MiniBasic: a BASIC interpreter\n"); fprintf(stderr, "MiniBasic: a BASIC interpreter\n");
printf("usage:\n"); fprintf(stderr, "usage:\n");
printf("Basic <script>\n"); fprintf(stderr, "Basic <script>\n");
printf("See documentation for BASIC syntax.\n"); fprintf(stderr, "See documentation for BASIC syntax.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -157,12 +161,14 @@ int basic_main(int argc, char *argv[])
if (argc == 1) if (argc == 1)
{ {
/* Comment out usage call to run test script */ #ifdef CONFIG_INTERPRETER_MINIBASIC_TESTSCRIPT
usage();
basic(script, stdin, stdout, stderr); basic(script, stdin, stdout, stderr);
#else
fprintf(stderr, "ERROR: Missing argument.\n");
usage();
#endif
} }
else else if (argc == 2)
{ {
scr = loadfile(argv[1]); scr = loadfile(argv[1]);
if (scr) if (scr)
@ -171,6 +177,11 @@ int basic_main(int argc, char *argv[])
free(scr); free(scr);
} }
} }
else
{
fprintf(stderr, "ERROR: Too many arguments.\n");
usage();
}
return 0; return 0;
} }