public ParserState(NodeParent root, string script)
 {
     this.RootNode = root;
     this.CurNode  = root;
     _source       = script;
     _index        = 0;
 }
Exemple #2
0
        public void SkillInit(NodeParent node)
        {
            DrawSkill(node);

            NodeParent neightboor;

            for (int j = 0; j < node.GetNeightboor.Count; j++)
            {
                neightboor = node.GetNeightboor[j];
                if (node.GetNeightboor.Exists(x => x.ID == neightboor.ID) && !(node.connectedNeighboor.Exists(x => x.ID == neightboor.ID)))
                {
                    DrawConnection((neightboor.GetActivate || node.GetActivate) ? Color.GreenYellow : Color.Gray, node.menuPos, neightboor.menuPos, node, neightboor);
                    node.connectedNeighboor.Add(neightboor);
                    neightboor.connectedNeighboor.Add(node);
                }
            }

            int listSize = allConnection.Count;

            for (int i = 0; i < listSize; i++)
            {
                backGround.Append(allConnection[i]);
            }
            listSize = allBasePanel.Count;
            for (int i = 0; i < listSize; i++)
            {
                backGround.Append(allBasePanel[i]);
            }
        }
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            if (node is IsDefinedExpression)
            {
                var ide = (IsDefinedExpression)node;
                switch(ide.Expression.NodeType)
                {
                    case NodeTypes.MethodCall:
                        compiler.AppendLine("$_stack[] = F_TrueClass::__from_bool(function_exists('{0}'));", Mangling.RubyMethodToPHP(((MethodCall)ide.Expression).MethodName));
                        return;
                    case NodeTypes.ConstantVariable:
                        compiler.AppendLine("$_stack[] = F_TrueClass::__from_bool(class_exists('{0}'));", Mangling.RubyMethodToPHP(((ConstantVariable)ide.Expression).Name));
                        return;
                    case NodeTypes.ClassVariable:
                    case NodeTypes.GlobalVariable:
                    case NodeTypes.InstanceVariable:
                    case NodeTypes.LocalVariable:
                        compiler.AppendLine("$_stack[] = F_TrueClass::__from_bool(isset({0}));", ((Variable)ide.Expression).ToPHPVariable());
                        return;
                    default:
                        throw new FructoseCompileException("Not supported yet: defined?( " + ide.Expression.NodeType.ToString() + " )", ide.Expression);
                }

            }
            else
                throw new FructoseCompileException("Not supported yet: " + node.ToString(), node);
        }
Exemple #4
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            compiler.AppendLine("while(true)");
            compiler.AppendLine("{");
            compiler.Indent();

            if(((WhileLoopExpression)node).IsPostTest)
            {
                foreach(var stmt in ((WhileLoopExpression)node).Statements)
                    compiler.CompileNode(stmt, parent.CreateChild(node));
            }

            if (((WhileLoopExpression)node).IsWhileLoop)
                compiler.CompileNode(((WhileLoopExpression)node).Condition, parent.CreateChild(node));
            else
                compiler.CompileNode(new NotExpression(((WhileLoopExpression)node).Condition, node.Location), parent.CreateChild(node));

            compiler.AppendLine("$_cond = array_pop($_stack);");
            compiler.AppendLine("if(get_class($_cond) === 'F_NilClass' || get_class($_cond) === 'F_FalseClass' || is_subclass_of($_cond, 'F_NilClass') || is_subclass_of($_cond, 'F_FalseClass'))");
            compiler.AppendLine("{");
            compiler.Indent();
            compiler.AppendLine("break;");
            compiler.Dedent();
            compiler.AppendLine("}");

            if (!((WhileLoopExpression)node).IsPostTest)
            {
                foreach (var stmt in ((WhileLoopExpression)node).Statements)
                    compiler.CompileNode(stmt, parent.CreateChild(node));
            }

            compiler.Dedent();
            compiler.AppendLine("}");
        }
Exemple #5
0
 private void OnRightClickNode(UIMouseEvent evt, UIElement listeningElement, NodeParent node)
 {
     for (int i = 0; i < 5; i++)
     {
         OnClickNode(evt, listeningElement, node);
     }
 }
Exemple #6
0
        public void DrawConnection(Color _color, Vector2 point1, Vector2 point2, NodeParent node, NodeParent neighboor)
        {
            float angle    = 0;
            float distance = 0;

            //angle = (float)(Math.Atan2(point2.Y- point1.Y, point2.X - point1.X) *(180f/Math.PI));
            angle    = (float)(Math.Atan2(point2.Y - point1.Y, point2.X - point1.X));
            distance = (float)Math.Sqrt(Math.Pow(point2.X - point1.X, 2) + Math.Pow(point2.Y - point1.Y, 2));
            Connection BG = new Connection(angle, distance, 10)
            {
                color = Color.DarkSlateGray
            };

            Connection connection = new Connection(angle, distance, 6)
            {
                color = _color,
            };

            BG.basePos         = new Vector2(point1.X + SKILL_SIZE * 0.5f, point1.Y + SKILL_SIZE * 0.5f);
            connection.basePos = new Vector2(point1.X + SKILL_SIZE * 0.5f, point1.Y + SKILL_SIZE * 0.5f);
            BG.bg                = true;
            BG.neighboor         = neighboor;
            BG.node              = node;
            connection.neighboor = neighboor;
            connection.node      = node;
            allConnection.Add(BG);
            allConnection.Add(connection);
        }
Exemple #7
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var pa = (ParallelAssignmentExpression)node;
            if (pa.Left.HasUnsplattedValue)
                throw new FructoseCompileException("Unsplats not yet supported.", node);

            if (pa.Right.Length == 1)
            {
                compiler.CompileNode(pa.Right[0], parent.CreateChild(node));
                compiler.AppendLine("if(get_class($_stack[count($_stack)-1]) !== 'F_Array')");
                compiler.Indent();
                compiler.AppendLine("$_stack[] = F_Array::__from_array(array(array_pop($_stack)));");
                compiler.Dedent();
            }
            else
            {
                compiler.AppendLine("$ptmp = array();");
                foreach (var rval in pa.Right)
                {
                    compiler.CompileNode(rval, parent.CreateChild(node));
                    compiler.AppendLine("$ptmp[] = array_pop($_stack);");
                }
                compiler.AppendLine("$_stack[] = F_Array::__from_array($ptmp);");
            }

            var sb = new StringBuilder();
            foreach (var lval in pa.Left.LeftValues)
                sb.Append(SimpleAssignment.assignmentVar(lval, parent) + ",");
            compiler.AppendLine("@list({0}) = array_pop($_stack)->__ARRAY;", sb.ToString());

            foreach (var lval in pa.Left.LeftValues.Reverse())
                compiler.AppendLine("if({0} === NULL) {0} = new F_NilClass;", SimpleAssignment.assignmentVar(lval, parent));
        }
