示例#1
0
 public override async Task WalkAsync(PythonWalkerAsync walker, CancellationToken cancellationToken = default)
 {
     if (await walker.WalkAsync(this, cancellationToken))
     {
         if (Test != null)
         {
             await Test.WalkAsync(walker, cancellationToken);
         }
         if (Body != null)
         {
             await Body.WalkAsync(walker, cancellationToken);
         }
         if (ElseStatement != null)
         {
             await ElseStatement.WalkAsync(walker, cancellationToken);
         }
     }
     await walker.PostWalkAsync(this, cancellationToken);
 }
示例#2
0
            public override string ToCode()
            {
                var ret = "version";

                if (VersionIdentifierOrLiteral != null)
                {
                    ret += '(' + VersionIdentifierOrLiteral.ToString() + ')';
                }

                if (ScopedStatement != null)
                {
                    ret += ' ' + ScopedStatement.ToCode();
                }

                if (ElseStatement != null)
                {
                    ret += " else " + ElseStatement.ToCode();
                }

                return(ret);
            }
示例#3
0
        public static CodeLine BuildCodeLine(string filename, int linenr, string codeline)
        {
            CodeLine result = null;
            Match    m      = line_regex.Match(codeline);

            if (m.Success)
            {
                string sCommand = m.Groups["command"].Value;
                int    pos      = m.Length;
                switch (sCommand)
                {
                case DoStatement.keyword:
                    result = new DoStatement(linenr, m, codeline);
                    break;

                case WhileStatement.keyword:
                    result = new WhileStatement(linenr, m, codeline);
                    break;

                case IfStatement.keyword:
                    result = new IfStatement(linenr, m, codeline);
                    break;

                case ElseStatement.keyword:
                    result = new ElseStatement(linenr, m, codeline);
                    break;

                case ElseIfStatement.keyword:
                    result = new ElseIfStatement(linenr, m, codeline);
                    break;

                case EndIfStatement.keyword:
                    result = new EndIfStatement(linenr, m, codeline);
                    break;

                case ErrorMessage.keyword:
                    result = new ErrorMessage(linenr, m, codeline);
                    break;

                case InfoMessage.keyword:
                    result = new InfoMessage(linenr, m, codeline);
                    break;

                case LoopStatement.keyword:
                    result = new LoopStatement(linenr, m, codeline);
                    break;

                case BreakStatement.keyword:
                    result = new BreakStatement(linenr, m, codeline);
                    break;

                case ReturnStatement.keyword:
                    result = new ReturnStatement(linenr, m, codeline);
                    break;

                case ExitStatement.keyword:
                    result = new ExitStatement(linenr, m, codeline);
                    break;

                case VarDeclaration.keyword:
                    result = new VarDeclaration(linenr, m, codeline);
                    break;

                case GlobalVarDeclaration.keyword:
                    result = new GlobalVarDeclaration(linenr, m, codeline);
                    break;

                case FunctionDeclaration.keyword:
                    result = new FunctionDeclaration(linenr, m, codeline);
                    break;

                case EndFunctionDeclaration.keyword:
                    result = new EndFunctionDeclaration(linenr, m, codeline);
                    break;

                case Include.keyword:
                    result = new Include(linenr, m, codeline);
                    break;
                }
            }
            // Misschien is het een comment?
            if (result == null)
            {
                m = cmmt_regex.Match(codeline);
                if (m.Success)
                {
                    result = new Comment(linenr, m, codeline);
                }
            }
            if (result == null)
            {
                m = ass_regex.Match(codeline);
                if (m.Success)
                {
                    result = new Assignment(linenr, m, codeline);
                }
            }
            if (result == null)
            {
                m = vfc_regex.Match(codeline);
                if (m.Success)
                {
                    result = new VoidFunctionCall(linenr, m, codeline);
                }
            }
            if (result == null && codeline.Trim() == "@")
            {
                m = cmm2_regex.Match(codeline);
                if (m.Success)
                {
                    result = new Comment(linenr, m, codeline);
                }
            }
            if (result == null)
            {
                // Anders zal het een gewone tekst regel zijn? Met evt een paar expressies erin
                result = new CodeLine(linenr, codeline);
            }
            result._filename = filename;
            return(result);
        }
