Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
trs
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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
trs
Commits
6556ca49
Commit
6556ca49
authored
Feb 27, 2012
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of kompiler.org:trs
parents
eaf393e5
1d219423
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
107 additions
and
69 deletions
+107
-69
src/parser.py
src/parser.py
+2
-1
src/possibilities.py
src/possibilities.py
+0
-9
src/rules/numerics.py
src/rules/numerics.py
+19
-1
src/strategy.py
src/strategy.py
+18
-0
tests/test_leiden_oefenopgave.py
tests/test_leiden_oefenopgave.py
+43
-49
tests/test_leiden_oefenopgave_v12.py
tests/test_leiden_oefenopgave_v12.py
+25
-9
No files found.
src/parser.py
View file @
6556ca49
...
...
@@ -17,7 +17,8 @@ from graph_drawing.graph import generate_graph
from
node
import
ExpressionNode
as
Node
,
ExpressionLeaf
as
Leaf
,
OP_MAP
,
\
TOKEN_MAP
,
TYPE_OPERATOR
,
OP_COMMA
,
OP_NEG
,
OP_MUL
,
Scope
,
PI
from
rules
import
RULES
from
possibilities
import
filter_duplicates
,
pick_suggestion
,
apply_suggestion
from
strategy
import
pick_suggestion
from
possibilities
import
filter_duplicates
,
apply_suggestion
import
Queue
...
...
src/possibilities.py
View file @
6556ca49
...
...
@@ -54,15 +54,6 @@ def filter_duplicates(possibilities):
return
unique
def
pick_suggestion
(
possibilities
):
if
not
possibilities
:
return
# TODO: pick the best suggestion.
suggestion
=
0
return
possibilities
[
suggestion
]
def
find_parent_node
(
root
,
child
):
nodes
=
[
root
]
...
...
src/rules/numerics.py
View file @
6556ca49
...
...
@@ -15,12 +15,20 @@ def match_add_numerics(node):
2 + -3 -> -1
-2 + 3 -> 1
-2 + -3 -> -5
0 + 3 -> 3
0 + -3 -> -3
"""
assert
node
.
is_op
(
OP_ADD
)
p
=
[]
scope
=
Scope
(
node
)
numerics
=
filter
(
lambda
n
:
n
.
is_numeric
(),
scope
)
numerics
=
[]
for
n
in
scope
:
if
n
==
0
:
p
.
append
(
P
(
node
,
remove_zero
,
(
scope
,
n
)))
elif
n
.
is_numeric
():
numerics
.
append
(
n
)
for
c0
,
c1
in
combinations
(
numerics
,
2
):
p
.
append
(
P
(
node
,
add_numerics
,
(
scope
,
c0
,
c1
)))
...
...
@@ -28,6 +36,16 @@ def match_add_numerics(node):
return
p
def
remove_zero
(
root
,
args
):
"""
0 + a -> a
"""
scope
,
n
=
args
scope
.
remove
(
n
)
return
scope
.
as_nary_node
()
def
add_numerics
(
root
,
args
):
"""
2 + 3 -> 5
...
...
src/strategy.py
0 → 100644
View file @
6556ca49
from
rules.sort
import
move_constant
def
pick_suggestion
(
possibilities
):
if
not
possibilities
:
return
# TODO: pick the best suggestion.
for
suggestion
,
p
in
enumerate
(
possibilities
+
[
None
]):
if
p
and
p
.
handler
not
in
[
move_constant
]:
break
if
not
p
:
return
possibilities
[
0
]
return
possibilities
[
suggestion
]
tests/test_leiden_oefenopgave.py
View file @
6556ca49
...
...
@@ -25,39 +25,36 @@ class TestLeidenOefenopgave(TestCase):
self
.
assertEqual
(
str
(
rewrite
(
exp
)),
solution
)
def
test_1_2
(
self
):
for
chain
in
[[
'(x+1)^3'
,
'(x + 1)(x + 1) ^ 2'
,
'(x + 1)(x + 1)(x + 1)'
,
'(xx + x * 1 + 1x + 1 * 1)(x + 1)'
,
'(x ^ (1 + 1) + x * 1 + 1x + 1 * 1)(x + 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x + 1)'
,
'xx + x * 1 + 1x + 1 * 1'
,
'x ^ (1 + 1) + x * 1 + 1x + 1 * 1'
,
'x ^ 2 + x * 1 + 1x + 1 * 1'
,
...
...
@@ -65,26 +62,24 @@ class TestLeidenOefenopgave(TestCase):
'x ^ 2 + x + x + 1 * 1'
,
'x ^ 2 + (1 + 1)x + 1 * 1'
,
'x ^ 2 + 2x + 1 * 1'
,
'x ^ 2 + 2x + 1'
],
]:
self
.
assertRewrite
(
chain
)
'x ^ 2 + 2x + 1'
,
])
def
test_1_4
(
self
):
# (x-1)^2 -> x^2 - 2x + 1
for
chain
in
[[
'(x-1)^2'
,
'(x - 1)(x - 1)'
,
'xx + x * -1 - 1x - 1 * -1'
,
'x ^ (1 + 1) + x * -1 - 1x - 1 * -1'
,
'x ^ 2 + x * -1 - 1x - 1 * -1'
,
'x ^ 2 - x * 1 - 1x - 1 * -1'
,
'x ^ 2 - x - 1x - 1 * -1'
,
'x ^ 2 - x - x - 1 * -1'
,
'x ^ 2 + (1 + 1) * -x - 1 * -1'
,
'x ^ 2 + 2 * -x - 1 * -1'
,
'x ^ 2 - 2x - 1 * -1'
,
'x ^ 2 - 2x - -1'
,
'x ^ 2 - 2x + 1'
,
]]:
self
.
assertRewrite
(
chain
)
self
.
assertRewrite
([
'(x-1)^2'
,
'(x - 1)(x - 1)'
,
'xx + x * -1 - 1x - 1 * -1'
,
'x ^ (1 + 1) + x * -1 - 1x - 1 * -1'
,
'x ^ 2 + x * -1 - 1x - 1 * -1'
,
'x ^ 2 - x * 1 - 1x - 1 * -1'
,
'x ^ 2 - x - 1x - 1 * -1'
,
'x ^ 2 - x - x - 1 * -1'
,
'x ^ 2 + (1 + 1) * -x - 1 * -1'
,
'x ^ 2 + 2 * -x - 1 * -1'
,
'x ^ 2 - 2x - 1 * -1'
,
'x ^ 2 - 2x - -1'
,
'x ^ 2 - 2x + 1'
,
])
def
test_1_4_1
(
self
):
self
.
assertRewrite
([
'x * -1 + 1x'
,
...
...
@@ -132,8 +127,7 @@ class TestLeidenOefenopgave(TestCase):
'(-20 + 20)x + 16x ^ 2 - 25'
,
'0x + 16x ^ 2 - 25'
,
'0 + 16x ^ 2 - 25'
,
'-25 + 16x ^ 2'
])
# FIXME: '16x ^ 2 - 25'])
'16x ^ 2 - 25'
])
def
test_2
(
self
):
pass
...
...
tests/test_leiden_oefenopgave_v12.py
View file @
6556ca49
from
tests.rulestestcase
import
RulesTestCase
as
TestCase
,
rewrite
from
tests.rulestestcase
import
RulesTestCase
as
TestCase
class
TestLeidenOefenopgaveV12
(
TestCase
):
def
test_1_a
(
self
):
self
.
assertRewrite
([
'-5(x2 - 3x + 6)'
,
'-5(x ^ 2 - 3x) - 5 * 6'
,
'-5x ^ 2 - 5 * -3x - 5 * 6'
,
'-5x ^ 2 - -15x - 5 * 6'
,
'-5x ^ 2 + 15x - 5 * 6'
,
'-5x ^ 2 + 15x - 30'
])
def
test_1_d
(
self
):
self
.
assertRewrite
([
'(2x + x)x'
,
'(2 + 1)xx'
,
'3xx'
,
'3x ^ (1 + 1)'
,
'3x ^ 2'
])
def
test_1_e
(
self
):
self
.
assertRewrite
([
'-2(6x - 4) ^ 2x'
,
...
...
@@ -24,13 +39,14 @@ class TestLeidenOefenopgaveV12(TestCase):
'x(-72x ^ 2 + 96x) + x * -32'
,
'x * -72x ^ 2 + x * 96x + x * -32'
,
'-x * 72x ^ 2 + x * 96x + x * -32'
,
'
72 * -xx ^
2 + x * 96x + x * -32'
,
'-
72xx ^
2 + x * 96x + x * -32'
,
'-
72x ^ (1 + 2) + x * 96x
+ x * -32'
,
'-
72x ^ 3 + x * 96x
+ x * -32'
,
'-
72x ^ 3 + 96xx + x * -
32'
,
'
-72x ^ 3 + 96x ^ (1 + 1) + x * -
32'
,
'-72x ^ 3 +
96x ^ 2 + x * -
32'
,
'
-x ^ (1 + 2) * 7
2 + x * 96x + x * -32'
,
'-
x ^ 3 * 7
2 + x * 96x + x * -32'
,
'-
x ^ 3 * 72 + x ^ (1 + 1) * 96
+ x * -32'
,
'-
x ^ 3 * 72 + x ^ 2 * 96
+ x * -32'
,
'-
x ^ 3 * 72 + x ^ 2 * 96 - x *
32'
,
'
72 * -x ^ 3 + x ^ 2 * 96 - x *
32'
,
'-72x ^ 3 +
x ^ 2 * 96 - x *
32'
,
'-72x ^ 3 + 96x ^ 2 - x * 32'
,
'-72x ^ 3 + 96x ^ 2 + 32 * -x'
,
'-72x ^ 3 + 96x ^ 2 - 32x'
])
'-72x ^ 3 + 96x ^ 2 - 32x'
,
])
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