コード例 #1
0
    def transpile_body(self, body_ast, nesting_count):
        body_exec_string = ""

        for ast in body_ast:

            if self.check_ast('VariableDecleration', ast):
                var_obj = VariableObject(ast)
                transpile = var_obj.transpile()
                if self.should_dedent_trailing(ast, self.ast):
                    body_exec_string += (
                        "   " * (nesting_count - 1)) + transpile + "\n"
                else:
                    body_exec_string += ("   " *
                                         nesting_count) + transpile + "\n"

            if self.check_ast('PrebuiltFunction', ast):
                gen_builtin = BuiltInFunctionObject(ast)
                transpile = gen_builtin.transpile()
                if self.should_dedent_trailing(ast, self.ast):
                    body_exec_string += (
                        "   " * (nesting_count - 1)) + transpile + "\n"
                else:
                    body_exec_string += ("   " *
                                         nesting_count) + transpile + "\n"

            if self.check_ast('Comment', ast):
                gen_comment = CommentObject(ast)
                transpile = gen_comment.transpile()
                if self.should_dedent_trailing(ast, self.ast):
                    body_exec_string += (
                        "   " * (nesting_count - 1)) + transpile + "\n"
                else:
                    body_exec_string += ("   " *
                                         nesting_count) + transpile + "\n"

            if self.check_ast('ConditionalStatement', ast):

                if self.should_increment_nest_count(ast, self.ast):
                    nesting_count += 1
                condition_obj = ConditionObject(ast, nesting_count)

                if nesting_count == 2:

                    body_exec_string += "   " + condition_obj.transpile()
                else:
                    body_exec_string += (
                        "   " *
                        (nesting_count - 1)) + condition_obj.transpile()

            if self.check_ast('ForLoop', ast):
                if self.should_increment_nest_count(ast, self.ast):
                    nesting_count += 1
                loop_obj = LoopObject(ast, nesting_count)
                body_exec_string += (
                    "   " * (nesting_count - 1)) + loop_obj.transpile()

        return body_exec_string
コード例 #2
0
    def transpile_body(self, body_ast, nesting_count):
        """ Transpile Body
        
        This method will use the body AST in order to create a python version of the tachyon
        code for the body statement while managing indentations

        return:
            body_exec_string (str) : The python transpiled code
        """

        # Holds the body executable string of the first statement
        body_exec_string = ""

        # Loop through each ast item in the body dictionary
        for ast in body_ast:

            # This will parse variable declerations within the body
            if self.check_ast('VariableDecleration', ast):
                var_obj = VariableObject(ast)
                transpile = var_obj.transpile()
                if self.should_dedent_trailing(ast, self.ast):
                    body_exec_string += (
                        "   " * (nesting_count - 1)) + transpile + "\n"
                else:
                    body_exec_string += ("   " *
                                         nesting_count) + transpile + "\n"

            # This will parse built-in within the body
            if self.check_ast('PrebuiltFunction', ast):
                gen_builtin = BuiltInFunctionObject(ast)
                transpile = gen_builtin.transpile()
                if self.should_dedent_trailing(ast, self.ast):
                    body_exec_string += (
                        "   " * (nesting_count - 1)) + transpile + "\n"
                else:
                    body_exec_string += ("   " *
                                         nesting_count) + transpile + "\n"

            # This will parse nested conditional statement within the body
            if self.check_ast('ConditionalStatement', ast):
                # Increase nesting count because this is a condition statement inside a conditional statement
                # Only increase nest count if needed
                if self.should_increment_nest_count(ast, self.ast):
                    nesting_count += 1
                # Create conditional statement exec string
                condition_obj = ConditionObject(ast, nesting_count)
                # The second nested statament only needs 1 indent not 2
                if nesting_count == 2:
                    # Add the content of conditional statement with correct indentation
                    body_exec_string += "   " + condition_obj.transpile()
                else:
                    # Add the content of conditional statement with correct indentation
                    body_exec_string += (
                        "   " *
                        (nesting_count - 1)) + condition_obj.transpile()

        return body_exec_string
