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
551ba3cc
Commit
551ba3cc
authored
Aug 30, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix various things, calcbasic works
parent
759c796e
Changes
10
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
49 additions
and
29 deletions
+49
-29
rt/include/python/builtins.hpp
rt/include/python/builtins.hpp
+17
-15
trans/tests/a_calcbasic.py
trans/tests/a_calcbasic.py
+0
-0
trans/tests/enumtest.py
trans/tests/enumtest.py
+0
-0
trans/transpiler/phases/emit_cpp/block.py
trans/transpiler/phases/emit_cpp/block.py
+1
-1
trans/transpiler/phases/emit_cpp/class_.py
trans/transpiler/phases/emit_cpp/class_.py
+1
-1
trans/transpiler/phases/emit_cpp/module.py
trans/transpiler/phases/emit_cpp/module.py
+3
-2
trans/transpiler/phases/typing/block.py
trans/transpiler/phases/typing/block.py
+3
-1
trans/transpiler/phases/typing/expr.py
trans/transpiler/phases/typing/expr.py
+8
-7
trans/transpiler/phases/typing/stdlib.py
trans/transpiler/phases/typing/stdlib.py
+1
-1
trans/transpiler/phases/typing/types.py
trans/transpiler/phases/typing/types.py
+15
-1
No files found.
rt/include/python/builtins.hpp
View file @
551ba3cc
...
...
@@ -66,32 +66,34 @@ class PyObj : public std::shared_ptr<typename RealType<T>::type> {
public:
using
inner
=
typename
RealType
<
T
>::
type
;
PyObj
()
:
std
::
shared_ptr
<
inner
>
()
{}
PyObj
(
std
::
nullptr_t
)
:
std
::
shared_ptr
<
inner
>
(
nullptr
)
{}
PyObj
(
inner
*
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
(
const
std
::
shared_ptr
<
inner
>
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
(
std
::
shared_ptr
<
inner
>
&&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
(
const
PyObj
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
(
PyObj
&&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
template
<
typename
...
Args
>
PyObj
(
Args
&&
...
args
)
:
std
::
shared_ptr
<
inner
>
(
std
::
make_shared
<
inner
>
(
std
::
forward
<
Args
>
(
args
)...))
{}
PyObj
()
:
std
::
shared_ptr
<
inner
>
()
{}
PyObj
(
std
::
nullptr_t
)
:
std
::
shared_ptr
<
inner
>
(
nullptr
)
{}
PyObj
(
inner
*
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
(
const
std
::
shared_ptr
<
inner
>
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
(
std
::
shared_ptr
<
inner
>
&&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
(
const
PyObj
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
(
PyObj
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
(
PyObj
&&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
PyObj
&
operator
=
(
const
PyObj
&
ptr
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
ptr
);
return
*
this
;
}
PyObj
&
operator
=
(
PyObj
&&
ptr
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
ptr
);
return
*
this
;
}
PyObj
&
operator
=
(
std
::
nullptr_t
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
nullptr
);
return
*
this
;
}
PyObj
&
operator
=
(
inner
*
ptr
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
ptr
);
return
*
this
;
}
PyObj
&
operator
=
(
const
std
::
shared_ptr
<
inner
>
&
ptr
)
{
std
::
shared_ptr
<
inner
>::
operator
=
(
ptr
);
return
*
this
;
}
template
<
typename
U
>
template
<
typename
U
>
PyObj
(
const
PyObj
<
U
>
&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
template
<
typename
U
>
PyObj
(
PyObj
<
U
>
&&
ptr
)
:
std
::
shared_ptr
<
inner
>
(
ptr
)
{}
//PyObj(PyObj<U> &&ptr) : std::shared_ptr<inner>(ptr) {}
// using make_shared
template
<
class
U
>
PyObj
(
U
&&
other
)
:
std
::
shared_ptr
<
inner
>
(
std
::
make_shared
<
inner
>
(
other
))
{}
/*template<class U>
PyObj(U&& other) : std::shared_ptr<inner>(std::make_shared<inner>(other)) {}*/
/*template<typename... Args>
PyObj(Args&&... args) : std::shared_ptr<inner>(std::forward<Args>(args)...) {}*/
...
...
@@ -124,7 +126,7 @@ public:
}
};
template
<
typename
T
,
typename
...
Args
>
auto
pyobj
(
Args
&&
...
args
)
->
PyObj
<
T
>
{
template
<
typename
T
,
typename
...
Args
>
auto
pyobj
(
Args
&&
...
args
)
->
PyObj
<
typename
RealType
<
T
>::
type
>
{
return
std
::
make_shared
<
typename
RealType
<
T
>::
type
>
(
std
::
forward
<
Args
>
(
args
)...);
}
...
...
trans/tests/a_calcbasic.py
2
→
trans/tests/a_calcbasic.py
View file @
551ba3cc
File moved
trans/tests/
a_a_
enumtest.py
→
trans/tests/enumtest.py
View file @
551ba3cc
File moved
trans/transpiler/phases/emit_cpp/block.py
View file @
551ba3cc
...
...
@@ -91,7 +91,7 @@ class BlockVisitor(NodeVisitor):
else
:
yield
from
self
.
visit
(
argty
)
yield
arg
.
arg
if
emission
in
{
FunctionEmissionKind
.
DECLARATION
,
FunctionEmissionKind
.
LAMBDA
}
and
default
:
if
emission
in
{
FunctionEmissionKind
.
DECLARATION
,
FunctionEmissionKind
.
LAMBDA
,
FunctionEmissionKind
.
METHOD
}
and
default
:
yield
" = "
yield
from
self
.
expr
().
visit
(
default
)
yield
")"
...
...
trans/transpiler/phases/emit_cpp/class_.py
View file @
551ba3cc
...
...
@@ -34,7 +34,7 @@ class ClassVisitor(NodeVisitor):
else
:
yield
"void py_repr(std::ostream &s) const {"
yield
"s << '{';"
for
i
,
(
name
,
memb
)
in
enumerate
(
node
.
type
.
fields
.
items
()):
for
i
,
(
name
,
memb
)
in
enumerate
(
node
.
type
.
get_members
()
.
items
()):
if
i
!=
0
:
yield
's << ", ";'
yield
f's << "
\
\
"
{
name
}\
\
": ";'
...
...
trans/transpiler/phases/emit_cpp/module.py
View file @
551ba3cc
...
...
@@ -27,8 +27,9 @@ class ModuleVisitor(BlockVisitor):
yield
f"struct
{
concrete
}
_t {{"
for
name
,
obj
in
alias
.
module_obj
.
fields
.
items
():
if
obj
.
type
.
python_func_used
:
yield
from
self
.
emit_python_func
(
alias
.
name
,
name
,
name
,
obj
.
type
)
ty
=
obj
.
type
.
resolve
()
if
getattr
(
ty
,
"python_func_used"
,
False
):
yield
from
self
.
emit_python_func
(
alias
.
name
,
name
,
name
,
ty
)
yield
"} all;"
yield
f"auto& get_all() {{ return all; }}"
...
...
trans/transpiler/phases/typing/block.py
View file @
551ba3cc
...
...
@@ -32,7 +32,9 @@ class ScoperBlockVisitor(ScoperVisitor):
# copy all functions to mod_scope
for
fname
,
obj
in
py_mod
.
__dict__
.
items
():
if
callable
(
obj
):
fty
=
FunctionType
([],
TypeVariable
())
# fty = FunctionType([], TypeVariable())
# fty.is_python_func = True
fty
=
TypeVariable
()
fty
.
is_python_func
=
True
mod_scope
.
vars
[
fname
]
=
VarDecl
(
VarKind
.
LOCAL
,
fty
)
mod
=
make_mod_decl
(
name
,
mod_scope
)
...
...
trans/transpiler/phases/typing/expr.py
View file @
551ba3cc
...
...
@@ -7,7 +7,7 @@ from transpiler.phases.typing import ScopeKind, VarDecl, VarKind
from
transpiler.phases.typing.common
import
ScoperVisitor
,
get_iter
,
get_next
from
transpiler.phases.typing.types
import
BaseType
,
TupleType
,
TY_STR
,
TY_BOOL
,
TY_INT
,
\
TY_COMPLEX
,
TY_NONE
,
FunctionType
,
PyList
,
TypeVariable
,
PySet
,
TypeType
,
PyDict
,
Promise
,
PromiseKind
,
UserType
,
\
TY_SLICE
,
TY_FLOAT
TY_SLICE
,
TY_FLOAT
,
RuntimeValue
from
transpiler.utils
import
linenodata
DUNDER
=
{
...
...
@@ -92,11 +92,12 @@ class ScoperExprVisitor(ScoperVisitor):
if
not
obj
:
from
transpiler.phases.typing.exceptions
import
UnknownNameError
raise
UnknownNameError
(
node
.
id
)
if
isinstance
(
obj
.
type
,
TypeType
)
and
isinstance
(
obj
.
type
.
type_object
,
TypeVariable
):
ty
=
obj
.
type
.
resolve
()
if
isinstance
(
ty
,
TypeType
)
and
isinstance
(
ty
.
type_object
,
TypeVariable
):
raise
NameError
(
f"Use of type variable"
)
# todo: when does this happen exactly?
if
getattr
(
obj
,
"is_python_func"
,
False
):
obj
.
python_func_used
=
True
return
obj
.
type
if
getattr
(
ty
,
"is_python_func"
,
False
):
ty
.
python_func_used
=
True
return
ty
def
visit_BoolOp
(
self
,
node
:
ast
.
BoolOp
)
->
BaseType
:
for
value
in
node
.
values
:
...
...
@@ -199,12 +200,12 @@ class ScoperExprVisitor(ScoperVisitor):
# else:
# return meth
if
field
:
=
ltype
.
fields
.
get
(
name
):
ty
=
field
.
type
ty
=
field
.
type
.
resolve
()
if
getattr
(
ty
,
"is_python_func"
,
False
):
ty
.
python_func_used
=
True
if
isinstance
(
ty
,
FunctionType
):
ty
=
ty
.
gen_sub
(
ltype
,
{})
if
bound
and
field
.
in_class_def
:
if
bound
and
field
.
in_class_def
and
type
(
field
.
val
)
!=
RuntimeValue
:
return
ty
.
remove_self
()
return
ty
...
...
trans/transpiler/phases/typing/stdlib.py
View file @
551ba3cc
...
...
@@ -111,7 +111,7 @@ class StdlibVisitor(NodeVisitorSeq):
if
isinstance
(
self
.
cur_class
.
type_object
,
ABCMeta
):
self
.
cur_class
.
type_object
.
gen_methods
[
node
.
name
]
=
lambda
t
:
ty
.
gen_sub
(
t
,
self
.
typevars
)
else
:
self
.
cur_class
.
type_object
.
fields
[
node
.
name
]
=
MemberDef
(
ty
.
gen_sub
(
self
.
cur_class
.
type_object
,
self
.
typevars
))
self
.
cur_class
.
type_object
.
fields
[
node
.
name
]
=
MemberDef
(
ty
.
gen_sub
(
self
.
cur_class
.
type_object
,
self
.
typevars
)
,
()
)
self
.
scope
.
vars
[
node
.
name
]
=
VarDecl
(
VarKind
.
LOCAL
,
ty
)
def
visit_Assert
(
self
,
node
:
ast
.
Assert
):
...
...
trans/transpiler/phases/typing/types.py
View file @
551ba3cc
...
...
@@ -148,6 +148,18 @@ def next_var_id():
class
TypeVariable
(
BaseType
):
name
:
str
=
field
(
default_factory
=
lambda
:
next_var_id
())
resolved
:
Optional
[
BaseType
]
=
None
patch_attrs
:
dict
=
field
(
default_factory
=
dict
)
def
__setattr__
(
self
,
key
,
value
):
if
"patch_attrs"
in
self
.
__dict__
and
key
not
in
self
.
__dict__
:
self
.
patch_attrs
[
key
]
=
value
else
:
super
().
__setattr__
(
key
,
value
)
def
__getattr__
(
self
,
item
):
if
"patch_attrs"
in
self
.
__dict__
and
item
in
self
.
patch_attrs
:
return
self
.
patch_attrs
[
item
]
raise
AttributeError
(
item
)
def
__str__
(
self
):
if
self
.
resolved
is
None
:
...
...
@@ -166,6 +178,8 @@ class TypeVariable(BaseType):
from
transpiler.phases.typing.exceptions
import
RecursiveTypeUnificationError
raise
RecursiveTypeUnificationError
(
self
,
other
)
self
.
resolved
=
other
for
k
,
v
in
self
.
patch_attrs
.
items
():
setattr
(
other
,
k
,
v
)
def
contains_internal
(
self
,
other
:
BaseType
)
->
bool
:
return
self
.
resolve
()
is
other
.
resolve
()
...
...
@@ -210,7 +224,7 @@ class TypeOperator(BaseType, ABC):
if
self
.
name
is
None
:
self
.
name
=
self
.
__class__
.
__name__
for
name
,
factory
in
self
.
gen_methods
.
items
():
self
.
fields
[
name
]
=
MemberDef
(
factory
(
self
))
self
.
fields
[
name
]
=
MemberDef
(
factory
(
self
)
,
()
)
for
gp
in
self
.
gen_parents
:
if
not
isinstance
(
gp
,
BaseType
):
gp
=
gp
(
self
.
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