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
5f91e8b6
Commit
5f91e8b6
authored
Jun 14, 2023
by
Tom Niget
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix type call evaluation
parent
d3d93dac
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
100 additions
and
76 deletions
+100
-76
trans/requirements.txt
trans/requirements.txt
+1
-1
trans/test_runner.py
trans/test_runner.py
+11
-2
trans/tests/builtins_test.py
trans/tests/builtins_test.py
+0
-0
trans/tests/gen_test.py
trans/tests/gen_test.py
+12
-12
trans/tests/naive_fibonacci_fork.py
trans/tests/naive_fibonacci_fork.py
+54
-54
trans/tests/webserver.py
trans/tests/webserver.py
+0
-0
trans/transpiler/phases/typing/__init__.py
trans/transpiler/phases/typing/__init__.py
+20
-6
trans/transpiler/phases/typing/expr.py
trans/transpiler/phases/typing/expr.py
+2
-1
No files found.
trans/requirements.txt
View file @
5f91e8b6
clang-format==15.0.7
pytype~=2023.4.11
dataclasses~=0.6
python-dotenv~=1.0.0
\ No newline at end of file
python-dotenv~=1.0.0
trans/test_runner.py
View file @
5f91e8b6
# coding: utf-8
import
argparse
from
os
import
system
,
environ
from
pathlib
import
Path
...
...
@@ -10,7 +11,13 @@ from dotenv import load_dotenv
load_dotenv
()
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"-c"
,
"--compile"
,
action
=
"store_true"
)
args
=
parser
.
parse_args
()
def
run_tests
():
for
path
in
Path
(
'tests'
).
glob
(
'*.py'
):
print
(
path
.
name
)
if
path
.
name
.
startswith
(
'_'
):
...
...
@@ -22,9 +29,12 @@ def run_tests():
name_cpp
=
path
.
with_suffix
(
'.cpp'
)
with
open
(
name_cpp
,
"w"
,
encoding
=
"utf-8"
)
as
fcpp
:
fcpp
.
write
(
res
)
print
(
".cpp generated"
)
if
args
.
compile
:
continue
name_bin
=
path
.
with_suffix
(
''
).
as_posix
()
commands
=
[
f"bash -c 'PYTHONPATH=stdlib python3 ./
{
path
.
as_posix
()
}
'"
,
#
f"bash -c 'PYTHONPATH=stdlib python3 ./{path.as_posix()}'",
]
if
alt
:
=
environ
.
get
(
"ALT_RUNNER"
):
commands
.
append
(
alt
.
format
(
name_bin
=
name_bin
,
name_cpp_posix
=
name_cpp
.
as_posix
()))
...
...
@@ -34,7 +44,6 @@ def run_tests():
if
system
(
cmd
)
!=
0
:
print
(
f"Error running command:
{
cmd
}
"
)
break
#exit()
if
__name__
==
"__main__"
:
...
...
trans/tests/
a_a_
builtins_test.py
→
trans/tests/builtins_test.py
View file @
5f91e8b6
File moved
trans/tests/
a_
gen_test.py
→
trans/tests/gen_test.py
View file @
5f91e8b6
# coding: utf-8
def
fib
(
upto
):
a
=
0
b
=
1
while
b
<
upto
:
yield
a
a
,
b
=
b
,
a
+
b
if
__name__
==
"__main__"
:
f
=
fib
(
50
)
for
i
in
range
(
15
):
# coding: utf-8
def
fib
(
upto
):
a
=
0
b
=
1
while
b
<
upto
:
yield
a
a
,
b
=
b
,
a
+
b
if
__name__
==
"__main__"
:
f
=
fib
(
50
)
for
i
in
range
(
15
):
print
(
next
(
f
,
None
))
\ No newline at end of file
trans/tests/
a_
naive_fibonacci_fork.py
→
trans/tests/naive_fibonacci_fork.py
View file @
5f91e8b6
from
typon
import
fork
,
sync
def
fibo
(
n
:
int
)
->
int
:
if
n
<
2
:
return
n
a
=
fork
(
lambda
:
fibo
(
n
-
1
))
b
=
fork
(
lambda
:
fibo
(
n
-
2
))
sync
()
return
a
.
get
()
+
b
.
get
()
"""
def fibo(n: int) -> int:
if n < 2:
return n
with sync(): # {
a = fork(lambda: fibo(n - 1))
b = fork(lambda: fibo(n - 2))
# }
return a + b
"""
"""
Task<int> fibo(int n) {
if (n < 2) {
return n;
}
Forked<int> a;
Forked<int> b;
{
a = fork(fibo(n - 1));
// cvcvc
b = fork(fibo(n - 2));
co_await sync();
}
co_return a.get() + b.get();
"""
"""
Task<int> fibo(int n) {
int a, b;
co_return []() -> Join<int> {
if (n < 2) {
return n;
}
co_await fork(fibo(n - 1), a);
co_await fork(fibo(n - 2), b);
co_await Sync();
co_return a + b;
}();
}
"""
if
__name__
==
"__main__"
:
from
typon
import
fork
,
sync
def
fibo
(
n
:
int
)
->
int
:
if
n
<
2
:
return
n
a
=
fork
(
lambda
:
fibo
(
n
-
1
))
b
=
fork
(
lambda
:
fibo
(
n
-
2
))
sync
()
return
a
.
get
()
+
b
.
get
()
"""
def fibo(n: int) -> int:
if n < 2:
return n
with sync(): # {
a = fork(lambda: fibo(n - 1))
b = fork(lambda: fibo(n - 2))
# }
return a + b
"""
"""
Task<int> fibo(int n) {
if (n < 2) {
return n;
}
Forked<int> a;
Forked<int> b;
{
a = fork(fibo(n - 1));
// cvcvc
b = fork(fibo(n - 2));
co_await sync();
}
co_return a.get() + b.get();
"""
"""
Task<int> fibo(int n) {
int a, b;
co_return []() -> Join<int> {
if (n < 2) {
return n;
}
co_await fork(fibo(n - 1), a);
co_await fork(fibo(n - 2), b);
co_await Sync();
co_return a + b;
}();
}
"""
if
__name__
==
"__main__"
:
print
(
fibo
(
20
))
# should display 832040
\ No newline at end of file
trans/tests/
a_a_a_
webserver.py
→
trans/tests/webserver.py
View file @
5f91e8b6
File moved
trans/transpiler/phases/typing/__init__.py
View file @
5f91e8b6
import
ast
from
pathlib
import
Path
from
transpiler.phases.typing.scope
import
VarKind
,
VarDecl
,
ScopeKind
from
transpiler.phases.typing.scope
import
VarKind
,
VarDecl
,
ScopeKind
,
Scope
from
transpiler.phases.typing.stdlib
import
PRELUDE
,
StdlibVisitor
from
transpiler.phases.typing.types
import
TY_TYPE
,
TY_INT
,
TY_STR
,
TY_BOOL
,
TY_COMPLEX
,
TY_NONE
,
FunctionType
,
\
TypeVariable
,
TY_MODULE
,
CppType
,
PyList
,
TypeType
,
Forked
,
Task
,
Future
,
PyIterator
,
TupleType
TypeVariable
,
CppType
,
PyList
,
TypeType
,
Forked
,
Task
,
Future
,
PyIterator
,
TupleType
,
TypeOperator
,
BaseType
,
\
ModuleType
,
TY_BYTES
PRELUDE
.
vars
.
update
({
# "int": VarDecl(VarKind.LOCAL, TY_TYPE, TY_INT),
...
...
@@ -18,6 +19,7 @@ PRELUDE.vars.update({
# "list": VarDecl(VarKind.LOCAL, TY_TYPE, PyList),
"int"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
TY_INT
)),
"str"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
TY_STR
)),
"bytes"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
TY_BYTES
)),
"bool"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
TY_BOOL
)),
"complex"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
TY_COMPLEX
)),
"None"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
TY_NONE
)),
...
...
@@ -29,27 +31,39 @@ PRELUDE.vars.update({
"Task"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
Task
)),
"Future"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
Future
)),
"Iterator"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
PyIterator
)),
"
T
uple"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
TupleType
)),
"
t
uple"
:
VarDecl
(
VarKind
.
LOCAL
,
TypeType
(
TupleType
)),
})
typon_std
=
Path
(
__file__
).
parent
.
parent
.
parent
.
parent
/
"stdlib"
def
make_module
(
name
:
str
,
scope
:
Scope
)
->
BaseType
:
ty
=
ModuleType
([],
f"module$
{
name
}
"
)
for
n
,
v
in
scope
.
vars
.
items
():
ty
.
members
[
n
]
=
v
.
type
return
ty
def
discover_module
(
path
:
Path
,
scope
):
for
child
in
path
.
iterdir
(
):
for
child
in
sorted
(
path
.
iterdir
()
):
if
child
.
is_dir
():
mod_scope
=
PRELUDE
.
child
(
ScopeKind
.
GLOBAL
)
discover_module
(
child
,
mod_scope
)
scope
.
vars
[
child
.
name
]
=
VarDecl
(
VarKind
.
LOCAL
,
TY_MODULE
,
{
k
:
v
.
type
for
k
,
v
in
mod_scope
.
vars
.
items
()}
)
scope
.
vars
[
child
.
name
]
=
make_mod_decl
(
child
,
mod_scope
)
elif
child
.
name
==
"__init__.py"
:
StdlibVisitor
(
scope
).
visit
(
ast
.
parse
(
child
.
read_text
()))
print
(
f"Visited
{
child
}
"
)
elif
child
.
suffix
==
".py"
:
mod_scope
=
PRELUDE
.
child
(
ScopeKind
.
GLOBAL
)
StdlibVisitor
(
mod_scope
).
visit
(
ast
.
parse
(
child
.
read_text
()))
scope
.
vars
[
child
.
stem
]
=
VarDecl
(
VarKind
.
LOCAL
,
TY_MODULE
,
{
k
:
v
.
type
for
k
,
v
in
mod_scope
.
vars
.
items
()})
scope
.
vars
[
child
.
stem
]
=
make_mod_decl
(
child
,
mod_scope
)
print
(
f"Visited
{
child
}
"
)
def
make_mod_decl
(
child
,
mod_scope
):
return
VarDecl
(
VarKind
.
MODULE
,
make_module
(
child
.
name
,
mod_scope
),
{
k
:
v
.
type
for
k
,
v
in
mod_scope
.
vars
.
items
()})
discover_module
(
typon_std
,
PRELUDE
)
print
(
"Stdlib visited!"
)
#exit()
\ No newline at end of file
trans/transpiler/phases/typing/expr.py
View file @
5f91e8b6
...
...
@@ -109,7 +109,8 @@ class ScoperExprVisitor(ScoperVisitor):
def
visit_function_call
(
self
,
ftype
:
BaseType
,
arguments
:
List
[
BaseType
]):
if
isinstance
(
ftype
,
TypeType
):
# and isinstance(ftype.type_object, UserType):
init
:
FunctionType
=
self
.
visit_getattr
(
ftype
.
type_object
,
"__init__"
)
init
:
FunctionType
=
self
.
visit_getattr
(
ftype
,
"__init__"
).
remove_self
()
init
.
return_type
=
ftype
.
type_object
return
self
.
visit_function_call
(
init
,
arguments
)
if
not
isinstance
(
ftype
,
FunctionType
):
raise
IncompatibleTypesError
(
f"Cannot call
{
ftype
}
"
)
...
...
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