Пример #1
0
    def tryNew(cls, lineNumber: int, indentation: int,
               line: str) -> Optional[ParsedLine]:
        """Teste si la ligne respecte le modèle variable = expression
        et crée l'item correspondant le cas échéant

        :param lineNumber: numéro de la ligne
        :type lineNumber: int
        :param indentation: indentation de la ligne
        :type indentation: int
        :param line: ligne à analyser
        :type line: str
        :return: noeud du type print ou None
        :rtype: Optional[ParsedLine_Print]
        :raises: ParseError si l'expression ou variable détectée n'ont pas la bonne forme
        """
        allGroup = re.search(cls.regex(), line)
        if allGroup is None:
            return None
        variableName = allGroup[1].strip()  # la variable
        expressionStr = allGroup[
            2]  # tout ce qu'il y a dans les ( ) de l'input
        if not ExpressionParser.strIsVariableName(variableName):
            raise ParseError(
                "La variable <{}> est incorrecte.".format(variableName),
                {"lineNumber": lineNumber})
        expr = ExpressionParser.buildExpression(expressionStr)
        if not isinstance(expr, ArithmeticExpressionNode):
            raise ParseError("L'expression <{}> est incorrecte.".format(expr),
                             {"lineNumber": lineNumber})
        return ParsedLine_Affectation(lineNumber, indentation,
                                      Variable.add(variableName), expr)
Пример #2
0
 def test5(self):
     varX = Variable('x')
     varY = Variable('y')
     affectationX = TransfertNode(4, varX, EP.buildExpression('x+1'))
     affectationY = TransfertNode(5, varY, EP.buildExpression('y+x'))
     whileItem = WhileNode(3, EP.buildExpression('x < 10 or y < 100'),
                           [affectationX, affectationY])
     good = "\n".join([
         "	while ((@x < #10) or (@y < #100)) {", "		@x ← (@x + #1)",
         "		@y ← (@y + @x)", "	}"
     ])
     self.assertEqual(str(whileItem), good)
Пример #3
0
    def tryNew(cls, lineNumber: int, indentation: int,
               line: str) -> Optional[ParsedLine]:
        """Teste si la ligne respecte le modèle print
        et crée l'item correspondant le cas échéant

        :param lineNumber: numéro de la ligne
        :type lineNumber: int
        :param indentation: indentation de la ligne
        :type indentation: int
        :param line: ligne à analyser
        :type line: str
        :return: noeud du type print ou None
        :rtype: Optional[ParsedLine_Print]
        :raises: ParseError si l'expression détectée n'est pas du bon type
        """

        allGroup = re.search(cls.regex(), line)
        if allGroup is None:
            return None
        firstGroup = allGroup[1]  # tout ce qui match dans les ( )
        expr = ExpressionParser.buildExpression(firstGroup)
        if not isinstance(expr, ArithmeticExpressionNode):
            raise ParseError("L'expression <{}> est incorrecte.".format(expr),
                             {"lineNumber": lineNumber})
        return ParsedLine_Print(lineNumber, indentation, expr)
Пример #4
0
    def test1(self):
        strExpression = "5*(2*x+4)-x*y"
        engine = Processor12Bits()
        regManager = RegistersManager(engine.registersNumber())
        cem = CompileExpressionManager(engine, regManager)

        parsed = ExpressionParser.buildExpression(strExpression)
        fifo = parsed.getFIFO(engine.litteralDomain)
        
        
        actions = cem.compile(fifo)
        
        good = "\n".join([
            "	@x r3 load",
            "	@#2 r2 load",
            "	r3 r2 r3 *",
            "	@#4 r2 load",
            "	r3 r2 r3 +",
            "	@#5 r2 load",
            "	r3 r2 r3 *",
            "	@x r2 load",
            "	@y r1 load",
            "	r2 r1 r2 *",
            "	r3 r2 r3 -"
        ])
        
        self.assertEqual(str(actions), good)
