Skip to content
This repository has been archived by the owner on Jun 4, 2018. It is now read-only.

In-memory AST for generated C source code #60

Merged
merged 15 commits into from
Jan 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions opesci/cgen_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from cgen import *


class IfDef(Module):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

""" docstring please """

"""
Class to represent IfDef-Else-EndIf construct for the C preprocessor.
While Cgen has classes for #define, #include, #pragma etc., it has nothing for IfDef.
:param condition: the condition in IfDef
:param iflines: the block of code inside the if [an array of type Generable]
:param elselines: the block of code inside the else [an array of type Generable]
"""
def __init__(self, condition, iflines, elselines):
ifdef_line = Line('#ifdef %s' % condition)
else_line = Line('#else')
endif_line = Line('#endif')
lines = [ifdef_line]+iflines+[else_line]+elselines+[endif_line]
super(IfDef, self).__init__(lines)


class InlineInitializer(Initializer):
"""
Class to represent Initializers (int i=0) without the semicolon in the end(e.g. in a for statement)
Usage: same as cgen.Initializer
Result: same as cgen.Initializer except for the lack of a semi-colon at the end
"""
def generate(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

""" docstring please """

result = super(InlineInitializer, self).generate()
for v in result:
if v.endswith(';'):
yield v[:-1]
else:
yield v


def replace_in_code(code, str_from, str_to):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

""" docstring please """

"""
Helper function to find and replace strings inside code blocks
:param code: The code block in which to carry out the find/replace. This can be array of Generables or a Block or a Loop
:param str_from: The string to find (and to be replaced)
:param str_to: The string to replace all instances of str_from with
"""
if isinstance(code, Block):
replace_in_code(code.contents, str_from, str_to)
return code
if isinstance(code, Loop):
replace_in_code(code.body, str_from, str_to)
return code
for code_element in code:
if isinstance(code_element, Statement) or isinstance(code_element, Line):
code_element.text.replace(str_from, str_to)
if isinstance(code_element, Block):
replace_in_code(code_element.contents, str_from, str_to)
if isinstance(code_element, Loop):
replace_in_code(code_element.body, str_from, str_to)
return code
7 changes: 4 additions & 3 deletions opesci/compilation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from os import path, environ
import subprocess
import cgen_wrapper as cgen


def get_package_dir():
Expand Down Expand Up @@ -62,7 +63,7 @@ def __init__(self, cppargs=[], ldargs=[]):

@property
def _ivdep(self):
return '#pragma GCC ivdep'
return cgen.Pragma('GCC ivdep')


class ClangCompiler(Compiler):
Expand All @@ -82,7 +83,7 @@ def __init__(self, cppargs=[], ldargs=[]):

@property
def _ivdep(self):
return '#pragma ivdep'
return cgen.Pragma('ivdep')


class IntelCompiler(Compiler):
Expand All @@ -100,4 +101,4 @@ def __init__(self, cppargs=[], ldargs=[]):

@property
def _ivdep(self):
return '#pragma ivdep'
return cgen.Pragma('ivdep')
Loading