Execute input in the REPL.
Guesses whether the input is an expression or statement and handles it appropriately. Finally, after over a year, the Wren REPL automatically prints "3" if you type in "1 + 2". \o/
This commit is contained in:
@@ -15,8 +15,12 @@ void metaCompile(WrenVM* vm)
|
||||
ObjClosure* caller = vm->fiber->frames[vm->fiber->numFrames - 2].closure;
|
||||
ObjModule* module = caller->fn->module;
|
||||
|
||||
const char* source = wrenGetSlotString(vm, 1);
|
||||
bool isExpression = wrenGetSlotBool(vm, 2);
|
||||
bool printErrors = wrenGetSlotBool(vm, 3);
|
||||
|
||||
// Compile it.
|
||||
ObjFn* fn = wrenCompile(vm, module, wrenGetSlotString(vm, 1), false);
|
||||
ObjFn* fn = wrenCompile(vm, module, source, isExpression, printErrors);
|
||||
|
||||
// Return the result. We can't use the public API for this since we have a
|
||||
// bare ObjFn.
|
||||
@@ -45,7 +49,7 @@ WrenForeignMethodFn wrenMetaBindForeignMethod(WrenVM* vm,
|
||||
// There is only one foreign method in the meta module.
|
||||
ASSERT(strcmp(className, "Meta") == 0, "Should be in Meta class.");
|
||||
ASSERT(isStatic, "Should be static.");
|
||||
ASSERT(strcmp(signature, "compile_(_)") == 0, "Should be compile method.");
|
||||
ASSERT(strcmp(signature, "compile_(_,_,_)") == 0, "Should be compile method.");
|
||||
|
||||
return metaCompile;
|
||||
}
|
||||
|
||||
@@ -2,12 +2,17 @@ class Meta {
|
||||
static eval(source) {
|
||||
if (!(source is String)) Fiber.abort("Source code must be a string.")
|
||||
|
||||
var fn = compile_(source)
|
||||
var fn = compile_(source, false, false)
|
||||
// TODO: Include compile errors.
|
||||
if (fn == null) Fiber.abort("Could not compile source code.")
|
||||
|
||||
Fiber.new(fn).call()
|
||||
}
|
||||
|
||||
foreign static compile_(source)
|
||||
static compileExpression(source) {
|
||||
if (!(source is String)) Fiber.abort("Source code must be a string.")
|
||||
return compile_(source, true, true)
|
||||
}
|
||||
|
||||
foreign static compile_(source, isExpression, printErrors)
|
||||
}
|
||||
|
||||
@@ -4,12 +4,17 @@ static const char* metaModuleSource =
|
||||
" static eval(source) {\n"
|
||||
" if (!(source is String)) Fiber.abort(\"Source code must be a string.\")\n"
|
||||
"\n"
|
||||
" var fn = compile_(source)\n"
|
||||
" var fn = compile_(source, false, false)\n"
|
||||
" // TODO: Include compile errors.\n"
|
||||
" if (fn == null) Fiber.abort(\"Could not compile source code.\")\n"
|
||||
"\n"
|
||||
" Fiber.new(fn).call()\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" foreign static compile_(source)\n"
|
||||
" static compileExpression(source) {\n"
|
||||
" if (!(source is String)) Fiber.abort(\"Source code must be a string.\")\n"
|
||||
" return compile_(source, true, true)\n"
|
||||
" }\n"
|
||||
"\n"
|
||||
" foreign static compile_(source, isExpression, printErrors)\n"
|
||||
"}\n";
|
||||
|
||||
Reference in New Issue
Block a user