Project

Profile

Help

Revision ed873743

Added by Vincent Le Goff over 4 years ago

Add in SharpScript the support for Python code at various levels

View differences:

src/sharp/engine.py
55 55

  
56 56
    def execute(self, code):
57 57
        """Execute the SharpScript code given as an argument."""
58
        instructions = self.feed(code)
58
        if isinstance(code, str):
59
            instructions = self.feed(code)
60
        else:
61
            instructions = [code]
62

  
59 63
        globals = self.globals
60 64
        locals = self.locals
61 65
        for instruction in instructions:
......
70 74
        this suite of statements.
71 75

  
72 76
        """
73
        # First, splits into statements
74
        statements = self.split_statements(content)
77
        # Execute Python code if necessary
75 78
        codes = []
79
        while content.startswith("{+"):
80
            end = self.find_right_brace(content)
81
            code = content[2:end - 1].lstrip("\n").rstrip("\n ")
82
            code = repr(dedent(code)).replace("\\n", "\n")
83
            code = "compile(" + code + ", 'SharpScript', 'exec')"
84
            codes.append(code)
85
            content = content[end + 1:]
86

  
87
        # The remaining must be SharpScript, splits into statements
88
        statements = self.split_statements(content)
76 89
        for statement in statements:
77 90
            pycode = self.convert_to_python(statement)
78 91
            codes.append(pycode)
......
93 106
        kwargs = {}
94 107
        for argument in statement[1:]:
95 108
            if argument.startswith("{+"):
96
                argument = argument[3:-2]
109
                argument = argument[2:-1].lstrip("\n").rstrip("\n ")
97 110
                argument = repr(dedent(argument)).replace("\\n", "\n")
98 111
                argument = "compile(" + argument + ", 'SharpScript', 'exec')"
99 112
            elif argument.startswith("{"):
src/tests/sharp/test_syntax.py
81 81
            print var
82 82
        }""")
83 83
        self.assertEqual(statements, [
84
            "trigger('Should it work?', compile('var = 2 + 3\nprint var\n', " \
84
            "trigger('Should it work?', compile('var = 2 + 3\nprint var', " \
85 85
            "'SharpScript', 'exec'))"
86 86
        ])
87 87

  
......
91 91
        self.assertEqual(statements, [
92 92
            "say('A message', braille=False, speech=True)"
93 93
        ])
94

  
95
    def test_python(self):
96
        """Test Python code in SharpScript."""
97
        statements = self.engine.feed("""{+
98
            print 1
99
            print 3
100
        }""")
101
        self.assertEqual(statements, [
102
            "compile('print 1\nprint 3', 'SharpScript', 'exec')",
103
        ])

Also available in: Unified diff