root/tools/ubasic_test/run-ubasic.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

   1 /*
   2  * Copyright (c) 2006, Adam Dunkels
   3  * All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the author nor the names of its contributors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  *
  29  */
  30 
  31 #include "versions.h"
  32 #include "ubasic.h"
  33 #include <stdio.h>
  34 #include <assert.h>
  35 #include <stdlib.h>
  36 
  37 static const char program_static[] =
  38 "sleep 1000\n\
  39  if a<1 then let a=2\n\
  40  if b<1 then let b=3\n\
  41  for s=1 to a\n\
  42    shoot\n\
  43    gosub \"incEv\"\n\
  44  next s\n\
  45  shoot\n\
  46 end\n\
  47 \n\
  48 :incEv   \n\
  49 \n\
  50 for n=1 to b\n\
  51   click \"right\"\n\
  52 next n\n\
  53 return\n";
  54 
  55 extern int ubasic_init(const char *program, int is_ptp);
  56 extern int ubasic_run(void);
  57 extern int ubasic_linenumber();
  58 extern int ubasic_error;
  59 extern const char *ubasic_errstrings[];
  60 
  61 /*---------------------------------------------------------------------------*/
  62 int
  63 main(int argc, char * argv[])
  64 {
  65     char *program;
  66     if (argc == 1) {
  67         // buildin program
  68         program = (char *) program_static;
  69     } else {
  70         assert(argc == 2);
  71         FILE * f = fopen(argv[1], "rb");
  72         if (!f) {
  73             perror("Error opening file");
  74             exit(-1);
  75         }
  76 
  77         fseek(f,0, SEEK_END);
  78         int sz = ftell(f);
  79         fseek(f,0, SEEK_SET);
  80         program = (char *) malloc(sz+3);
  81         int szr = fread(program,1,sz, f);
  82         fclose(f);
  83         if (szr != sz) {
  84             perror("Error reading program");
  85         }
  86         printf("size: %d, read: %d \n", sz, szr); 
  87         program[sz] = '\n';
  88         program[sz+1] = 0;
  89     }
  90 
  91     printf("Program:\n%s\n=====================================\n\n", program);
  92     ubasic_init(program,0);
  93     int running = SCRIPT_RUN_RUNNING;
  94     do {
  95         running = ubasic_run();
  96     } while(running == SCRIPT_RUN_RUNNING);
  97 
  98     if (ubasic_error){
  99         const char *msg;
 100         if (ubasic_error >= UBASIC_E_ENDMARK) {
 101             msg = ubasic_errstrings[UBASIC_E_UNKNOWN_ERROR];
 102         } else {
 103             msg = ubasic_errstrings[ubasic_error];
 104         }
 105         fprintf(stderr, "Ubasic error near Line %d: %s\n", ubasic_linenumber(), msg);
 106     }
 107 
 108   return 0;
 109 }
 110 /*---------------------------------------------------------------------------*/
 111 

/* [<][>][^][v][top][bottom][index][help] */