This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
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