Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Franck Pommereau
/
codanim
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Franck Pommereau
2020-01-31 18:55:34 +0100
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
88c6a57a41f5b5324dd5bbdc267a0ba6aca98f8d
88c6a57a
1 parent
9394c6c4
added verbose __repr__ on objects
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
6 deletions
codanim/__init__.py
codanim/data.py
codanim/flow.py
codanim/__init__.py
View file @
88c6a57
import
inspect
from
functools
import
wraps
from
yapf.yapflib.yapf_api
import
FormatCode
class
ExecEnv
(
dict
)
:
_ValueClass
=
None
def
__init__
(
self
,
*
l
,
**
k
)
:
...
...
@@ -21,6 +25,14 @@ class ExecEnv (dict) :
def
eval
(
self
,
code
)
:
return
eval
(
code
,
self
)
def
_repr
(
obj
)
:
if
inspect
.
isclass
(
obj
)
:
return
obj
.
__name__
elif
inspect
.
isfunction
(
obj
)
:
return
obj
.
__name__
else
:
return
repr
(
obj
)
class
CAni
(
object
)
:
_env
=
ExecEnv
()
@property
...
...
@@ -40,3 +52,29 @@ class CAni (object) :
def
eval
(
self
,
code
)
:
RET
=
self
.
RET
=
self
.
_env
.
eval
(
code
)
return
RET
_init_args
=
(
None
,
None
)
def
__repr__
(
self
)
:
largs
,
kargs
=
self
.
_init_args
if
largs
and
kargs
:
r
=
"{}({}, {})"
.
format
(
self
.
__class__
.
__name__
,
", "
.
join
(
_repr
(
l
)
for
l
in
largs
),
", "
.
join
(
"{}={}"
.
format
(
k
,
_repr
(
v
))
for
k
,
v
in
kargs
.
items
()))
elif
largs
:
r
=
"{}({})"
.
format
(
self
.
__class__
.
__name__
,
", "
.
join
(
_repr
(
l
)
for
l
in
largs
))
elif
kargs
:
r
=
"{}({})"
.
format
(
self
.
__class__
.
__name__
,
", "
.
join
(
"{}={}"
.
format
(
k
,
_repr
(
v
))
for
k
,
v
in
kargs
.
items
()))
else
:
r
=
"{}()"
.
format
(
self
.
__class__
.
__name__
)
return
FormatCode
(
r
)[
0
]
def
autorepr
(
method
)
:
@wraps
(
method
)
def
wrapper
(
self
,
*
l
,
**
k
)
:
self
.
__dict__
[
"_init_args"
]
=
(
l
,
k
)
return
method
(
self
,
*
l
,
**
k
)
return
wrapper
...
...
codanim/data.py
View file @
88c6a57
from
itertools
import
chain
from
inspect
import
cleandoc
from
collections
import
defaultdict
from
.
import
CAni
,
ExecEnv
from
.
import
CAni
,
ExecEnv
,
autorepr
class
dopt
(
dict
)
:
def
__str__
(
self
)
:
...
...
@@ -100,6 +100,7 @@ class CAniTikZ (CAni) :
code
=
"
\n
"
.
join
(
self
.
tikz
(
**
tikz
)
.
splitlines
()))
class
Pointer
(
CAniTikZ
)
:
@autorepr
def
__init__
(
self
,
data
)
:
self
.
__dict__
.
update
(
_d
=
data
,
nodeid
=
None
)
def
val
(
self
)
:
...
...
@@ -120,6 +121,7 @@ class Pointer (CAniTikZ) :
setattr
(
self
.
_d
,
key
,
val
)
class
Value
(
CAniTikZ
)
:
@autorepr
def
__init__
(
self
,
init
=
None
,
**
tikz
)
:
super
()
.
__init__
(
tikz
)
self
.
_h
=
[[
init
,
self
.
IP
,
None
]]
...
...
@@ -190,6 +192,7 @@ class Value (CAniTikZ) :
ExecEnv
.
_ValueClass
=
Value
class
Aggregate
(
CAniTikZ
)
:
@autorepr
def
__init__
(
self
,
init
,
**
tikz
)
:
super
()
.
__init__
(
tikz
)
if
isinstance
(
init
,
int
)
:
...
...
@@ -315,6 +318,7 @@ class Aggregate (CAniTikZ) :
class
Array
(
Aggregate
)
:
_defaults
=
{
"aggregate"
:
{
"index"
:
"north"
}}
@autorepr
def
__init__
(
self
,
init
,
index
=
[],
**
tikz
)
:
super
()
.
__init__
(
init
,
**
tikz
)
self
.
_o
.
aggregatescope
=
self
.
_o
.
arrayscope
...
...
@@ -366,6 +370,7 @@ class Array (Aggregate) :
class
Struct
(
Aggregate
)
:
_defaults
=
{
"aggregate"
:
{
"grow"
:
"south"
,
"ticks"
:
"west"
}}
@autorepr
def
__init__
(
self
,
init
,
**
tikz
)
:
self
.
__dict__
.
update
(
_d
=
{},
_o
=
None
,
_first
=
None
,
_last
=
None
,
nodeid
=
None
)
super
()
.
__init__
(
init
,
**
tikz
)
...
...
@@ -382,6 +387,7 @@ class Struct (Aggregate) :
class
Heap
(
CAniTikZ
)
:
_defaults
=
{
"group"
:
{
"inner sep"
:
"5mm"
}}
@autorepr
def
__init__
(
self
,
**
tikz
)
:
super
()
.
__init__
(
tikz
)
self
.
_alloc
=
{}
...
...
@@ -426,6 +432,7 @@ _flip = {"right": "below",
"above"
:
"left"
}
class
Box
(
CAniTikZ
)
:
@autorepr
def
__init__
(
self
,
*
content
,
grow
=
"right"
,
distance
=
"15mm"
,
parent
=
None
,
**
tikz
)
:
super
()
.
__init__
(
tikz
)
self
.
_grow
=
grow
...
...
codanim/flow.py
View file @
88c6a57
from
inspect
import
Signature
,
Parameter
from
.
import
CAni
from
.
import
CAni
,
autorepr
from
.highlight
import
pygmentize
class
_CODE
(
CAni
)
:
_fields
=
[]
_options
=
[]
@autorepr
def
__init__
(
self
,
*
l
,
**
k
)
:
params
=
[]
for
i
,
name
in
enumerate
(
self
.
_fields
)
:
...
...
@@ -19,7 +20,7 @@ class _CODE (CAni) :
params
.
append
(
Parameter
(
name
,
Parameter
.
POSITIONAL_OR_KEYWORD
,
default
=
None
))
params
.
append
(
Parameter
(
"src"
,
Parameter
.
KEYWORD_ONLY
,
default
=
None
))
sig
=
Signature
(
params
)
args
=
sig
.
bind
(
*
l
,
**
k
)
args
=
s
elf
.
_args
=
s
ig
.
bind
(
*
l
,
**
k
)
args
.
apply_defaults
()
for
key
,
val
in
args
.
arguments
.
items
()
:
setattr
(
self
,
key
,
val
)
...
...
@@ -84,6 +85,7 @@ class STMT (_CODE) :
class
EXPR
(
_CODE
)
:
_fields
=
[
"expr"
]
@autorepr
def
__init__
(
self
,
*
l
,
**
k
)
:
super
()
.
__init__
(
*
l
,
**
k
)
if
self
.
src
is
None
:
...
...
@@ -113,6 +115,7 @@ class ENV (_CODE) :
class
WS
(
_CODE
)
:
_fields
=
[]
@autorepr
def
__init__
(
self
,
src
)
:
super
()
.
__init__
(
src
=
src
)
def
__call__
(
self
)
:
...
...
@@ -120,6 +123,14 @@ class WS (_CODE) :
def
tex
(
self
)
:
return
self
.
src
class
RAW
(
_CODE
)
:
_fields
=
[]
@autorepr
def
__init__
(
self
,
src
)
:
super
()
.
__init__
(
src
=
src
)
def
__call__
(
self
)
:
pass
class
XDECL
(
_CODE
)
:
_fields
=
[
"*names"
]
def
__call__
(
self
)
:
...
...
@@ -155,8 +166,7 @@ class DECL (_CODE) :
value
=
tail
.
format
(
value
=
value
))
class
BreakLoop
(
Exception
)
:
def
__init__
(
self
)
:
super
()
.
__init__
()
pass
class
BREAK
(
_CODE
)
:
def
__call__
(
self
)
:
...
...
@@ -233,4 +243,3 @@ class FUNC (_CODE) :
self
.
body
()
except
FunctionReturn
as
exc
:
self
.
_env
[
"RET"
]
=
exc
.
RET
...
...
Please
register
or
login
to post a comment