コード例 #3
0
    def parse_variable_decleration(self, token_stream):

        tokens_checked = 0

        name = ""
        operator = ""
        value = ""

        for token in range(0, len(token_stream)):

            token_type = token_stream[tokens_checked][0]
            token_value = token_stream[tokens_checked][1]

            #if statement end found break out of loop as we have parsed variable decleration
            if token_type == "STATEMENT_END":
                break

                #this will get variable name and perform error validation for invalid name
            elif token == 1 and token_type == "IDENTIFIER":
                name = token_value
            elif token == 1 and token_type != "IDENTIFIER":
                print("ERROR: Invalid variable name '" + token_value + "'")
                quit()

            #this will get variable assignment operator e.g = or in future += and error validation
            elif token == 2 and token_type == "OPERATOR":
                operator = token_value
            elif token == 2 and token_type != "OPERATOR":
                print(
                    "ERROR: Assignment Operator is missing or invalid it should be '='"
                )
                quit()

            #this will get the variable value assigned
            elif token == 3 and token_type in [
                    'STRING', 'INTEGER', 'IDENTIFIER'
            ]:
                value = token_value
            elif token == 3 and token_type not in [
                    'STRING', 'INTEGER', 'IDENTIFIER'
            ]:
                print("Invalid variable assignment value '" + token_value +
                      "'")
                quit()

            tokens_checked += 1

        varObj = VariableObject()
        self.transpiled_code += varObj.transpile(name, operator, value)

        #increment token index by amount of token checked so we dont check them again
        self.token_index += tokens_checked
コード例 #4
0
    def parse_variable_declaration(self, token_stream):

        tokens_checked = 0

        name = ""
        operator = ""
        value = ""

        for token in range(0, len(token_stream)):
            token_type = token_stream[tokens_checked][0]
            token_value = token_stream[tokens_checked][1]

            #If the statement end is found it breaks out
            if token_type == "STATEMENT_END":
                break

            #this will get the variable name
            elif token == 1 and token_type == "IDENTIFIER":
                name = token_value

            #this will do error validation for invalid variable names
            elif token == 1 and token_type != "IDENTIFIER":
                print("Error: Invalid variable name '" + token_value + "'")
                quit()

            #this will get the variable assignment operator
            elif token == 2 and token_type == "OPERATOR":
                operator = token_value
            elif token == 2 and token_type != "OPERATOR":
                print("Error: Assignment operator is missing or invalid ")
                quit()

            #this will get the variable value assigned
            elif token == 3 and token_type in [
                    'STRING', 'INTEGER', 'IDENTIFIER'
            ]:
                value = token_value
            elif token == 3 and token_type not in [
                    'STRING', 'INTEGER', 'IDENTIFIER'
            ]:
                print("Error: Invalid variable assignment value '" +
                      token_value + "'")
                quit()

            tokens_checked += 1

        varObj = VariableObject()
        self.transpiled_code += varObj.transpile(name, operator, value)

        #increment token index by amount of tokens checked so we dont check again
        self.token_index += tokens_checked
コード例 #5
0
    def parse_variable_declaration(self, token_stream):

        tokens_checked = 0

        name = ""
        operator = ""
        value = ""

        for token in range(0, len(token_stream)):
            # Get token type e.g.: INTEGER
            token_type = token_stream[tokens_checked][0]
            # Get token value e.g: 7
            token_value = token_stream[tokens_checked][1]

            # If the STATEMENT_END token is found, break out of loop
            if token_type == "STATEMENT_END":
                break

            elif token == 1 and token_type == 'IDENTIFIER':
                name = token_value
            elif token == 1 and token_type != 'IDENTIFIER':
                print("ERROR: Invalid variable name '" + token_value + "'")
                quit()

            # This will get an assignment operator
            elif token == 2 and token_type == 'OPERATOR':
                operator = token_value
            elif token == 2 and token_type != 'OPERATOR':
                print(
                    "ERROR: Assignment  Operator is missing or invalid, should be '='"
                )
                quit()

            # This will get the variable value assigned:
            elif token == 3 and token_type in [
                    'STRING', 'INTEGER', 'IDENTIFIER'
            ]:
                value = token_value
            elif token_type == 3 and token_type not in [
                    'STRING', 'INTEGER', 'IDENTIFIER'
            ]:
                print("Invalid variable assignment value '" + token_value +
                      "'")
                quit()

            tokens_checked += 1

        self.token_index += tokens_checked
        varObj = VariableObject()
        self.transpiled_code += varObj.transpile(name, operator, value)
