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
ad9cff6e
Commit
ad9cff6e
authored
Dec 12, 2011
by
Sander Mathijs van Veen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Applied pep8 and pyflakes recommendations.
parent
9bb7dede
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
29 additions
and
21 deletions
+29
-21
src/calc.py
src/calc.py
+10
-4
src/logger.py
src/logger.py
+4
-4
src/parser.py
src/parser.py
+11
-12
src/suggestions.py
src/suggestions.py
+3
-0
tests/test_b1_ch8.py
tests/test_b1_ch8.py
+1
-1
No files found.
src/calc.py
View file @
ad9cff6e
...
@@ -16,6 +16,7 @@ sys.path.insert(1, EXTERNAL_MODS)
...
@@ -16,6 +16,7 @@ sys.path.insert(1, EXTERNAL_MODS)
from
pybison
import
BisonParser
from
pybison
import
BisonParser
class
Parser
(
BisonParser
):
class
Parser
(
BisonParser
):
"""
"""
Implements the calculator parser. Grammar rules are defined in the method
Implements the calculator parser. Grammar rules are defined in the method
...
@@ -197,9 +198,13 @@ class Parser(BisonParser):
...
@@ -197,9 +198,13 @@ class Parser(BisonParser):
#define YYSTYPE void *
#define YYSTYPE void *
#include "tokens.h"
#include "tokens.h"
extern void *py_parser;
extern void *py_parser;
extern void (*py_input)(PyObject *parser, char *buf, int *result, int max_size);
extern void (*py_input)(PyObject *parser, char *buf, int *result,
#define returntoken(tok) yylval = PyString_FromString(strdup(yytext)); return (tok);
int max_size);
#define YY_INPUT(buf,result,max_size) { (*py_input)(py_parser, buf, &result, max_size); }
#define returntoken(tok) \
yylval = PyString_FromString(strdup(yytext)); return (tok);
#define YY_INPUT(buf,result,max_size) { \
(*py_input)(py_parser, buf, &result, max_size); \
}
%}
%}
%%
%%
...
@@ -217,7 +222,8 @@ class Parser(BisonParser):
...
@@ -217,7 +222,8 @@ class Parser(BisonParser):
[ \t\v\f] {}
[ \t\v\f] {}
[\n] {yylineno++; returntoken(NEWLINE); }
[\n] {yylineno++; returntoken(NEWLINE); }
. { printf("unknown char %c ignored, yytext=0x%lx\n", yytext[0], yytext); /* ignore bad chars */}
. { printf("unknown char %c ignored, yytext=0x%lx\n",
yytext[0], yytext); /* ignore bad chars */}
%%
%%
...
...
src/logger.py
View file @
ad9cff6e
...
@@ -3,16 +3,16 @@ import logging.config
...
@@ -3,16 +3,16 @@ import logging.config
import
sys
import
sys
try
:
try
:
import
config
import
config
as
cfg
except
ImportError
:
except
ImportError
:
c
onfi
g
=
object
()
c
f
g
=
object
()
import
default_config
as
default
import
default_config
as
default
try
:
try
:
logging
.
basicConfig
(
level
=
logging
.
DEBUG
,
logging
.
basicConfig
(
level
=
logging
.
DEBUG
,
format
=
getattr
(
c
onfi
g
,
'LOG_FORMAT'
,
default
.
LOG_FORMAT
),
format
=
getattr
(
c
f
g
,
'LOG_FORMAT'
,
default
.
LOG_FORMAT
),
filename
=
getattr
(
c
onfi
g
,
'LOG_FILE'
,
default
.
LOG_FILE
),
filename
=
getattr
(
c
f
g
,
'LOG_FILE'
,
default
.
LOG_FILE
),
filemode
=
'a'
)
filemode
=
'a'
)
except
IOError
as
e
:
# pragma: no cover
except
IOError
as
e
:
# pragma: no cover
print
>>
sys
.
stderr
,
'warning: IOError raised: "%s"'
%
str
(
e
)
print
>>
sys
.
stderr
,
'warning: IOError raised: "%s"'
%
str
(
e
)
...
...
src/parser.py
View file @
ad9cff6e
...
@@ -19,7 +19,7 @@ sys.path.insert(1, EXTERNAL_MODS)
...
@@ -19,7 +19,7 @@ sys.path.insert(1, EXTERNAL_MODS)
from
pybison
import
BisonParser
,
BisonSyntaxError
from
pybison
import
BisonParser
,
BisonSyntaxError
from
graph_drawing.graph
import
generate_graph
from
graph_drawing.graph
import
generate_graph
from
node
import
TYPE_OPERATOR
,
OP_ADD
,
OP_MUL
,
OP_SUB
,
OP_NEG
from
node
import
TYPE_OPERATOR
,
OP_ADD
,
OP_MUL
,
OP_SUB
# Check for n-ary operator in child nodes
# Check for n-ary operator in child nodes
...
@@ -28,13 +28,6 @@ def combine(op, op_type, *nodes):
...
@@ -28,13 +28,6 @@ def combine(op, op_type, *nodes):
res
=
[
op
]
res
=
[
op
]
for
n
in
nodes
:
for
n
in
nodes
:
try
:
assert
n
.
type
!=
TYPE_OPERATOR
or
n
.
op
!=
OP_NEG
or
len
(
n
.
nodes
)
==
1
assert
n
.
type
!=
TYPE_OPERATOR
or
n
.
op
!=
OP_SUB
or
len
(
n
.
nodes
)
>
1
except
AssertionError
:
print
n
,
type
(
n
),
n
.
type
,
OP_NEG
,
OP_SUB
,
n
.
nodes
,
len
(
n
.
nodes
)
raise
# Merge the children for all nodes which have the same operator.
# Merge the children for all nodes which have the same operator.
if
n
.
type
==
TYPE_OPERATOR
and
n
.
op
==
op_type
:
if
n
.
type
==
TYPE_OPERATOR
and
n
.
op
==
op_type
:
res
+=
n
.
nodes
res
+=
n
.
nodes
...
@@ -367,14 +360,20 @@ class Parser(BisonParser):
...
@@ -367,14 +360,20 @@ class Parser(BisonParser):
def
get_args
():
def
get_args
():
parser
=
argparse
.
ArgumentParser
(
prog
=
'parser'
,
description
=
__doc__
)
parser
=
argparse
.
ArgumentParser
(
prog
=
'parser'
,
description
=
__doc__
)
parser
.
add_argument
(
'--debug'
,
'-d'
,
action
=
'store_true'
,
default
=
False
,
parser
.
add_argument
(
'--debug'
,
'-d'
,
action
=
'store_true'
,
default
=
False
,
help
=
'Enable debug mode in bison and flex.'
)
help
=
'Enable debug mode in bison and flex.'
)
parser
.
add_argument
(
'--verbose'
,
'-v'
,
action
=
'store_true'
,
default
=
False
,
parser
.
add_argument
(
'--verbose'
,
'-v'
,
action
=
'store_true'
,
default
=
False
,
help
=
'Enable verbose output messages (printed to stdout).'
)
help
=
'Enable verbose output messages (printed to stdout).'
)
parser
.
add_argument
(
'--keepfiles'
,
'-k'
,
action
=
'store_true'
,
default
=
False
,
parser
.
add_argument
(
'--keepfiles'
,
'-k'
,
action
=
'store_true'
,
default
=
False
,
help
=
'Keep temporary generated bison and lex files.'
)
help
=
'Keep temporary generated bison and lex files.'
)
parser
.
add_argument
(
'--batch'
,
'-b'
,
action
=
'store_true'
,
default
=
False
,
parser
.
add_argument
(
'--batch'
,
'-b'
,
action
=
'store_true'
,
default
=
False
,
help
=
'Disable interactive mode and execute expressions in batch mode.'
)
help
=
'Disable interactive mode and execute expressions in batch'
\
' mode.'
)
return
parser
.
parse_args
()
return
parser
.
parse_args
()
...
...
src/suggestions.py
View file @
ad9cff6e
...
@@ -5,6 +5,7 @@ from rules import rules
...
@@ -5,6 +5,7 @@ from rules import rules
# (node, funcptr, (args...))
# (node, funcptr, (args...))
def
get_node_possibilities
(
node
):
def
get_node_possibilities
(
node
):
"""
"""
Get all possible rewrite steps for this node.
Get all possible rewrite steps for this node.
...
@@ -18,6 +19,7 @@ def get_node_possibilities(node):
...
@@ -18,6 +19,7 @@ def get_node_possibilities(node):
return
possibilities
return
possibilities
def
get_possibilities
(
node
):
def
get_possibilities
(
node
):
"""
"""
Get all possible rewrite steps for this node and its children.
Get all possible rewrite steps for this node and its children.
...
@@ -29,6 +31,7 @@ def get_possibilities(node):
...
@@ -29,6 +31,7 @@ def get_possibilities(node):
return
possibilities
return
possibilities
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
node
=
main
()
node
=
main
()
print
'node:'
,
node
print
'node:'
,
node
...
...
tests/test_b1_ch8.py
View file @
ad9cff6e
...
@@ -2,7 +2,7 @@ import unittest
...
@@ -2,7 +2,7 @@ import unittest
from
src.parser
import
Parser
from
src.parser
import
Parser
from
src.node
import
ExpressionNode
as
N
,
ExpressionLeaf
as
L
from
src.node
import
ExpressionNode
as
N
,
ExpressionLeaf
as
L
from
tests.parser
import
ParserWrapper
,
run_expressions
from
tests.parser
import
run_expressions
class
TestB1Ch8
(
unittest
.
TestCase
):
class
TestB1Ch8
(
unittest
.
TestCase
):
...
...
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