Example #1
0
    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)
Example #2
0
    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)
Example #3
0
    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)
Example #4
0
    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)
Example #5
0
    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)
Example #6
0
    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)
Example #7
0
    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)
Example #8
0
    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)
Example #9
0
    def test_template(self):
        test_string = \
            """
"""
        correct_translation = \
            """"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Example #10
0
    def test_get_car_position(self):
        test_string = \
            """print getCarPosition
"""
        correct_translation = \
            """print_to_console(getCurrentPosition())
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Example #11
0
    def test_empty_statement(self):
        test_string = \
            """
"""
        correct_translation = \
            """"""
        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)
Example #12
0
    def test_all_expression(self):
        test_string = \
            """print (1 + 2 * (3 + 4))
"""
        correct_translation = \
            """print_to_console(((1) + (((2) * (((3) + (4)))))))
"""
        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)
Example #13
0
    def test_comment_singleline(self):
        test_string = \
            """:) this is a single line comment
drive forward 5 steps
"""
        correct_translation = \
            """"""
        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)
Example #14
0
    def test_declare(self):
        test_string = \
            """myNum is a number
"""
        correct_translation = \
            """myNum = None
"""
        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)
Example #15
0
    def test_print(self):
        test_string = \
            """print "hello world"
"""
        correct_translation = \
            """print_to_console("hello world")
"""
        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)
Example #16
0
    def test_turn_right(self):
        test_string = \
            """turn right
"""
        correct_translation = \
            """rotate_car(WheelDirection.RIGHT)
"""
        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)
Example #17
0
    def test_if_plus(self):
        test_string = \
            """if 2 + 5
{
    print "yay"
}
"""
        correct_translation = \
            """if ((2) + (5)):
    print_to_console("yay")
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Example #18
0
    def test_can_move(self):
        test_string = \
            """if canDrive forward 5 steps
{
    drive forward 5 steps
}
"""
        correct_translation = \
            """if can_move(5, CarDirection.FORWARDS):
    translate_car(5, CarDirection.FORWARDS)
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Example #19
0
    def test_string_concatenation_complicated(self):
        test_string = \
            """myWord is a word
set myWord to "hi "
print "hey" ++ myWord ++ "now"
"""
        correct_translation = \
            """myWord = None
myWord = "hi "
print_to_console((str((str("hey") + str(myWord))) + str("now")))
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Example #20
0
    def test_boolean_opeartors(self):
        test_string = \
            """if 1 < 2
{
    print "yes"
}
elseIf 1 is 2
{
    print "yes"
}
if 1 >= 2
{
    print "no"
}
elseIf 1 is not 2
{
    print "yes"
}
elseIf 1 > 2
{
    print "yes"
}
elseIf 1 < 2
{
    print "yes"
}
"""
        correct_translation = \
            """if 1 < 2:
    print_to_console("yes")
elif 1 == 2:
    print_to_console("yes")
if 1 >= 2:
    print_to_console("no")
elif 1 != 2:
    print_to_console("yes")
elif 1 > 2:
    print_to_console("yes")
elif 1 < 2:
    print_to_console("yes")
"""
        result = Compiler.getPythonCode(test_string)
        ast = Parser.parseString(test_string)

        self.assertEqual(len(ast.errors), 0)
        self.assertEqual(result[0], correct_translation)
Example #21
0
    def test_comment_multiline(self):
        test_string = \
            """:-( this is
a multiline
comment
:-)
"""
        correct_translation = \
            """"""
        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)
Example #22
0
    def test_assign_num_change(self):
        test_string = \
            """num is a number
set num to 10
set num to num*2
"""
        correct_translation = \
            """num = None
num = 10
num = ((num) * (2))
"""
        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)
Example #23
0
    def test_define(self):
        test_string = \
            """define moveForwardFive
{
    drive forward 5 steps
}
"""
        correct_translation = \
            """def moveForwardFive():
    translate_car(5, 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)
Example #24
0
    def test_function_invocation_with_one_parameter(self):
        test_string = \
            """define move5Steps using direction (word)
{
    print direction
}
move5Steps "forwards"
"""
        correct_translation = \
            """def move5Steps(direction):
    print_to_console(direction)
move5Steps("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)
Example #25
0
    def test_assign(self):
        test_string = \
            """myVar is a number
otherThing is a number
set myVar to 10
set otherThing to 11
set myVar to otherThing
"""
        correct_translation = \
            """myVar = None
otherThing = None
myVar = 10
otherThing = 11
myVar = otherThing
"""
        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)
Example #26
0
    def test_if_if_else_complicated(self):
        test_string = \
            """if 1
{
    print "yay"
    if 1
    {
        print "yahoo"
    }
    else
    {
        print "oh no"
        if 1
        {
            print "good"
        }
        if 1
        {
            print "yay"
        }
        elseIf 2
        {
            print "no"
            if 1
            {
                print "hi"
            }
        }
        elseIf 3
        {
            print "yes"
        }
        else
        {
            if 5
            {
                print "works"
            }
            print "end"
        }
    }
}
else
{
    print "no"
}
"""
        correct_translation = \
            """if 1:
    print_to_console("yay")
    if 1:
        print_to_console("yahoo")
    else:
        print_to_console("oh no")
        if 1:
            print_to_console("good")
        if 1:
            print_to_console("yay")
        elif 2:
            print_to_console("no")
            if 1:
                print_to_console("hi")
        elif 3:
            print_to_console("yes")
        else:
            if 5:
                print_to_console("works")
            print_to_console("end")
else:
    print_to_console("no")
"""
        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)