示例#1
0
    def testReturnStatement(self):
        inputs = '''return 5;
                    return 10;
                    return 838383;    :'''
        lex = Lexer(inputs)
        curtoken = lex.nextToken()
        peektoken = lex.nextToken()
        pas = Parser(lex, curtoken, peektoken)
        program = pas.parseProgram()

        self.assertEquals(3, len(program.statements))
示例#2
0
    def testErrors(self):
        inputs = '''let five  5;
                    let =  10;
                    let   838383;    :'''
        lex = Lexer(inputs)
        curtoken = lex.nextToken()
        peektoken = lex.nextToken()
        pas = Parser(lex, curtoken, peektoken)
        program = pas.parseProgram()

        self.assertEquals(3, len(pas.errors))
        print 'Number of error is {}'.format(len(pas.errors))
示例#3
0
    def testLetStatement(self):
        inputs = '''let five = 5;
                    let ten = 10;
                    let foobar = 838383;    :'''
        lex = Lexer(inputs)
        curtoken = lex.nextToken()
        peektoken = lex.nextToken()
        pas = Parser(lex, curtoken, peektoken)
        program = pas.parseProgram()

        self.assertNotEqual(program, None)
        self.assertEquals(3, len(program.statements))
        for i in range(len(program.statements) - 1):
            self.assertEquals(program.statements[i].tokenLiteral(), 'let')
示例#4
0
 def test_nextToken(self):
     inputs = '''let five = 5;
                 let ten = 10;
                 let add = fn(x, y) {
                 x + y;
                 };
                 let result = add(five, ten);
                 !-/*5;
                 5 < 10 > 5;
                 10 == 10;
                 10 != 9;'''
     l = Lexer(inputs)
     while l.readPosition <= len(inputs):
         tk = l.nextToken()
         print 'Literal is ' + tk.literal
         self.assertEquals(tk.tokenType, testToken[tk.literal])