コード例 #6
0
ファイル: parserscript.py プロジェクト: Depressuki/Choco
    def parse_variable_declaration(self, token_stream):

        tokens_checked = 0

        name = ""
        operator = ""
        value = ""

        for token in range(0, len(token_stream)):

            token_type = token_stream[tokens_checked][0]
            token_value = token_stream[tokens_checked][1]
            print(token_value)
            if token_type == "STATEMENT_END":
                break
            elif token == 1 and token_type == "IDENTIFIER":
                name = token_value

            elif token == 1 and token_type != "IDENTIFIER":
                print("Nyaaa! Invalid identifier name somewhere! The token " +
                      str(token) + " doesn't match up with the token type " +
                      token_type + "! Please fix it so I can run smoothly!")
                quit()

            elif token == 2 and token_type == "OPERATOR":
                operator = token_value
            elif token == 2 and token_type != "OPERATOR":
                print("Nyaaa! Invalid operator somewhere! The token " +
                      str(token) + " doesn't match up with the token type " +
                      token_type + "! Please fix it so I can run smoothly!")
                quit()

            elif token == 3 and token_type in [
                    'STRING', 'INTEGER', 'IDENTIFIER'
            ]:
                value = token_value
                print("Valid " + token_type + " received!")
            elif token == 3 and token_type not in [
                    'STRING', 'INTEGER', 'IDENTIFIER'
            ]:
                print("Nyaaa! " + token_type +
                      " isn't a valid string, integer, or identifier!")
                quit()

            tokens_checked += 1
        varObj = VariableObject()
        self.transpiled_code += varObj.transpile(name, operator, value)

        self.token_index += tokens_checked
コード例 #7
0
    def parse_variable_declaration(self, token_stream):
        tokens_checked = 0

        name = ""
        operator = ""
        value = ""

        for token in range(0, len(token_stream)):

            token_type = token_stream[tokens_checked][0]
            token_value = token_stream[tokens_checked][1]

            # Helps parse 1 statement at a time
            if token_type == 'STATEMENT_END':
                break

            # This will get variable type rg. var, let, const
            #if token == 0:
            #    print('Variable type: ' + token_value)

            # This will take variable name and also perform error validation
            elif token == 1 and token_type == "IDENTIFIER":
                name = token_value

            elif token == 1 and token_type != "IDENTIFIER":
                print("ERROR: Invalid Variable Name '" + token_value + "'")
                quit()

            # This will get variable assignment operator ie. '='
            elif token == 2 and token_type == "OPERATOR":
                operator = token_value
            elif token == 2 and token_type != "OPERATOR":
                print("ERROR: Assignment Operator is missing or invalid, should be '='")
                quit()

            # This will get the variable value assigned
            elif token == 3 and token_type in ['STRING', 'INTEGER', 'IDENTIFIER']:
                value = token_value
            elif token == 3 and token_type not in ['STRING', 'INTEGER', 'IDENTIFIER']:
                print("ERROR: Invalid Variable Assignment Value '" + token_value + "'")
                quit()

            tokens_checked += 1

        varObj = VariableObject()
        self.transpiled_code += varObj.transpile(name, operator, value)

        # Increment token index by amount of tokens we checked to avoid repetition
        self.token_index += tokens_checked
コード例 #8
0
    def parse_variable_declaration(self, token_stream):

        tokens_checked = 0

        name = ""
        operator = ""
        value = ""

        for token in range(0, len(token_stream)):

            token_type = token_stream[tokens_checked][0]
            token_value = token_stream[tokens_checked][1]

            # if the statement end is found, break the for loop.
            if token_type == "STATEMENT_END": break

            if token == 0:
                pass
                #print('Variable type: ' + token_value)
            elif token == 1 and token_type == "IDENTIFIER":
                name = token_value
            elif token == 1 and token_type != "IDENTIFIER":
                print("ERROR: invalid variable name '" + token_value + "'")
                quit()
            elif token == 2 and token_type == "OPERATOR":
                operator = token_value
            elif token == 2 and token_type != "OPERATOR":
                print(
                    "ERROR: invalid asignament operator is missing or invalid, it should be '='"
                )
                quit()
            elif token == 3 and token_type in [
                    "STRING", "INTEGER", "IDENTIFIER"
            ]:
                value = token_value
            elif token == 3 and token_type not in [
                    "STRING", "INTEGER", "IDENTIFIER"
            ]:
                print('ERROR: invalid variable name')
                quit()

            tokens_checked += 1

        varObj = VariableObject()
        self.transpiled_code += varObj.transpile(name, operator, value)

        self.token_index += tokens_checked
