git @ Cat's Eye Technologies SixtyPical / d90ac92
Merge branch 'develop-0.18' into goto-at-end-of-block Chris Pressey 3 years ago
3 changed file(s) with 17 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
00 History of SixtyPical
11 =====================
2
3 0.18
4 ----
5
6 * Fixed pathological memory use in the lexical scanner - should
7 be much less inefficient now when parsing large source files.
28
39 0.17
410 ----
00 SixtyPical
11 ==========
22
3 _Version 0.17. Work-in-progress, everything is subject to change._
3 _Version 0.18. Work-in-progress, everything is subject to change._
44
55 **SixtyPical** is a low-level programming language with advanced
66 static analysis. Many of its primitive instructions resemble
1616 self.filename = filename
1717 self.token = None
1818 self.type = None
19 self.pos = 0
1920 self.line_number = 1
2021 self.scan()
2122
22 def scan_pattern(self, pattern, type, token_group=1, rest_group=2):
23 pattern = r'^(' + pattern + r')(.*?)$'
24 match = re.match(pattern, self.text, re.DOTALL)
23 def scan_pattern(self, pattern, type, token_group=1):
24 pattern = r'(' + pattern + r')'
25 regexp = re.compile(pattern, flags=re.DOTALL)
26 match = regexp.match(self.text, pos=self.pos)
2527 if not match:
2628 return False
2729 else:
2830 self.type = type
2931 self.token = match.group(token_group)
30 self.text = match.group(rest_group)
32 self.pos += len(match.group(0))
3133 self.line_number += self.token.count('\n')
3234 return True
3335
3537 self.scan_pattern(r'[ \t\n\r]*', 'whitespace')
3638 while self.scan_pattern(r'\/\/.*?[\n\r]', 'comment'):
3739 self.scan_pattern(r'[ \t\n\r]*', 'whitespace')
38 if not self.text:
40 if self.pos >= len(self.text):
3941 self.token = None
4042 self.type = 'EOF'
4143 return
4345 return
4446 if self.scan_pattern(r'\d+', 'integer literal'):
4547 return
46 if self.scan_pattern(r'\$([0-9a-fA-F]+)', 'integer literal',
47 token_group=2, rest_group=3):
48 if self.scan_pattern(r'\$([0-9a-fA-F]+)', 'integer literal', token_group=2):
4849 # ecch
4950 self.token = str(eval('0x' + self.token))
5051 return
51 if self.scan_pattern(r'\"(.*?)\"', 'string literal',
52 token_group=2, rest_group=3):
52 if self.scan_pattern(r'\"(.*?)\"', 'string literal', token_group=2):
5353 return
5454 if self.scan_pattern(r'\w+', 'identifier'):
5555 return
5656 if self.scan_pattern(r'.', 'unknown character'):
5757 return
5858 else:
59 raise AssertionError("this should never happen, self.text=({})".format(self.text))
59 raise AssertionError("this should never happen, self.text=({}), self.pos=({})".format(self.text, self.pos))
6060
6161 def expect(self, token):
6262 if self.token == token: