Beispiel #1
0
        private HassiumModule resolveModuleFromDll(string path)
        {
            var module = new HassiumModule();

            var ass = Assembly.LoadFrom(path);

            foreach (var type in ass.GetTypes())
            {
                if (type.IsSubclassOf(typeof(InternalModule)))
                {
                    foreach (var pair in ((InternalModule)Activator.CreateInstance(type)).BoundAttributes)
                    {
                        module.AddAttribute(pair.Key, pair.Value);
                    }
                }
            }

            return(module);
        }
Beispiel #2
0
        public void Accept(UseFromNode node)
        {
            string        path = node.Module.Replace(".", "/").Replace("\\", "/");
            HassiumObject mod;

            if (InternalModule.InternalModules.ContainsKey(path))
            {
                mod = InternalModule.InternalModules[path];
            }
            else
            {
                mod = resolveModuleByPath(node.SourceLocation, path);
            }

            if (mod is HassiumModule && !suppressWarns)
            {
                (mod as HassiumModule).DisplayWarnings();
            }

            // Hassium source code imports will contain __global__, this is where
            // we have to look for the desired classes.
            if (mod.ContainsAttribute("__global__"))
            {
                var globalClass = mod.BoundAttributes["__global__"];
                // Copy over the __init__ method into ours
                foreach (var attrib in globalClass.BoundAttributes)
                {
                    if (attrib.Key == "__init__")
                    {
                        foreach (var instruction in (attrib.Value as HassiumMethod).Instructions)
                        {
                            methodStack.Peek().Instructions.Add(instruction);
                        }
                    }
                }
                // Import *everything*
                if (node.Class == "*")
                {
                    // use * is bad
                    module.AddWarning(node.SourceLocation, "Importing '*' is bad practice!");

                    foreach (var attrib in globalClass.BoundAttributes)
                    {
                        if (!classStack.Peek().ContainsAttribute(attrib.Key))
                        {
                            var value = attrib.Value.Clone() as HassiumObject;
                            value.Parent = classStack.Peek();
                            classStack.Peek().AddAttribute(attrib.Key, value);
                        }
                    }
                }
                else
                {
                    classStack.Peek().AddAttribute(node.Class, globalClass.GetAttribute(null, node.Class));
                }
            }
            else
            {
                if (node.Class == "*")
                {
                    // use * is bad
                    module.AddWarning(node.SourceLocation, "Importing '*' is bad practice!");
                    foreach (var attrib in mod.BoundAttributes)
                    {
                        if (!module.ContainsAttribute(attrib.Key))
                        {
                            var value = attrib.Value.Clone() as HassiumObject;
                            value.Parent = classStack.Peek();
                            if (!module.ContainsAttribute(attrib.Key))
                            {
                                module.AddAttribute(attrib.Key, value);
                            }
                        }
                    }
                }
                else if (!mod.ContainsAttribute(node.Class))
                {
                    throw new CompilerException(node.SourceLocation, "Could not find attribute '{0}' in module '{1}'", node.Class, node.Module);
                }
                else
                {
                    module.AddAttribute(node.Class, mod.GetAttribute(null, node.Class));
                }
            }
        }
Beispiel #3
0
        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);
                }
            }
        }