add exit() function (#138)

* add exit() function

* add integer range function

* add pr request

squash

* add pr changes
This commit is contained in:
Jordan Ellis Coppard 2021-06-27 00:12:54 +08:00 committed by GitHub
parent 3f6209f20d
commit 794c789fca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -606,13 +606,35 @@ DEF(coreInput,
RET(VAR_OBJ(line)); RET(VAR_OBJ(line));
} }
DEF(coreExit,
"exit([value:int]) -> null\n"
"Sets the VM's fiber to NULL, causing the VM to return from execution "
"and thereby terminating the process entirely. The exit code of the "
"process is the optional argument [value] given to exit() which must be "
"between 0 and 255 inclusive. If no argument is given, the exit code is 0 "
"by default.") {
int argc = ARGC;
if (argc > 1) { // exit() or exit(val).
RET_ERR(newString(vm, "Invalid argument count."));
}
int64_t value = 0;
if (argc == 1) {
if (!validateInteger(vm, ARG(1), &value, "Argument 1")) return;
}
// TODO: this actually needs to be the VM fiber being set to null though.
exit(value);
}
// String functions. // String functions.
// ----------------- // -----------------
// TODO: substring. // TODO: substring.
DEF(coreStrChr, DEF(coreStrChr,
"str_chr(value:number) -> string\n" "str_chr(value:num) -> string\n"
"Returns the ASCII string value of the integer argument.") { "Returns the ASCII string value of the integer argument.") {
int64_t num; int64_t num;
if (!validateInteger(vm, ARG(1), &num, "Argument 1")) return; if (!validateInteger(vm, ARG(1), &num, "Argument 1")) return;
@ -626,7 +648,7 @@ DEF(coreStrChr,
} }
DEF(coreStrOrd, DEF(coreStrOrd,
"str_ord(value:string) -> number\n" "str_ord(value:string) -> num\n"
"Returns integer value of the given ASCII character.") { "Returns integer value of the given ASCII character.") {
String* c; String* c;
@ -1081,6 +1103,7 @@ void initializeCore(PKVM* vm) {
INITIALIZE_BUILTIN_FN("to_string", coreToString, 1); INITIALIZE_BUILTIN_FN("to_string", coreToString, 1);
INITIALIZE_BUILTIN_FN("print", corePrint, -1); INITIALIZE_BUILTIN_FN("print", corePrint, -1);
INITIALIZE_BUILTIN_FN("input", coreInput, -1); INITIALIZE_BUILTIN_FN("input", coreInput, -1);
INITIALIZE_BUILTIN_FN("exit", coreExit, -1);
// String functions. // String functions.
INITIALIZE_BUILTIN_FN("str_chr", coreStrChr, 1); INITIALIZE_BUILTIN_FN("str_chr", coreStrChr, 1);