int picolCommandSet(struct picolInterp *i, int argc, char **argv, void *pd) { if (argc != 3) return picolArityErr(i, argv[0]); picolSetVar(i, argv[1], argv[2]); picolSetResult(i, argv[2]); return PICOL_OK; }
int picolCommandMath(struct picolInterp *i, int argc, char **argv, void *pd) { char buf[64]; int a, b, c; if (argc != 3) return picolArityErr(i, argv[0]); a = atoi(argv[1]); b = atoi(argv[2]); if (argv[0][0] == '+') c = a + b; else if (argv[0][0] == '-') c = a - b; else if (argv[0][0] == '*') c = a * b; else if (argv[0][0] == '/') c = a / b; else if (argv[0][0] == '>' && argv[0][1] == '\0') c = a > b; else if (argv[0][0] == '>' && argv[0][1] == '=') c = a >= b; else if (argv[0][0] == '<' && argv[0][1] == '\0') c = a < b; else if (argv[0][0] == '<' && argv[0][1] == '=') c = a <= b; else if (argv[0][0] == '=' && argv[0][1] == '=') c = a == b; else if (argv[0][0] == '!' && argv[0][1] == '=') c = a != b; else c = 0; /* I hate warnings */ sniprintf(buf, 64, "%d", c); picolSetResult(i, buf); return PICOL_OK; }
int picolCommandProc(struct picolInterp *i, int argc, char **argv, void *pd) { char **procdata = malloc(sizeof(char*)*2); if (argc != 4) return picolArityErr(i,argv[0]); procdata[0] = strdup(argv[2]); /* arguments list */ procdata[1] = strdup(argv[3]); /* procedure body */ return picolRegisterCommand(i,argv[1],picolCommandCallProc,procdata); }
int picolCommandIf(struct picolInterp *i, int argc, char **argv, void *pd) { int retcode; if (argc != 3 && argc != 5) return picolArityErr(i,argv[0]); if ((retcode = picolEval(i,argv[1])) != PICOL_OK) return retcode; if (atoi(i->result)) return picolEval(i,argv[2]); else if (argc == 5) return picolEval(i,argv[4]); return PICOL_OK; }
int picolCommandRetCodes(struct picolInterp *i, int argc, char **argv, void *pd) { if (argc != 1) return picolArityErr(i, argv[0]); if (strcmp(argv[0], "break") == 0) return PICOL_BREAK; else if (strcmp(argv[0], "continue") == 0) return PICOL_CONTINUE; return PICOL_OK; }
int picolCommandWhile(struct picolInterp *i, int argc, char **argv, void *pd) { if (argc != 3) return picolArityErr(i,argv[0]); while(1) { int retcode = picolEval(i,argv[1]); if (retcode != PICOL_OK) return retcode; if (atoi(i->result)) { if ((retcode = picolEval(i,argv[2])) == PICOL_CONTINUE) continue; else if (retcode == PICOL_OK) continue; else if (retcode == PICOL_BREAK) return PICOL_OK; else return retcode; } else { return PICOL_OK; } } }
int picolCommandReturn(struct picolInterp *i, int argc, char **argv, void *pd) { if (argc != 1 && argc != 2) return picolArityErr(i, argv[0]); picolSetResult(i, (argc == 2) ? argv[1] : ""); return PICOL_RETURN; }
int picolCommandPuts(struct picolInterp *i, int argc, char **argv, void *pd) { if (argc != 2) return picolArityErr(i, argv[0]); iprintf("%s\n", argv[1]); return PICOL_OK; }