Exemple #8
0
        public override void Compile(Compiler compiler, IronRuby.Compiler.Ast.Node node, NodeParent parent)
        {
            var sae = (SimpleAssignmentExpression)node;

            // substitute a method call of []= rather than the crap that ironruby's parser produces:
            switch (sae.Left.NodeType)
            {
                case NodeTypes.ArrayItemAccess:
                    var aia = (ArrayItemAccess)sae.Left;
                    compiler.CompileNode(new MethodCall(aia.Array, "[]=", new Arguments(aia.Arguments.Expressions.Concat(new[] { sae.Right }).ToArray()), sae.Location), parent.CreateChild(node));
                    return;
                case NodeTypes.AttributeAccess:
                    var aa = (AttributeAccess)sae.Left;
                    compiler.CompileNode(new MethodCall(aa.Qualifier, aa.Name, new Arguments(sae.Right), aa.Location), parent.CreateChild(node));
                    return;
            }

            compiler.CompileNode(sae.Right, parent.CreateChild(node));
            if (sae.Operation != null)
            {
                compiler.AppendLine("$_stack[] = {0};", assignmentVar(sae.Left, parent));
                compiler.AppendLine("$_stack[] = array_pop($_stack)->{0}(NULL, array_pop($_stack));", Mangling.RubyMethodToPHP(sae.Operation));
            }
            compiler.AppendLine("{0} = $_stack[count($_stack)-1];", assignmentVar(sae.Left, parent));
        }
Exemple #9
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            if (((Literal)node).Value == null)
            {
                // nil
                compiler.AppendLine("$_stack[] = new F_NilClass;");
                return;
            }
            if (((Literal)node).Value is bool)
            {
                // True or False
                compiler.AppendLine("$_stack[] = new F_{0}Class;", ((Literal)node).Value.ToString());
                return;
            }
            if (((Literal)node).Value is string)
            {
                switch (((Literal)node).Value.ToString())
                {
                    case "self":
                        compiler.AppendLine("$_stack[] = $_locals->self;", ((Literal)node).Value.ToString());
                        break;
                    case "__FILE__":
                    case "__LINE__":
                        compiler.AppendLine("$_stack[] = F_String::__from_string({0});", ((Literal)node).Value.ToString());
                        break;
                }
                return;
            }

            compiler.AppendLine("$_stack[] = F_Number::__from_number({0});", ((Literal)node).Value.ToString());
        }
Exemple #10
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var body = (Body)node;

            if (body.RescueClauses != null)
            {
                for (int i = 0; i < body.RescueClauses.Count; i++)
                {
                    compiler.AppendLine("try");
                    compiler.AppendLine("{");
                    compiler.Indent();
                }
            }

            foreach (var stmt in body.Statements)
            {
                compiler.CompileNode(stmt, parent.CreateChild(node));
            }

            if (body.RescueClauses != null)
            {
                foreach (var rescue in body.RescueClauses)
                {
                    compiler.Dedent();
                    compiler.AppendLine("}");
                    compiler.AppendLine("catch(ErrorCarrier $err)");
                    compiler.AppendLine("{");
                    compiler.Indent();
                    StringBuilder clause = new StringBuilder();
                    bool          first  = true;
                    foreach (var t in rescue.Types)
                    {
                        if (t.NodeType != NodeTypes.ConstantVariable)
                        {
                            throw new FructoseCompileException("Type constant must be supplied in rescue expression", rescue);
                        }

                        if (!first)
                        {
                            clause.Append(" && ");
                        }
                        first = false;
                        clause.Append(string.Format("!is_a($err->val, '{0}')", Mangling.RubyIdentifierToPHP(((ConstantVariable)t).Name)));
                    }
                    compiler.AppendLine("if({0})", clause.ToString());
                    compiler.Indent();
                    compiler.AppendLine("throw $err;");
                    compiler.Dedent();
                    compiler.AppendLine("{0} = $err->val;", ((Variable)(rescue.Target ?? new GlobalVariable("!", rescue.Location))).ToPHPVariable());

                    foreach (var stmt in rescue.Statements)
                    {
                        compiler.CompileNode(stmt, parent.CreateChild(node));
                    }

                    compiler.Dedent();
                    compiler.AppendLine("}");
                }
            }
        }
Exemple #11
0
        public void SkillInit(NodeParent node)
        {
            DrawSkill(node);
            RPGPlayer  rPGPlayer = Main.player[Main.myPlayer].GetModPlayer <RPGPlayer>();
            NodeParent neightboor;

            for (int j = 0; j < node.GetNeightboor.Count; j++)
            {
                neightboor = node.GetNeightboor[j];
                if (node.GetNeightboor.Exists(x => x.ID == neightboor.ID) && !(node.connectedNeighboor.Exists(x => x.ID == neightboor.ID)))
                {
                    if (!(neightboor.GetNodeType == NodeType.LimitBreak && rPGPlayer.GetLevel() < 1000 || neightboor.GetNode.GetAscended && !skillTree.IsLimitBreak()))
                    {
                        DrawConnection((neightboor.GetActivate || node.GetActivate) ? Color.GreenYellow : Color.Gray, node.menuPos, neightboor.menuPos, node, neightboor);
                        node.connectedNeighboor.Add(neightboor);
                        neightboor.connectedNeighboor.Add(node);
                    }
                }
            }

            int listSize = allConnection.Count;

            for (int i = 0; i < listSize; i++)
            {
                backGround.Append(allConnection[i]);
            }
            listSize = allBasePanel.Count;
            for (int i = 0; i < listSize; i++)
            {
                backGround.Append(allBasePanel[i]);
            }
        }
Exemple #12
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var pmethod = parent.OfType<MethodDefinition>().SingleOrDefault();
            var isInBlock = pmethod != null && pmethod.Name.Contains("__lambda_");

            if (((ReturnStatement)node).Arguments == null || ((ReturnStatement)node).Arguments.Expressions.Count() == 0)
            {
                compiler.AppendLine("$_stack[] = new F_NilClass;");
            }
            else if (((ReturnStatement)node).Arguments.Expressions.Count() > 0)
            {
                compiler.CompileNode(((ReturnStatement)node).Arguments.Expressions.First(), parent.CreateChild(node));
            }
            else
            {
                compiler.CompileNode(new ArrayConstructor(((ReturnStatement)node).Arguments, ((ReturnStatement)node).Location), parent.CreateChild(node));
            }

            if (!isInBlock)
            {
                compiler.AppendLine("return array_pop($_stack);");
            }
            else
            {
                compiler.AppendLine("throw new ReturnFromBlock(array_pop($_stack));");
            }
        }