Пример #5
0
    def tryNew(cls, lineNumber: int, indentation: int,
               line: str) -> Optional[ParsedLine]:
        """Teste si la ligne respecte le modèle [mot-clef] condition
        et crée l'item correspondant le cas échéant

        :param lineNumber: numéro de la ligne
        :type lineNumber: int
        :param indentation: indentation de la ligne
        :type indentation: int
        :param line: ligne à analyser
        :type line: str
        :return: noeud du type reconnu ou None
        :rtype: Union[ParsedLine_If, ParsedLine_Elif, ParsedLine_While, None]
        :raises: ParseError si l'expression trouvée n'a pas le bon type
        """

        allGroup = re.search(cls.regex(), line)

        if allGroup is None:
            return None

        firstGroup = allGroup[
            1]  # tout ce qui match après testStructureKeyword et avant les :
        condition = ExpressionParser.buildExpression(firstGroup)
        if not isinstance(condition,
                          (LogicExpressionNode, ComparaisonExpressionNode)):
            raise ParseError(
                "L'expression <{}> n'est pas une condition.".format(condition),
                {"lineNumber": lineNumber})
        node = cls(lineNumber, indentation, condition)
        return node
Пример #6
0
    def tryNew(cls, lineNumber: int, indentation: int,
               line: str) -> Optional[ParsedLine]:
        """Teste si la ligne respecte le modèle print
        et crée l'item correspondant le cas échéant

        :param lineNumber: numéro de la ligne
        :type lineNumber: int
        :param indentation: indentation de la ligne
        :type indentation: int
        :param line: ligne à analyser
        :type line: str
        :return: noeud du type print ou None
        :rtype: Optional[ParsedLine_Print]
        :raises: ParseError si la variable n'a pas la bonne forme
        """
        allGroup = re.search(cls.regex(), line)
        if allGroup is None:
            return None
        variableName = allGroup[1].strip()  # la variable

        if not ExpressionParser.strIsVariableName(variableName):
            raise ParseError(
                "La variable <{}> est incorrecte.".format(variableName),
                {"lineNumber": lineNumber})
        return ParsedLine_Input(lineNumber, indentation,
                                Variable.add(variableName))
Пример #7
0
    def test2(self):
        varX = Variable('x')
        varY = Variable('y')

        affectationX = TransfertNode(
            4, varX, EP.buildExpression('-3*x+1'))  # mypy: ignore
        affectationY = TransfertNode(5, varY,
                                     EP.buildExpression('y+x'))  # mypy: ignore
        structuredList = [
            TransfertNode(1, varX, EP.buildExpression('0')),
            TransfertNode(2, varY, EP.buildExpression('0')),
            WhileNode(3, EP.buildExpression('x < 10 or y < 100'),
                      [affectationX, affectationY]),
            TransfertNode(6, None, EP.buildExpression('y'))
        ]

        engine = Processor12Bits()
        cm = CM(engine, structuredList)
        actionsList = cm.compile()
        strGlobal = "\n".join([str(item) for item in actionsList])

        good = "\n".join([
            "	@#0 r3 load", "	r3 @x store", "	@#0 r3 load", "	r3 @y store",
            "Lab2	@x r3 load", "	@#10 r2 load", "	r3 r2 <", "	Lab1 goto",
            "	@y r3 load", "	@#100 r2 load", "	r3 r2 <", "	Lab1 goto",
            "	Lab3 goto", "Lab1	@x r3 load", "	@#3 r2 load", "	r3 r2 r3 *",
            "	r3 r3 -", "	@#1 r2 load", "	r3 r2 r3 +", "	r3 @x store",
            "	@y r3 load", "	@x r2 load", "	r3 r2 r3 +", "	r3 @y store",
            "	Lab2 goto", "Lab3	@y r3 load", "	r3 print", "	halt"
        ])
        self.assertEqual(strGlobal, good)
