public HassiumConfig Parse(string[] args) { this.args = args; var config = new HassiumConfig(); if (args.Length <= 0) { displayHelp(); } for (position = 0; position < args.Length; position++) { switch (args[position].ToLower()) { case "-d": case "--dev": config.Dev = true; break; case "-h": case "--help": displayHelp(); break; case "-p": case "--pkg": config.HassiumRunType = HassiumRunType.PackageManager; config.Action = expectData("[ACTION]"); config.Package = expectData("[PKGNAME]"); break; case "-r": case "--repl": config.HassiumRunType = HassiumRunType.REPL; break; case "-s": case "--suppress-warns": config.SuppressWarnings = true; break; default: config.FilePath = args[position++]; List <string> pargs = new List <string>(); pargs.Add(config.FilePath); for (; position < args.Length; position++) { pargs.Add(args[position]); } config.Args = pargs.ToArray(); break; } } return(config); }
public static void Run(HassiumConfig config) { HassiumModule module = new HassiumModule(); module.AddAttribute("__global__", new HassiumClass("__global__")); var attribs = new Dictionary <string, HassiumObject>(); VirtualMachine vm = new VirtualMachine(module); while (true) { Console.Write("(1)> "); string code = Console.ReadLine(); try { // Read var tokens = new Scanner().Scan("stdin", code); // If we missed a closing ), }, or ], keep reading and appending lines until the code is good. int line = 2; while (countOpenTokens(tokens) > countCloseTokens(tokens)) { Console.Write("({0})> ", line++); string temp = Console.ReadLine(); foreach (var token in new Scanner().Scan("stdin", temp)) { tokens.Add(token); } code += temp + System.Environment.NewLine; } var ast = new Parser().Parse(tokens); module = new HassiumCompiler(config.SuppressWarnings).Compile(ast, module); // Import foreach (var attrib in module.BoundAttributes["__global__"].BoundAttributes) { if (attribs.ContainsKey(attrib.Key)) { attribs.Remove(attrib.Key); } attribs.Add(attrib.Key, attrib.Value); } module.BoundAttributes["__global__"].BoundAttributes = attribs; var init = (module.BoundAttributes["__global__"].BoundAttributes["__init__"] as HassiumMethod); init.Module = module; // Eval vm.ImportGlobals(); var ret = vm.ExecuteMethod(init); // PrintLine if (!(ret is HassiumNull)) { Console.WriteLine(ret.ToString(vm, ret, vm.CurrentSourceLocation).String); } else { Console.WriteLine(); } } catch (CompilerException ex) { Console.WriteLine(ex.Message); if (config.Dev) { Console.WriteLine(ex); } } catch (ParserException ex) { Console.WriteLine(ex.Message); if (config.Dev) { Console.WriteLine(ex); } } catch (ScannerException ex) { Console.WriteLine(ex.Message); if (config.Dev) { Console.WriteLine(ex); } } catch (UnhandledException ex) { Console.WriteLine("Unhandled Exception:"); Console.WriteLine(ex.Message); Console.WriteLine("\nNear:"); ex.SourceLocation.PrintLocation(new System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(code))); Console.WriteLine(ex.CallStack); } } }
public static void ExecuteConfig(HassiumConfig config) { if (config.HassiumRunType == HassiumRunType.Code) { try { VirtualMachine vm = new VirtualMachine(); var module = HassiumCompiler.CompileModuleFromFilePath(System.IO.Path.GetFullPath(config.FilePath), config.SuppressWarnings); HassiumList hargs = new HassiumList(new HassiumObject[0]); foreach (var arg in config.Args) { hargs.Values.Add(new HassiumString(arg)); } vm.Execute(module, hargs); } catch (CompilerException ex) { Console.WriteLine(ex.Message); if (config.Dev) { Console.WriteLine(ex); } } catch (ParserException ex) { Console.WriteLine(ex.Message); if (config.Dev) { Console.WriteLine(ex); } } catch (ScannerException ex) { Console.WriteLine(ex.Message); if (config.Dev) { Console.WriteLine(ex); } } catch (UnhandledException ex) { Console.WriteLine("Unhandled Exception:"); Console.WriteLine(ex.Message); Console.WriteLine("\nNear:"); ex.SourceLocation.PrintLocation(); Console.WriteLine(ex.CallStack); Environment.Exit(0); } } else if (config.HassiumRunType == HassiumRunType.PackageManager) { var mgr = new HassiumPackageManager(); bool status; switch (config.Action.ToLower()) { case "check": status = mgr.CheckInstalled(config.Package); if (status) { Console.WriteLine("Package '{0}' is installed! Remove it using Hassium.exe --pkg uninstall [PKGNAME]", config.Package); } else { Console.WriteLine("Package '{0}' is not installed! Install it using Hassium.exe --pkg install [PKGNAME]", config.Package); } break; case "install": status = mgr.InstallPackage(config.Package); if (status) { Console.WriteLine("Successfully installed '{0}'. Include ```use * from {0}/<mod>``` to use.", config.Package); } else { Console.WriteLine("Installation failed! Is the package name correct?"); } break; case "uninstall": status = mgr.UninstallPackage(config.Package); if (status) { Console.WriteLine("Successfully uninstalled '{0}'.", config.Package); } else { Console.WriteLine("Uninstall failed! Is the package already uninstalled?"); } break; } } else if (config.HassiumRunType == HassiumRunType.REPL) { HassiumREPL.Run(config); } }