How to use the m2cgen.interpreters.code_generator.CodeTemplate function in m2cgen

To help you get started, we’ve selected a few m2cgen examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github BayesWitnesses / m2cgen / m2cgen / interpreters / python / code_generator.py View on Github external
import contextlib

from m2cgen.interpreters.code_generator import BaseCodeGenerator
from m2cgen.interpreters.code_generator import CodeTemplate as CT


class PythonCodeGenerator(BaseCodeGenerator):

    tpl_num_value = CT("${value}")
    tpl_infix_expression = CT("(${left}) ${op} (${right})")
    tpl_return_statement = CT("return ${value}")
    tpl_array_index_access = CT("${array_name}[${index}]")
    tpl_array_convert_to_numpy = CT("np.asarray(${value})")
    tpl_if_statement = CT("if ${if_def}:")
    tpl_else_statement = CT("else:")
    tpl_var_assignment = CT("${var_name} = ${value}")

    tpl_var_declaration = CT("")
    tpl_block_termination = CT("")

    def add_function_def(self, name, args):
        method_def = "def " + name + "("
        method_def += ", ".join(args)
        method_def += "):"
        self.add_code_line(method_def)
        self.increase_indent()

    @contextlib.contextmanager
    def function_definition(self, name, args):
        self.add_function_def(name, args)
github BayesWitnesses / m2cgen / m2cgen / interpreters / python / code_generator.py View on Github external
import contextlib

from m2cgen.interpreters.code_generator import BaseCodeGenerator
from m2cgen.interpreters.code_generator import CodeTemplate as CT


class PythonCodeGenerator(BaseCodeGenerator):

    tpl_num_value = CT("${value}")
    tpl_infix_expression = CT("(${left}) ${op} (${right})")
    tpl_return_statement = CT("return ${value}")
    tpl_array_index_access = CT("${array_name}[${index}]")
    tpl_array_convert_to_numpy = CT("np.asarray(${value})")
    tpl_if_statement = CT("if ${if_def}:")
    tpl_else_statement = CT("else:")
    tpl_var_assignment = CT("${var_name} = ${value}")

    tpl_var_declaration = CT("")
    tpl_block_termination = CT("")

    def add_function_def(self, name, args):
        method_def = "def " + name + "("
        method_def += ", ".join(args)
        method_def += "):"
        self.add_code_line(method_def)
github BayesWitnesses / m2cgen / m2cgen / interpreters / c / code_generator.py View on Github external
import contextlib

from m2cgen.interpreters.code_generator import CLikeCodeGenerator
from m2cgen.interpreters.code_generator import CodeTemplate as CT


class CCodeGenerator(CLikeCodeGenerator):

    tpl_scalar_var_declare = CT("double ${var_name};")
    tpl_vector_var_declare = CT("double ${var_name}[${size}];")

    scalar_type = "double"
    vector_type = "double *"

    def __init__(self, *args, **kwargs):
        super(CCodeGenerator, self).__init__(*args, **kwargs)

    def add_function_def(self, name, args, is_scalar_output):
        return_type = self.scalar_type if is_scalar_output else "void"

        function_def = return_type + " " + name + "("
        function_def += ", ".join([
            self._get_var_type(is_vector) + " " + n
            for is_vector, n in args])
        function_def += ") {"
github BayesWitnesses / m2cgen / m2cgen / interpreters / python / code_generator.py View on Github external
import contextlib

from m2cgen.interpreters.code_generator import BaseCodeGenerator
from m2cgen.interpreters.code_generator import CodeTemplate as CT