Exemple #13
0
 public Capture(NodeParent parent, Definition capname)
     : base(parent)
 {
     _capName        = capname;
     _capName.Source = this;
     _anonymous      = false;
 }
Exemple #14
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            if (((Literal)node).Value == null)
            {
                // nil
                compiler.AppendLine("$_stack[] = new F_NilClass;");
                return;
            }
            if (((Literal)node).Value is bool)
            {
                // True or False
                compiler.AppendLine("$_stack[] = new F_{0}Class;", ((Literal)node).Value.ToString());
                return;
            }
            if (((Literal)node).Value is string)
            {
                switch (((Literal)node).Value.ToString())
                {
                case "self":
                    compiler.AppendLine("$_stack[] = $_locals->self;", ((Literal)node).Value.ToString());
                    break;

                case "__FILE__":
                case "__LINE__":
                    compiler.AppendLine("$_stack[] = F_String::__from_string({0});", ((Literal)node).Value.ToString());
                    break;
                }
                return;
            }

            compiler.AppendLine("$_stack[] = F_Number::__from_number({0});", ((Literal)node).Value.ToString());
        }
Exemple #15
0
 public Options(NodeParent parent, Token token)
     : base(parent)
 {
     Tokens.Add(token);
     _onOptions  = token.ReadOnOptions();
     _offOptions = token.ReadOffOptions();
 }
Exemple #16
0
 public override void Compile(Compiler compiler, Node node, NodeParent parent)
 {
     var re = (RangeExpression)node;
     compiler.CompileNode(re.End, parent.CreateChild(node));
     compiler.CompileNode(re.Begin, parent.CreateChild(node));
     compiler.AppendLine("$_stack[] = F_Range::SF_new(NULL, array_pop($_stack), array_pop($_stack), new F_{0}Class);", re.IsExclusive.ToString());
 }
Exemple #17
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var pmethod   = parent.OfType <MethodDefinition>().SingleOrDefault();
            var isInBlock = pmethod != null && pmethod.Name.Contains("__lambda_");

            if (((ReturnStatement)node).Arguments == null || ((ReturnStatement)node).Arguments.Expressions.Count() == 0)
            {
                compiler.AppendLine("$_stack[] = new F_NilClass;");
            }
            else if (((ReturnStatement)node).Arguments.Expressions.Count() > 0)
            {
                compiler.CompileNode(((ReturnStatement)node).Arguments.Expressions.First(), parent.CreateChild(node));
            }
            else
            {
                compiler.CompileNode(new ArrayConstructor(((ReturnStatement)node).Arguments, ((ReturnStatement)node).Location), parent.CreateChild(node));
            }

            if (!isInBlock)
            {
                compiler.AppendLine("return array_pop($_stack);");
            }
            else
            {
                compiler.AppendLine("throw new ReturnFromBlock(array_pop($_stack));");
            }
        }
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            if (node is IsDefinedExpression)
            {
                var ide = (IsDefinedExpression)node;
                switch (ide.Expression.NodeType)
                {
                case NodeTypes.MethodCall:
                    compiler.AppendLine("$_stack[] = F_TrueClass::__from_bool(function_exists('{0}'));", Mangling.RubyMethodToPHP(((MethodCall)ide.Expression).MethodName));
                    return;

                case NodeTypes.ConstantVariable:
                    compiler.AppendLine("$_stack[] = F_TrueClass::__from_bool(class_exists('{0}'));", Mangling.RubyMethodToPHP(((ConstantVariable)ide.Expression).Name));
                    return;

                case NodeTypes.ClassVariable:
                case NodeTypes.GlobalVariable:
                case NodeTypes.InstanceVariable:
                case NodeTypes.LocalVariable:
                    compiler.AppendLine("$_stack[] = F_TrueClass::__from_bool(isset({0}));", ((Variable)ide.Expression).ToPHPVariable());
                    return;

                default:
                    throw new FructoseCompileException("Not supported yet: defined?( " + ide.Expression.NodeType.ToString() + " )", ide.Expression);
                }
            }
            else
            {
                throw new FructoseCompileException("Not supported yet: " + node.ToString(), node);
            }
        }
Exemple #19
0
 public Capture(NodeParent parent, int anonymous)
     : base(parent)
 {
     _capName        = new Definition(anonymous);
     _capName.Source = this;
     _anonymous      = true;
 }
Exemple #20
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var re = (RangeExpression)node;

            compiler.CompileNode(re.End, parent.CreateChild(node));
            compiler.CompileNode(re.Begin, parent.CreateChild(node));
            compiler.AppendLine("$_stack[] = F_Range::SF_new(NULL, array_pop($_stack), array_pop($_stack), new F_{0}Class);", re.IsExclusive.ToString());
        }
Exemple #21
0
        private Capture CreateCapture(NodeParent parent)
        {
            Capture capture = new Capture(_concatenate, _lexer.AnonymousCapCount);

            _slots.Assign(capture);
            _sequence.Add(capture);
            return(capture);
        }
Exemple #22
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var unless = (UnlessExpression)node;

            compiler.CompileNode(new IfExpression(new NotExpression(unless.Condition, unless.Condition.Location), unless.Statements, new List <ElseIfClause> {
                unless.ElseClause
            }, unless.Location), parent.CreateChild(node));
        }
Exemple #23
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var cond = (ConditionalExpression)node;

            compiler.CompileNode(new IfExpression(cond.Condition, new Statements(cond.TrueExpression), new List <ElseIfClause>()
            {
                new ElseIfClause(null, new Statements(cond.FalseExpression), cond.FalseExpression.Location)
            }, cond.Location), parent.CreateChild(node));
        }
Exemple #24
0
 public CharClass(NodeParent parent, bool neg)
 {
     Parent = parent;
     if (parent != null && parent.Type != NodeType.CharClass)
     {
         parent.AddChild(this);
     }
     _negative = neg;
 }
Exemple #25
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var ac = (ArrayConstructor)node;

            foreach (var exp in ac.Arguments.Expressions.Reverse())
                compiler.CompileNode(exp, parent.CreateChild(node));

            compiler.AppendLine("$_stack[] = F_Array::S__operator_arrayget(NULL" + string.Join("", ac.Arguments.Expressions.Select(e => ", array_pop($_stack)").ToArray()) + ");");
        }
Exemple #26
0
 public Range(NodeParent parent, Token start, Token subtract, Token end)
     : base(parent)
 {
     Tokens.Add(start);
     Tokens.Add(subtract);
     Tokens.Add(end);
     _start = start.ReadChar();
     _end   = end.ReadChar();
 }
