Exemplo n.º 1
0
        private static bool NextNode(CharEnumerator enumerator, out INode node)
        {
            do
            {
                switch (enumerator.Current)
                {
                case '>':
                    node = new MoveRightNode();
                    return(true);

                case '<':
                    node = new MoveLeftNode();
                    return(true);

                case '+':
                    node = new IncrementNode();
                    return(true);

                case '-':
                    node = new DecrementNode();
                    return(true);

                case ',':
                    node = new InputNode();
                    return(true);

                case '.':
                    node = new OutputNode();
                    return(true);

                case '[':
                    var nodes = new List <INode>();
                    while (enumerator.MoveNext())
                    {
                        switch (enumerator.Current)
                        {
                        case ']':
                            node = new LoopNode(nodes);
                            return(true);

                        default:
                            if (NextNode(enumerator, out var nextNode))
                            {
                                nodes.Add(nextNode);
                            }
                            break;
                        }
                    }
                    throw new InvalidBFProgramException("Expected more symbols after encountering a loop begin symbol", true);

                case ']':
                    throw new InvalidBFProgramException("Encountered a loop end symbol without matching loop begin", false);

                default:
                    break;
                }
            }while (enumerator.MoveNext());
            node = null;
            return(false);
        }
Exemplo n.º 2
0
 public void Visit(DecrementNode node)
 {
     foreach (var subnode in node)
     {
         Visit((dynamic)subnode);
     }
 }
Exemplo n.º 3
0
        private object DecrementNode(DecrementNode d)
        {
            var    name = d.l.Name;
            object output;

            if (variables.TryGetValue(name, out output))
            {
                if (output is decimal)
                {
                    variables[name] = ((decimal)output) + 1;
                    return(((decimal)output) + 1);
                }
                else
                {
                    throw(new Exception($"Can't decrement {output.GetType()}"));
                }
            }
            else
            {
                throw(new Exception("Undefined Variable"));
            }
        }
Exemplo n.º 4
0
        public Node Stmt()
        {
            switch (CurrentToken)
            {
            case TokenCategory.IDENTIFIER:
                var idToken = Expect(TokenCategory.IDENTIFIER);
                switch (CurrentToken)
                {
                case TokenCategory.ASSIGN:
                    Expect(TokenCategory.ASSIGN);
                    var ass = new AssignmentNode()
                    {
                        AnchorToken = idToken
                    };
                    ass.Add(Expr());
                    Expect(TokenCategory.SEMICOLON);
                    return(ass);

                case TokenCategory.INCREMENT:
                    Expect(TokenCategory.INCREMENT);

                    var inc = new IncrementNode()
                    {
                        AnchorToken = idToken
                    };
                    Expect(TokenCategory.SEMICOLON);
                    return(inc);

                case TokenCategory.DECREMENT:
                    Expect(TokenCategory.DECREMENT);
                    var dec = new DecrementNode()
                    {
                        AnchorToken = idToken
                    };
                    Expect(TokenCategory.SEMICOLON);
                    return(dec);

                case TokenCategory.PARENTHESIS_OPEN:
                    Expect(TokenCategory.PARENTHESIS_OPEN);
                    var fun = new FunCallNode()
                    {
                        AnchorToken = idToken
                    };

                    fun.Add(ExprList());

                    Expect(TokenCategory.PARENTHESIS_CLOSE);
                    Expect(TokenCategory.SEMICOLON);
                    return(fun);
                }
                break;

            case TokenCategory.IF:
                return(If());

            case TokenCategory.WHILE:
                return(While());

            case TokenCategory.BREAK:
                var bre = new StmtNode()
                {
                    AnchorToken = Expect(TokenCategory.BREAK)
                };
                Expect(TokenCategory.SEMICOLON);
                return(bre);

            case TokenCategory.RETURN:
                return(Return());

            case TokenCategory.SEMICOLON:
                return(new StmtNode()
                {
                    AnchorToken = Expect(TokenCategory.SEMICOLON)
                });

            default:
                throw new SyntaxError(firstOfStmt, tokenStream.Current);
            }
            throw new SyntaxError(firstOfStmt, tokenStream.Current);
        }