示例#4
0
        public override object VisitAsmline(Z80AsmParser.AsmlineContext context)
        {
            CurrentLabel      = null;
            CurrentLabelColon = false;
            CurrentComment    = null;
            LabelSpan         = null;
            KeywordSpan       = null;
            CommentSpan       = null;
            IssueToEmit       = null;
            IsFieldAssignment = false;
            NumberSpans       = null;
            IdentifierSpans   = null;
            StringSpans       = null;
            FunctionSpans     = null;
            SemiVarSpans      = null;
            MacroParamSpans   = null;
            MacroParamNames   = null;
            SemiVarSpans      = null;
            StatementSpans    = null;
            OperandSpans      = null;
            MnemonicSpans     = null;

            if (context?.Start == null || context.Stop == null)
            {
                return(null);
            }

            var startIndex = context.start.StartIndex;
            var stopIndex  = context.stop.StopIndex;

            CurrentSourceText = InputStream.GetText(new Interval(startIndex, stopIndex));

            CurrentSourceLine = context.Start.Line;
            FirstColumn       = context.Start.Column;
            FirstPosition     = context.Start.StartIndex;
            LastPosition      = context.Stop.StopIndex;

            object mainInstructionPart = null;

            // --- Obtain label
            var labelCtx = context.label();

            if (labelCtx != null)
            {
                CurrentLabel      = labelCtx.GetChild(0).NormalizeToken();
                CurrentLabelColon = labelCtx.COLON() != null;
                LabelSpan         = new TextSpan(labelCtx.Start.StartIndex, labelCtx.Start.StopIndex + 1);
            }

            // --- Obtain line body/directive
            var lineBodyCtx = context.lineBody();

            if (lineBodyCtx != null)
            {
                // --- Special case, when a macro parameters is used as the main line
                LastInstructionPos = lineBodyCtx.Stop.StopIndex;
                var macroParamCtx = lineBodyCtx.macroParam();
                if (macroParamCtx != null)
                {
                    VisitMacroParam(macroParamCtx);
                    mainInstructionPart = new MacroParamLine(macroParamCtx.IDENTIFIER()?.NormalizeToken());
                }
                else
                {
                    mainInstructionPart = VisitLineBody(context.lineBody());
                }
            }
            else if (context.directive() != null)
            {
                mainInstructionPart = VisitDirective(context.directive());
            }

            // --- Obtain comment
            if (context.comment() != null)
            {
                var commentCtx = context.comment();
                CurrentComment = commentCtx.GetText();
                CommentSpan    = new TextSpan(commentCtx.Start.StartIndex, commentCtx.Stop.StopIndex + 1);
            }

            // --- Now, we have every part of the line, and create some special main instruction part
            if (context.exception != null)
            {
                mainInstructionPart = new ParserErrorLine();
            }
            else if (mainInstructionPart == null && (CurrentLabel != null || CurrentComment != null))
            {
                mainInstructionPart = new NoInstructionLine();
                if (CurrentLabel != null && !CurrentLabelColon)
                {
                    var statementFound = true;
                    switch (CurrentLabel.ToLower())
                    {
                    case "continue":
                        mainInstructionPart = new ContinueStatement();
                        break;

                    case "break":
                        mainInstructionPart = new BreakStatement();
                        break;

                    case "endm":
                    case "mend":
                        mainInstructionPart = new MacroEndStatement();
                        break;

                    case "endl":
                    case "lend":
                        mainInstructionPart = new LoopEndStatement();
                        break;

                    case "proc":
                        mainInstructionPart = new ProcStatement();
                        break;

                    case "endp":
                    case "pend":
                        mainInstructionPart = new ProcEndStatement();
                        break;

                    case "repeat":
                        mainInstructionPart = new RepeatStatement();
                        break;

                    case "endw":
                    case "wend":
                        mainInstructionPart = new WhileEndStatement();
                        break;

                    case "ends":
                        mainInstructionPart = new StructEndStatement();
                        break;

                    case "else":
                        mainInstructionPart = new ElseStatement();
                        break;

                    case "endif":
                        mainInstructionPart = new IfEndStatement();
                        break;

                    default:
                        statementFound = false;
                        break;
                    }

                    if (statementFound)
                    {
                        KeywordSpan  = new TextSpan(context.Start.StartIndex, context.Start.StopIndex + 1);
                        CurrentLabel = null;
                    }
                }
            }

            return(mainInstructionPart is SourceLineBase sourceLine
                ? AddLine(sourceLine, context)
                : mainInstructionPart);
        }