コード例 #9
0
ファイル: parser.py プロジェクト: gr33nm0nk2802/Plang
    def parse_variable_declaration(self, token_stream):
    
        tokens_checked = 0

        name     = ""
        operator = ""
        value    = ""

        for token in range (0, len(token_stream)):

            token_type  = token_stream[tokens_checked][0]
            token_value = token_stream[tokens_checked][1]

            # Breaks out of loop in case of statement end
            if token == 4 and token_type == "STATEMENT_END": break

            
            # This will get the variable name and also perform error validiation
            elif token == 1 and token_type == "IDENTIFIER":
                name = token_value
            elif token == 1 and token_type != "IDENTIFIER":
                print("ERROR: Invalid variable name ' " + token_value +" ' ")
                quit()

            # This will check variable assignment operator    
            elif token == 2 and token_type =="OPERATOR":
                operator = token_value
            elif token == 2 and token_type !="OPERATOR":
                print("ERROR: Assignment operator is missing or invalid")
                quit()

            # This will get variable values assigned 
            elif token == 3 and token_type in['STRING', 'INTEGER', 'IDENTIFIER']:
                value = token_value
            elif token == 3 and token_type not in['STRING', 'INTEGER', 'IDENTIFIER']:
                print("Invalid Variable assignment value '"+token_value+"'")
                quit()

            tokens_checked += 1

        # Increment token index by number of tokens checked
        self.token_index = tokens_checked
    
        varObj = VariableObject()
        self.transpiled_code += varObj.transpile(name, operator, value)
コード例 #10
0
ファイル: parser.py プロジェクト: sarveshbhatnagar/Compiler
    def parse_variable_declaration(self, token_stream):
        # print(token_stream)
        tokens_checked = 0
        name = ""
        operator = ""
        value = ""

        for token in range(0, len(token_stream)):
            token_type = token_stream[tokens_checked][0]
            token_value = token_stream[tokens_checked][1]

            if token == 4 and token_type == 'STATEMENT_END':
                break

                # if token == 0:
                #     print('Variable type : ' + token_value)

            elif token == 1 and token_type == 'IDENTIFIER':
                name = token_value

            elif token == 1 and token_type != 'IDENTIFIER':
                print('Error : Invalid Variable name ' + token_value + ' * ')
                quit()
            elif token == 2 and token_type == 'OPERATOR':
                operator = token_value
            elif token == 2 and token_type != 'OPERATOR':
                print(
                    'Error : Assignment operator is missing or invalid, it should be "="'
                )
            elif token == 3 and token_type in [
                    'STRING', 'INTEGER', 'IDENTIFIER'
            ]:
                value = token_value
            elif token == 3 and token_type not in [
                    'STRING', 'INTEGER', 'IDENTIFIER'
            ]:
                print('Invalid variable assignment value ' + token_value)
                quit()

            tokens_checked += 1
        varObj = VariableObject()
        self.transpile_code += varObj.transpile(name, operator, value)

        self.token_index += tokens_checked
コード例 #11
0
    def object_definer(self, isGettingBody):
        for ast in self.source_ast:

            if self.check_ast('VariableDecleration', ast):
                gen_var = VariableObject(ast)
                self.exec_string += gen_var.transpile() + '\n'

            if self.check_ast('ConditionalStatement', ast):
                gen_condition = ConditionObject(ast, 1)
                self.exec_string += gen_condition.transpile() + '\n'

            if self.check_ast('PrebuiltFunction', ast):
                gen_builtin = BuiltInFunctionObject(ast)
                self.exec_string += gen_builtin.transpile() + "\n"

            if self.check_ast('ForLoop', ast):
                gen_loop = LoopObject(ast, 1)
                self.exec_string += gen_loop.transpile() + "\n"

        return self.exec_string
コード例 #12
0
    def object_definer(self, isGettingBody):
        """ Object Definer 
        
        This method will find all the different ast objects within the ast dictionary
        and call all the objects and pass in the ast dictionary to get a python 
        string of code back which is the Y# code transpiled into python
        
        returns:
            python_code (str) : This will written a string of python code
        """
        
        # Iterate through all ast dictionaries
        for ast in self.source_ast:

            # Create dictionary var object and append exec string global exec string
            if self.check_ast('VariableDecleration', ast):
                gen_var = VariableObject(ast)
                self.exec_string += gen_var.transpile() + '\n'

            # Create dictionary condition object and append exec string global exec string
            if self.check_ast('ConditionalStatement', ast):
                gen_condition = ConditionObject(ast, 1)
                self.exec_string += gen_condition.transpile() + '\n'

            # Create dictionary builtin object and append exec string global exec string
            if self.check_ast('PrebuiltFunction', ast):
                gen_builtin = BuiltInFunctionObject(ast)
                self.exec_string += gen_builtin.transpile() + "\n"

            # Create dictionary comment object and append exec string to global exec string when return from transpile methof
            if self.check_ast('Comment', ast):
                gen_comment = CommentObject(ast)
                self.exec_string += gen_comment.transpile() + "\n"

            # Create dictionary repetition object and append exec string global exec string
            if self.check_ast('ForLoop', ast):
                gen_loop = LoopObject(ast, 1)
                self.exec_string += gen_loop.transpile() + "\n"

        return self.exec_string