root/lib/ubasic/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 "../../modules/ubasic.h"
  32 #include <stdio.h>
  33 #include <assert.h>
  34 #include <stdlib.h>
  35 
  36 static const char program_static[] =
  37 "sleep 1000\n\
  38  if a<1 then let a=2\n\
  39  if b<1 then let b=3\n\
  40  for s=1 to a\n\
  41    shoot\n\
  42    gosub \"incEv\"\n\
  43  next s\n\
  44  shoot\n\
  45 end\n\
  46 \n\
  47 :incEv   \n\
  48 \n\
  49 for n=1 to b\n\
  50   click \"right\"\n\
  51 next n\n\
  52 return\n";
  53 
  54 /*---------------------------------------------------------------------------*/
  55 int
  56 main(int argc, char * argv[])
  57 {
  58     char *program;
  59     if (argc == 1) {
  60         // buildin program
  61         program = (char *) program_static;
  62     } else {
  63         assert(argc == 2);
  64         FILE * f = fopen(argv[1], "rb");
  65         if (!f) {
  66             perror("Error opening file");
  67             exit(-1);
  68         }
  69 
  70         fseek(f,0, SEEK_END);
  71         int sz = ftell(f);
  72         fseek(f,0, SEEK_SET);
  73         program = (char *) malloc(sz+3);
  74         int szr = fread(program,1,sz, f);
  75         fclose(f);
  76         if (szr != sz) {
  77             perror("Error reading program");
  78         }
  79         printf("size: %d, read: %d \n", sz, szr); 
  80         program[sz] = '\n';
  81         program[sz+1] = 0;
  82     }
  83 
  84     printf("Program:\n%s\n=====================================\n\n", program);
  85     ubasic_init(program);
  86     do {
  87         ubasic_run();
  88     } while(!ubasic_finished());
  89 
  90     if (ubasic_error){
  91         const char *msg;
  92         if (ubasic_error >= UBASIC_E_ENDMARK) {
  93             msg = ubasic_errstrings[UBASIC_E_UNKNOWN_ERROR];
  94         } else {
  95             msg = ubasic_errstrings[ubasic_error];
  96         }
  97         fprintf(stderr, "Ubasic error near Line %d: %s\n", ubasic_linenumber(), msg);
  98     }
  99 
 100   return 0;
 101 }
 102 /*---------------------------------------------------------------------------*/
 103 

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