Exemple #27
0
 public override void Compile(Compiler compiler, Node node, NodeParent parent)
 {
     YieldCall yield = (YieldCall)node;
     if(yield.Arguments != null)
         foreach (var arg in yield.Arguments.Expressions.Reverse())
         {
             compiler.CompileNode(arg, parent.CreateChild(node));
         }
     compiler.AppendLine("$_blockfn = $_locals->block;");
     compiler.AppendLine("$_stack[] = $_blockfn(NULL" + string.Join("", (yield.Arguments ?? new Arguments()).Expressions.Select(ex => ", array_pop($_stack)").ToArray()) + ");");
 }
Exemple #28
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var ac = (ArrayConstructor)node;

            foreach (var exp in ac.Arguments.Expressions.Reverse())
            {
                compiler.CompileNode(exp, parent.CreateChild(node));
            }

            compiler.AppendLine("$_stack[] = F_Array::S__operator_arrayget(NULL" + string.Join("", ac.Arguments.Expressions.Select(e => ", array_pop($_stack)").ToArray()) + ");");
        }
Exemple #29
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var cond = (ConditionalStatement)node;
            Expression cond_expr = cond.Condition;
            if(cond.IsUnless)
                cond_expr = new NotExpression(cond_expr, cond.Location);

            var elseif = cond.ElseStatement != null
                ? new List<ElseIfClause>() { new ElseIfClause(null, new Statements(cond.ElseStatement), cond.ElseStatement.Location) }
                : new List<ElseIfClause>();

            compiler.CompileNode(new IfExpression(cond_expr, new Statements(cond.Body), elseif, cond.Location), parent.CreateChild(node));
        }
Exemple #30
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var body = (Body)node;
            if (body.RescueClauses != null)
            {
                for(int i = 0; i < body.RescueClauses.Count; i++)
                {
                    compiler.AppendLine("try");
                    compiler.AppendLine("{");
                    compiler.Indent();
                }
            }

            foreach (var stmt in body.Statements)
                compiler.CompileNode(stmt, parent.CreateChild(node));

            if (body.RescueClauses != null)
            {
                foreach (var rescue in body.RescueClauses)
                {
                    compiler.Dedent();
                    compiler.AppendLine("}");
                    compiler.AppendLine("catch(ErrorCarrier $err)");
                    compiler.AppendLine("{");
                    compiler.Indent();
                    StringBuilder clause = new StringBuilder();
                    bool first = true;
                    foreach (var t in rescue.Types)
                    {
                        if (t.NodeType != NodeTypes.ConstantVariable)
                            throw new FructoseCompileException("Type constant must be supplied in rescue expression", rescue);

                        if (!first)
                            clause.Append(" && ");
                        first = false;
                        clause.Append(string.Format("!is_a($err->val, '{0}')", Mangling.RubyIdentifierToPHP(((ConstantVariable)t).Name)));
                    }
                    compiler.AppendLine("if({0})", clause.ToString());
                    compiler.Indent();
                    compiler.AppendLine("throw $err;");
                    compiler.Dedent();
                    compiler.AppendLine("{0} = $err->val;", ((Variable)(rescue.Target ?? new GlobalVariable("!", rescue.Location))).ToPHPVariable());

                    foreach (var stmt in rescue.Statements)
                        compiler.CompileNode(stmt, parent.CreateChild(node));

                    compiler.Dedent();
                    compiler.AppendLine("}");
                }
            }
        }
Exemple #31
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            YieldCall yield = (YieldCall)node;

            if (yield.Arguments != null)
            {
                foreach (var arg in yield.Arguments.Expressions.Reverse())
                {
                    compiler.CompileNode(arg, parent.CreateChild(node));
                }
            }
            compiler.AppendLine("$_blockfn = $_locals->block;");
            compiler.AppendLine("$_stack[] = $_blockfn(NULL" + string.Join("", (yield.Arguments ?? new Arguments()).Expressions.Select(ex => ", array_pop($_stack)").ToArray()) + ");");
        }
Exemple #32
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            compiler.CompileNode(((AndExpression)node).Left, parent.CreateChild(node));

            compiler.AppendLine("$_and_tmp = get_class($_stack[count($_stack)-1]);");
            compiler.AppendLine("if($_and_tmp !== 'F_NilClass' && $_and_tmp !== 'F_FalseClass')");
            compiler.AppendLine("{");
            compiler.Indent();

                compiler.CompileNode(((AndExpression)node).Right, parent.CreateChild(node));

            compiler.Dedent();
            compiler.AppendLine("}");
        }
Exemple #33
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            compiler.CompileNode(((AndExpression)node).Left, parent.CreateChild(node));

            compiler.AppendLine("$_and_tmp = get_class($_stack[count($_stack)-1]);");
            compiler.AppendLine("if($_and_tmp !== 'F_NilClass' && $_and_tmp !== 'F_FalseClass')");
            compiler.AppendLine("{");
            compiler.Indent();

            compiler.CompileNode(((AndExpression)node).Right, parent.CreateChild(node));

            compiler.Dedent();
            compiler.AppendLine("}");
        }
Exemple #34
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            if (parent.OfType <ClassDefinition>().Count() > 0)
            {
                throw new FructoseCompileException("Nested classes are not supported.", node);
            }

            if (((ClassDefinition)node).SuperClass != null && ((ClassDefinition)node).SuperClass.NodeType != NodeTypes.ConstantVariable)
            {
                throw new FructoseCompileException("Classes may only inherit from constant expressions", node);
            }

            var super = (ConstantVariable)((ClassDefinition)node).SuperClass;

            var cname = Mangling.RubyIdentifierToPHP(((ClassDefinition)node).QualifiedName.Name);

            compiler.AppendLine("class {0} extends {1}", cname,
                                super == null ? "F_Object" : Mangling.RubyIdentifierToPHP(super.Name));
            compiler.AppendLine("{");

            compiler.Indent();

            compiler.AppendLine("function __construct() { }");
            compiler.AppendLine("public static function SF_new()");
            compiler.AppendLine("{");
            compiler.Indent();
            compiler.AppendLine("$obj = new {0};", cname);
            compiler.AppendLine("$args = func_get_args();");
            compiler.AppendLine("if(method_exists($obj,'F_initialize'))");
            compiler.Indent();
            compiler.AppendLine("call_user_func_array(array($obj,'F_initialize'), $args);");
            compiler.Dedent();
            compiler.AppendLine("return $obj;");
            compiler.Dedent();
            compiler.AppendLine("}");

            foreach (var child in ((ClassDefinition)node).Body.Statements)
            {
                if (child.NodeType == NodeTypes.MethodDefinition)
                {
                    compiler.CompileNode(child, parent.CreateChild(node));
                    continue;
                }

                throw new FructoseCompileException("Classes may only contain method declarations at this point", node);
            }

            compiler.Dedent();
            compiler.AppendLine("}");
        }
