예제 #1
0
        public string GetCodeForNext(ASTVariable loopVar)
        {
            var counter = GetCodeForVar(loopVar);

            if (!loops.Keys.Contains(counter))
            {
                Croak($"Called NEXT before defining a loop for variable \"{counter}\"");
            }

            var loop      = loops[counter];
            var increment = Target.GetComplexSnippet("loops", "increment", new Dictionary <string, string>()
            {
                { "counter", counter },
                { "step", GetCodeForExpression(loop.Step) }
            });

            var gotoLine = Target.GetSnippet("expressions", "number", "value", loop.DefinedOnLine.ToString());
            var gotoCode = Target.GetSnippet("commands", "goto", "lineNumber", gotoLine);
            var jump     = Target.GetComplexSnippet("loops", "jump", new Dictionary <string, string>()
            {
                { "condition", GetCodeForExpression(loop.Condition) },
                { "skip", loop.SkipVar },
                { "goto", gotoCode }
            });

            return(increment + jump);
        }
예제 #2
0
        protected override bool Visit(ASTVariable node)
        {
            if (!Visit(node.Value, node.DeclaredType))
            {
                return(false);
            }

            if (node.DeclaredType.TypeInfo is UnknownType)
            {
                node.DeclaredType.TypeInfo = node.Value.TypeInfo;
            }
            else if (!Expect(node.Position, node.DeclaredType.TypeInfo, node.Value.TypeInfo))
            {
                return(false);
            }

            node.TypeInfo = node.DeclaredType.TypeInfo;
            if (_scope.TryAddSymbol(node.Name, node.TypeInfo))
            {
                return(true);
            }

            Error(node.Position, "");
            return(false);
        }
예제 #3
0
        private VisitResult VisitVariable(ASTVariable node)
        {
            if (node.ArrayIndexFrom != null)
            {
                var array            = (List <dynamic>)_callStack.Top[node.Name];
                var indexFrom        = (int)Visit(node.ArrayIndexFrom).Value;
                var integreIndexFrom = indexFrom < 0 ? array.Count + indexFrom : indexFrom;
                var integreIndexTo   = array.Count - 1;
                var integerIndexStep = 1;

                if (node.ArrayIndexTo != null)
                {
                    var indexTo = (int)Visit(node.ArrayIndexTo).Value;
                    integreIndexTo = indexTo < 0 ? array.Count + indexTo : indexTo == int.MaxValue ? integreIndexTo : indexTo;

                    if (node.ArrayIndexStep != null)
                    {
                        integerIndexStep = (int)Visit(node.ArrayIndexStep).Value;
                    }

                    var result = new List <dynamic>();

                    var from = integreIndexTo > integreIndexFrom ? integreIndexFrom : integreIndexTo;
                    var to   = integreIndexFrom > integreIndexTo ? integreIndexFrom : integreIndexTo;
                    var step = integerIndexStep < 0 ? -integerIndexStep : integerIndexStep;

                    for (var i = from; i < to; i += step)
                    {
                        result.Add(array[i]);
                    }

                    if ((integreIndexFrom <= integreIndexTo && integerIndexStep < 0) ||
                        (integreIndexFrom > integreIndexTo && integerIndexStep > 0))
                    {
                        result.Reverse();
                    }

                    return(new VisitResult
                    {
                        ControlType = ControlType.Return,
                        Value = result
                    });
                }

                return(new VisitResult
                {
                    ControlType = ControlType.Return,
                    Value = array[integreIndexFrom]
                });
            }

            return(new VisitResult
            {
                ControlType = ControlType.Return,
                Value = _callStack.Top[node.Name]
            });
        }
예제 #4
0
        protected override bool Visit(ASTVariable node)
        {
            // This should be safe, if we did typechecking correct
            var type = (TypeType)node.DeclaredType.TypeInfo;

            // TODO: Handle structs and type variables

            if (!Visit(node.Value))
            {
                return(false);
            }
            _lastNode = new CDeclaration {
                Name = GetVariableName(node.Name), Type = GetCType(type.Type), Value = _lastNode
            };
            return(true);
        }
예제 #5
0
        public string GetCodeForVar(ASTVariable vr, bool autoDefine = true, bool usesAccess = true)
        {
            var name = vr.IsString ? vr.Name + "_string" : vr.Name + "_number";

            if (autoDefine && !seenVars.Contains(name))
            {
                Console.WriteLine($"[debug] Auto-defining {name}");
                seenVars.Add(name);
                var type = vr.IsString ? "stringDeclaration" : "numberDeclaration";
                declarations += Target.GetSnippet("vars", type, "name", name);
            }

            return(usesAccess
              ? Target.GetSnippet("vars", "varAccess", "name", name)
              : name);
        }
예제 #6
0
        public string GetCodeForList(ASTVariable op)
        {
            // Exclude this var from Auto-Define
            var nm = GetCodeForVar(op, false);

            seenVars.Add(nm);

            var type = op.IsString ? "string" : "number";
            var snip = type + "Declaration";
            var code = Target.GetSnippet("lists", snip, "name", nm);

            declarations += code;

            var initSnip = type + "Init";
            var initCode = Target.GetSnippet("lists", initSnip, "name", nm);

            return(initCode);
        }
예제 #7
0
        public string GetCodeForTopof(ASTVariable loopVar)
        {
            var counter = GetCodeForVar(loopVar);

            if (!loops.Keys.Contains(counter))
            {
                Croak($"Called TOPOF before defining a loop for variable \"{counter}\"");
            }

            var loop     = loops[counter];
            var gotoCode = Target.GetSnippet("commands", "goto", "lineNumber", loop.DefinedOnLine.ToString());
            var topCode  = Target.GetComplexSnippet("loops", "topofJump", new Dictionary <string, string>()
            {
                { "skip", loop.SkipVar },
                { "goto", gotoCode }
            });

            return(topCode);
        }
 public ASTVariableDeclaration(ASTType type, ASTVariable variable) => (Token, VariableType, Variable) = (type.Token, type, variable);
예제 #9
0
 public ASTParam(ASTType type, ASTVariable variable) => (Token, VariableType, Variable) = (type.Token, type, variable);
예제 #10
0
 protected abstract bool Visit(ASTVariable node);