示例#5
0
        public void Evaluate(List <Step> evaluationSteps)
        {
            for (int index = 0; index < evaluationSteps.Count; index++)
            {
                Step evalStep = evaluationSteps[index];
                // .Type() can only be "VAR_DECLARE", "VAR_CHANGE", "FUNC_CALL", "IF_STATEMENT", "WHILE_LOOP"
                // It could also be "ELSE_STATEMENT", but we should only check for that DIRECTLY after an IF_STATEMENT
                if (evalStep.Type().Equals("IF_STATEMENT"))
                {
                    // Evaluate if statement - contains OPERAND1, OPERAND2, COMPARISON, codeBlockContents
                    IfStatement ifState         = (IfStatement)evalStep;                                                                           // Cast as we know it is now an IfStatement obj
                    bool        conditionResult = CompareExpressions(ifState.GetOp1(), ifState.GetOp2(), ifState.GetComparator());
                    bool        hasElse         = index + 1 < evaluationSteps.Count && evaluationSteps[index + 1].Type().Equals("ELSE_STATEMENT"); // No chance of index out of range error as set to False before reaching it

                    if (conditionResult)
                    // If the 'IfStatement' condition is TRUE
                    {
                        Evaluate(ifState.GetCBContents()); // 'run' the contents of the if statement - this is RECURSIVE
                        if (hasElse)
                        {
                            evaluationSteps.RemoveAt(index + 1);
                        }
                        // If we have an ELSE_STATEMENT after this, we need to remove it as the IF_STATEMENT has triggered (therefore the ELSE will not be triggered).
                    }
                    else if (hasElse)
                    {
                        // If the CONDITION is FALSE and the next Step obj is an ELSE_STATEMENT type

                        ElseStatement elseState = (ElseStatement)evaluationSteps[index + 1];
                        // Cast to else
                        Evaluate(elseState.GetCBContents()); // 'run' the contents of the else (RECURSION)

                        evaluationSteps.RemoveAt(index + 1); // Remove ELSE_STATEMENT as we have used it and do not want to go over it again.
                    }
                }
                else if (evalStep.Type().Equals("WHILE_LOOP"))
                {
                    WhileLoop whileLoop = (WhileLoop)evalStep;
                    // Similar to if statement evaluation though no need to set a 'condition' variable because that condition may change
                    // Basically just reusing the C# while loop with the template of the Interpreted one
                    while (CompareExpressions(whileLoop.GetOp1(), whileLoop.GetOp2(), whileLoop.GetComparator()))
                    {
                        // While the condition is true, evaluate code inside
                        Evaluate(whileLoop.GetCBContents());
                    }
                }
                else if (evalStep.Type().Equals("VAR_DECLARE"))
                // Declare a variable in the variableScope
                {
                    VarDeclare varDecl = (VarDeclare)evalStep; // Cast as we know it's a VarDeclare obj
                    if (variableScope.ContainsKey(varDecl.GetName()))
                    {
                        throw new DeclareError();
                    }
                    // If scope already has a variable that name, you cannot redeclare it as it already exists.
                    // Potential endpoint if variable exists - entire program will stop (crash).
                    Token varExpr = ResolveExpression(varDecl.Value());

                    if (!varExpr.Type().Equals(varDecl.GetVarType()))
                    {
                        throw new TypeError();
                    }
                    // Value of variable does not match type with declared one. e.g 'int x = "Hello";'


                    variableScope.Add(varDecl.GetName(), varExpr);
                    // Type of variable can be found out by the .Type() of the key's Token.
                    // e.g 'int x = 1 + 2;'
                    // if we want to find variable 'x' type, we find variableScope[x].Type() which will return 'number', with variableScope[x].Value() being '3'
                }
                else if (evalStep.Type().Equals("VAR_CHANGE"))
                // Change a pre-existing variable
                {
                    VarChange varChan = (VarChange)evalStep; // Cast as we know it is a VarChange obj

                    if (!variableScope.ContainsKey(varChan.GetName()))
                    {
                        throw new ReferenceError();
                    }
                    // If variable is NOT in the variableScope then we cannot change it as it doesn't exist.
                    // Potential endpoint for program crash
                    string varType  = variableScope[varChan.GetName()].Type();
                    Token  newValue = ResolveExpression(varChan.Value());

                    if (!varType.Equals(newValue.Type()))
                    {
                        throw new TypeError();
                    }
                    // If the new value of the variable is not the right type, then crash.
                    // Potential endpoint
                    // e.g int x = 0; x = "hi"; will cause this error
                    variableScope[varChan.GetName()] = newValue; // Assign new value (Token)
                }
                else if (evalStep.Type().Equals("FUNC_CALL"))
                // Call a function
                {
                    FuncCall functionCall = (FuncCall)evalStep;                                                   // Cast as we know it is a FuncCall obj now
                    if (!functionCall.GetName().Equals("inputstr") && !functionCall.GetName().Equals("inputint")) // If NOT calling 'input' function
                    {
                        CallFunction(functionCall.GetName(), ResolveExpression(functionCall.GetArguments()));
                        // Call function with name and *resolved* list of arguments
                        // Resolve function always outputs a single token which is the result of an expression (list of tokens) being evaluated
                    }
                    else
                    // SPECIAL CASE: Calling inputStr or inputInt functions indicates that the 'argument' is NOT an expression to be resolved, but rather a variable name to store input value in.
                    // This means functionCall.Argumnets() will only have 1 token:
                    {
                        CallFunction(functionCall.GetName(), functionCall.GetArguments()[0]); // Pass in first value in Arguments as there only should be one - the variable to be input to
                    }
                }
                else
                {
                    throw new SyntaxError();  // Unrecognised Step, crash program.
                }
            }
        }
