public void Execute(HassiumModule module, string[] args, StackFrame.Frame frame = null)
        {
            Stack = new Stack<HassiumObject>();
            StackFrame = new StackFrame();
            CallStack = new Stack<string>();
            Handlers = new Stack<HassiumExceptionHandler>();
            ExceptionReturns = new Dictionary<HassiumMethod, int>();
            Globals = new Dictionary<string, HassiumObject>();
            CurrentModule = module;
            importGlobals();
            importInitials();

            HassiumList parameters = new HassiumList(new HassiumObject[0]);
            foreach (string arg in args)
                parameters.add(this, new HassiumString(arg));

            if (frame != null)
                StackFrame.Frames.Push(frame);
            else
                StackFrame.PushFrame();
            module.Attributes["main"].Invoke(this, parameters);
            StackFrame.PopFrame();
        }
Beispiel #2
0
        public HassiumModule Compile(AstNode ast, SymbolTable table)
        {
            this.table = table;
            module = new HassiumModule();
            method = new HassiumMethod();

            var globalParent = new HassiumClass();
            foreach (AstNode child in ast.Children)
            {
                if (child is FuncNode)
                {
                    compileFunc(child as FuncNode);
                    method.Parent = new HassiumClass();
                    if (module.Attributes.ContainsKey(method.Name))
                    {
                        if (module.Attributes[method.Name] is HassiumMultiFunc)
                            ((HassiumMultiFunc)module.Attributes[method.Name]).Methods.Add(method);
                        else if (module.Attributes[method.Name] is HassiumMethod)
                            module.Attributes[method.Name] = new HassiumMultiFunc(new List<HassiumMethod>() { method, (HassiumMethod)module.Attributes[method.Name] });
                    }
                    else
                        module.Attributes.Add(method.Name, method);
                }
                else if (child is ClassNode)
                {
                    var clazz = compileClass(child as ClassNode);
                    clazz.Parent = globalParent;
                    module.Attributes.Add(clazz.Name, clazz);
                }
                else if (child is ExpressionStatementNode)
                {
                    if (child.Children[0] is BinaryOperationNode)
                    {
                        var binop = child.Children[0] as BinaryOperationNode;
                        if (binop.BinaryOperation == BinaryOperation.Assignment)
                        {
                            string variable = ((IdentifierNode)binop.Left).Identifier;
                            table.AddGlobalSymbol(variable);
                            var temp = method;
                            method = new HassiumMethod();
                            method.Name = variable;
                            method.SourceRepresentation = string.Format("set_{0}", variable);
                            binop.Right.Visit(this);
                            method.Emit(child.SourceLocation, InstructionType.Return);
                            module.InitialVariables.Add(table.GetGlobalSymbol(variable), method);
                            method = temp;
                        }
                    }
                }
                else if (child is TraitNode || child is PropertyNode || child is UseNode || child is EnumNode)
                    child.Visit(this);
            }
            preformInherits(module);
            return module;
        }