Skip to content

Commit

Permalink
apps/interpreters/minibasic: Add an option to enable/disable the test…
Browse files Browse the repository at this point in the history
… script
  • Loading branch information
gregory-nutt committed Aug 11, 2016
1 parent 37b6303 commit 6e80d39
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
9 changes: 9 additions & 0 deletions interpreters/minibasic/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,13 @@ config INTERPRETER_MINIBASIC_IOBUFSIZE
---help---
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
33 changes: 22 additions & 11 deletions interpreters/minibasic/script.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include "interpreters/minibasic.h"

/****************************************************************************
* Private Data
****************************************************************************/

#ifdef CONFIG_INTERPRETER_MINIBASIC_TESTSCRIPT
/* Here is a simple script to play with */

static FAR char *script =
Expand All @@ -64,7 +66,9 @@ static FAR char *script =
"40 PRINT INSTR(\"FRED\", \"ED\", 4)\n"
"50 PRINT VALLEN(\"12a\"), VALLEN(\"xyz\")\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
Expand All @@ -91,7 +95,7 @@ static FAR char *loadfile(FAR char *path)
fp = fopen(path, "r");
if (!fp)
{
printf("Can't open %s\n", path);
fprintf(stderr, "ERROR: Failed to open %s: %d\n", path, errno);
return 0;
}

Expand All @@ -102,7 +106,7 @@ static FAR char *loadfile(FAR char *path)
answer = malloc(size + 100);
if (!answer)
{
printf("Out of memory\n");
fprintf(stderr, "ERROR: Out of memory\n");
fclose(fp);
return 0;
}
Expand All @@ -128,10 +132,10 @@ static FAR char *loadfile(FAR char *path)

static void usage(void)
{
printf("MiniBasic: a BASIC interpreter\n");
printf("usage:\n");
printf("Basic <script>\n");
printf("See documentation for BASIC syntax.\n");
fprintf(stderr, "MiniBasic: a BASIC interpreter\n");
fprintf(stderr, "usage:\n");
fprintf(stderr, "Basic <script>\n");
fprintf(stderr, "See documentation for BASIC syntax.\n");
exit(EXIT_FAILURE);
}

Expand All @@ -157,12 +161,14 @@ int basic_main(int argc, char *argv[])

if (argc == 1)
{
/* Comment out usage call to run test script */

usage();
#ifdef CONFIG_INTERPRETER_MINIBASIC_TESTSCRIPT
basic(script, stdin, stdout, stderr);
#else
fprintf(stderr, "ERROR: Missing argument.\n");
usage();
#endif
}
else
else if (argc == 2)
{
scr = loadfile(argv[1]);
if (scr)
Expand All @@ -171,6 +177,11 @@ int basic_main(int argc, char *argv[])
free(scr);
}
}
else
{
fprintf(stderr, "ERROR: Too many arguments.\n");
usage();
}

return 0;
}

0 comments on commit 6e80d39

Please sign in to comment.