示例#6
0
        public override XElement ToXML()
        {
            var conditionXml = new XElement("Condition");

            if (IsComparison)
            {
                if (IsComparisonToConstant)
                {
                    conditionXml.Add(new XElement("Type", "ComparisonToConstant"));
                }
                else if (IsComparisonToVariable)
                {
                    conditionXml.Add(new XElement("Type", "ComparisonToVariable"));
                }

                if (this.SelectedVariable != null && this.SelectedVariable.LinkedVariable != null)
                {
                    conditionXml.Add(new XElement("Left", this.SelectedVariable.LinkedVarId));
                }


                if (this.IsEquals)
                {
                    conditionXml.Add(new XElement("Comparison", "EQ"));
                }
                else if (this.IsNotEquals)
                {
                    conditionXml.Add(new XElement("Comparison", "NEQ"));
                }
                else if (this.IsLessThan)
                {
                    conditionXml.Add(new XElement("Comparison", "LT"));
                }
                else if (this.IsLessThanOrEqualTo)
                {
                    conditionXml.Add(new XElement("Comparison", "LTE"));
                }
                else if (this.IsGreaterThan)
                {
                    conditionXml.Add(new XElement("Comparison", "GT"));
                }
                else if (this.IsGreaterThanOrEqualTo)
                {
                    conditionXml.Add(new XElement("Comparison", "GTE"));
                }

                if (this.IsComparisonToVariable && this.VariableToCompare != null && this.VariableToCompare.LinkedVariable != null)
                {
                    conditionXml.Add(new XElement("Right", this.VariableToCompare.LinkedVarId));
                    if (this.IsDateTime)
                    {
                        conditionXml.Add(new XElement("VarType", "DateTime"));
                    }
                    else if (this.IsNumber)
                    {
                        conditionXml.Add(new XElement("VarType", "Number"));
                    }
                    else if (this.IsString)
                    {
                        conditionXml.Add(new XElement("VarType", "String"));
                    }
                }
                if (this.IsComparisonToConstant)
                {
                    if (this.IsDateTime)
                    {
                        conditionXml.Add(new XElement("Right", this.DateTimeToCompareTo.ToString()));
                        conditionXml.Add(new XElement("VarType", "DateTime"));
                    }
                    else if (this.IsNumber)
                    {
                        conditionXml.Add(new XElement("Right", this.NumberToCompareTo.ToString()));
                        conditionXml.Add(new XElement("VarType", "Number"));
                    }
                    else if (this.IsString)
                    {
                        conditionXml.Add(new XElement("Right", this.StringToCompareTo));
                        conditionXml.Add(new XElement("VarType", "String"));
                    }
                }
            }
            if (ItemIsNotNull)
            {
                conditionXml.Add(new XElement("Type", "ItemIsNotNull"));
                conditionXml.Add(new XElement("VarRef", this.SelectedVariable.LinkedVarId));
            }
            if (ItemIsClass)
            {
                conditionXml.Add(new XElement("Type", "ItemIsClass"));
                conditionXml.Add(new XElement("VarRef", this.SelectedVariable.LinkedVarId));
                conditionXml.Add(new XElement("ClassName", this.SelectedClassName));
            }
            if (PlayerHasItem)
            {
                conditionXml.Add(new XElement("Type", "PlayerHasItem"));
                conditionXml.Add(new XElement("ItemRef", this.SelectedItem.LinkedItemId));
            }
            return(new XElement("If", conditionXml,
                                new XElement("Then", ThenStatement.ToXML()),
                                new XElement("Else", ElseStatement.ToXML())));
        }
        private void CheckStatementCurlyBracketPlacement(Element element, Statement statement)
        {
            Param.AssertNotNull(element, "element");
            Param.AssertNotNull(statement, "statement");

            switch (statement.StatementType)
            {
            case StatementType.Else:
                // Check that there is nothing between the starting else keyword and the opening bracket.
                this.CheckChainedStatementCurlyBracketPlacement(element, statement);
                this.CheckBlockStatementsCurlyBracketPlacement(element, statement);

                // Check that there is nothing between the closing bracket and the else keyword of the attached else statement.
                ElseStatement elseStatement = (ElseStatement)statement;
                if (elseStatement.AttachedElseStatement != null)
                {
                    this.CheckTrailingStatementCurlyBracketPlacement(element, statement);
                }

                break;

            case StatementType.Catch:
            case StatementType.Finally:
                // Check that there is nothing between the starting catch or finally keyword and the opening bracket.
                this.CheckChainedStatementCurlyBracketPlacement(element, statement);
                this.CheckBlockStatementsCurlyBracketPlacement(element, statement);
                break;

            case StatementType.If:
                this.CheckBlockStatementsCurlyBracketPlacement(element, statement);

                // Check that there is nothing between the closing bracket and the else keyword of the attached else statement.
                IfStatement ifStatement = (IfStatement)statement;
                if (ifStatement.AttachedElseStatement != null)
                {
                    this.CheckTrailingStatementCurlyBracketPlacement(element, statement);
                }

                break;

            case StatementType.Try:
                // Check that there is nothing between the starting try keyword and the opening bracket.
                this.CheckBlockStatementsCurlyBracketPlacement(element, statement);

                TryStatement tryStatement = (TryStatement)statement;
                if (tryStatement.FinallyStatement != null || (tryStatement.CatchStatements != null && tryStatement.CatchStatements.Count > 0))
                {
                    // There is something attached to the end of this try statement. Check that there is nothing between
                    // the closing bracket of the try statement and the start of the attached statement.
                    this.CheckTrailingStatementCurlyBracketPlacement(element, tryStatement);
                }

                if (tryStatement.CatchStatements != null && tryStatement.CatchStatements.Count > 0)
                {
                    CatchStatement[] catchStatementArray = new CatchStatement[tryStatement.CatchStatements.Count];
                    tryStatement.CatchStatements.CopyTo(catchStatementArray, 0);

                    for (int i = 0; i < catchStatementArray.Length; ++i)
                    {
                        if (catchStatementArray.Length > i + 1 || tryStatement.FinallyStatement != null)
                        {
                            // There is something attached to the end of this catch statement, either another catch or a finally.
                            // Check that there is nothing between the closing bracket of this catch statement and the start of the attached
                            // statement.
                            this.CheckTrailingStatementCurlyBracketPlacement(element, catchStatementArray[i]);
                        }
                    }
                }

                break;

            case StatementType.Checked:
            case StatementType.Fixed:
            case StatementType.For:
            case StatementType.Foreach:
            case StatementType.Lock:
            case StatementType.Switch:
            case StatementType.Unchecked:
            case StatementType.Unsafe:
            case StatementType.Using:
            case StatementType.While:
                // Check that there is nothing between the starting keyword and the opening bracket.
                this.CheckBlockStatementsCurlyBracketPlacement(element, statement);
                break;

            case StatementType.DoWhile:
                this.CheckBlockStatementsCurlyBracketPlacement(element, statement);
                this.CheckTrailingStatementCurlyBracketPlacement(element, statement);
                break;

            default:
                break;
            }
        }
示例#8
0
 protected abstract void VisitElse(ElseStatement statement);
示例#9
0
 public void Visit(ElseStatement l)
 {
     _level -= _templatetabs;
     Format(l);
     _level += _templatetabs;
 }
示例#10
0
 public void Visit(ElseStatement l)
 {
     VisitStatementComment(l);
 }