interpreter/shell.c

80 lines
1.2 KiB
C
Raw Normal View History

2018-01-23 08:17:07 -05:00
#include <parser.h>
2018-02-07 09:26:36 -05:00
#include "stack.h"
2018-01-23 08:17:07 -05:00
#include <stdio.h>
2018-01-29 20:19:22 -05:00
#include <stdlib.h>
#include <string.h>
2018-01-23 08:17:07 -05:00
2018-02-05 11:28:24 -05:00
int s_put(interpreter *it)
2018-01-23 08:17:07 -05:00
{
2018-01-29 20:19:22 -05:00
char *s = igetarg_string(it, 0);
printf("%s\n", s);
2018-02-05 11:28:24 -05:00
return 0;
2018-01-23 08:17:07 -05:00
}
2018-02-05 11:28:24 -05:00
int s_type(interpreter *it)
2018-01-31 17:54:47 -05:00
{
variable *v = (variable *)it->stack[0];
switch (v->valtype) {
case STRING:
puts("string");
break;
case INTEGER:
puts("integer");
break;
case FLOAT:
puts("float");
break;
case FUNC:
2018-02-05 11:28:24 -05:00
puts("func");
2018-01-31 17:54:47 -05:00
break;
default:
puts("unknown");
break;
}
2018-02-05 11:28:24 -05:00
return 0;
2018-01-31 17:54:47 -05:00
}
2018-02-05 11:28:24 -05:00
int quit(interpreter *it)
2018-01-29 20:19:22 -05:00
{
(void)it;
exit(0);
2018-02-05 11:28:24 -05:00
return 0;
2018-01-29 20:19:22 -05:00
}
2018-02-06 08:48:21 -05:00
int main(int argc, char **argv)
2018-01-23 08:17:07 -05:00
{
interpreter interp;
2018-02-06 08:48:21 -05:00
if (argc != 2) {
printf("Usage: %s file\n", argv[0]);
return -1;
}
FILE *fp = fopen(argv[1], "r");
if (fp == 0) {
printf("Could not open file: %s\n", argv[1]);
return -1;
}
2018-01-29 20:19:22 -05:00
iinit(&interp);
2018-01-31 17:54:47 -05:00
inew_cfunc(&interp, "put", s_put);
inew_cfunc(&interp, "tp", s_type);
inew_cfunc(&interp, "q", quit);
2018-01-29 20:19:22 -05:00
2018-01-23 08:17:07 -05:00
2018-01-29 20:19:22 -05:00
char *line = 0;
unsigned int size;
int result;
2018-02-06 08:48:21 -05:00
while (getline(&line, &size, fp) != -1) {
2018-01-29 20:19:22 -05:00
*strchr(line, '\n') = '\0';
result = idoline(&interp, line);
if (result != 0)
printf("Error: %d\n", result);
2018-01-23 08:17:07 -05:00
}
2018-02-06 08:48:21 -05:00
fclose(fp);
2018-01-23 08:17:07 -05:00
return 0;
}