Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
trs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
trs
Commits
17c6fae8
Commit
17c6fae8
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added logical operators.
parent
685f3ea3
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/node.py
+28
-16
28 additions, 16 deletions
src/node.py
src/parser.py
+14
-6
14 additions, 6 deletions
src/parser.py
tests/test_parser.py
+8
-0
8 additions, 0 deletions
tests/test_parser.py
with
50 additions
and
22 deletions
src/node.py
+
28
−
16
View file @
17c6fae8
...
...
@@ -28,34 +28,36 @@ OP_MUL = 5
OP_DIV
=
6
OP_POW
=
7
OP_SUBSCRIPT
=
8
OP_AND
=
9
OP_OR
=
10
# N-ary (functions)
OP_INT
=
9
OP_INT_INDEF
=
1
0
OP_COMMA
=
1
1
OP_SQRT
=
1
2
OP_DER
=
1
3
OP_LOG
=
1
4
OP_INT
=
11
OP_INT_INDEF
=
1
2
OP_COMMA
=
1
3
OP_SQRT
=
1
4
OP_DER
=
1
5
OP_LOG
=
1
6
# Goniometry
OP_SIN
=
1
5
OP_COS
=
1
6
OP_TAN
=
1
7
OP_SIN
=
1
7
OP_COS
=
1
8
OP_TAN
=
1
9
OP_SOLVE
=
18
OP_EQ
=
1
9
OP_SOLVE
=
20
OP_EQ
=
2
1
OP_POSSIBILITIES
=
2
0
OP_HINT
=
2
1
OP_REWRITE_ALL
=
2
2
OP_REWRITE
=
2
3
OP_POSSIBILITIES
=
2
2
OP_HINT
=
2
3
OP_REWRITE_ALL
=
2
4
OP_REWRITE
=
2
5
# Special identifiers
PI
=
'
pi
'
E
=
'
e
'
INFINITY
=
'
oo
'
SPECIAL_TOKENS
=
[
PI
,
INFINITY
]
SPECIAL_TOKENS
=
[
PI
,
E
,
INFINITY
]
# Default base to use in parsing 'log(...)'
DEFAULT_LOGARITHM_BASE
=
10
...
...
@@ -75,6 +77,8 @@ OP_MAP = {
'
/
'
:
OP_DIV
,
'
^
'
:
OP_POW
,
'
_
'
:
OP_SUBSCRIPT
,
'
^^
'
:
OP_AND
,
'
vv
'
:
OP_OR
,
'
sin
'
:
OP_SIN
,
'
cos
'
:
OP_COS
,
'
tan
'
:
OP_TAN
,
...
...
@@ -103,6 +107,8 @@ TOKEN_MAP = {
OP_DIV
:
'
DIVIDE
'
,
OP_POW
:
'
POW
'
,
OP_SUBSCRIPT
:
'
SUB
'
,
OP_AND
:
'
AND
'
,
OP_OR
:
'
OR
'
,
OP_SQRT
:
'
FUNCTION
'
,
OP_SIN
:
'
FUNCTION
'
,
OP_COS
:
'
FUNCTION
'
,
...
...
@@ -226,6 +232,12 @@ class ExpressionBase(object):
def
__pos__
(
self
):
return
self
.
reduce_negation
()
def
__and__
(
self
,
other
):
return
ExpressionNode
(
OP_AND
,
self
,
to_expression
(
other
))
def
__or__
(
self
,
other
):
return
ExpressionNode
(
OP_OR
,
self
,
to_expression
(
other
))
def
reduce_negation
(
self
,
n
=
1
):
"""
Remove n negation flags from the node.
"""
assert
self
.
negated
...
...
This diff is collapsed.
Click to expand it.
src/parser.py
+
14
−
6
View file @
17c6fae8
...
...
@@ -87,10 +87,12 @@ class Parser(BisonParser):
precedences
=
(
(
'
left
'
,
(
'
COMMA
'
,
)),
(
'
right
'
,
(
'
INTEGRAL
'
,
'
DERIVATIVE
'
)),
(
'
left
'
,
(
'
OR
'
,
)),
(
'
left
'
,
(
'
AND
'
,
)),
(
'
left
'
,
(
'
EQ
'
,
)),
(
'
left
'
,
(
'
MINUS
'
,
'
PLUS
'
)),
(
'
left
'
,
(
'
TIMES
'
,
'
DIVIDE
'
)),
(
'
right
'
,
(
'
FUNCTION
'
,
)),
(
'
left
'
,
(
'
EQ
'
,
)),
(
'
left
'
,
(
'
NEG
'
,
)),
(
'
right
'
,
(
'
POW
'
,
)),
(
'
left
'
,
(
'
SUB
'
,
)),
...
...
@@ -225,6 +227,9 @@ class Parser(BisonParser):
for
i
,
keyword
in
enumerate
(
words
):
data
=
data
.
replace
(
chr
(
i
),
keyword
)
# Fix TIMES operator next to OR
data
=
re
.
sub
(
r
'
\*?vv\*?
'
,
'
vv
'
,
data
)
if
self
.
verbose
and
data_before
!=
data
:
# pragma: nocover
print
'
hook_read_after() modified the input data:
'
print
'
before:
'
,
repr
(
data_before
)
...
...
@@ -527,14 +532,16 @@ class Parser(BisonParser):
| exp TIMES exp
| exp DIVIDE exp
| exp EQ exp
| exp AND exp
| exp OR exp
| exp MINUS exp
| power
"""
if
0
<=
option
<
4
:
# rule: exp {PLUS,TIMES,DIVIDE,EQ} exp
if
0
<=
option
<
=
5
:
# rule: exp {PLUS,TIMES,DIVIDE,EQ
,AND,OR
} exp
return
Node
(
values
[
1
],
values
[
0
],
values
[
2
])
if
option
==
4
:
# rule: exp MINUS exp
if
option
==
6
:
# rule: exp MINUS exp
node
=
values
[
2
]
# Add negation to the left-most child
...
...
@@ -549,7 +556,7 @@ class Parser(BisonParser):
return
Node
(
OP_ADD
,
values
[
0
],
values
[
2
])
if
option
==
5
:
# rule: power
if
option
==
7
:
# rule: power
return
Node
(
OP_POW
,
*
values
[
0
])
raise
BisonSyntaxError
(
'
Unsupported option %d in target
"
%s
"
.
'
...
...
@@ -573,8 +580,9 @@ class Parser(BisonParser):
functions
=
[]
for
token
in
SPECIAL_TOKENS
:
operators
+=
'"
%s
"
%s{ returntoken(IDENTIFIER); }
\n
'
\
%
(
token
,
'
'
*
(
8
-
len
(
token
)))
if
len
(
token
)
>
1
:
operators
+=
'"
%s
"
%s{ returntoken(IDENTIFIER); }
\n
'
\
%
(
token
,
'
'
*
(
8
-
len
(
token
)))
for
op_str
,
op
in
OP_MAP
.
iteritems
():
if
TOKEN_MAP
[
op
]
==
'
FUNCTION
'
:
...
...
This diff is collapsed.
Click to expand it.
tests/test_parser.py
+
8
−
0
View file @
17c6fae8
...
...
@@ -38,6 +38,14 @@ class TestParser(unittest.TestCase):
self
.
assertNotEqual
(
possibilities1
,
possibilities2
)
def
test_binary
(
self
):
a
,
b
,
c
=
tree
(
'
a, b, c
'
)
self
.
assertEqual
(
tree
(
'
a ^^ b
'
),
a
&
b
)
self
.
assertEqual
(
tree
(
'
a vv b
'
),
a
|
b
)
self
.
assertEqual
(
tree
(
'
a vv b vv c
'
),
(
a
|
b
)
|
c
)
self
.
assertEqual
(
tree
(
'
a vv b ^^ c
'
),
a
|
(
b
&
c
))
def
test_preprocessor
(
self
):
self
.
assertEqual
(
tree
(
'
ab
'
),
tree
(
'
a * b
'
))
self
.
assertEqual
(
tree
(
'
abc
'
),
tree
(
'
a * b * c
'
))
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment