示例#1
0
        private HassiumClass compileClass(ClassNode node)
        {
            if (!module.ConstantPool.ContainsValue(node.Name))
                module.ConstantPool.Add(node.Name.GetHashCode(), node.Name);
            HassiumClass clazz = new HassiumClass();
            clazz.Name = node.Name;
            clazz.Inherits = node.Inherits;
            clazz.IsPrivate = node.IsPrivate;
            clazz.TypeDefinition = new HassiumTypeDefinition(clazz.Name);
            clazz.AddType(clazz.TypeDefinition);

            foreach (AstNode child in node.Body.Children)
            {
                if (child is FuncNode)
                {
                    compileFunc(child as FuncNode);
                    method.Parent = clazz;
                    if (clazz.Attributes.ContainsKey(method.Name))
                    {
                        if (clazz.Attributes[method.Name] is HassiumMultiFunc)
                            ((HassiumMultiFunc)clazz.Attributes[method.Name]).Methods.Add(method);
                        else if (clazz.Attributes[method.Name] is HassiumMethod)
                            clazz.Attributes[method.Name] = new HassiumMultiFunc(new List<HassiumMethod>() { method, (HassiumMethod)clazz.Attributes[method.Name] });
                    }
                    else
                        clazz.AddAttribute(method.Name, method);
                }
                else if (child is ClassNode)
                    clazz.AddAttribute(((ClassNode)child).Name, compileClass(child as ClassNode));
                else if (child is EnumNode)
                    clazz.AddAttribute(((EnumNode)child).Name, compileEnum(child as EnumNode));
                else if (child is PropertyNode)
                    clazz.AddAttribute(((PropertyNode)child).Variable, compileProperty(child as PropertyNode, clazz));
            }
            return clazz;
        }
示例#2
0
        private HassiumProperty compileProperty(PropertyNode node, HassiumClass parent)
        {
            var temp = method;
            method = new HassiumMethod();
            method.Name = string.Format("get_{0}", node.Variable);
            method.IsPrivate = node.IsPrivate;
            method.SourceRepresentation = string.Format("{0} ()", method.Name);
            table.PushScope();
            node.GetBody.Visit(this);
            table.PopScope();
            HassiumMethod getBody = method;
            getBody.Parent = parent;
            getBody.ReturnType = "";
            method = new HassiumMethod();
            method.Name = string.Format("set_{0}", node.Variable);
            method.IsPrivate = node.IsPrivate;
            method.SourceRepresentation = string.Format("{0} (value)", method.Name);
            table.PushScope();
            if (!table.ContainsSymbol("value"))
                table.AddSymbol("value");
            method.Parameters.Add(new FuncParameter("value"), table.GetSymbol("value"));
            node.SetBody.Visit(this);
            table.PopScope();
            HassiumMethod setBody = method;
            setBody.Parent = parent;
            setBody.ReturnType = "";
            HassiumProperty property = new HassiumProperty(getBody, setBody);
            property.Parent = parent;
            method = temp;

            return property;
        }
示例#3
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;
        }