Пример #8
0
    def test3(self):
        strExpression = "(((2+4)*(4+1)) - ((2+4)*(4+1))) * (((2+4)*(4+1)) - ((2+4)*(4+1)))"
        engine = Processor12Bits()
        regManager = RegistersManager(engine.registersNumber())

        cem = CompileExpressionManager(engine, regManager)
        parsed = ExpressionParser.buildExpression(strExpression)
        fifo = parsed.getFIFO(engine.litteralDomain)
        actions = cem.compile(fifo)
        
        good = "\n".join([
            "	@#4 r3 load",
            "	@#2 r2 load",
            "	r3 r2 r3 +",
            "	@#4 r2 load",
            "	@#1 r1 load",
            "	r2 r1 r2 +",
            "	r3 r2 r3 *",
            "	@#4 r2 load",
            "	@#2 r1 load",
            "	r2 r1 r2 +",
            "	@#4 r1 load",
            "	@#1 r0 load",
            "	r1 r0 r1 +",
            "	r2 r1 r2 *",
            "	r3 r2 r3 -",
            "	@#4 r2 load",
            "	@#2 r1 load",
            "	r2 r1 r2 +",
            "	@#4 r1 load",
            "	@#1 r0 load",
            "	r1 r0 r1 +",
            "	r2 r1 r2 *",
            "	@#4 r1 load",
            "	@#2 r0 load",
            "	r1 r0 r1 +",
            "	@#4 r0 load",
            "	r3 _m0 store",
            "	r0 r3 move",
            "	@#1 r0 load",
            "	r3 r0 r3 +",
            "	r1 r3 r3 *",
            "	r2 r3 r3 -",
            "	_m0 r2 load",
            "	r2 r3 r3 *"
        ])
        
        self.assertEqual(str(actions), good)
Пример #9
0
    def test4(self):
        strExpression = "3*x > 4"
        engine = Processor16Bits()
        regManager = RegistersManager(engine.registersNumber())

        cem = CompileExpressionManager(engine, regManager)
        parsed = ExpressionParser.buildExpression(strExpression)
        fifo = parsed.getFIFO(engine.litteralDomain)
        actions = cem.compile(fifo)
        
        good = "\n".join([
            "	@x r7 load",
            "	r7 #3 r7 *",
            "	#4 r6 move",
            "	r7 r6 >"
        ])
        
        self.assertEqual(str(actions), good)
Пример #10
0
 def test1(self):
     varX = Variable('x')
     varY = Variable('y')
     initialisationX = TransfertNode(1, varX, EP.buildExpression('0'))
     initialisationY = TransfertNode(2, varY, EP.buildExpression('0'))
     affectationX = TransfertNode(4, varX, EP.buildExpression('x+1'))
     affectationY = TransfertNode(5, varY, EP.buildExpression('y+x'))
     whileItem = WhileNode(3, EP.buildExpression('x < 10 or y < 100'),
                           [affectationX, affectationY])
     affichageFinal = TransfertNode(6, None, EP.buildExpression('y'))
     structureList = StructureNodeList(
         [initialisationX, initialisationY, whileItem, affichageFinal])
     good = "\n".join([
         "	@x ← #0", "	@y ← #0", "	while ((@x < #10) or (@y < #100)) {",
         "		@x ← (@x + #1)", "		@y ← (@y + @x)", "	}", "	@y → Affichage"
     ])
     self.assertEqual(str(structureList), good)
Пример #11
0
    def test2(self):
        strExpression = "5*(2*x+4)-x*y"
        engine = Processor16Bits()
        regManager = RegistersManager(engine.registersNumber())
        cem = CompileExpressionManager(engine, regManager)

        parsed = ExpressionParser.buildExpression(strExpression)
        fifo = parsed.getFIFO(engine.litteralDomain)
       
        actions = cem.compile(fifo)
        
        good = "\n".join([
            "	@x r7 load",
            "	@y r6 load",
            "	r7 r6 r7 *",
            "	@x r6 load",
            "	r6 #2 r6 *",
            "	r6 #4 r6 +",
            "	r6 #5 r6 *",
            "	r6 r7 r7 -"
        ])
        
        self.assertEqual(str(actions), good)
Пример #12
0
    def test2(self):
        varX = Variable('x')
        varY = Variable('y')
        initialisationX = TransfertNode(1, varX, EP.buildExpression('0'))
        initialisationY = TransfertNode(2, varY, EP.buildExpression('0'))
        affectationX = TransfertNode(4, varX, EP.buildExpression('x+1'))
        affectationY = TransfertNode(5, varY, EP.buildExpression('y+x'))
        whileItem = WhileNode(3, EP.buildExpression('x < 10 or y < 100'),
                              [affectationX, affectationY])
        affichageFinal = TransfertNode(6, None, EP.buildExpression('y'))
        structureList = StructureNodeList(
            [initialisationX, initialisationY, whileItem, affichageFinal])
        structureList.linearize([Operators.INF, Operators.EQ])
        good = "\n".join([
            "	@x ← #0", "	@y ← #0", "Lab1	Saut Lab2 si (@x < #10)",
            "	Saut Lab2 si (@y < #100)", "	Saut Lab3", "Lab2	@x ← (@x + #1)",
            "	@y ← (@y + @x)", "	Saut Lab1", "Lab3	@y → Affichage", "	halt"
        ])

        self.assertEqual(str(structureList), good)
