Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
typon-compiler
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
typon
typon-compiler
Commits
fd3a6d6a
Commit
fd3a6d6a
authored
Mar 10, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor builtins structure, enhance module emulation system
parent
0b91cf1f
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
74 additions
and
52 deletions
+74
-52
rt/include/python/builtins.hpp
rt/include/python/builtins.hpp
+1
-44
rt/include/python/builtins/bool.hpp
rt/include/python/builtins/bool.hpp
+2
-0
rt/include/python/builtins/complex.hpp
rt/include/python/builtins/complex.hpp
+4
-1
rt/include/python/builtins/print.hpp
rt/include/python/builtins/print.hpp
+57
-0
rt/include/python/sys.hpp
rt/include/python/sys.hpp
+7
-5
trans/tests/builtins_test.py
trans/tests/builtins_test.py
+1
-0
trans/transpiler/__init__.py
trans/transpiler/__init__.py
+2
-2
No files found.
rt/include/python/builtins.hpp
View file @
fd3a6d6a
...
...
@@ -12,41 +12,7 @@
using
namespace
std
::
literals
;
template
<
typename
T
>
concept
Streamable
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
{
s
<<
x
}
->
std
::
same_as
<
std
::
ostream
&>
;
};
template
<
Streamable
T
>
void
print_to
(
const
T
&
x
,
std
::
ostream
&
s
)
{
s
<<
x
;
}
template
<
typename
T
>
concept
FunctionPointer
=
std
::
is_function_v
<
T
>
or
std
::
is_member_function_pointer_v
<
T
>
or
std
::
is_function_v
<
std
::
remove_pointer_t
<
T
>>
;
template
<
Streamable
T
>
requires
(
FunctionPointer
<
T
>
)
void
print_to
(
const
T
&
x
,
std
::
ostream
&
s
)
{
s
<<
"<function at 0x"
<<
std
::
hex
<<
(
size_t
)
x
<<
">"
;
}
template
<
typename
T
>
concept
PyPrint
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
{
x
.
py_print
(
s
)
}
->
std
::
same_as
<
void
>
;
};
template
<
PyPrint
T
>
void
print_to
(
const
T
&
x
,
std
::
ostream
&
s
)
{
x
.
py_print
(
s
);
}
template
<
typename
T
>
concept
Printable
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
{
print_to
(
x
,
s
)
}
->
std
::
same_as
<
void
>
;
};
template
<
typename
T
>
concept
PyIterator
=
requires
(
T
t
)
{
...
...
@@ -73,9 +39,6 @@ size_t len(const T &t) {
return
t
.
py_len
();
}
void
print
()
{
std
::
cout
<<
'\n'
;
}
bool
is_cpp
()
{
return
true
;
...
...
@@ -85,14 +48,8 @@ bool is_cpp() {
#include "builtins/complex.hpp"
#include "builtins/dict.hpp"
#include "builtins/list.hpp"
#include "builtins/print.hpp"
#include "builtins/set.hpp"
#include "builtins/str.hpp"
template
<
Printable
T
,
Printable
...
Args
>
void
print
(
T
const
&
head
,
Args
const
&
...
args
)
{
print_to
(
head
,
std
::
cout
);
(((
std
::
cout
<<
' '
),
print_to
(
args
,
std
::
cout
)),
...);
std
::
cout
<<
'\n'
;
}
#endif //TYPON_BUILTINS_HPP
rt/include/python/builtins/bool.hpp
View file @
fd3a6d6a
...
...
@@ -7,6 +7,8 @@
#include <ostream>
#include "print.hpp"
template
<
>
void
print_to
<
bool
>
(
const
bool
&
x
,
std
::
ostream
&
s
)
{
s
<<
(
x
?
"True"
:
"False"
);
...
...
rt/include/python/builtins/complex.hpp
View file @
fd3a6d6a
...
...
@@ -8,6 +8,8 @@
#include <complex>
#include <ostream>
#include "print.hpp"
using
PyComplex
=
std
::
complex
<
double
>
;
PyComplex
operator
+
(
int
a
,
const
PyComplex
&
b
)
{
...
...
@@ -18,7 +20,8 @@ PyComplex operator-(int a, const PyComplex &b) {
return
PyComplex
(
a
)
-
b
;
}
void
print_to
(
const
PyComplex
&
x
,
std
::
ostream
&
s
)
{
template
<
>
void
print_to
<
PyComplex
>
(
const
PyComplex
&
x
,
std
::
ostream
&
s
)
{
if
(
x
.
real
()
==
0
)
{
s
<<
x
.
imag
()
<<
"j"
;
}
else
{
...
...
rt/include/python/builtins/print.hpp
0 → 100644
View file @
fd3a6d6a
//
// Created by Tom on 09/03/2023.
//
#ifndef TYPON_PRINT_HPP
#define TYPON_PRINT_HPP
#include <iostream>
#include <ostream>
template
<
typename
T
>
concept
Streamable
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
{
s
<<
x
}
->
std
::
same_as
<
std
::
ostream
&>
;
};
template
<
Streamable
T
>
void
print_to
(
const
T
&
x
,
std
::
ostream
&
s
)
{
s
<<
x
;
}
template
<
typename
T
>
concept
FunctionPointer
=
std
::
is_function_v
<
T
>
or
std
::
is_member_function_pointer_v
<
T
>
or
std
::
is_function_v
<
std
::
remove_pointer_t
<
T
>>
;
template
<
Streamable
T
>
requires
(
FunctionPointer
<
T
>
)
void
print_to
(
const
T
&
x
,
std
::
ostream
&
s
)
{
s
<<
"<function at 0x"
<<
std
::
hex
<<
(
size_t
)
x
<<
">"
;
}
template
<
typename
T
>
concept
PyPrint
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
{
x
.
py_print
(
s
)
}
->
std
::
same_as
<
void
>
;
};
template
<
PyPrint
T
>
void
print_to
(
const
T
&
x
,
std
::
ostream
&
s
)
{
x
.
py_print
(
s
);
}
template
<
typename
T
>
concept
Printable
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
{
print_to
(
x
,
s
)
}
->
std
::
same_as
<
void
>
;
};
template
<
Printable
T
,
Printable
...
Args
>
void
print
(
T
const
&
head
,
Args
const
&
...
args
)
{
print_to
(
head
,
std
::
cout
);
(((
std
::
cout
<<
' '
),
print_to
(
args
,
std
::
cout
)),
...);
std
::
cout
<<
'\n'
;
}
void
print
()
{
std
::
cout
<<
'\n'
;
}
#endif //TYPON_PRINT_HPP
rt/include/python/sys.hpp
View file @
fd3a6d6a
...
...
@@ -7,10 +7,12 @@
#include <iostream>
struct
sys_t
{
static
constexpr
auto
&
stdin
=
std
::
cin
;
static
constexpr
auto
&
stdout
=
std
::
cout
;
static
constexpr
auto
&
stderr
=
std
::
cerr
;
}
sys
;
namespace
py_sys
{
struct
sys_t
{
static
constexpr
auto
&
stdin
=
std
::
cin
;
static
constexpr
auto
&
stdout
=
std
::
cout
;
static
constexpr
auto
&
stderr
=
std
::
cerr
;
}
all
;
}
#endif //TYPON_SYS_HPP
trans/tests/builtins_test.py
View file @
fd3a6d6a
# coding: utf-8
from
typon
import
is_cpp
import
sys
as
sis
from
sys
import
stdout
as
truc
test
=
(
2
+
3
)
*
4
...
...
trans/transpiler/__init__.py
View file @
fd3a6d6a
...
...
@@ -387,7 +387,7 @@ class BlockVisitor(NodeVisitor):
yield
""
else
:
yield
from
self
.
import_module
(
alias
.
name
)
#raise NotImplementedError(node)
yield
f'auto&
{
alias
.
asname
or
alias
.
name
}
= py_
{
alias
.
name
}
::all;'
def
import_module
(
self
,
name
:
str
)
->
Iterable
[
str
]:
yield
f'#include "python/
{
name
}
.hpp"'
...
...
@@ -398,7 +398,7 @@ class BlockVisitor(NodeVisitor):
else
:
yield
from
self
.
import_module
(
node
.
module
)
for
alias
in
node
.
names
:
yield
f"auto&
{
alias
.
asname
or
alias
.
name
}
=
{
node
.
module
}
.
{
alias
.
name
}
;"
yield
f"auto&
{
alias
.
asname
or
alias
.
name
}
=
py_
{
node
.
module
}
::all
.
{
alias
.
name
}
;"
def
visit_FunctionDef
(
self
,
node
:
ast
.
FunctionDef
)
->
Iterable
[
str
]:
templ
,
args
=
self
.
process_args
(
node
.
args
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment