Eliminate unused parameters to krk_compile, interpret, runfile, callfile

This commit is contained in:
K. Lange 2021-02-25 08:17:19 +09:00
parent 77b7f3ab22
commit 4c1fe774e7
9 changed files with 29 additions and 45 deletions

View File

@ -2626,15 +2626,14 @@ static ParseRule * getRule(KrkTokenType type) {
static volatile int _compilerLock = 0;
#endif
KrkFunction * krk_compile(const char * src, int newScope, char * fileName) {
KrkFunction * krk_compile(const char * src, char * fileName) {
_obtain_lock(_compilerLock);
krk_initScanner(src);
Compiler compiler;
initCompiler(&compiler, TYPE_MODULE);
compiler.function->chunk.filename = krk_copyString(fileName, strlen(fileName));
if (newScope) beginScope();
compiler.function->name = krk_copyString("<module>",8);
parser.hadError = 0;
parser.panicMode = 0;

View File

@ -11,11 +11,10 @@
* Compiles the source string 'src' into a code object.
*
* @param src Source code string to compile.
* @param newScope Whether the compiler should treat the source as a new module.
* @param fileName Path name of the source file or a representative string like "<stdin>"
* @return The code object resulting from the compilation, or NULL if compilation failed.
*/
extern KrkFunction * krk_compile(const char * src, int newScope, char * fileName);
extern KrkFunction * krk_compile(const char * src, char * fileName);
/**
* @brief Mark objects owned by the compiler as in use.

View File

@ -321,7 +321,7 @@ static int runString(char * argv[], int flags, char * string) {
findInterpreter(argv);
krk_initVM(flags);
krk_startModule("__main__");
krk_interpret(string, 1, "<stdin>","<stdin>");
krk_interpret(string, "<stdin>");
krk_freeVM();
return 0;
}
@ -666,7 +666,7 @@ _finishArgs:
FREE_ARRAY(char *, lines, lineCapacity);
if (valid) {
KrkValue result = krk_interpret(allData, 0, "<module>","<stdin>");
KrkValue result = krk_interpret(allData, "<stdin>");
if (!IS_NONE(result)) {
KrkClass * type = krk_getType(result);
const char * formatStr = " \033[1;30m=> %s\033[0m\n";
@ -691,7 +691,7 @@ _finishArgs:
}
} else {
krk_startModule("__main__");
result = krk_runfile(argv[optind],0,"__main__",argv[optind]);
result = krk_runfile(argv[optind],argv[optind]);
if (IS_NONE(result) && krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) result = INTEGER_VAL(1);
}

View File

@ -1542,7 +1542,8 @@ int krk_loadModule(KrkString * path, KrkValue * moduleOut, KrkString * runAs) {
krk_startModule(runAs->chars);
krk_tableSet(&vm.modules, OBJECT_VAL(runAs), OBJECT_VAL(krk_currentThread.module));
if (isPackage) krk_attachNamedValue(&krk_currentThread.module->fields,"__ispackage__",BOOLEAN_VAL(1));
*moduleOut = krk_callfile(fileName,runAs->chars,fileName);
krk_callfile(fileName,fileName);
*moduleOut = OBJECT_VAL(krk_currentThread.module);
krk_currentThread.module = enclosing;
if (!IS_OBJECT(*moduleOut)) {
if (!(krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION)) {
@ -2507,8 +2508,8 @@ KrkInstance * krk_startModule(const char * name) {
return module;
}
KrkValue krk_interpret(const char * src, int newScope, char * fromName, char * fromFile) {
KrkFunction * function = krk_compile(src, 0, fromFile);
KrkValue krk_interpret(const char * src, char * fromFile) {
KrkFunction * function = krk_compile(src, fromFile);
if (!function) {
if (!krk_currentThread.frameCount) handleException();
return NONE_VAL();
@ -2517,34 +2518,24 @@ KrkValue krk_interpret(const char * src, int newScope, char * fromName, char * f
krk_push(OBJECT_VAL(function));
krk_attachNamedObject(&krk_currentThread.module->fields, "__file__", (KrkObj*)function->chunk.filename);
function->name = krk_copyString(fromName, strlen(fromName));
KrkClosure * closure = krk_newClosure(function);
krk_pop();
krk_push(OBJECT_VAL(closure));
if (!newScope) {
/* Quick little kludge so that empty statements return None from REPLs */
krk_push(NONE_VAL());
krk_pop();
}
/* Quick little kludge so that empty statements return None from REPLs */
krk_push(NONE_VAL());
krk_pop();
krk_callValue(OBJECT_VAL(closure), 0, 1);
KrkValue result = run();
if (newScope) {
return OBJECT_VAL(krk_currentThread.module);
} else {
return result;
}
return run();
}
KrkValue krk_runfile(const char * fileName, int newScope, char * fromName, char * fromFile) {
KrkValue krk_runfile(const char * fileName, char * fromFile) {
FILE * f = fopen(fileName,"r");
if (!f) {
if (!newScope) {
fprintf(stderr, "kuroko: could not read file '%s': %s\n", fileName, strerror(errno));
}
fprintf(stderr, "kuroko: could not read file '%s': %s\n", fileName, strerror(errno));
return INTEGER_VAL(errno);
}
@ -2559,16 +2550,16 @@ KrkValue krk_runfile(const char * fileName, int newScope, char * fromName, char
fclose(f);
buf[size] = '\0';
KrkValue result = krk_interpret(buf, newScope, fromName, fromFile);
KrkValue result = krk_interpret(buf, fromFile);
free(buf);
return result;
}
KrkValue krk_callfile(const char * fileName, char * fromName, char * fromFile) {
KrkValue krk_callfile(const char * fileName, char * fromFile) {
int previousExitFrame = krk_currentThread.exitOnFrame;
krk_currentThread.exitOnFrame = krk_currentThread.frameCount;
KrkValue out = krk_runfile(fileName, 1, fromName, fromFile);
KrkValue out = krk_runfile(fileName, fromFile);
krk_currentThread.exitOnFrame = previousExitFrame;
return out;
}

View File

@ -296,8 +296,6 @@ extern void krk_resetStack(void);
* of commands from a REPL or when executing a file.
*
* @param src Source code to compile and run.
* @param newScope Whether this code should be interpreted in the context of a new module.
* @param fromName Name of the function or module being interpreted.
* @param fromFile Path to the source file, or a representative string like "<stdin>".
* @return When newScope is non-zero, an object representing the globals of the new scope;
* otherwise, the returned result of the execution of this code. If an uncaught
@ -305,7 +303,7 @@ extern void krk_resetStack(void);
* indicate @c KRK_THREAD_HAS_EXCEPTION and @c krk_currentThread.currentException
* should contain the raised exception value.
*/
extern KrkValue krk_interpret(const char * src, int newScope, char * fromName, char * fromFile);
extern KrkValue krk_interpret(const char * src, char * fromFile);
/**
* @brief Load and run a source file and return when execution completes.
@ -315,13 +313,11 @@ extern KrkValue krk_interpret(const char * src, int newScope, char * fromName, c
* of the current module state (eg. as if they were lines entered on a repl)
*
* @param fileName Path to the source file to read and execute.
* @param newScope Whether this file should be run in the context of a new module.
* @param fromName Value to assign to @c \__name__
* @param fromFile Value to assign to @c \__file__
* @return As with @c krk_interpret, an object representing the newly created module,
* or the final return value of the VM execution.
*/
extern KrkValue krk_runfile(const char * fileName, int newScope, char * fromName, char * fromFile);
extern KrkValue krk_runfile(const char * fileName, char * fromFile);
/**
* @brief Load and run a file as a module.
@ -331,11 +327,10 @@ extern KrkValue krk_runfile(const char * fileName, int newScope, char * fromName
* a new module context and is used internally by the import mechanism.
*
* @param fileName Path to the source file to read and execute.
* @param fromName Value to assign to @c \__name__
* @param fromFile Value to assign to @c \__file__
* @return The object representing the module, or None if execution of the file failed.
*/
extern KrkValue krk_callfile(const char * fileName, char * fromName, char * fromFile);
extern KrkValue krk_callfile(const char * fileName, char * fromFile);
/**
* @brief Push a stack value.

View File

@ -368,7 +368,7 @@ static int compileFile(char * fileName) {
krk_startModule("__main__");
KrkFunction * func = krk_compile(buf, 0, fileName);
KrkFunction * func = krk_compile(buf, fileName);
if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) {
fprintf(stderr, "%s: exception during compilation:\n", fileName);

View File

@ -5,7 +5,7 @@
int main(int argc, char *argv[]) {
krk_initVM(0);
krk_startModule("__main__");
krk_interpret("import kuroko\nprint('Kuroko',kuroko.version)\n", 1, "<stdin>","<stdin>");
krk_interpret("import kuroko\nprint('Kuroko',kuroko.version)\n", "<stdin>");
krk_freeVM();
return 0;
}

View File

@ -16,7 +16,7 @@ int main(int argc, char * argv[]) {
krk_tableDelete(&vm.builtins->fields, OBJECT_VAL(S("print")));
/* Set up our module context. */
krk_startModule("<module>");
krk_startModule("__main__");
/* Attach a docstring so that we can interpret strings */
krk_attachNamedValue(&krk_currentThread.module->fields,"__doc__", NONE_VAL());
@ -24,7 +24,7 @@ int main(int argc, char * argv[]) {
int retval = 0;
if (argc > 1) {
KrkValue result = krk_interpret(argv[1], 0, "<stdin>","<stdin>");
KrkValue result = krk_interpret(argv[1], "<stdin>");
if (!IS_NONE(result)) {
if (IS_INTEGER(result)) {
retval = AS_INTEGER(result);

View File

@ -99,7 +99,7 @@ static int runSimpleRepl(void) {
}
FREE_ARRAY(char *, lines, lineCapacity);
if (valid) {
KrkValue result = krk_interpret(allData, 0, "<module>","<stdin>");
KrkValue result = krk_interpret(allData, "<stdin>");
if (!IS_NONE(result)) {
KrkClass * type = krk_getType(result);
const char * formatStr = " \033[1;30m=> %s\033[0m\n";