| OLD | NEW |
| 1 # Copyright 2013 the V8 project authors. All rights reserved. | 1 # Copyright 2013 the V8 project authors. All rights reserved. |
| 2 # Redistribution and use in source and binary forms, with or without | 2 # Redistribution and use in source and binary forms, with or without |
| 3 # modification, are permitted provided that the following conditions are | 3 # modification, are permitted provided that the following conditions are |
| 4 # met: | 4 # met: |
| 5 # | 5 # |
| 6 # * Redistributions of source code must retain the above copyright | 6 # * Redistributions of source code must retain the above copyright |
| 7 # notice, this list of conditions and the following disclaimer. | 7 # notice, this list of conditions and the following disclaimer. |
| 8 # * Redistributions in binary form must reproduce the above | 8 # * Redistributions in binary form must reproduce the above |
| 9 # copyright notice, this list of conditions and the following | 9 # copyright notice, this list of conditions and the following |
| 10 # disclaimer in the documentation and/or other materials provided | 10 # disclaimer in the documentation and/or other materials provided |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 def actions(self): | 46 def actions(self): |
| 47 return self.__actions | 47 return self.__actions |
| 48 | 48 |
| 49 def add_transition(self, key, state): | 49 def add_transition(self, key, state): |
| 50 assert not self.__transitions.has_key(key) | 50 assert not self.__transitions.has_key(key) |
| 51 self.__transitions[key] = state | 51 self.__transitions[key] = state |
| 52 | 52 |
| 53 def transitions(self): | 53 def transitions(self): |
| 54 return self.__transitions | 54 return self.__transitions |
| 55 | 55 |
| 56 def to_code(self): | 56 def to_code(self, start_node_number): |
| 57 # FIXME: add different check types (if, switch, lookup table) | 57 # FIXME: add different check types (if, switch, lookup table) |
| 58 # FIXME: add action + break / continue | 58 # FIXME: add action + break / continue |
| 59 # FIXME: add default action | 59 # FIXME: add default action |
| 60 code = ''' | 60 code = ''' |
| 61 code_%s: | 61 code_%s: |
| 62 fprintf(stderr, "state %s, char at hand is %%c (%%d)\\n", c, c); | 62 fprintf(stderr, "state %s, char at hand is %%c (%%d)\\n", c, c); |
| 63 ''' % (self.node_number(), self.node_number()) | 63 ''' % (self.node_number(), self.node_number()) |
| 64 | 64 |
| 65 for key, state in self.__transitions.items(): | 65 for key, state in self.__transitions.items(): |
| 66 code += key.to_code() | 66 code += key.to_code() |
| 67 code += ''' { | 67 code += ''' { |
| 68 c = *(++cursor); | 68 c = *(++cursor); |
| 69 goto code_%s; | 69 goto code_%s; |
| 70 } | 70 } |
| 71 ''' % state.node_number() | 71 ''' % state.node_number() |
| 72 | 72 |
| 73 action = self.action() | 73 action = self.action() |
| 74 if action: | 74 if action: |
| 75 if action[1] == 'terminate': | 75 if action[1] == 'terminate': |
| 76 code += 'return 0;' | 76 code += 'return 0;' |
| 77 elif action[1] == 'terminate_illegal': | 77 elif action[1] == 'terminate_illegal': |
| 78 code += 'return 1;' | 78 code += 'return 1;' |
| 79 else: | 79 else: |
| 80 code += self.action()[1]; | 80 code += self.action()[1]; |
| 81 code += 'goto code_%s;' % start_node_number |
| 81 | 82 |
| 82 return code | 83 return code |
| 83 | 84 |
| 84 class Dfa(Automaton): | 85 class Dfa(Automaton): |
| 85 | 86 |
| 86 def __init__(self, start_name, mapping): | 87 def __init__(self, start_name, mapping): |
| 87 super(Dfa, self).__init__() | 88 super(Dfa, self).__init__() |
| 88 self.__terminal_set = set() | 89 self.__terminal_set = set() |
| 89 self.__name_map = {} | 90 self.__name_map = {} |
| 90 for i, (name, node_data) in enumerate(mapping.items()): | 91 for i, (name, node_data) in enumerate(mapping.items()): |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 | 159 |
| 159 def minimize(self): | 160 def minimize(self): |
| 160 pass | 161 pass |
| 161 | 162 |
| 162 def to_code(self): | 163 def to_code(self): |
| 163 code = ''' | 164 code = ''' |
| 164 char c = *cursor; | 165 char c = *cursor; |
| 165 goto code_%s; | 166 goto code_%s; |
| 166 ''' % (self.__start.node_number()) | 167 ''' % (self.__start.node_number()) |
| 167 for n in self.__name_map.values(): | 168 for n in self.__name_map.values(): |
| 168 code += n.to_code() | 169 code += n.to_code(self.__start.node_number()) |
| 169 return code | 170 return code |
| OLD | NEW |