Merge pull request #1 from catseye/support-python-3
Support Python 3
Chris Pressey authored 2 years ago
GitHub committed 2 years ago
0 | 4938a2222c2250779560047a61cfa7ad11dc03e8 rel_1_1_2011_0510 | |
1 | 3bd7e4328239a60da67ae0382394c8c7da33a582 rel_1_1_2012_0623 | |
2 | 9d2a2d1cf4b1ab59adf6a511c609e20c8d0a6ff0 rel_1_1_2014_0819 |
0 | Eightebed is distributed under the following, BSD-compatible licenses. | |
0 | BSD 3-Clause License | |
1 | 1 | |
2 | All documentation is covered by this license, modelled after the | |
3 | "Report on the Programming Language Haskell 98" license: | |
4 | ||
5 | ----------------------------------------------------------------------------- | |
6 | ||
7 | Copyright (c)2010-2012 Chris Pressey, Cat's Eye Technologies. | |
8 | All rights reserved. | |
9 | ||
10 | The authors intend this Report to belong to the entire Eightebed | |
11 | community, and so we grant permission to copy and distribute it for | |
12 | any purpose, provided that it is reproduced in its entirety, | |
13 | including this Notice. Modified versions of this Report may also be | |
14 | copied and distributed for any purpose, provided that the modified | |
15 | version is clearly presented as such, and that it does not claim to | |
16 | be a definition of the Eightebed Programming Language. | |
17 | ||
18 | ----------------------------------------------------------------------------- | |
19 | ||
20 | All source code for the reference implementation, except the `rooibos` | |
21 | module, is covered by this license: | |
22 | ||
23 | ----------------------------------------------------------------------------- | |
24 | ||
25 | Copyright (c)2010-2012 Chris Pressey, Cat's Eye Technologies. | |
2 | Copyright (c) 2010-2021, Chris Pressey, Cat's Eye Technologies. | |
26 | 3 | All rights reserved. |
27 | 4 | |
28 | 5 | Redistribution and use in source and binary forms, with or without |
29 | modification, are permitted provided that the following conditions | |
30 | are met: | |
6 | modification, are permitted provided that the following conditions are met: | |
31 | 7 | |
32 | 1. Redistributions of source code must retain the above copyright | |
33 | notices, this list of conditions and the following disclaimer. | |
34 | 2. Redistributions in binary form must reproduce the above copyright | |
35 | notices, this list of conditions, and the following disclaimer in | |
36 | the documentation and/or other materials provided with the | |
37 | distribution. | |
38 | 3. Neither the names of the copyright holders nor the names of their | |
39 | contributors may be used to endorse or promote products derived | |
40 | from this software without specific prior written permission. | |
8 | * Redistributions of source code must retain the above copyright notice, this | |
9 | list of conditions and the following disclaimer. | |
41 | 10 | |
42 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
43 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
44 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
45 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
46 | COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
47 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
48 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
49 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
50 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
51 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
52 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
53 | POSSIBILITY OF SUCH DAMAGE. | |
11 | * Redistributions in binary form must reproduce the above copyright notice, | |
12 | this list of conditions and the following disclaimer in the documentation | |
13 | and/or other materials provided with the distribution. | |
54 | 14 | |
55 | ----------------------------------------------------------------------------- | |
15 | * Neither the name of the copyright holder nor the names of its | |
16 | contributors may be used to endorse or promote products derived from | |
17 | this software without specific prior written permission. | |
56 | 18 | |
57 | The `rooibos` module is a work by Chris Pressey, placed into the public | |
58 | domain. | |
19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
20 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
22 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | |
23 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
25 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | |
26 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
27 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
28 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
172 | 172 | ------------------------ |
173 | 173 | |
174 | 174 | Cat's Eye Technologies provides a cockamamie reference implementation of |
175 | Eightebed called `8ebed2c.py`. Written in Python 2.6, it compiles | |
175 | Eightebed called `8ebed2c.py`. Written in Python 2.7 or 3.6, it compiles | |
176 | 176 | Eightebed code to C, and for convenience will optionally compile that C |
177 | 177 | with the C compiler of your choice and run the resulting executable. |
178 | 178 |
5 | 5 | (in.8ebed|@testprog) (out.c|-) |
6 | 6 | |
7 | 7 | 8ebed2c.py: A compiler (to C) for the Eightebed programming language. |
8 | Language version 1.1. Implementation version 2011.0510. | |
8 | Language version 1.1. Implementation version 2021.0621. | |
9 | 9 | |
10 | 10 | The @testprog syntax can be used to acquire input from the |
11 | 11 | specified attribute of the Tests class of the tests module. |
87 | 87 | infilename = args[0] |
88 | 88 | outfilename = args[1] |
89 | 89 | except IndexError: |
90 | print "Usage:", __doc__, "\n" | |
91 | print "Run with the -h option to see a list of all options." | |
90 | print("Usage: {}\n".format(__doc__)) | |
91 | print("Run with the -h option to see a list of all options.") | |
92 | 92 | sys.exit(1) |
93 | 93 | parse_and_gen(options, infilename, outfilename, tests=tests.Tests) |
94 | 94 | if options.compile: |
11 | 11 | """ |
12 | 12 | >>> d = Context({ 'a': 2, 'b': 3 }) |
13 | 13 | >>> e = Context({ 'c': 4 }, parent=d) |
14 | >>> print e.lookup('c') | |
14 | >>> e.lookup('c') | |
15 | 15 | 4 |
16 | >>> print e.lookup('b') | |
16 | >>> e.lookup('b') | |
17 | 17 | 3 |
18 | >>> print e.lookup('e', None) | |
19 | None | |
20 | >>> print e.lookup('e') | |
18 | >>> e.lookup('e', None) is None | |
19 | True | |
20 | >>> e.lookup('e') | |
21 | 21 | Traceback (most recent call last): |
22 | 22 | ... |
23 | 23 | KeyError: 'e' |
24 | 24 | >>> d.declare('d', 7) |
25 | >>> print e.lookup('d') | |
25 | >>> e.lookup('d') | |
26 | 26 | 7 |
27 | 27 | >>> d.declare('b', 4) |
28 | 28 | Traceback (most recent call last): |
33 | 33 | ... |
34 | 34 | KeyError: 'b already declared' |
35 | 35 | >>> e.empty() |
36 | >>> print e.lookup('c', None) | |
37 | None | |
38 | >>> print d.lookup('a', None) | |
39 | None | |
36 | >>> e.lookup('c', None) is None | |
37 | True | |
38 | >>> d.lookup('a', None) is None | |
39 | True | |
40 | 40 | |
41 | 41 | """ |
42 | 42 |
55 | 55 | output = Popen([options.compiler, filename], stdout=PIPE).communicate()[0] |
56 | 56 | if options.verbose: |
57 | 57 | sys.stdout.write(output) |
58 | if output != '': | |
58 | if output not in ('', b''): | |
59 | 59 | raise RuntimeError("Compilation failed!") |
60 | 60 | if options.run: |
61 | 61 | logger.info("Running...") |
62 | 62 | output = Popen([a_out], stdout=PIPE).communicate()[0] |
63 | try: | |
64 | # Python 2 | |
65 | output = unicode(output).encode('ascii') | |
66 | except NameError: | |
67 | # Python 3 | |
68 | output = output.decode('ascii') | |
63 | 69 | if options.clean: |
64 | 70 | os.remove(filename) |
65 | 71 | os.remove(a_out) |
84 | 90 | |
85 | 91 | def cmdline(options): |
86 | 92 | cmd = "" |
87 | print "Eightebed interactive! Type 'quit' to quit." | |
93 | print("Eightebed interactive! Type 'quit' to quit.") | |
88 | 94 | options.run = True |
89 | 95 | options.clean = True |
90 | 96 | while True: |
96 | 102 | ast = parse_and_check(cmd, options=options) |
97 | 103 | result = load_and_go(ast, options=options) |
98 | 104 | sys.stdout.write(result) |
99 | except Exception, e: | |
100 | print "Exception!", repr(e) | |
105 | except Exception as e: | |
106 | print("Exception!", repr(e)) |
51 | 51 | def peek(self): |
52 | 52 | if not self.buffer: |
53 | 53 | try: |
54 | self.buffer.append(self.generator.next()) | |
54 | self.buffer.append(next(self.generator)) | |
55 | 55 | except StopIteration: |
56 | 56 | return None |
57 | 57 | return self.buffer[0] |
58 | 58 | |
59 | 59 | def advance(self): |
60 | 60 | if not self.buffer: |
61 | self.buffer.extend(self.generator.next()) | |
61 | self.buffer.extend(next(self.generator)) | |
62 | 62 | self.buffer.pop() |
63 | 63 | |
64 | 64 | |
464 | 464 | |
465 | 465 | def __getitem__(self, key): |
466 | 466 | if self.trace: |
467 | print "Reading production ", key | |
467 | print("Reading production ", key) | |
468 | 468 | if key in self.productions: |
469 | 469 | return self.productions[key] |
470 | 470 | elif self.parent: |
3 | 3 | """ |
4 | 4 | Test suite for (Python implementations of) the Eightebed programming language. |
5 | 5 | """ |
6 | ||
7 | ||
8 | def expect_type_error(fun): | |
9 | from .parser import TypeError | |
10 | try: | |
11 | fun() | |
12 | except TypeError as e: | |
13 | print('Traceback (most recent call last):') | |
14 | print('...') | |
15 | print('TypeError: {}'.format(e)) | |
6 | 16 | |
7 | 17 | |
8 | 18 | class Tests(object): |
26 | 36 | ... |
27 | 37 | KeyError: 'jim already declared' |
28 | 38 | |
29 | >>> parse_and_check(Tests.ptr_to_ptr) | |
39 | >>> expect_type_error(lambda: parse_and_check(Tests.ptr_to_ptr)) | |
30 | 40 | Traceback (most recent call last): |
31 | 41 | ... |
32 | 42 | TypeError: Pointer type must point to named type |
33 | 43 | |
34 | >>> parse_and_check(Tests.ptr_to_int) | |
44 | >>> expect_type_error(lambda: parse_and_check(Tests.ptr_to_int)) | |
35 | 45 | Traceback (most recent call last): |
36 | 46 | ... |
37 | 47 | TypeError: Pointer type must point to named type |
38 | 48 | |
39 | >>> parse_and_check(Tests.struct_within_struct) | |
49 | >>> expect_type_error(lambda: parse_and_check(Tests.struct_within_struct)) | |
40 | 50 | Traceback (most recent call last): |
41 | 51 | ... |
42 | 52 | TypeError: Structs may not contain other structs |
43 | 53 | |
44 | >>> parse_and_check(Tests.named_int) | |
54 | >>> expect_type_error(lambda: parse_and_check(Tests.named_int)) | |
45 | 55 | Traceback (most recent call last): |
46 | 56 | ... |
47 | 57 | TypeError: Only structs may be named |
48 | 58 | |
49 | >>> parse_and_check(Tests.dereference_outside_conditional) | |
59 | >>> expect_type_error(lambda: parse_and_check(Tests.dereference_outside_conditional)) | |
50 | 60 | Traceback (most recent call last): |
51 | 61 | ... |
52 | 62 | TypeError: Attempt to dereference jim in non-safe context |
53 | 63 | |
54 | >>> parse_and_check(Tests.dereference_outside_safe_area) | |
64 | >>> expect_type_error(lambda: parse_and_check(Tests.dereference_outside_safe_area)) | |
55 | 65 | Traceback (most recent call last): |
56 | 66 | ... |
57 | 67 | TypeError: Attempt to dereference jim in non-safe context |
60 | 70 | >>> p is None |
61 | 71 | False |
62 | 72 | |
63 | >>> parse_and_check(Tests.dereference_after_free) | |
73 | >>> expect_type_error(lambda: parse_and_check(Tests.dereference_after_free)) | |
64 | 74 | Traceback (most recent call last): |
65 | 75 | ... |
66 | 76 | TypeError: Attempt to dereference jim in non-safe context |