class PythonCodeGenerator(BaseCodeGenerator):

    tpl_num_value = CT("${value}")
    tpl_infix_expression = CT("(${left}) ${op} (${right})")
    tpl_return_statement = CT("return ${value}")
    tpl_array_index_access = CT("${array_name}[${index}]")
    tpl_array_convert_to_numpy = CT("np.asarray(${value})")
    tpl_if_statement = CT("if ${if_def}:")
    tpl_else_statement = CT("else:")
    tpl_var_assignment = CT("${var_name} = ${value}")

    tpl_var_declaration = CT("")
    tpl_block_termination = CT("")

    def add_function_def(self, name, args):
        method_def = "def " + name + "("
        method_def += ", ".join(args)
        method_def += "):"
        self.add_code_line(method_def)
        self.increase_indent()
github BayesWitnesses / m2cgen / m2cgen / interpreters / visual_basic / code_generator.py View on Github external
import contextlib

from m2cgen.ast import CompOpType
from m2cgen.interpreters.code_generator import BaseCodeGenerator, CodeTemplate


class VisualBasicCodeGenerator(BaseCodeGenerator):
    tpl_num_value = CodeTemplate("${value}")
    tpl_infix_expression = CodeTemplate("(${left}) ${op} (${right})")
    tpl_var_declaration = \
        CodeTemplate("Dim ${var_name}${type_modifier} As ${var_type}")
    tpl_return_statement = CodeTemplate("${func_name} = ${value}")
    tpl_if_statement = CodeTemplate("If ${if_def} Then")
    tpl_else_statement = CodeTemplate("Else")
    tpl_block_termination = CodeTemplate("End ${block_name}")
    tpl_array_index_access = CodeTemplate("${array_name}(${index})")
    tpl_array_set_by_index = CodeTemplate("${array_name}(${index}) = ${value}")
    tpl_var_assignment = CodeTemplate("${var_name} = ${value}")
    tpl_module_definition = CodeTemplate("Module ${module_name}")

    scalar_type = "Double"

    def __init__(self, *args, **kwargs):
        super(VisualBasicCodeGenerator, self).__init__(*args, **kwargs)

    def add_return_statement(self, value, func_name):
        self.add_code_line(self.tpl_return_statement(
            func_name=func_name, value=value))
github BayesWitnesses / m2cgen / m2cgen / interpreters / code_generator.py View on Github external
def _get_var_declare_type(self, expr):
        return NotImplemented

    def _comp_op_overwrite(self, op):
        return op.value


class CLikeCodeGenerator(BaseCodeGenerator):
    """
    This code generator provides C-like syntax so that subclasses will only
    have to provide logic for wrapping expressions into functions/classes/etc.
    """

    tpl_num_value = CodeTemplate("${value}")
    tpl_infix_expression = CodeTemplate("(${left}) ${op} (${right})")
    tpl_var_declaration = CodeTemplate("${var_type} ${var_name};")
    tpl_return_statement = CodeTemplate("return ${value};")
    tpl_array_index_access = CodeTemplate("${array_name}[${index}]")
    tpl_if_statement = CodeTemplate("if (${if_def}) {")
    tpl_else_statement = CodeTemplate("} else {")
    tpl_block_termination = CodeTemplate("}")
    tpl_var_assignment = CodeTemplate("${var_name} = ${value};")
github BayesWitnesses / m2cgen / m2cgen / interpreters / code_generator.py View on Github external
def _comp_op_overwrite(self, op):
        return op.value


class CLikeCodeGenerator(BaseCodeGenerator):
    """
    This code generator provides C-like syntax so that subclasses will only
    have to provide logic for wrapping expressions into functions/classes/etc.
    """

    tpl_num_value = CodeTemplate("${value}")
    tpl_infix_expression = CodeTemplate("(${left}) ${op} (${right})")
    tpl_var_declaration = CodeTemplate("${var_type} ${var_name};")
    tpl_return_statement = CodeTemplate("return ${value};")
    tpl_array_index_access = CodeTemplate("${array_name}[${index}]")
    tpl_if_statement = CodeTemplate("if (${if_def}) {")
    tpl_else_statement = CodeTemplate("} else {")
    tpl_block_termination = CodeTemplate("}")
    tpl_var_assignment = CodeTemplate("${var_name} = ${value};")