Exemple #35
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var hash = (HashConstructor)node;
            compiler.AppendLine("$_tmp_pairs[] = array();");

            foreach (var maplet in hash.Maplets)
            {
                compiler.CompileNode(maplet.Key, parent.CreateChild(node));
                compiler.AppendLine("$_tmp_pairs[count($_tmp_pairs)-1][] = array_pop($_stack);");
                compiler.CompileNode(maplet.Value, parent.CreateChild(node));
                compiler.AppendLine("$_tmp_pairs[count($_tmp_pairs)-1][] = array_pop($_stack);");
            }

            compiler.AppendLine("$_stack[] = F_Hash::__from_flatpairs(array_pop($_tmp_pairs));");
        }
Exemple #36
0
        private Reference CreateReference(NodeParent parent, Token token)
        {
            Definition id     = token.ReadReference();
            Definition source = _slots.FindAssigned(id);

            if (source != null)
            {
                id.Source = source.Source;
            }
            else
            {
                throw new ArgumentException("Internal Error");
            }
            return(new Reference(parent, token, id));
        }
Exemple #37
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var regex = (RegularExpression)node;

            string rstr = "";
            if(regex.Options.HasFlag(RubyRegexOptions.IgnoreCase))
                rstr += "i";
            if(regex.Options.HasFlag(RubyRegexOptions.Multiline))
                rstr += "m";
            if(regex.Options.HasFlag(RubyRegexOptions.Extended))
                rstr += "x";

            compiler.CompileNode(new StringConstructor(regex.Pattern, StringKind.Mutable, regex.Location));

            compiler.AppendLine("$_stack[] = F_Regexp::SF_new(NULL, array_pop($_stack), F_String::__from_string('{0}'));", rstr);
        }
Exemple #38
0
 public override void Compile(Compiler compiler, Node node, NodeParent parent)
 {
     var method = parent.OfType<MethodDefinition>().ToArray();
     if (method.Length > 0 && method[0].Parameters != null
         && method[0].Parameters.Mandatory.Where(p => p.ToString() == ((LocalVariable)node).Name).Count() > 0)
     {
         compiler.AppendLine("$_stack[] = isset(${0}) ? (${0}) : (new F_NilClass);", Mangling.RubyIdentifierToPHP(((Variable)node).Name));
         return;
     }
     if (parent.OfType<ClassDefinition>().Count() == 0)
     {
         compiler.AppendLine("$_stack[] = isset($_locals->{0}) ? ($_locals->{0}) : (new F_NilClass);", Mangling.RubyIdentifierToPHP(((Variable)node).Name));
         return;
     }
     compiler.AppendLine("$_stack[] = isset({0}) ? ({0}) : (new F_NilClass);", ((Variable)node).ToPHPVariable());
 }
Exemple #39
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var hash = (HashConstructor)node;

            compiler.AppendLine("$_tmp_pairs[] = array();");

            foreach (var maplet in hash.Maplets)
            {
                compiler.CompileNode(maplet.Key, parent.CreateChild(node));
                compiler.AppendLine("$_tmp_pairs[count($_tmp_pairs)-1][] = array_pop($_stack);");
                compiler.CompileNode(maplet.Value, parent.CreateChild(node));
                compiler.AppendLine("$_tmp_pairs[count($_tmp_pairs)-1][] = array_pop($_stack);");
            }

            compiler.AppendLine("$_stack[] = F_Hash::__from_flatpairs(array_pop($_tmp_pairs));");
        }
Exemple #40
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            CaseExpression c = (CaseExpression)node;

            compiler.CompileNode(c.Value, parent.CreateChild(node));
            compiler.AppendLine("$_caseval = array_pop($_stack);");

            foreach (var when in c.WhenClauses)
            {
                var sb = new StringBuilder();
                compiler.AppendLine("$_comparisons = array();");
                foreach (var compar in when.Comparisons)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(" || ");
                    }
                    sb.Append("_isTruthy(array_pop($_comparisons))");
                    compiler.CompileNode(compar, parent.CreateChild(node).CreateChild(when));
                    compiler.AppendLine("$_comparisons[] = array_pop($_stack)->__operator_stricteq(NULL, $_caseval);");
                }
                compiler.AppendLine("if({0})", sb.ToString());
                compiler.AppendLine("{");
                compiler.Indent();
                foreach (var stmt in when.Statements)
                {
                    compiler.CompileNode(stmt, parent.CreateChild(node).CreateChild(when));
                }
                compiler.Dedent();
                compiler.AppendLine("}");
                compiler.AppendLine("else");
                compiler.AppendLine("{");
                compiler.Indent();
            }
            if (c.ElseStatements != null)
            {
                foreach (var stmt in c.ElseStatements)
                {
                    compiler.CompileNode(stmt, parent.CreateChild(node));
                }
            }
            for (int i = 0; i < c.WhenClauses.Length; i++)
            {
                compiler.Dedent();
                compiler.AppendLine("}");
            }
        }
Exemple #41
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var method = parent.OfType <MethodDefinition>().ToArray();

            if (method.Length > 0 && method[0].Parameters != null &&
                method[0].Parameters.Mandatory.Where(p => p.ToString() == ((LocalVariable)node).Name).Count() > 0)
            {
                compiler.AppendLine("$_stack[] = isset(${0}) ? (${0}) : (new F_NilClass);", Mangling.RubyIdentifierToPHP(((Variable)node).Name));
                return;
            }
            if (parent.OfType <ClassDefinition>().Count() == 0)
            {
                compiler.AppendLine("$_stack[] = isset($_locals->{0}) ? ($_locals->{0}) : (new F_NilClass);", Mangling.RubyIdentifierToPHP(((Variable)node).Name));
                return;
            }
            compiler.AppendLine("$_stack[] = isset({0}) ? ({0}) : (new F_NilClass);", ((Variable)node).ToPHPVariable());
        }
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var sc = (StringConstructor)node;

            switch (sc.Kind)
            {
            case StringKind.Symbol:
                if (sc.Parts.Count != 1 || sc.Parts[0].NodeType != NodeTypes.StringLiteral)
                {
                    throw new FructoseCompileException("Symbol interpolation not supported _yet_", node);
                }
                compiler.AppendLine("$_stack[] = F_Symbol::__from_string('{0}');", ((StringLiteral)sc.Parts[0]).Value.ToString().Replace("'", "\\'"));
                break;

            case StringKind.Command:
                if (sc.Parts.Count != 1)
                {
                    throw new FructoseCompileException("`` is escape-to-PHP operator, and only works with a string literal.", node);
                }

                if (sc.Parts[0].NodeType != NodeTypes.StringLiteral)
                {
                    throw new FructoseCompileException("`` is escape-to-PHP operator, and only works with a string literal.", node);
                }

                compiler.AppendLine("// BEGIN: escape-to-PHP");
                compiler.AppendLine(((StringLiteral)sc.Parts[0]).Value.ToString());
                compiler.AppendLine("// END: escape-to-PHP");
                break;

            case StringKind.Mutable:
                foreach (var part in ((IEnumerable <Expression>)sc.Parts).Reverse())
                {
                    compiler.CompileNode(part, parent.CreateChild(node));
                }

                compiler.AppendLine("$_stack[] = F_String::__from_string('');");

                for (int i = 0; i < sc.Parts.Count; i++)
                {
                    compiler.AppendLine("$_stack[] = array_pop($_stack)->__operator_lshift(NULL, array_pop($_stack));");
                }

                break;
            }
        }
