Skip to content

Commit

Permalink
Embedded code before module namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Sep 12, 2020
1 parent 6a4162c commit d9f63c9
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ message_passing:
mkdir -p wip/message_passing/build/transpiled/src/message_passing
cd wip/message_passing && cp *.hpp build/transpiled/include/message_passing
cd wip/message_passing && cp *.cpp build/transpiled/src/message_passing
cd wip/message_passing && env MYS="$(MYS)" $(MYS) build
cd wip/message_passing && env MYS="$(MYS)" $(MYS) build || true

lint:
cd exceptions && $(MYS) lint
Expand Down
10 changes: 0 additions & 10 deletions mys/lib/mys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,6 @@ int main(int argc, const char *argv[])

#endif

namespace mys::random
{

float random()
{
return 0.0;
}

}

namespace mys::thread
{

Expand Down
11 changes: 10 additions & 1 deletion mys/lint/pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,16 @@ notes=FIXME,

# List of additional names supposed to be defined in builtins. Remember that
# you should avoid defining new builtins when possible.
additional-builtins=public,Optional,Final,test,assert_eq
additional-builtins=public,
Optional,
Final,
test,
assert_eq,
assert_ne,
assert_gt,
assert_ge,
assert_lt,
assert_le

# Tells whether unused global variables should be treated as a violation.
allow-global-unused-variables=yes
Expand Down
24 changes: 23 additions & 1 deletion mys/transpile.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ def __init__(self, module_hpp, skip_tests, namespace):
self.namespace = namespace
self.forward_declarations = []
self.add_package_main = False
self.before_namespace = []

def visit_Module(self, node):
body = [
Expand All @@ -650,7 +651,8 @@ def visit_Module(self, node):

return '\n\n'.join([
'// This file was generated by mys. DO NOT EDIT!!!',
f'#include "{self.module_hpp}"',
f'#include "{self.module_hpp}"'
] + self.before_namespace + [
f'namespace {self.namespace}',
'{'
] + self.forward_declarations + body + [
Expand Down Expand Up @@ -772,6 +774,26 @@ def visit_FunctionDef(self, node):

return code

def handle_string_source(self, value):
if value.startswith('mys-embedded-c++-before-namespace'):
self.before_namespace.append('\n'.join([
'/* mys-embedded-c++-before-namespace start */\n',
textwrap.dedent(value[33:]).strip(),
'\n/* mys-embedded-c++-before-namespace stop */']))

return ''
else:
return handle_string(value)

def visit_Constant(self, node):
if isinstance(node.value, str):
return self.handle_string_source(node.value)
else:
return super().visit_Constant(node)

def visit_Str(self, node):
return self.handle_string_source(node.s)

def generic_visit(self, node):
raise Exception(node)

Expand Down
2 changes: 1 addition & 1 deletion mys/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.62.1'
__version__ = '0.63.0'
8 changes: 8 additions & 0 deletions tests/files/basics.mys
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
'''mys-embedded-c++-before-namespace
#include <before>
'''

'''mys-embedded-c++
#include <not-before>
'''

def func_1(a: int) -> (int, Final[str]):
return 2 * a, 'Bar'

Expand Down
14 changes: 14 additions & 0 deletions tests/files/basics.mys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

#include "basics.mys.hpp"

/* mys-embedded-c++-before-namespace start */

#include <before>

/* mys-embedded-c++-before-namespace stop */

namespace mys::basics

{
Expand All @@ -18,6 +24,14 @@ void func_5(void);

int main(int __argc, const char *__argv[]);

;

/* mys-embedded-c++ start */

#include <not-before>

/* mys-embedded-c++ stop */;

Tuple<int, String> func_1(int a)
{
return Tuple<todo>({(2 * a), "Bar"});
Expand Down

0 comments on commit d9f63c9

Please sign in to comment.