github BayesWitnesses / m2cgen / m2cgen / interpreters / powershell / code_generator.py View on Github external
import contextlib

from m2cgen.ast import CompOpType
from m2cgen.interpreters.code_generator import CLikeCodeGenerator
from m2cgen.interpreters.code_generator import CodeTemplate as CT


class PowershellCodeGenerator(CLikeCodeGenerator):

    tpl_var_declare = CT("${var_type}${var_name} = ${init_val}")
    tpl_var_assignment = CT("${var_name} = ${value}")
    tpl_array_index_access = CT("$$${array_name}[${index}]")
    tpl_return_statement = CT("return ${value}")

    scalar_type = "[double]"
    vector_type = "[double[]]"

    operator_map = {CompOpType.EQ: "-eq", CompOpType.NOT_EQ: "-ne",
                    CompOpType.GTE: "-ge", CompOpType.LTE: "-le",
                    CompOpType.GT: "-gt", CompOpType.LT: "-lt"}

    def __init__(self, *args, **kwargs):
        super(PowershellCodeGenerator, self).__init__(*args, **kwargs)

    def add_function_def(self, name, args, is_scalar_output):
        function_def = "function " + name + "("
        function_def += ", ".join([
            self._get_var_type(is_vector) + " $" + n
            for is_vector, n in args])
github BayesWitnesses / m2cgen / m2cgen / interpreters / visual_basic / code_generator.py View on Github external
import contextlib

from m2cgen.ast import CompOpType
from m2cgen.interpreters.code_generator import BaseCodeGenerator, CodeTemplate


class VisualBasicCodeGenerator(BaseCodeGenerator):
    tpl_num_value = CodeTemplate("${value}")
    tpl_infix_expression = CodeTemplate("(${left}) ${op} (${right})")
    tpl_var_declaration = \
        CodeTemplate("Dim ${var_name}${type_modifier} As ${var_type}")
    tpl_return_statement = CodeTemplate("${func_name} = ${value}")
    tpl_if_statement = CodeTemplate("If ${if_def} Then")
    tpl_else_statement = CodeTemplate("Else")
    tpl_block_termination = CodeTemplate("End ${block_name}")
    tpl_array_index_access = CodeTemplate("${array_name}(${index})")
    tpl_array_set_by_index = CodeTemplate("${array_name}(${index}) = ${value}")
    tpl_var_assignment = CodeTemplate("${var_name} = ${value}")
    tpl_module_definition = CodeTemplate("Module ${module_name}")

    scalar_type = "Double"

    def __init__(self, *args, **kwargs):
        super(VisualBasicCodeGenerator, self).__init__(*args, **kwargs)
github BayesWitnesses / m2cgen / m2cgen / interpreters / python / code_generator.py View on Github external
import contextlib

from m2cgen.interpreters.code_generator import BaseCodeGenerator
from m2cgen.interpreters.code_generator import CodeTemplate as CT


class PythonCodeGenerator(BaseCodeGenerator):

    tpl_num_value = CT("${value}")
    tpl_infix_expression = CT("(${left}) ${op} (${right})")
    tpl_return_statement = CT("return ${value}")
    tpl_array_index_access = CT("${array_name}[${index}]")
    tpl_array_convert_to_numpy = CT("np.asarray(${value})")
    tpl_if_statement = CT("if ${if_def}:")
    tpl_else_statement = CT("else:")
    tpl_var_assignment = CT("${var_name} = ${value}")

    tpl_var_declaration = CT("")
    tpl_block_termination = CT("")

    def add_function_def(self, name, args):
        method_def = "def " + name + "("
        method_def += ", ".join(args)
        method_def += "):"
        self.add_code_line(method_def)
        self.increase_indent()

    @contextlib.contextmanager
    def function_definition(self, name, args):
        self.add_function_def(name, args)
        yield