Exemple #43
0
 private Capture CreateCapture(NodeParent parent, Definition cap, Definition uncap)
 {
     if (uncap != null)
     {
         Balance balance = new Balance(_concatenate, cap, uncap);
         _slots.Assign(balance);
         _sequence.Add(balance);
         return(balance);
     }
     else
     {
         Capture capture = new Capture(_concatenate, cap);
         _slots.Assign(capture);
         _sequence.Add(capture);
         return(capture);
     }
 }
Exemple #44
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var forexp = (ForLoopExpression)node;

            compiler.CompileNode(forexp.List, parent.CreateChild(node));
            compiler.AppendLine("$_tmp_enumerator[] = array_pop($_stack)->F_each(NULL);");
            compiler.AppendLine("while(true)");
            compiler.AppendLine("{");
            compiler.Indent();
            compiler.AppendLine("try");
            compiler.AppendLine("{");
            compiler.Indent();
            compiler.AppendLine("$_forcur = $_tmp_enumerator[count($_tmp_enumerator)-1]->F_next(NULL);");
            compiler.Dedent();
            compiler.AppendLine("}");
            compiler.AppendLine("catch(ErrorCarrier $err)");
            compiler.AppendLine("{");
            compiler.Indent();
            compiler.AppendLine("if(is_a($err->val, 'F_StopIteration'))");
            compiler.Indent();
            compiler.AppendLine("break;");
            compiler.Dedent();
            compiler.Dedent();
            compiler.AppendLine("}");

            if (forexp.Block.Parameters.Mandatory.Length == 1)
            {
                compiler.AppendLine("{0} = $_forcur;", forexp.Block.Parameters.Mandatory.Cast <Variable>().Single().ToPHPVariable());
            }
            else
            {
                foreach (var var in forexp.Block.Parameters.Mandatory.Cast <Variable>())
                {
                    compiler.AppendLine("{0} = array_shift($_forcur->__ARRAY);", var.ToPHPVariable());
                }
            }

            foreach (var stmt in forexp.Block.Body)
            {
                compiler.CompileNode(stmt, parent.CreateChild(node));
            }

            compiler.Dedent();
            compiler.AppendLine("}");
        }
Exemple #45
0
        internal static string assignmentVar(LeftValue lval, NodeParent parent)
        {
            if (lval is LocalVariable)
            {
                var method = parent.OfType<MethodDefinition>().ToArray();
                if (method.Length > 0 && method[0].Parameters != null
                    && method[0].Parameters.Mandatory.Where(p => p.ToString() == ((LocalVariable)lval).Name).Count() > 0)
                {
                    return "$" + Mangling.RubyIdentifierToPHP(((LocalVariable)lval).Name);
                }
                if (parent.OfType<ClassDefinition>().Count() == 0)
                {
                    return "$_locals->" + Mangling.RubyIdentifierToPHP(((LocalVariable)lval).Name);
                }
            }

            return ((Variable)lval).ToPHPVariable();
        }
Exemple #46
0
        internal static string assignmentVar(LeftValue lval, NodeParent parent)
        {
            if (lval is LocalVariable)
            {
                var method = parent.OfType <MethodDefinition>().ToArray();
                if (method.Length > 0 && method[0].Parameters != null &&
                    method[0].Parameters.Mandatory.Where(p => p.ToString() == ((LocalVariable)lval).Name).Count() > 0)
                {
                    return("$" + Mangling.RubyIdentifierToPHP(((LocalVariable)lval).Name));
                }
                if (parent.OfType <ClassDefinition>().Count() == 0)
                {
                    return("$_locals->" + Mangling.RubyIdentifierToPHP(((LocalVariable)lval).Name));
                }
            }

            return(((Variable)lval).ToPHPVariable());
        }
Exemple #47
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            compiler.CompileNode(((NotExpression)node).Expression, parent.CreateChild(node));

            compiler.AppendLine("$_and_tmp = get_class(array_pop($_stack));");
            compiler.AppendLine("if($_and_tmp !== 'F_NilClass' && $_and_tmp !== 'F_FalseClass')");
            compiler.AppendLine("{");
            compiler.Indent();

            compiler.AppendLine("$_stack[] = new F_FalseClass;");

            compiler.Dedent();
            compiler.AppendLine("}");
            compiler.AppendLine("else");
            compiler.AppendLine("{");
            compiler.Indent();

            compiler.AppendLine("$_stack[] = new F_TrueClass;");

            compiler.Dedent();
            compiler.AppendLine("}");
        }
Exemple #48
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            compiler.CompileNode(((IfExpression)node).Condition, parent.CreateChild(node));

            compiler.AppendLine("$_cond = array_pop($_stack);");
            compiler.AppendLine("if(get_class($_cond) !== 'F_NilClass' && get_class($_cond) !== 'F_FalseClass' && !is_subclass_of($_cond, 'F_NilClass') && !is_subclass_of($_cond, 'F_FalseClass'))");
            compiler.AppendLine("{");
            compiler.Indent();

            foreach (var stmt in ((IfExpression)node).Body)
                compiler.CompileNode(stmt, parent.CreateChild(node));

            compiler.Dedent();
            compiler.AppendLine("}");

            if (((IfExpression)node).ElseIfClauses.Count > 0)
            {
                compiler.AppendLine("else");
                compiler.AppendLine("{");
                compiler.Indent();

                var firstelseif = ((IfExpression)node).ElseIfClauses.First();
                var rest = ((IfExpression)node).ElseIfClauses.Skip(1).ToList();
                if (firstelseif.Condition == null)
                {
                    foreach (var stmt in firstelseif.Statements)
                        compiler.CompileNode(stmt, parent.CreateChild(node));
                }
                else
                {
                    compiler.CompileNode(new IfExpression(firstelseif.Condition, firstelseif.Statements, rest,
                        new SourceSpan(firstelseif.Location.Start, (rest.Count > 0 ? rest.Last() : firstelseif).Location.End)), parent.CreateChild(node));
                }

                compiler.Dedent();
                compiler.AppendLine("}");
            }
        }
