本文整理汇总了Python中Racecar.Parser.parseString方法的典型用法代码示例。如果您正苦于以下问题:Python Parser.parseString方法的具体用法?Python Parser.parseString怎么用?Python Parser.parseString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Racecar.Parser
的用法示例。
在下文中一共展示了Parser.parseString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_assign_word_print_complicated
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_assign_word_print_complicated(self):
test_string = \
"""color is a word
set color to "blue"
print color
c2 is a word
set c2 to "green"
set color to c2
"""
correct_translation = \
"""color = None
color = "blue"
print_to_console(color)
c2 = None
c2 = "green"
color = c2
"""
result = Compiler.getPythonCode(test_string)
ast = Parser.parseString(test_string)
saErrors = SemanticAnalyzer.analyzeStart(ast)
self.assertEqual(len(saErrors), 0)
self.assertEqual(len(ast.errors), 0)
self.assertEqual(result[0], correct_translation)
示例2: test_function_invocation_no_params
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_function_invocation_no_params(self):
test_string = \
"""define moveBackwardFive
{
drive backward 5
}
define moveForwardThenBackward
{
drive forward 5
moveBackwardFive
}
moveForwardThenBackward
"""
correct_translation = \
"""def moveBackwardFive():
translate_car(5, CarDirection.BACKWARDS)
def moveForwardThenBackward():
translate_car(5, CarDirection.FORWARDS)
moveBackwardFive()
moveForwardThenBackward()
"""
result = Compiler.getPythonCode(test_string)
ast = Parser.parseString(test_string)
saErrors = SemanticAnalyzer.analyzeStart(ast)
self.assertEqual(len(saErrors), 0)
self.assertEqual(len(ast.errors), 0)
self.assertEqual(result[0], correct_translation)
示例3: test_function_invocation_with_two_parameters
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_function_invocation_with_two_parameters(self):
test_string = \
"""define turnLeftThenDriveStraight using numStepsTurn \
(number) and numStepsDrive (number)
{
turn left
drive forward numStepsTurn steps
turn right
drive forward numStepsDrive steps
}
turnLeftThenDriveStraight 5 10
"""
correct_translation = \
"""def turnLeftThenDriveStraight(numStepsTurn, numStepsDrive):
rotate_car(WheelDirection.LEFT)
translate_car(numStepsTurn, CarDirection.FORWARDS)
rotate_car(WheelDirection.RIGHT)
translate_car(numStepsDrive, CarDirection.FORWARDS)
turnLeftThenDriveStraight(5, 10)
"""
result = Compiler.getPythonCode(test_string)
ast = Parser.parseString(test_string)
saErrors = SemanticAnalyzer.analyzeStart(ast)
self.assertEqual(len(saErrors), 0)
self.assertEqual(len(ast.errors), 0)
self.assertEqual(result[0], correct_translation)
示例4: test_if_elseif_else_statement
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_if_elseif_else_statement(self):
test_string = \
"""if 1
{
print "yay"
}
elseIf 2
{
print "no"
}
else
{
print "done"
}
"""
correct_translation = \
"""if 1:
print_to_console("yay")
elif 2:
print_to_console("no")
else:
print_to_console("done")
"""
result = Compiler.getPythonCode(test_string)
ast = Parser.parseString(test_string)
saErrors = SemanticAnalyzer.analyzeStart(ast)
self.assertEqual(len(saErrors), 0)
self.assertEqual(len(ast.errors), 0)
self.assertEqual(result[0], correct_translation)
示例5: test_if_statement_nested
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_if_statement_nested(self):
test_string = \
"""if 1
{
print "yay"
if 1
{
print "yahoo"
}
}
"""
correct_translation = \
"""if 1:
print_to_console("yay")
if 1:
print_to_console("yahoo")
"""
result = Compiler.getPythonCode(test_string)
ast = Parser.parseString(test_string)
saErrors = SemanticAnalyzer.analyzeStart(ast)
self.assertEqual(len(saErrors), 0)
self.assertEqual(len(ast.errors), 0)
self.assertEqual(result[0], correct_translation)
示例6: test_loop_for_nested
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_loop_for_nested(self):
test_string = \
"""myCounter is a number
set myCounter to 10
myCounter2 is a number
set myCounter2 to 10
repeat myCounter times
{
drive forward 1 step
repeat myCounter2 times
{
drive forward 1 step
}
}
"""
correct_translation = \
"""myCounter = None
myCounter = 10
myCounter2 = None
myCounter2 = 10
for x in range(myCounter):
translate_car(1, CarDirection.FORWARDS)
for x in range(myCounter2):
translate_car(1, CarDirection.FORWARDS)
"""
result = Compiler.getPythonCode(test_string)
ast = Parser.parseString(test_string)
saErrors = SemanticAnalyzer.analyzeStart(ast)
self.assertEqual(len(saErrors), 0)
self.assertEqual(len(ast.errors), 0)
self.assertEqual(result[0], correct_translation)
示例7: test_loop_while
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_loop_while(self):
test_string = \
"""myCounter is a number
set myCounter to 1
repeat if myCounter is not 5
{
drive forward 1 step
set myCounter to myCounter + 1
}
"""
correct_translation = \
"""myCounter = None
myCounter = 1
while myCounter != 5:
translate_car(1, CarDirection.FORWARDS)
myCounter = ((myCounter) + (1))
"""
result = Compiler.getPythonCode(test_string)
ast = Parser.parseString(test_string)
saErrors = SemanticAnalyzer.analyzeStart(ast)
self.assertEqual(len(saErrors), 0)
self.assertEqual(len(ast.errors), 0)
self.assertEqual(result[0], correct_translation)
示例8: test_drive_backwards
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_drive_backwards(self):
test_string1 = \
"""drive backwards 10 steps
"""
test_string2 = \
"""drive backward 10 steps
"""
test_string3 = \
"""drive backwards 10 step
"""
test_string4 = \
"""drive backward 10 step
"""
correct_translation = \
"""translate_car(10, CarDirection.BACKWARDS)
"""
ast1 = Parser.parseString(test_string1)
ast2 = Parser.parseString(test_string1)
ast3 = Parser.parseString(test_string1)
ast4 = Parser.parseString(test_string1)
self.assertEqual(len(ast1.errors), 0)
self.assertEqual(len(ast2.errors), 0)
self.assertEqual(len(ast3.errors), 0)
self.assertEqual(len(ast4.errors), 0)
result1 = Compiler.getPythonCode(test_string1)
result2 = Compiler.getPythonCode(test_string2)
result3 = Compiler.getPythonCode(test_string3)
result4 = Compiler.getPythonCode(test_string4)
saErrors1 = SemanticAnalyzer.analyzeStart(ast1)
saErrors2 = SemanticAnalyzer.analyzeStart(ast2)
saErrors3 = SemanticAnalyzer.analyzeStart(ast3)
saErrors4 = SemanticAnalyzer.analyzeStart(ast4)
self.assertEqual(len(saErrors1), 0)
self.assertEqual(len(saErrors2), 0)
self.assertEqual(len(saErrors3), 0)
self.assertEqual(len(saErrors4), 0)
self.assertEqual(result1[0], correct_translation)
self.assertEqual(result2[0], correct_translation)
self.assertEqual(result3[0], correct_translation)
self.assertEqual(result4[0], correct_translation)
示例9: test_built_in_functions_params
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_built_in_functions_params(self):
test_string = \
"""drive forwards five steps
"""
ast = Parser.parseString(test_string)
self.assertEqual(len(ast.errors), 0, "Test failed at parser.")
saErrors = SemanticAnalyzer.analyzeStart(ast)
self.assertEqual(len(saErrors), 1)
示例10: test_template
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_template(self):
test_string = \
"""
"""
ast = Parser.parseString(test_string)
self.assertEqual(len(ast.errors), 0, "Test failed at parser.")
saErrors = SemanticAnalyzer.analyzeStart(ast)
self.assertEqual(len(saErrors), 0)
示例11: test_print_undeclared_var
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_print_undeclared_var(self):
test_string = \
"""print myNum
"""
ast = Parser.parseString(test_string)
self.assertEqual(len(ast.errors), 0, "Test failed at parser.")
saErrors = SemanticAnalyzer.analyzeStart(ast)
self.assertEqual(len(saErrors), 1)
示例12: test_basic
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_basic(self):
test_string = \
"""drive forward 5 steps
"""
ast = Parser.parseString(test_string)
self.assertEqual(len(ast.errors), 0, "Test failed at parser.")
saErrors = SemanticAnalyzer.analyzeStart(ast)
self.assertEqual(len(saErrors), 0)
示例13: test_calling_nonexistant_function
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_calling_nonexistant_function(self):
test_string = \
"""fullTurn "left"
"""
ast = Parser.parseString(test_string)
self.assertEqual(len(ast.errors), 0, "Test failed at parser.")
saErrors = SemanticAnalyzer.analyzeStart(ast)
self.assertEqual(len(saErrors), 1)
示例14: test_assignments_var_str_literal
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_assignments_var_str_literal(self):
test_string = \
"""myNum is a number
set myNum to "hello"
"""
ast = Parser.parseString(test_string)
self.assertEqual(len(ast.errors), 0, "Test failed at parser.")
saErrors = SemanticAnalyzer.analyzeStart(ast)
self.assertEqual(len(saErrors), 1)
示例15: test_set_undeclared_var
# 需要导入模块: from Racecar import Parser [as 别名]
# 或者: from Racecar.Parser import parseString [as 别名]
def test_set_undeclared_var(self):
test_string = \
"""set myNum to 10
"""
ast = Parser.parseString(test_string)
self.assertEqual(len(ast.errors), 0, "Test failed at parser.")
saErrors = SemanticAnalyzer.analyzeStart(ast)
#should have an error saying mySecondNum doesnt exist
self.assertEqual(len(saErrors), 1)