Пример #13
0
 def test5(self):
     strExpression = "x<4 and y>3*x"
     objExpression = EP.buildExpression(strExpression)
     self.assertEqual(str(objExpression),
                      "((@x < #4) and (@y > (@x * #3)))")
Пример #14
0
 def test4(self):
     strExpression = "+ 6 -4*x / 3"
     objExpression = EP.buildExpression(strExpression)
     self.assertEqual(str(objExpression), "(#6 - ((@x * #4) / #3))")
Пример #15
0
 def test3(self):
     strExpression = "((3*x)+ (5 +-y))"
     objExpression = EP.buildExpression(strExpression)
     self.assertEqual(str(objExpression), "((@x * #3) + ( - (@y) + #5))")
Пример #16
0
 def test2(self):
     strExpression = "(x < 10 or y < 100)"
     objExpression = EP.buildExpression(strExpression)
     self.assertEqual(str(objExpression), "((@x < #10) or (@y < #100))")
Пример #17
0
 def test1(self):
     strExpression = "-2 + x"
     objExpression = EP.buildExpression(strExpression)
     self.assertEqual(str(objExpression), "(#-2 + @x)")
Пример #18
0
 def test6(self):
     affichageFinal = TransfertNode(6, None, EP.buildExpression('y'))
     self.assertEqual(str(affichageFinal), "	@y → Affichage")
Пример #19
0
 def test3(self):
     varX = Variable('x')
     affectationX = TransfertNode(4, varX, EP.buildExpression('x+1'))
     self.assertEqual(str(affectationX), "	@x ← (@x + #1)")
Пример #20
0
 def test6(self):
     # mult on logic terms
     strExpression = "(2 < 4) * (3+x)"
     with self.assertRaises(ExpressionError) as context:
         EP.buildExpression(strExpression)
     self.assertTrue("Erreur. Vérifiez." in str(context.exception))
Пример #21
0
 def test1(self):
     varX = Variable('x')
     initialisationX = TransfertNode(1, varX, EP.buildExpression('0'))
     self.assertEqual(str(initialisationX), "	@x ← #0")
Пример #22
0
 def test8(self):
     strExpression = "45^x"
     objExpression = EP.buildExpression(strExpression)
     self.assertEqual(str(objExpression), "(@x ^ #45)")
Пример #23
0
 def regex(cls):
     return "^(" + ExpressionParser.variableRegex(
     ) + r")\s*=\s*" + cls.KEYWORD + r"\s*\(\s*\)$"
Пример #24
0
 def test4(self):
     varY = Variable('y')
     affectationY = TransfertNode(5, varY, EP.buildExpression('y+x'))
     self.assertEqual(str(affectationY), "	@y ← (@y + @x)")
Пример #25
0
 def regex(cls):
     return "^" + cls.KEYWORD + r"\s*\((" + ExpressionParser.expressionRegex(
     ) + r")\)$"
Пример #26
0
 def test7(self):
     # and on arithmetics terms
     strExpression = "(2+x) and (x-1)"
     with self.assertRaises(ExpressionError) as context:
         EP.buildExpression(strExpression)
     self.assertTrue("Erreur. Vérifiez." in str(context.exception))
Пример #27
0
 def test2(self):
     varY = Variable('y')
     initialisationY = TransfertNode(2, varY, EP.buildExpression('0'))
     self.assertEqual(str(initialisationY), "	@y ← #0")
Пример #28
0
 def test9(self):
     strExpression = "x"
     objExpression = EP.buildExpression(strExpression)
     self.assertEqual(str(objExpression), "@x")
Пример #29
0
 def regex(cls):
     return "^(" + ExpressionParser.variableRegex(
     ) + r")\s*=\s*(" + ExpressionParser.expressionRegex() + ")$"