Skip to content

Commit

Permalink
Merge pull request #1 from catseye/support-python-3
Browse files Browse the repository at this point in the history
Support Python 3
  • Loading branch information
cpressey authored Jun 21, 2021
2 parents 1611bd7 + 3ce5e62 commit 96a2c9f
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 92 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__
*.pyc
a.out
*.c
4 changes: 0 additions & 4 deletions .hgignore

This file was deleted.

3 changes: 0 additions & 3 deletions .hgtags

This file was deleted.

80 changes: 25 additions & 55 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,59 +1,29 @@
Eightebed is distributed under the following, BSD-compatible licenses.
BSD 3-Clause License

All documentation is covered by this license, modelled after the
"Report on the Programming Language Haskell 98" license:

-----------------------------------------------------------------------------

Copyright (c)2010-2012 Chris Pressey, Cat's Eye Technologies.
All rights reserved.

The authors intend this Report to belong to the entire Eightebed
community, and so we grant permission to copy and distribute it for
any purpose, provided that it is reproduced in its entirety,
including this Notice. Modified versions of this Report may also be
copied and distributed for any purpose, provided that the modified
version is clearly presented as such, and that it does not claim to
be a definition of the Eightebed Programming Language.

-----------------------------------------------------------------------------

All source code for the reference implementation, except the `rooibos`
module, is covered by this license:

-----------------------------------------------------------------------------

Copyright (c)2010-2012 Chris Pressey, Cat's Eye Technologies.
Copyright (c) 2010-2021, Chris Pressey, Cat's Eye Technologies.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notices, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notices, this list of conditions, and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. Neither the names of the copyright holders nor the names of their
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

-----------------------------------------------------------------------------

The `rooibos` module is a work by Chris Pressey, placed into the public
domain.
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Reference Implementation
------------------------

Cat's Eye Technologies provides a cockamamie reference implementation of
Eightebed called `8ebed2c.py`. Written in Python 2.6, it compiles
Eightebed called `8ebed2c.py`. Written in Python 2.7 or 3.6, it compiles
Eightebed code to C, and for convenience will optionally compile that C
with the C compiler of your choice and run the resulting executable.

Expand Down
6 changes: 3 additions & 3 deletions src/8ebed2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
(in.8ebed|@testprog) (out.c|-)
8ebed2c.py: A compiler (to C) for the Eightebed programming language.
Language version 1.1. Implementation version 2011.0510.
Language version 1.1. Implementation version 2021.0621.
The @testprog syntax can be used to acquire input from the
specified attribute of the Tests class of the tests module.
Expand Down Expand Up @@ -88,8 +88,8 @@ def main(argv):
infilename = args[0]
outfilename = args[1]
except IndexError:
print "Usage:", __doc__, "\n"
print "Run with the -h option to see a list of all options."
print("Usage: {}\n".format(__doc__))
print("Run with the -h option to see a list of all options.")
sys.exit(1)
parse_and_gen(options, infilename, outfilename, tests=tests.Tests)
if options.compile:
Expand Down
20 changes: 10 additions & 10 deletions src/eightebed/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ class Context(dict):
"""
>>> d = Context({ 'a': 2, 'b': 3 })
>>> e = Context({ 'c': 4 }, parent=d)
>>> print e.lookup('c')
>>> e.lookup('c')
4
>>> print e.lookup('b')
>>> e.lookup('b')
3
>>> print e.lookup('e', None)
None
>>> print e.lookup('e')
>>> e.lookup('e', None) is None
True
>>> e.lookup('e')
Traceback (most recent call last):
...
KeyError: 'e'
>>> d.declare('d', 7)
>>> print e.lookup('d')
>>> e.lookup('d')
7
>>> d.declare('b', 4)
Traceback (most recent call last):
Expand All @@ -34,10 +34,10 @@ class Context(dict):
...
KeyError: 'b already declared'
>>> e.empty()
>>> print e.lookup('c', None)
None
>>> print d.lookup('a', None)
None
>>> e.lookup('c', None) is None
True
>>> d.lookup('a', None) is None
True
"""

