Beispiel #1
0
        public void consumeCurrentToken()
        {
            Token nextToken;

            if (m_nextTokenIndex < m_tokens.Count) {
                nextToken = m_tokens[m_nextTokenIndex];
                m_nextTokenIndex++;
            }
            else {
                nextToken = new Token(Token.TokenType.EOF, "<EOF>");
            }

            m_lookahead[m_lookaheadIndex] = nextToken;
            m_lookaheadIndex = (m_lookaheadIndex + 1) % k;
        }
Beispiel #2
0
        public void backtrackToToken(Token pToken)
        {
            m_nextTokenIndex = 0;
            m_lookaheadIndex = 0;

            for (int i = 0; i < k; i++) {
                consumeCurrentToken();
            }

            while (lookAhead(1) != pToken) {
                consumeCurrentToken ();
            }

            Console.WriteLine ("Found token");
        }
Beispiel #3
0
 public AST(Token token)
 {
     m_token = token;
     nrOfASTsInMemory++;
 }
 public AST_FunctionCall(Token token)
     : base(token)
 {
 }
Beispiel #5
0
        void AddLocalVariables(AST ast, VariableDefinition[] variableDefinitions)
        {
            AST nodeForDefiningGlobalVariables = ast.getChild(0).getChild(0);

            if(variableDefinitions == null) { return; }

            foreach(VariableDefinition vd in variableDefinitions) {

                Token token = new Token(Token.TokenType.VAR_DECLARATION,"<VAR_DECL>", ast.getToken().LineNr, ast.getToken().LinePosition);

                AST_VariableDeclaration declarationTree =
                    new AST_VariableDeclaration(token,
                                                vd.initValue.getReturnValueType(),
                                                vd.variableName);

                if(vd.initValue != null)
                {
                    AST assignmentTree = CreateAssignmentTreeFromInitValue(vd.variableName, vd.initValue);
                    AST declarationAndAssignmentTree = new AST(new Token(Token.TokenType.STATEMENT_LIST, "<DECLARATION_AND_ASSIGNMENT>", declarationTree.getToken().LineNr, declarationTree.getToken().LinePosition));
                    declarationAndAssignmentTree.addChild(declarationTree);
                    declarationAndAssignmentTree.addChild(assignmentTree);
                    nodeForDefiningGlobalVariables.addChild(declarationAndAssignmentTree);
                }
                else
                {
                    nodeForDefiningGlobalVariables.addChild(declarationTree);
                }
            }
        }
Beispiel #6
0
 public AST_LoopBlockNode(Token token)
     : base(token)
 {
 }
Beispiel #7
0
 public AST_IfNode(Token token )
     : base(token)
 {
 }
Beispiel #8
0
 public AST(Token token)
 {
     m_token = token;
 }
 public AST_ArrayEndSignal(Token token)
     : base(token)
 {
 }
Beispiel #10
0
 void checkRightHandSide(AST rhs, Token operatorToken)
 {
     if(rhs == null) {
         throw new Error("No expression on the right side of '" + operatorToken.getTokenString() + "'",
             Error.ErrorType.SYNTAX, operatorToken.LineNr, operatorToken.LinePosition + 2);
     }
 }
Beispiel #11
0
 void checkLeftHandSide(AST lhs, Token operatorToken)
 {
     if(lhs == null) {
         throw new Error("No expression on the left side of '" + operatorToken.getTokenString() + "'",
             Error.ErrorType.SYNTAX, operatorToken.LineNr, operatorToken.LinePosition - 1);
     }
 }
Beispiel #12
0
        private AST assignmentToArray()
        {
            #if WRITE_DEBUG_INFO
            Console.WriteLine("assignment to array");
            #endif

            Token nameToken = null;
            AST indexNode = null;

            #if WRITE_DEBUG_INFO
                Console.WriteLine("normal array");
            #endif
            nameToken = match(Token.TokenType.NAME);
            match(Token.TokenType.BRACKET_LEFT);
            indexNode = expression();
            match(Token.TokenType.BRACKET_RIGHT);

            if(lookAheadType(1) == Token.TokenType.NEW_LINE ||
               lookAheadType(1) == Token.TokenType.EOF)
            {
                // it's a statement without assignment
                return indexNode;
            }

            Token assignmentToken = match(Token.TokenType.ASSIGNMENT);
            AST expressionTree = expression();

            if(expressionTree != null) {
                Token arrayAssignmentToken = new Token(Token.TokenType.ASSIGNMENT_TO_ARRAY, "=", assignmentToken.LineNr, assignmentToken.LinePosition);
                AST_Assignment assignmentTree = new AST_Assignment(arrayAssignmentToken, nameToken.getTokenString());
                assignmentTree.addChild(indexNode);
                assignmentTree.addChild(expressionTree);

                /*ASTPainter p = new ASTPainter();
                Console.WriteLine("---AST---");
                p.PaintAST(assignmentTree);
                Console.WriteLine("---------");*/
                return assignmentTree;
            }
            else {
                throw new Error("The expression after = makes no sense", Error.ErrorType.SYNTAX,
                    assignmentToken.LineNr, assignmentToken.LinePosition);
            }
        }
Beispiel #13
0
        public virtual Token match(Token.TokenType expectedTokenType)
        {
            Token matchedToken = lookAhead(1);

            if(lookAheadType(1) == expectedTokenType) {
            #if WRITE_DEBUG_INFO
            Console.WriteLine("MATCHED TOKEN " + lookAhead(1).getTokenString() + " (line " + lookAhead(1).LineNr + ")");
            #endif
                consumeCurrentToken();

            } else {
            #if WRITE_DEBUG_INFO
            Console.WriteLine("FAILED TO MATCH TOKEN OF TYPE " + expectedTokenType.ToString() +
                    " ...FOUND " + lookAhead(1).getTokenString() + " (line " + lookAhead(1).LineNr + ")");
            #endif
                throw new Error(
                    "The code word '" + lookAhead(1).getTokenString() + "'" +
                    " does not compute. Expected " + expectedTokenType,
                    Error.ErrorType.SYNTAX,
                    lookAhead(1).LineNr,
                    lookAhead(1).LinePosition);
            }

            return matchedToken;
        }
 public AST_Assignment(Token token, string variableName)
     : base(token)
 {
     m_variableName = variableName;
 }
Beispiel #15
0
 public AST_LoopNode(Token token )
     : base(token)
 {
 }
Beispiel #16
0
 public void addChild(Token token)
 {
     AST childTree = new AST(token);
     addChild(childTree);
 }
 public AST_FunctionDefinitionNode(Token token)
     : base(token)
 {
 }
 public AST_VariableDeclaration(Token token, ReturnValueType type, string name)
     : base(token)
 {
     m_type = type;
     m_name = name;
 }