| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2013 the V8 project authors. All rights reserved. |
| 2 # Redistribution and use in source and binary forms, with or without |
| 3 # modification, are permitted provided that the following conditions are |
| 4 # met: |
| 5 # |
| 6 # * Redistributions of source code must retain the above copyright |
| 7 # notice, this list of conditions and the following disclaimer. |
| 8 # * Redistributions in binary form must reproduce the above |
| 9 # copyright notice, this list of conditions and the following |
| 10 # disclaimer in the documentation and/or other materials provided |
| 11 # with the distribution. |
| 12 # * Neither the name of Google Inc. nor the names of its |
| 13 # contributors may be used to endorse or promote products derived |
| 14 # from this software without specific prior written permission. |
| 15 # |
| 16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 from dfa import Dfa |
| 29 |
| 30 class CodeGenerator: |
| 31 |
| 32 @staticmethod |
| 33 def key_to_code(key): |
| 34 code = 'if (' |
| 35 first = True |
| 36 for (kind, r) in key.range_iter(): |
| 37 if kind == 'CLASS': # FIXME: add class checks |
| 38 continue |
| 39 assert kind == 'LATIN_1' |
| 40 if not first: |
| 41 code += ' || ' |
| 42 if r[0] == r[1]: |
| 43 code += 'yych == %s' % r[0] |
| 44 elif r[0] == 0: |
| 45 code += 'yych <= %s' % r[1] |
| 46 elif r[1] == 255: # FIXME: this should depend on the char type maybe?? |
| 47 code += 'yych >= %s' % r[0] |
| 48 else: |
| 49 code += '(yych >= %s && yych <= %s)' % (r[0], r[1]) |
| 50 first = False |
| 51 code += ')' |
| 52 return code |
| 53 |
| 54 @staticmethod |
| 55 def dfa_state_to_code(state, start_node_number): |
| 56 # FIXME: add different check types (if, switch, lookup table) |
| 57 # FIXME: add action + break / continue |
| 58 # FIXME: add default action |
| 59 code = ''' |
| 60 code_%s: |
| 61 fprintf(stderr, "state %s\\n");''' % (state.node_number(), |
| 62 state.node_number()) |
| 63 |
| 64 action = state.action() |
| 65 if action: |
| 66 if action[1] == 'terminate': |
| 67 code += 'return 0;' |
| 68 return code |
| 69 elif action[1] == 'terminate_illegal': |
| 70 code += 'return 1;' |
| 71 return code |
| 72 |
| 73 code += ''' |
| 74 yych = *(++cursor_); |
| 75 fprintf(stderr, "char at hand is %c (%d)\\n", yych, yych);\n''' |
| 76 |
| 77 for key, s in state.transitions().items(): |
| 78 code += CodeGenerator.key_to_code(key) |
| 79 code += ''' { |
| 80 goto code_%s; |
| 81 } |
| 82 ''' % s.node_number() |
| 83 |
| 84 if action: |
| 85 code += '%s\nyych = *(--cursor_);\ngoto code_%s;\n' % (state.action()[1], |
| 86 start_node_number) |
| 87 return code |
| 88 |
| 89 @staticmethod |
| 90 def dfa_to_code(dfa): |
| 91 code = ''' |
| 92 YYCTYPE yych = *cursor_; |
| 93 goto code_%s; |
| 94 ''' % (dfa.start_state().node_number()) |
| 95 for n in dfa.all_states_iter(): |
| 96 code += CodeGenerator.dfa_state_to_code(n, |
| 97 dfa.start_state().node_number()) |
| 98 return code |
| OLD | NEW |