Ejemplo n.º 1
0
        public Item Compile(ByteCodeGenerator generator)
        {
            //TODO
            var item = new ExpressionCompiler(node.Child.Child).Compile(generator);

            if (node is UnaryNode.PlusNode)
            {
                return item.Load();
            }
            if (node is UnaryNode.MinusNode)
            {
                throw new NotImplementedException();
            }

            if (node is UnaryNode.PreIncNode)
            {
                return CompilePreOp(generator, item);
            }
            if (node is UnaryNode.PreDecNode)
            {
                return CompilePreOp(generator, item);
            }

            if (node is UnaryNode.PostIncNode)
            {
                return CompilePostOp(generator, item);
            }
            if (node is UnaryNode.PostDecNode)
            {
                return CompilePostOp(generator, item);
            }

            throw new NotImplementedException();
        }
        public Item Compile(ByteCodeGenerator generator)
        {
            Chain thenExit = null;

            var item = new ConditionCompiler(node.Condition).Compile(generator);
            var elseChain = item.JumpFalse();

            Item x = null;
            if (!item.IsFalse())
            {
                generator.ResolveChain(item.TrueJumps);

                x = new ExpressionCompiler(node.ThenExpression).Compile(generator).Load();

                thenExit = generator.Branch(OpCodeValue.@goto);
            }
            if (elseChain != null)
            {
                generator.ResolveChain(elseChain);

                x = new ExpressionCompiler(node.ElseExpression).Compile(generator).Load();
            }
            generator.ResolveChain(thenExit);

            //var type = tb.Type.FindCommonType(fb.Type);
            var type = x.Type;

            return new StackItem(generator, type);
        }
Ejemplo n.º 3
0
        public static void AppendStrings(ByteCodeGenerator generator, DefinedType sb, ExpressionNode node)
        {
            if (node is AdditiveNode)
            {
                var addNode = node as AdditiveNode;

                generator.Kill();
                var addType = new AdditiveCompiler(addNode).Compile(generator).Type;
                generator.Revive();

                if (addType.Name == BuiltinTypes.String.Name)
                {
                    AppendStrings(generator, sb, addNode.LeftChild.Child);
                    AppendStrings(generator, sb, addNode.RightChild.Child);

                    return;
                }
            }

            var item = new ExpressionCompiler(node).Compile(generator);

            var appendMethod = sb.FindMethod(generator, "append", new List<Type> { item.Type });
            if (appendMethod == null) throw new InvalidOperationException();

            item.Load();
            new MemberItem(generator, appendMethod, false).Invoke();
        }
        public Item Compile(ByteCodeGenerator generator)
        {
            var item = new ExpressionCompiler(expression).Compile(generator);

            if (type == null) return item;

            if (type.Primitive && item.Type.Primitive)
            {
                return item.Coerce(type);
            }

            if (item.Type.IsAssignableTo(type))
            {
                return item;
            }

            if (item.Type.Primitive && !type.Primitive)
            {
                // box!
                var primative = item.Type as PrimativeTypes.PrimativeType;

                return primative.Box(generator, item, type as DefinedType);
            }
            if (!item.Type.Primitive && type.Primitive)
            {
                // unbox!
                var primative = type as PrimativeTypes.PrimativeType;

                return primative.Unbox(generator, item);
            }

            throw new InvalidOperationException();
        }
Ejemplo n.º 5
0
        private Item CompileAssign(ByteCodeGenerator generator)
        {
            var lhs = new ExpressionCompiler(node.Left).Compile(generator);
            var type = ClassLocator.Find(lhs.Type, generator.Manager.Imports);

            new TranslationCompiler(node.Right, type).Compile(generator).Load();

            return new AssignItem(generator, lhs);
        }
Ejemplo n.º 6
0
        private Item CompileAssignOp(ByteCodeGenerator generator)
        {
            var lType = new TranslateNode { Child = node.Left }.GetType(generator, false, true);
            var rType = node.Right.GetType(generator, false, true);

            var type = lType.FindCommonType(rType);

            Item lhs;
            if (lType.Name == BuiltinTypes.String.Name)
            {
                var sb = BuiltinTypes.StringBuilder;

                AdditiveCompiler.MakeStringBuffer(generator, sb);

                lhs = new ExpressionCompiler(node.Left).Compile(generator);
                if (lhs.Width() > 0)
                {
                    generator.Emit(OpCodeValue.dup_x1 + (byte)(3 * (lhs.Width() - 1)));
                }

                lhs.Load();

                AdditiveCompiler.AppendStrings(generator, sb, node.Left);
                AdditiveCompiler.AppendStrings(generator, sb, node.Right.Child);

                AdditiveCompiler.BufferToString(generator, sb);
            }
            else
            {
                lhs = new ExpressionCompiler(node.Left).Compile(generator);
                lhs.Duplicate();

                lhs.Coerce(type).Load();

                new TranslationCompiler(node.Right, type).Compile(generator).Load();
                if (node is AssignmentNode.AddAssignNode)
                {
                    AdditiveCompiler.CompileAddition(generator, type);
                }
                else if (node is AssignmentNode.MinusAssignNode)
                {
                    AdditiveCompiler.CompileSubtraction(generator, type);
                }
                else
                {
                    throw new NotImplementedException();
                }
            }

            return new AssignItem(generator, lhs);
        }
Ejemplo n.º 7
0
        public void Compile(ByteCodeGenerator generator)
        {
            Item item = null;

            if (node is ExpressionNode)
            {
                item = new ExpressionCompiler(node as ExpressionNode).Compile(generator);
            }
            else if (node is AssertNode)
            {
                item = new AssertCompiler(node as AssertNode).Compile(generator);
            }
            else if (node is IfNode)
            {
                new IfCompiler(node as IfNode).Compile(generator);
            }
            else if (node is ForNode)
            {
                new ForCompiler(node as ForNode).Compile(generator);
            }
            else if (node is ForEachNode)
            {
                item = new ForEachCompiler(node as ForEachNode).Compile(generator);
            }
            else if (node is WhileNode)
            {
                new WhileCompiler(node as WhileNode).Compile(generator);
            }
            else if (node is DoNode)
            {
                item = new DoCompiler(node as DoNode).Compile(generator);
            }
            else if (node is TryNode)
            {
                item = new TryCompiler(node as TryNode).Compile(generator);
            }
            else if (node is SwitchNode)
            {
                item = new SwitchCompiler(node as SwitchNode).Compile(generator);
            }
            else if (node is SynchronizedNode)
            {
                item = new SynchronizedCompiler(node as SynchronizedNode).Compile(generator);
            }
            else if (node is ReturnNode)
            {
                item = new ReturnCompiler(node as ReturnNode).Compile(generator);
            }
            else if (node is ThrowNode)
            {
                item = new ThrowCompiler(node as ThrowNode).Compile(generator);
            }
            else if (node is BreakNode)
            {
                item = new BreakCompiler(node as BreakNode).Compile(generator);
            }
            else if (node is ContinueNode)
            {
                item = new ContinueCompiler(node as ContinueNode).Compile(generator);
            }
            else if (node is LabelNode)
            {
                item = new LabelCompiler(node as LabelNode).Compile(generator);
            }
            else if (node is MethodTree)
            {
                new BlockCompiler(node as MethodTree).Compile(generator);
            }
            else
            {
                throw new NotImplementedException();
            }

            if (item != null)
            {
                item.Drop();
            }
        }