Exemple #49
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            CaseExpression c = (CaseExpression)node;
            compiler.CompileNode(c.Value, parent.CreateChild(node));
            compiler.AppendLine("$_caseval = array_pop($_stack);");

            foreach(var when in c.WhenClauses)
            {
                var sb = new StringBuilder();
                compiler.AppendLine("$_comparisons = array();");
                foreach(var compar in when.Comparisons)
                {
                    if(sb.Length > 0)
                        sb.Append(" || ");
                    sb.Append("_isTruthy(array_pop($_comparisons))");
                    compiler.CompileNode(compar, parent.CreateChild(node).CreateChild(when));
                    compiler.AppendLine("$_comparisons[] = array_pop($_stack)->__operator_stricteq(NULL, $_caseval);");
                }
                compiler.AppendLine("if({0})", sb.ToString());
                compiler.AppendLine("{");
                compiler.Indent();
                foreach(var stmt in when.Statements)
                    compiler.CompileNode(stmt, parent.CreateChild(node).CreateChild(when));
                compiler.Dedent();
                compiler.AppendLine("}");
                compiler.AppendLine("else");
                compiler.AppendLine("{");
                compiler.Indent();
            }
            if(c.ElseStatements != null)
                foreach(var stmt in c.ElseStatements)
                    compiler.CompileNode(stmt, parent.CreateChild(node));
            for(int i = 0; i < c.WhenClauses.Length; i++)
            {
                compiler.Dedent();
                compiler.AppendLine("}");
            }
        }
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            var sc = (StringConstructor)node;

            switch (sc.Kind)
            {
                case StringKind.Symbol:
                    if (sc.Parts.Count != 1 || sc.Parts[0].NodeType != NodeTypes.StringLiteral)
                        throw new FructoseCompileException("Symbol interpolation not supported _yet_", node);
                    compiler.AppendLine("$_stack[] = F_Symbol::__from_string('{0}');", ((StringLiteral)sc.Parts[0]).Value.ToString().Replace("'", "\\'"));
                    break;

                case StringKind.Command:
                    if (sc.Parts.Count != 1)
                        throw new FructoseCompileException("`` is escape-to-PHP operator, and only works with a string literal.", node);

                    if (sc.Parts[0].NodeType != NodeTypes.StringLiteral)
                        throw new FructoseCompileException("`` is escape-to-PHP operator, and only works with a string literal.", node);

                    compiler.AppendLine("// BEGIN: escape-to-PHP");
                    compiler.AppendLine(((StringLiteral)sc.Parts[0]).Value.ToString());
                    compiler.AppendLine("// END: escape-to-PHP");
                    break;

                case StringKind.Mutable:
                    foreach (var part in ((IEnumerable<Expression>)sc.Parts).Reverse())
                        compiler.CompileNode(part, parent.CreateChild(node));

                    compiler.AppendLine("$_stack[] = F_String::__from_string('');");

                    for(int i = 0; i < sc.Parts.Count; i++)
                        compiler.AppendLine("$_stack[] = array_pop($_stack)->__operator_lshift(NULL, array_pop($_stack));");

                    break;
            }
        }
Exemple #51
0
 public override void Compile(Compiler compiler, Node node, NodeParent parent)
 {
     // nop
 }
Exemple #52
0
 public override void Compile(Compiler compiler, Node node, NodeParent parent)
 {
     compiler.AppendLine("$_stack[] = isset({0}) ? ({0}) : (new F_NilClass);", ((Variable)node).ToPHPVariable());
 }
Exemple #53
0
 public abstract void Compile(Compiler compiler, Node node, NodeParent parent);
Exemple #54
0
 public override void Compile(Compiler compiler, Node node, NodeParent parent)
 {
     var cond = (ConditionalExpression)node;
     compiler.CompileNode(new IfExpression(cond.Condition, new Statements(cond.TrueExpression), new List<ElseIfClause>() {
         new ElseIfClause(null, new Statements(cond.FalseExpression), cond.FalseExpression.Location)}, cond.Location), parent.CreateChild(node));
 }
Exemple #55
0
        public override void Compile(Compiler compiler, IronRuby.Compiler.Ast.Node node, NodeParent parent)
        {
            string mname = Mangling.RubyMethodToPHP(((MethodCall)node).MethodName);

            if (compilerMethods.ContainsKey(mname))
            {
                compilerMethods[mname].Compile(compiler, (MethodCall)node, parent);
                return;
            }

            if (((MethodCall)node).Arguments != null)
                foreach (var arg in ((MethodCall)node).Arguments.Expressions.Reverse())
                    compiler.CompileNode(arg, parent.CreateChild(node));

            bool callStatic = false;

            if (((MethodCall)node).Target == null)
            {
                compiler.AppendLine("$_stack[] = $_locals->self;");
            }
            else
            {
                if (((MethodCall)node).Target.NodeType == NodeTypes.ConstantVariable)
                    callStatic = true;
                else
                    compiler.CompileNode(((MethodCall)node).Target, parent.CreateChild(node));
            }

            if (((MethodCall)node).Block != null)
            {
                compiler.AppendLine("$_lambda_objs_offset = count($_lambda_objs);");
                compiler.AppendLine("$_lambda_objs[] = $_locals;");
            }

            if (((MethodCall)node).Block != null)
            {
                compiler.AppendLine("try");
                compiler.AppendLine("{");
                compiler.Indent();
            }

            string call = callStatic
                ? string.Format("$_stack[] = {0}::S{1}(", Mangling.RubyIdentifierToPHP(((ConstantVariable)((MethodCall)node).Target).Name), mname)
                : string.Format("$_stack[] = array_pop($_stack)->{0}(", mname);

            if (((MethodCall)node).Block != null)
            {
                if (((MethodCall)node).Block.NodeType == NodeTypes.BlockReference)
                {
                    var expr = ((BlockReference)((MethodCall)node).Block).Expression;
                    if(expr.NodeType != NodeTypes.LocalVariable
                        && parent.OfType<MethodDefinition>().Count() != 1
                        && parent.OfType<MethodDefinition>().Single().Parameters != null
                        && parent.OfType<MethodDefinition>().Single().Parameters.Block != null
                        && ((LocalVariable)expr).Name != parent.OfType<MethodDefinition>().Single().Parameters.Block.Name)
                        throw new FructoseCompileException("Block references not referring to a block parameter are not yet supported.", ((MethodCall)node).Block);

                    call += "$block";
                }
                else
                {
                    var block_mname = compiler.Transformations.RefactoredBlocksToMethods[(BlockDefinition)((MethodCall)node).Block];

                    call += "create_function('',sprintf('global $_lambda_objs; $args = func_get_args(); $offset = %d; $_locals = $_lambda_objs[$offset]; array_unshift($args, $_locals); $r = call_user_func_array(";

                    if (parent.OfType<ClassDefinition>().Count() > 0)
                        call += "array($_locals->self,\"" + Mangling.RubyIdentifierToPHP(block_mname) + "\")";
                    else
                        call += "\"" + Mangling.RubyIdentifierToPHP(block_mname) + "\"";

                    call += ", $args); $_lambda_objs[$offset] = $r[\"locals\"]; return $r[\"retval\"];',$_lambda_objs_offset))";
                }
            }
            else
                call += "NULL";

            if (((MethodCall)node).Arguments != null)
            {
                for (int i = 0; i < ((MethodCall)node).Arguments.Expressions.Length; i++)
                    call += ", array_pop($_stack)";
            }

            compiler.AppendLine(call + ");");

            if (((MethodCall)node).Block != null)
            {
                compiler.Dedent();
                compiler.AppendLine("}");
                compiler.AppendLine("catch(ReturnFromBlock $rfb)");
                compiler.AppendLine("{");
                compiler.Indent();
                compiler.AppendLine("return $rfb->val;");
                compiler.Dedent();
                compiler.AppendLine("}");
                compiler.AppendLine("$_locals = $_lambda_objs[$_lambda_objs_offset];");
            }
        }
