9 #include "mTaskSymbols.h"
12 #define MAXTASKSIZE 1024
14 #define STACKSIZE 1024
18 #define debug(s, ...) printf(s, ##__VA_ARGS__);
20 #define debug(s, ...) ;
23 #define pdie(s) {perror(s); exit(1);}
24 #define die(s, ...) {fprintf(stderr, s, ##__VA_ARGS__); exit(1);}
26 char tasks
[MAXTASKS
][MAXTASKSIZE
] = {0};
27 bool tasks_used
[MAXTASKS
] = {0};
29 void killHandler(int i
)
31 printf("%s caught, Bye...\n", strsignal(i
));
35 bool input_available(int fd
){
42 if (select(fd
+1, &fds
, NULL
, NULL
, &tv
) == -1)
44 return FD_ISSET(fd
, &fds
);
47 void read_message(int fd_in
, int fd_out
)
54 for(ct
= 0; ct
<MAXTASKS
; ct
++)
58 die("Trying to add too much tasks...\n");
60 debug("Receiving input for task %d\n", ct
);
63 debug("Receiving an sds\n");
64 } else if((char) c
== 't'){
69 if(tasklen
> MAXTASKSIZE
)
70 die("Task is too long: %d\n", tasklen
);
71 for(int i
= 0; i
<tasklen
; i
++){
72 debug("Read %d\n", i
);
73 read(fd_in
, tasks
[ct
]+i
, 1);
74 debug("t[][%i]: %d\n", i
,
77 //Return the task number for later removal
78 write(fd_out
, (void *)&ct
, 1);
79 debug("Received a task of length %d\n", tasklen
);
80 tasks_used
[ct
] = true;
82 die("Unknown message: %c?\n", c
);
86 void run_task(char *program
)
90 int plen
= strlen(program
);
91 char stack
[1024] = {0};
93 switch(program
[pc
++]){
97 stack
[sp
++] = program
[pc
++];
103 stack
[sp
] = stack
[sp
] > 0 ? 0 : 1;
106 stack
[sp
-1] = stack
[sp
] + stack
[sp
-1];
110 stack
[sp
-1] = stack
[sp
] - stack
[sp
-1];
114 stack
[sp
-1] = stack
[sp
] * stack
[sp
-1];
118 stack
[sp
-1] = stack
[sp
] / stack
[sp
-1];
122 stack
[sp
-1] = stack
[sp
] && stack
[sp
-1];
126 stack
[sp
-1] = stack
[sp
] || stack
[sp
-1];
130 stack
[sp
-1] = stack
[sp
] == stack
[sp
-1];
134 stack
[sp
-1] = stack
[sp
] != stack
[sp
-1];
137 case BCLES
: stack
[sp
-1] = stack
[sp
] < stack
[sp
-1];
141 stack
[sp
-1] = stack
[sp
] > stack
[sp
-1];
145 stack
[sp
-1] = stack
[sp
] <= stack
[sp
-1];
149 stack
[sp
-1] = stack
[sp
] >= stack
[sp
-1];
153 pc
= pc
+ program
[pc
];
156 pc
+= stack
[sp
] ? program
[pc
] : 1;
159 pc
+= stack
[sp
] ? 1 : program
[pc
];
162 printf("SerialAvailable()\n");
165 printf("SerialPrint()\n");
167 case BCSERIALPRINTLN
:
168 printf("SerialPrintln()\n");
171 printf("SerialRead()\n");
173 case BCSERIALPARSEINT
:
174 printf("SerialParseInt()\n");
177 printf("AnalogRead()\n");
180 printf("AnalogWrite()\n");
182 die("Unrecognized command: %X\n", program
[--pc
]);
192 fd_in
= fileno(stdin
);
193 fd_out
= fileno(stdout
);
195 //Register signal handler
196 if(signal(SIGINT
, killHandler
) == SIG_ERR
){
197 die("Couldn't register signal handler...\n");
199 if(signal(SIGTERM
, killHandler
) == SIG_ERR
){
200 die("Couldn't register signal handler...\n");
204 //Check for new tasks
205 if(input_available(fd_in
)){
206 read_message(fd_in
, fd_out
);
210 for(ct
= 0; ct
<MAXTASKS
; ct
++){
212 debug("Task %d not implemented\n", ct
);
215 debug("Going to execute task %d\n", ct
);
223 while ((c = getchar()) != EOF && pc < PROGRAMSIZE)
225 if (pc >= PROGRAMSIZE)
226 die("Max program size: %d\n", PROGRAMSIZE);
228 debug("Done reading, program length: %d\n", pl);
231 //Reset program counter and stack counter