private bool ImportVariable(Obj moduleName, Obj variableName, out Obj result) { ObjModule module = GetModule(moduleName); if (module == null) { result = Obj.MakeString("Could not load module"); return(false); // Should only look up loaded modules } ObjString variable = variableName as ObjString; if (variable == null) { result = Obj.MakeString("Variable name must be a string"); return(false); } int variableEntry = module.Variables.FindIndex(v => v.Name == variable.ToString()); // It's a runtime error if the imported variable does not exist. if (variableEntry == -1) { result = Obj.MakeString(string.Format("Could not find a variable named '{0}' in module '{1}'.", variableName, moduleName)); return(false); } result = module.Variables[variableEntry].Container; return(true); }
internal int DefineVariable(ObjModule module, string name, Obj c) { if (module == null) { module = GetCoreModule(); } if (module.Variables.Count == ObjModule.MaxModuleVars) { return(-2); } // See if the variable is already explicitly or implicitly declared. int symbol = module.Variables.FindIndex(m => m.Name == name); if (symbol == -1) { // Brand new variable. module.Variables.Add(new ModuleVariable { Name = name, Container = c }); symbol = module.Variables.Count - 1; } else if (module.Variables[symbol].Container == Obj.Undefined) { // Explicitly declaring an implicitly declared one. Mark it as defined. module.Variables[symbol].Container = c; } else { // Already explicitly declared. symbol = -1; } return(symbol); }
private ObjFiber LoadModule(Obj name, string source) { ObjModule module = GetModule(name); // See if the module has already been loaded. if (module == null) { module = new ObjModule(name as ObjString); // Store it in the VM's module registry so we don't load the same module // multiple times. _modules.Set(name, module); // Implicitly import the core module. ObjModule coreModule = GetCoreModule(); foreach (ModuleVariable t in coreModule.Variables) { DefineVariable(module, t.Name, t.Container); } } ObjFn fn = Compiler.Compile(this, module, name.ToString(), source, true); if (fn == null) { // TODO: Should we still store the module even if it didn't compile? return(null); } ObjFiber moduleFiber = new ObjFiber(fn); // Return the fiber that executes the module. return(moduleFiber); }
public Obj FindVariable(string name) { ObjModule coreModule = GetCoreModule(); int symbol = coreModule.Variables.FindIndex(v => v.Name == name); return(coreModule.Variables[symbol].Container); }
public Obj FindVariable(string moduleName, string name) { ObjModule m = GetModuleByName(moduleName); if (m == null) { return(Obj.Undefined); } int symbol = m.Variables.FindIndex(v => v.Name == name); return(m.Variables[symbol].Container); }
// Execute [source] in the context of the core module. private InterpretResult LoadIntoCore(string source) { ObjModule coreModule = GetCoreModule(); ObjFn fn = Compiler.Compile(this, coreModule, "", source, true); if (fn == null) { return(InterpretResult.CompileError); } Fiber = new ObjFiber(fn); return(RunInterpreter() ? InterpretResult.Success : InterpretResult.RuntimeError); }
internal int DeclareVariable(ObjModule module, string name) { if (module == null) { module = GetCoreModule(); } if (module.Variables.Count == ObjModule.MaxModuleVars) { return(-2); } module.Variables.Add(new ModuleVariable { Name = name, Container = Obj.Undefined }); return(module.Variables.Count - 1); }
public WrenVM() { MethodNames = new List <string>(); ObjString name = new ObjString("core"); // Implicitly create a "core" module for the built in libraries. ObjModule coreModule = new ObjModule(name); _modules = new ObjMap(); _modules.Set(Obj.Null, coreModule); CoreLibrary core = new CoreLibrary(this); core.InitializeCore(); // Load in System functions Meta.LoadLibrary(this); }
public WrenVM(Action <string> write, Action <string> error) { Write = write ?? (_ => Console.WriteLine(_)); Error = error ?? (_ => Console.Error.WriteLine(_)); MethodNames = new List <string>(); ObjString name = new ObjString("core"); // Implicitly create a "core" module for the built in libraries. ObjModule coreModule = new ObjModule(name); _modules = new ObjMap(); _modules.Set(Obj.Null, coreModule); CoreLibrary core = new CoreLibrary(this); core.InitializeCore(); // Load in System functions Meta.LoadLibrary(this); }
static bool Eval(WrenVM vm, Obj[] args, int stackStart) { if (args[stackStart + 1] is ObjString) { // Eval the code in the module where the calling function was defined. Obj callingFn = vm.Fiber.GetFrame().Fn; ObjModule module = (callingFn is ObjFn) ? ((ObjFn)callingFn).Module : ((ObjClosure)callingFn).Function.Module; // Compile it. ObjFn fn = Compiler.Compile(vm, module, "", args[stackStart + 1].ToString(), false); if (fn == null) { vm.Fiber.Error = Obj.MakeString("Could not compile source code."); return(false); } // TODO: Include the compile errors in the runtime error message. // Create a fiber to run the code in. ObjFiber evalFiber = new ObjFiber(fn) { Caller = vm.Fiber }; // Switch to the fiber. args[stackStart] = evalFiber; return(false); } vm.Fiber.Error = Obj.MakeString("Source code must be a string."); return(false); }
// Execute [source] in the context of the core module. private IEnumerator LoadIntoCore(string source, ResultRef resultRef) { ObjModule coreModule = GetCoreModule(); ObjFn fn = Compiler.Compile(this, coreModule, "", source, true); if (fn == null) { resultRef.value = InterpretResult.CompileError; yield break; } Fiber = new ObjFiber(fn); var succeeded = new SuccessRef(); var interpreter = RunInterpreter(succeeded); while (interpreter.MoveNext()) { yield return(interpreter.Current); } resultRef.value = succeeded.value ? InterpretResult.Success : InterpretResult.RuntimeError; }