Exemple #56
0
 public override void Compile(Compiler compiler, Node node, NodeParent parent)
 {
     var unless = (UnlessExpression)node;
     compiler.CompileNode(new IfExpression(new NotExpression(unless.Condition, unless.Condition.Location), unless.Statements, new List<ElseIfClause> { unless.ElseClause }, unless.Location), parent.CreateChild(node));
 }
Exemple #57
0
        public override void Compile(Compiler compiler, Node node, NodeParent parent)
        {
            if (parent.OfType<MethodDefinition>().Count() > 0)
                throw new FructoseCompileException("Nested methods are not supported.", node);

            if (((MethodDefinition)node).Parameters.Mandatory.Length > ((MethodDefinition)node).Parameters.LeadingMandatoryCount)
                throw new FructoseCompileException("Additional arguments after splats in methods are currently unsupported.", node);

            string visibility = parent.OfType<ClassDefinition>().Count() > 0 ?
                "public " : "";

            string signature = visibility + "function " + Mangling.RubyMethodToPHP(((MethodDefinition)node).Name) + "(";

            List<string> compiledParams = new List<string>();
            if (((MethodDefinition)node).Name.Contains("__lambda_"))
                compiledParams.Add("$_locals");
            compiledParams.Add("$block");
            foreach (var arg in ((MethodDefinition)node).Parameters.Mandatory)
                compiledParams.Add("$" + Mangling.RubyIdentifierToPHP(arg.ToString()));
            foreach (var arg in ((MethodDefinition)node).Parameters.Optional)
                compiledParams.Add("$" + Mangling.RubyIdentifierToPHP(arg.Left.ToString()) + "=NULL");
            signature += String.Join(", ", compiledParams);

            signature += ")";

            compiler.AppendLine(signature);
            compiler.AppendLine("{");
            compiler.Indent();

            compiler.AppendLine("$_stack = array();");
            compiler.AppendLine("if(!isset($_locals)) $_locals = new stdClass;");
            if (parent.OfType<ClassDefinition>().Count() > 0)
            {
                compiler.AppendLine("if(!isset($_locals->self)) $_locals->self = $this;");
            }
            else
            {
                compiler.AppendLine("global $_gthis;");
                compiler.AppendLine("if(!isset($_locals->self)) $_locals->self = $_gthis;");
            }
            compiler.AppendLine("if(!isset($_locals->block)) $_locals->block = $block;");

            compiler.AppendLine("foreach(array(" + string.Join(", ", ((MethodDefinition)node).Parameters.Mandatory
                .Select(arg => "\"" + Mangling.RubyIdentifierToPHP(arg.ToString()) + "\"")
                .ToArray()) + ") as $parm)");
            compiler.Indent();
            compiler.AppendLine("$_locals->$parm = $$parm;");
            compiler.Dedent();

            foreach (var arg in ((MethodDefinition)node).Parameters.Optional)
            {
                string parm = Mangling.RubyIdentifierToPHP(arg.Left.ToString());
                compiler.AppendLine("if($" + parm + " === NULL) {");
                compiler.Indent();
                compiler.CompileNode(arg.Right, parent.CreateChild(node));
                compiler.AppendLine("$_locals->" + parm + " = array_pop($_stack);");
                compiler.Dedent();
                compiler.AppendLine("} else {");
                compiler.Indent();
                compiler.AppendLine("$_locals->" + parm + " = $" + parm + ";");
                compiler.Dedent();
                compiler.AppendLine("}");
            }

            if (((MethodDefinition)node).Parameters.Unsplat != null)
                compiler.AppendLine("$_locals->" + Mangling.RubyIdentifierToPHP(((MethodDefinition)node).Parameters.Unsplat.ToString()) + " = "
                    + "F_Array::__from_array(array_slice(func_get_args(), " + compiledParams.Count + "));");

            compiler.AppendLine("global $_lambda_objs;");
            compiler.AppendLine("global $_globals;");

                foreach (var child in ((MethodDefinition)node).Body.Statements)
                    compiler.CompileNode(child, parent.CreateChild(node));

            if (((MethodDefinition)node).Name.Contains("__lambda_"))
                compiler.AppendLine("return array( 'locals' => $_locals, 'retval' => array_pop($_stack) );");
            else
                compiler.AppendLine("return array_pop($_stack);");

            compiler.Dedent();
            compiler.AppendLine("}");
        }
Exemple #58
0
        public void CompileNode(Node node, NodeParent parents)
        {
            if (node == null)
                return;

            if (!generators.ContainsKey(node.NodeType))
                throw new NotImplementedException("NodeType " + node.NodeType + " not supported yet: " + node.Location.ToString());

            generators[node.NodeType].Compile(this, node, parents);
        }
Exemple #59
0
 public override void Compile(Compiler compiler, Node node, NodeParent parent)
 {
     compiler.AppendLine("$_stack[] = F_String::__from_string('{0}');", ((StringLiteral)node).Value.ToString().Replace("\\","\\\\").Replace("'","\\'"));
 }
 public override void Compile(Compiler compiler, Node node, NodeParent parent)
 {
     compiler.AppendLine("$_stack[] = F_Symbol::__from_string('{0}');", ((SymbolLiteral)node).Value.ToString());
 }