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
919cd141
Commit
919cd141
authored
Jun 13, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make proper module type and add sys.argv
parent
da22f5a8
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
89 additions
and
20 deletions
+89
-20
rt/include/python/builtins/list.hpp
rt/include/python/builtins/list.hpp
+7
-3
rt/include/python/builtins/print.hpp
rt/include/python/builtins/print.hpp
+35
-4
rt/include/python/builtins/str.hpp
rt/include/python/builtins/str.hpp
+9
-1
rt/include/python/sys.hpp
rt/include/python/sys.hpp
+6
-0
trans/stdlib/sys.py
trans/stdlib/sys.py
+1
-0
trans/tests/a_a_a_webserver.py
trans/tests/a_a_a_webserver.py
+9
-0
trans/tests/usertype.py
trans/tests/usertype.py
+0
-0
trans/transpiler/phases/emit_cpp/block.py
trans/transpiler/phases/emit_cpp/block.py
+4
-1
trans/transpiler/phases/emit_cpp/module.py
trans/transpiler/phases/emit_cpp/module.py
+2
-2
trans/transpiler/phases/typing/block.py
trans/transpiler/phases/typing/block.py
+7
-5
trans/transpiler/phases/typing/scope.py
trans/transpiler/phases/typing/scope.py
+4
-3
trans/transpiler/phases/typing/types.py
trans/transpiler/phases/typing/types.py
+5
-1
No files found.
rt/include/python/builtins/list.hpp
View file @
919cd141
...
...
@@ -26,18 +26,22 @@ public:
size_t
py_len
()
const
{
return
this
->
size
();
}
void
py_
print
(
std
::
ostream
&
s
)
const
{
void
py_
repr
(
std
::
ostream
&
s
)
const
{
s
<<
'['
;
if
(
this
->
size
()
>
0
)
{
print
_to
(
this
->
operator
[](
0
),
s
);
repr
_to
(
this
->
operator
[](
0
),
s
);
for
(
size_t
i
=
1
;
i
<
this
->
size
();
i
++
)
{
s
<<
", "
;
print
_to
(
this
->
operator
[](
i
),
s
);
repr
_to
(
this
->
operator
[](
i
),
s
);
}
}
s
<<
']'
;
}
void
py_print
(
std
::
ostream
&
s
)
const
{
py_repr
(
s
);
}
PyList
<
T
>
operator
+
(
const
PyList
<
T
>
&
other
)
const
{
std
::
vector
<
T
>
v
;
v
.
reserve
(
this
->
size
()
+
other
.
size
());
...
...
rt/include/python/builtins/print.hpp
View file @
919cd141
...
...
@@ -9,6 +9,17 @@
#include <ostream>
#include <typon/typon.hpp>
#include "str.hpp"
template
<
typename
T
>
concept
Printable
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
{
print_to
(
x
,
s
)
}
->
std
::
same_as
<
void
>
;
};
template
<
typename
T
>
concept
Reprable
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
{
repr_to
(
x
,
s
)
}
->
std
::
same_as
<
void
>
;
};
template
<
typename
T
>
concept
Streamable
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
...
...
@@ -16,6 +27,7 @@ concept Streamable = requires(const T &x, std::ostream &s) {
};
template
<
Streamable
T
>
void
print_to
(
const
T
&
x
,
std
::
ostream
&
s
)
{
s
<<
x
;
}
template
<
Streamable
T
>
void
repr_to
(
const
T
&
x
,
std
::
ostream
&
s
)
{
s
<<
x
;
}
template
<
typename
T
>
concept
FunctionPointer
=
...
...
@@ -23,11 +35,16 @@ concept FunctionPointer =
std
::
is_function_v
<
std
::
remove_pointer_t
<
T
>>
;
template
<
Streamable
T
>
requires
(
FunctionPointer
<
T
>
)
void
print
_to
(
const
T
&
x
,
std
::
ostream
&
s
)
{
requires
(
FunctionPointer
<
T
>
)
void
repr
_to
(
const
T
&
x
,
std
::
ostream
&
s
)
{
s
<<
"<function at 0x"
<<
std
::
hex
<<
(
size_t
)
x
<<
">"
;
}
template
<
>
void
repr_to
(
const
PyStr
&
x
,
std
::
ostream
&
s
)
{
s
<<
'"'
<<
x
<<
'"'
;
}
template
<
typename
T
>
concept
PyPrint
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
{
x
.
py_print
(
s
)
}
->
std
::
same_as
<
void
>
;
...
...
@@ -38,9 +55,23 @@ template <PyPrint T> void print_to(const T &x, std::ostream &s) {
}
template
<
typename
T
>
concept
P
rintable
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
{
print_to
(
x
,
s
)
}
->
std
::
same_as
<
void
>
;
concept
P
yRepr
=
requires
(
const
T
&
x
,
std
::
ostream
&
s
)
{
{
x
.
py_repr
(
s
)
}
->
std
::
same_as
<
void
>
;
};
template
<
PyRepr
T
>
void
repr_to
(
const
T
&
x
,
std
::
ostream
&
s
)
{
x
.
py_repr
(
s
);
}
template
<
Printable
T
>
void
print_to
(
const
std
::
shared_ptr
<
T
>
&
x
,
std
::
ostream
&
s
)
{
print_to
(
*
x
,
s
);
}
template
<
Printable
T
>
void
repr_to
(
const
std
::
shared_ptr
<
T
>
&
x
,
std
::
ostream
&
s
)
{
repr_to
(
*
x
,
s
);
}
/*
template <Printable T, Printable... Args>
typon::Task<void> print(T const &head, Args const &...args) {
...
...
rt/include/python/builtins/str.hpp
View file @
919cd141
...
...
@@ -10,10 +10,18 @@
using
namespace
std
::
literals
;
template
<
typename
T
>
std
::
string
str
(
const
T
&
x
)
{
using
PyStr
=
std
::
string
;
template
<
typename
T
>
PyStr
str
(
const
T
&
x
)
{
std
::
stringstream
s
;
print_to
(
x
,
s
);
return
s
.
str
();
}
template
<
typename
T
>
PyStr
repr
(
const
T
&
x
)
{
std
::
stringstream
s
;
repr_to
(
x
,
s
);
return
s
.
str
();
}
#endif // TYPON_STR_HPP
rt/include/python/sys.hpp
View file @
919cd141
...
...
@@ -6,13 +6,19 @@
#define TYPON_SYS_HPP
#include <iostream>
#include "builtins.hpp"
namespace
py_sys
{
struct
sys_t
{
static
constexpr
auto
&
stdin
=
std
::
cin
;
static
constexpr
auto
&
stdout
=
std
::
cout
;
static
constexpr
auto
&
stderr
=
std
::
cerr
;
PyObj
<
PyList
<
PyStr
>>
argv
;
}
all
;
auto
&
get_all
()
{
return
all
;
}
}
// namespace py_sys
#endif // TYPON_SYS_HPP
trans/stdlib/sys.py
View file @
919cd141
stdout
:
CppType
[
"auto&"
]
argv
:
list
[
str
]
\ No newline at end of file
trans/tests/a_a_a_webserver.py
0 → 100644
View file @
919cd141
# coding: utf-8
import
sys
BACKLOG
=
1024
PORT
=
8000
if
__name__
==
"__main__"
:
print
(
sys
.
argv
)
\ No newline at end of file
trans/tests/
a_a_
usertype.py
→
trans/tests/usertype.py
View file @
919cd141
File moved
trans/transpiler/phases/emit_cpp/block.py
View file @
919cd141
...
...
@@ -42,7 +42,10 @@ class BlockVisitor(NodeVisitor):
from
transpiler.phases.emit_cpp.function
import
FunctionVisitor
yield
from
FunctionVisitor
(
self
.
scope
,
CoroutineMode
.
TASK
).
emit_block
(
node
.
scope
,
block
())
yield
"int main() { root().call(); }"
yield
"int main(int argc, char* argv[]) {"
yield
"py_sys::all.argv = pyobj<PyList<PyStr>>(std::vector<PyStr>(argv, argv + argc));"
yield
"root().call();"
yield
"}"
return
yield
"struct {"
...
...
trans/transpiler/phases/emit_cpp/module.py
View file @
919cd141
...
...
@@ -17,7 +17,7 @@ class ModuleVisitor(BlockVisitor):
yield
""
else
:
yield
from
self
.
import_module
(
alias
.
name
)
yield
f'auto&
{
alias
.
asname
or
alias
.
name
}
= py_
{
alias
.
name
}
::
all
;'
yield
f'auto&
{
alias
.
asname
or
alias
.
name
}
= py_
{
alias
.
name
}
::
get_all()
;'
def
import_module
(
self
,
name
:
str
)
->
Iterable
[
str
]:
yield
f'#include "python/
{
name
}
.hpp"'
...
...
@@ -28,7 +28,7 @@ class ModuleVisitor(BlockVisitor):
else
:
yield
from
self
.
import_module
(
node
.
module
)
for
alias
in
node
.
names
:
yield
f"auto&
{
alias
.
asname
or
alias
.
name
}
= py_
{
node
.
module
}
::
all
.
{
alias
.
name
}
;"
yield
f"auto&
{
alias
.
asname
or
alias
.
name
}
= py_
{
node
.
module
}
::
get_all()
.
{
alias
.
name
}
;"
def
visit_Expr
(
self
,
node
:
ast
.
Expr
)
->
Iterable
[
str
]:
if
isinstance
(
node
.
value
,
ast
.
Str
):
...
...
trans/transpiler/phases/typing/block.py
View file @
919cd141
import
ast
import
dataclasses
from
dataclasses
import
dataclass
from
transpiler.phases.typing.common
import
ScoperVisitor
from
transpiler.phases.typing.expr
import
ScoperExprVisitor
from
transpiler.phases.typing.class_
import
ScoperClassVisitor
from
transpiler.phases.typing.scope
import
VarDecl
,
VarKind
,
ScopeKind
from
transpiler.phases.typing.types
import
BaseType
,
TypeVariable
,
FunctionType
,
IncompatibleTypesError
,
TY_MODULE
,
\
Promise
,
TY_NONE
,
PromiseKind
,
TupleType
,
UserType
,
TypeType
from
transpiler.phases.typing.types
import
BaseType
,
TypeVariable
,
FunctionType
,
IncompatibleTypesError
,
\
Promise
,
TY_NONE
,
PromiseKind
,
TupleType
,
UserType
,
TypeType
,
ModuleType
@
dataclass
...
...
@@ -21,13 +22,14 @@ class ScoperBlockVisitor(ScoperVisitor):
def
visit_Import
(
self
,
node
:
ast
.
Import
):
for
alias
in
node
.
names
:
self
.
scope
.
vars
[
alias
.
asname
or
alias
.
name
]
=
VarDecl
(
VarKind
.
LOCAL
,
None
)
mod
=
self
.
scope
.
get
(
alias
.
name
,
VarKind
.
MODULE
)
self
.
scope
.
vars
[
alias
.
asname
or
alias
.
name
]
=
dataclasses
.
replace
(
mod
,
kind
=
VarKind
.
LOCAL
)
def
visit_ImportFrom
(
self
,
node
:
ast
.
ImportFrom
):
module
=
self
.
scope
.
get
(
node
.
module
)
module
=
self
.
scope
.
get
(
node
.
module
)
# TODO: VarKind.MODULE ?
if
not
module
:
raise
NameError
(
node
.
module
)
if
module
.
type
is
not
TY_MODULE
:
if
not
isinstance
(
module
.
type
,
ModuleType
)
:
raise
IncompatibleTypesError
(
f"
{
node
.
module
}
is not a module"
)
for
alias
in
node
.
names
:
thing
=
module
.
val
.
get
(
alias
.
name
)
...
...
trans/transpiler/phases/typing/scope.py
View file @
919cd141
...
...
@@ -15,6 +15,7 @@ class VarKind(Enum):
"""`nonlocal xxx`"""
SELF
=
4
OUTER_DECL
=
5
MODULE
=
6
class
VarType
:
...
...
@@ -70,12 +71,12 @@ class Scope:
"""Declares a local variable"""
self
.
vars
[
name
]
=
VarDecl
(
VarKind
.
LOCAL
,
type
)
def
get
(
self
,
name
:
str
)
->
Optional
[
VarDecl
]:
def
get
(
self
,
name
:
str
,
kind
:
VarKind
=
VarKind
.
LOCAL
)
->
Optional
[
VarDecl
]:
"""
Gets the variable declaration of a variable in the current scope or any parent scope.
"""
if
(
res
:
=
self
.
vars
.
get
(
name
))
and
res
.
kind
==
VarKind
.
LOCAL
:
if
(
res
:
=
self
.
vars
.
get
(
name
))
and
res
.
kind
==
kind
:
return
res
if
self
.
parent
is
not
None
:
return
self
.
parent
.
get
(
name
)
return
self
.
parent
.
get
(
name
,
kind
)
return
None
trans/transpiler/phases/typing/types.py
View file @
919cd141
...
...
@@ -214,6 +214,10 @@ class TypeOperator(BaseType, ABC):
def
to_list
(
self
)
->
List
[
"BaseType"
]:
return
[
self
,
*
self
.
args
]
@
dataclass
class
ModuleType
(
TypeOperator
):
pass
class
FunctionType
(
TypeOperator
):
def
__init__
(
self
,
args
:
List
[
BaseType
],
ret
:
BaseType
):
...
...
@@ -270,7 +274,7 @@ TY_STR = TypeOperator([], "str")
TY_BOOL
=
TypeOperator
([],
"bool"
)
TY_COMPLEX
=
TypeOperator
([],
"complex"
)
TY_NONE
=
TypeOperator
([],
"NoneType"
)
TY_MODULE
=
TypeOperator
([],
"module"
)
#
TY_MODULE = TypeOperator([], "module")
TY_VARARG
=
TypeOperator
([],
"vararg"
)
TY_SELF
=
TypeOperator
([],
"Self"
)
TY_SELF
.
gen_sub
=
lambda
this
,
typevars
,
_
:
this
...
...
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