Expand Down
14 changes: 10 additions & 4 deletions src/eightebed/drivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,17 @@ def compile_and_run(filename, options):
output = Popen([options.compiler, filename], stdout=PIPE).communicate()[0]
if options.verbose:
sys.stdout.write(output)
if output != '':
if output not in ('', b''):
raise RuntimeError("Compilation failed!")
if options.run:
logger.info("Running...")
output = Popen([a_out], stdout=PIPE).communicate()[0]
try:
# Python 2
output = unicode(output).encode('ascii')
except NameError:
# Python 3
output = output.decode('ascii')
if options.clean:
os.remove(filename)
os.remove(a_out)
Expand All @@ -85,7 +91,7 @@ class LoadAndGoOptions(object):

def cmdline(options):
cmd = ""
print "Eightebed interactive! Type 'quit' to quit."
print("Eightebed interactive! Type 'quit' to quit.")
options.run = True
options.clean = True
while True:
Expand All @@ -97,5 +103,5 @@ def cmdline(options):
ast = parse_and_check(cmd, options=options)
result = load_and_go(ast, options=options)
sys.stdout.write(result)
except Exception, e:
print "Exception!", repr(e)
except Exception as e:
print("Exception!", repr(e))
6 changes: 3 additions & 3 deletions src/eightebed/rooibos.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ def g():
def peek(self):
if not self.buffer:
try:
self.buffer.append(self.generator.next())
self.buffer.append(next(self.generator))
except StopIteration:
return None
return self.buffer[0]

def advance(self):
if not self.buffer:
self.buffer.extend(self.generator.next())
self.buffer.extend(next(self.generator))
self.buffer.pop()


Expand Down Expand Up @@ -465,7 +465,7 @@ def __init__(self, parent=None):

def __getitem__(self, key):
if self.trace:
print "Reading production ", key
print("Reading production ", key)
if key in self.productions:
return self.productions[key]
elif self.parent:
Expand Down
24 changes: 17 additions & 7 deletions src/eightebed/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
"""


def expect_type_error(fun):
from .parser import TypeError
try:
fun()
except TypeError as e:
print('Traceback (most recent call last):')
print('...')
print('TypeError: {}'.format(e))


class Tests(object):
"""Class containing test cases for Eightebed.
Expand All @@ -27,32 +37,32 @@ class Tests(object):
...
KeyError: 'jim already declared'
>>> parse_and_check(Tests.ptr_to_ptr)
>>> expect_type_error(lambda: parse_and_check(Tests.ptr_to_ptr))
Traceback (most recent call last):
...
TypeError: Pointer type must point to named type
>>> parse_and_check(Tests.ptr_to_int)
>>> expect_type_error(lambda: parse_and_check(Tests.ptr_to_int))
Traceback (most recent call last):
...
TypeError: Pointer type must point to named type
>>> parse_and_check(Tests.struct_within_struct)
>>> expect_type_error(lambda: parse_and_check(Tests.struct_within_struct))
Traceback (most recent call last):
...
TypeError: Structs may not contain other structs
>>> parse_and_check(Tests.named_int)
>>> expect_type_error(lambda: parse_and_check(Tests.named_int))
Traceback (most recent call last):
...
TypeError: Only structs may be named
>>> parse_and_check(Tests.dereference_outside_conditional)
>>> expect_type_error(lambda: parse_and_check(Tests.dereference_outside_conditional))
Traceback (most recent call last):
...
TypeError: Attempt to dereference jim in non-safe context
>>> parse_and_check(Tests.dereference_outside_safe_area)
>>> expect_type_error(lambda: parse_and_check(Tests.dereference_outside_safe_area))
Traceback (most recent call last):
...
TypeError: Attempt to dereference jim in non-safe context
Expand All @@ -61,7 +71,7 @@ class Tests(object):
>>> p is None
False
>>> parse_and_check(Tests.dereference_after_free)
>>> expect_type_error(lambda: parse_and_check(Tests.dereference_after_free))
Traceback (most recent call last):
...
TypeError: Attempt to dereference jim in non-safe context
Expand Down
5 changes: 3 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/sh
#!/bin/sh -x

src/8ebed2c.py -t -v
python2 src/8ebed2c.py -t || exit 1
python3 src/8ebed2c.py -t || exit 1

0 comments on commit 96a2c9f

Please sign in to comment.