annotate python/ppci/c3/parser.py @ 354:5477e499b039

Added some sort of string functionality
author Windel Bouwman
date Thu, 13 Mar 2014 18:59:06 +0100
parents d1ecc493384e
children c05ab629976a
rev   line source
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
1 import logging
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
2 from .. import CompilerError
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
3 from .astnodes import Member, Literal, TypeCast, Unop, Binop
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
4 from .astnodes import Assignment, ExpressionStatement, Compound
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
5 from .astnodes import Return, While, If, Empty, For
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
6 from .astnodes import FunctionType, Function, FormalParameter
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
7 from .astnodes import StructureType, DefinedType, PointerType, ArrayType
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
8 from .astnodes import Constant, Variable
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
9 from .astnodes import StructField, Deref, Index
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
10 from .astnodes import Package
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
11 from .astnodes import Identifier
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
12 from .astnodes import FunctionCall
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
13
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
14
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15 class Parser:
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
16 """ Parses sourcecode into an abstract syntax tree (AST) """
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
17 def __init__(self, diag):
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
18 self.logger = logging.getLogger('c3')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
19 self.diag = diag
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
20
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
21 def parseSource(self, tokens):
334
6f4753202b9a Added more recipes
Windel Bouwman
parents: 315
diff changeset
22 self.logger.debug('Parsing source')
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
23 self.tokens = tokens
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
24 self.token = self.tokens.__next__()
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
25 try:
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
26 self.parsePackage()
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
27 self.mod.ok = True # Valid until proven wrong :)
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
28 return self.mod
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
29 except CompilerError as e:
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
30 self.diag.addDiag(e)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
31
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
32 def Error(self, msg):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
33 raise CompilerError(msg, self.token.loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
34
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
35 # Lexer helpers:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
36 def Consume(self, typ):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
37 if self.Peak == typ:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
38 return self.NextToken()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
39 else:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
40 self.Error('Excected: "{0}", got "{1}"'.format(typ, self.Peak))
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
41
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
42 @property
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
43 def Peak(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
44 return self.token.typ
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
45
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
46 @property
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
47 def CurLoc(self):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
48 return self.token.loc
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
49
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
50 def hasConsumed(self, typ):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
51 if self.Peak == typ:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
52 self.Consume(typ)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
53 return True
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
54 return False
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
55
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
56 def NextToken(self):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
57 t = self.token
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
58 if t.typ != 'END':
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
59 self.token = self.tokens.__next__()
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
60 return t
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
61
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
62 def addDeclaration(self, decl):
336
d1ecc493384e Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents: 334
diff changeset
63 self.currentPart.add_declaration(decl)
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
64
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
65 def parseImport(self):
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
66 self.Consume('import')
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
67 name = self.Consume('ID').val
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
68 self.mod.imports.append(name)
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
69 self.Consume(';')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
70
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
71 def parsePackage(self):
284
05184b95fa16 Moved tests to seperate folder
Windel Bouwman
parents: 280
diff changeset
72 self.Consume('module')
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
73 name = self.Consume('ID')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
74 self.Consume(';')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
75 self.mod = Package(name.val, name.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
76 self.currentPart = self.mod
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
77 while self.Peak != 'END':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
78 self.parseTopLevel()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
79 self.Consume('END')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
80
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
81 def parseTopLevel(self):
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
82 if self.Peak == 'function':
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
83 self.parseFunctionDef()
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
84 elif self.Peak == 'var':
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
85 self.parseVarDef()
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
86 # TODO handle variable initialization
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
87 elif self.Peak == 'const':
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
88 self.parseConstDef()
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
89 elif self.Peak == 'type':
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
90 self.parseTypeDef()
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
91 elif self.Peak == 'import':
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
92 self.parseImport()
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
93 else:
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 232
diff changeset
94 self.Error('Expected function, var, const or type')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
95
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
96 def parseDesignator(self):
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
97 """ A designator designates an object with a name. """
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
98 name = self.Consume('ID')
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
99 return Identifier(name.val, name.loc)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
100
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
101 def parseIdSequence(self):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
102 ids = [self.Consume('ID')]
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
103 while self.hasConsumed(','):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
104 ids.append(self.Consume('ID'))
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
105 return ids
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
106
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
107 # Type system
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
108 def PostFixId(self):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
109 pfe = self.PrimaryExpression_Id()
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
110 while self.Peak in ['.']:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
111 if self.hasConsumed('.'):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
112 field = self.Consume('ID')
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
113 pfe = Member(pfe, field.val, field.loc)
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
114 else:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
115 raise Exception()
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
116 return pfe
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
117
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
118 def PrimaryExpression_Id(self):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
119 if self.Peak == 'ID':
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
120 return self.parseDesignator()
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
121 self.Error('Expected ID, got {0}'.format(self.Peak))
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
122
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
123 def parse_type_spec(self):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
124 """ Parse type specification """
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
125 if self.Peak == 'struct':
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
126 self.Consume('struct')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
127 self.Consume('{')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
128 mems = []
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
129 while self.Peak != '}':
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
130 mem_t = self.parse_type_spec()
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
131 for i in self.parseIdSequence():
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
132 mems.append(StructField(i.val, mem_t))
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
133 self.Consume(';')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
134 self.Consume('}')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
135 theT = StructureType(mems)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
136 elif self.Peak == 'enum':
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
137 # TODO)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
138 raise NotImplementedError()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
139 else:
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
140 theT = self.PostFixId()
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
141
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
142 # Check for pointer or array suffix:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
143 while self.Peak in ['*', '[']:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
144 if self.hasConsumed('*'):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
145 theT = PointerType(theT)
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
146 elif self.hasConsumed('['):
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
147 if self.Peak == ']':
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
148 size = 0
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
149 self.Consume(']')
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
150 else:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
151 size = self.Expression()
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
152 self.Consume(']')
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
153 theT = ArrayType(theT, size)
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
154 else:
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
155 raise Exception()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
156 return theT
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
157
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
158 def parseTypeDef(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
159 self.Consume('type')
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
160 newtype = self.parse_type_spec()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
161 typename = self.Consume('ID')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
162 self.Consume(';')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
163 df = DefinedType(typename.val, newtype, typename.loc)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
164 self.addDeclaration(df)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
165
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
166 # Variable declarations:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
167 def parseVarDef(self):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
168 self.Consume('var')
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
169 t = self.parse_type_spec()
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
170 for name in self.parseIdSequence():
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
171 v = Variable(name.val, t)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
172 v.loc = name.loc
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
173 self.addDeclaration(v)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
174 self.Consume(';')
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
175 return Empty()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
176
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
177 def parseConstDef(self):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
178 self.Consume('const')
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
179 t = self.parse_type_spec()
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
180 while True:
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
181 name = self.Consume('ID')
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
182 self.Consume('=')
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
183 val = self.Expression()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
184 c = Constant(name.val, t, val)
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 311
diff changeset
185 self.addDeclaration(c)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
186 c.loc = name.loc
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
187 if not self.hasConsumed(','):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
188 break
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
189 self.Consume(';')
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 284
diff changeset
190
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
191 def parseFunctionDef(self):
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
192 loc = self.Consume('function').loc
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
193 returntype = self.parse_type_spec()
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
194 fname = self.Consume('ID').val
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
195 f = Function(fname, loc)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
196 self.addDeclaration(f)
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
197 savePart = self.currentPart
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
198 self.currentPart = f
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
199 self.Consume('(')
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
200 parameters = []
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
201 if not self.hasConsumed(')'):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
202 while True:
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
203 typ = self.parse_type_spec()
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
204 name = self.Consume('ID')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
205 param = FormalParameter(name.val, typ)
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
206 param.loc = name.loc
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
207 self.addDeclaration(param)
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
208 parameters.append(param)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
209 if not self.hasConsumed(','):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
210 break
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
211 self.Consume(')')
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
212 paramtypes = [p.typ for p in parameters]
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
213 f.typ = FunctionType(paramtypes, returntype)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
214 f.body = self.parseCompound()
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
215 self.currentPart = savePart
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
216
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
217 def parse_if(self):
272
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
218 loc = self.Consume('if').loc
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
219 self.Consume('(')
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
220 condition = self.Expression()
e64bae57cda8 refactor ir
Windel Bouwman
parents: 254
diff changeset
221 self.Consume(')')
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
222 yes = self.Statement()
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
223 no = self.Statement() if self.hasConsumed('else') else Empty()
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
224 return If(condition, yes, no, loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
225
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
226 def parseWhile(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
227 loc = self.Consume('while').loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
228 self.Consume('(')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
229 condition = self.Expression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
230 self.Consume(')')
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
231 statements = self.Statement()
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
232 return While(condition, statements, loc)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
233
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
234 def parse_for(self):
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
235 loc = self.Consume('for').loc
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
236 self.Consume('(')
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
237 init = self.Statement()
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
238 self.Consume(';')
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
239 condition = self.Expression()
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
240 self.Consume(';')
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
241 final = self.Statement()
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
242 self.Consume(')')
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
243 statements = self.Statement()
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
244 return For(init, condition, final, statements, loc)
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
245
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
246 def parseReturn(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
247 loc = self.Consume('return').loc
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
248 if self.Peak == ';':
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
249 expr = Literal(0, loc)
280
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
250 else:
02385f62f250 Rework from str interface to Instruction interface
Windel Bouwman
parents: 272
diff changeset
251 expr = self.Expression()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
252 self.Consume(';')
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
253 return Return(expr, loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
254
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
255 def parseCompound(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
256 self.Consume('{')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
257 statements = []
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
258 while not self.hasConsumed('}'):
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
259 statements.append(self.Statement())
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
260 return Compound(statements)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
261
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
262 def Statement(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
263 # Determine statement type based on the pending token:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
264 if self.Peak == 'if':
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
265 return self.parse_if()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
266 elif self.Peak == 'while':
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
267 return self.parseWhile()
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
268 elif self.Peak == 'for':
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
269 return self.parse_for()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
270 elif self.Peak == '{':
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
271 return self.parseCompound()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
272 elif self.hasConsumed(';'):
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
273 return Empty()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
274 elif self.Peak == 'var':
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
275 return self.parseVarDef()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
276 elif self.Peak == 'return':
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
277 return self.parseReturn()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
278 else:
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
279 x = self.UnaryExpression()
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
280 if self.Peak == '=':
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
281 # We enter assignment mode here.
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
282 loc = self.Consume('=').loc
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
283 rhs = self.Expression()
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
284 return Assignment(x, rhs, loc)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
285 else:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
286 return ExpressionStatement(x, x.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
287
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
288 # Expression section:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
289 # We not implement these C constructs:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
290 # a(2), f = 2
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
291 # and this:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
292 # a = 2 < x : 4 ? 1;
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
293
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
294 def Expression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
295 exp = self.LogicalAndExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
296 while self.Peak == 'or':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
297 loc = self.Consume('or').loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
298 e2 = self.LogicalAndExpression()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
299 exp = Binop(exp, 'or', e2, loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
300 return exp
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
301
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
302 def LogicalAndExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
303 o = self.EqualityExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
304 while self.Peak == 'and':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
305 loc = self.Consume('and').loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
306 o2 = self.EqualityExpression()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
307 o = Binop(o, 'and', o2, loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
308 return o
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
309
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
310 def EqualityExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
311 ee = self.SimpleExpression()
300
Windel Bouwman
parents: 296
diff changeset
312 while self.Peak in ['<', '==', '>', '>=', '<=', '!=']:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
313 op = self.Consume(self.Peak)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
314 ee2 = self.SimpleExpression()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
315 ee = Binop(ee, op.typ, ee2, op.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
316 return ee
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
317
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
318 def SimpleExpression(self):
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
319 """ Shift operations before + and - ? """
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
320 e = self.AddExpression()
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
321 while self.Peak in ['>>', '<<']:
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
322 op = self.Consume(self.Peak)
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
323 e2 = self.AddExpression()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
324 e = Binop(e, op.typ, e2, op.loc)
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
325 return e
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
326
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
327 def AddExpression(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
328 e = self.Term()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
329 while self.Peak in ['+', '-']:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
330 op = self.Consume(self.Peak)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
331 e2 = self.Term()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
332 e = Binop(e, op.typ, e2, op.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
333 return e
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
334
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
335 def Term(self):
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
336 t = self.BitwiseOr()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
337 while self.Peak in ['*', '/']:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
338 op = self.Consume(self.Peak)
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
339 t2 = self.BitwiseOr()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
340 t = Binop(t, op.typ, t2, op.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
341 return t
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
342
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
343 def BitwiseOr(self):
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
344 a = self.BitwiseAnd()
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
345 while self.Peak == '|':
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
346 op = self.Consume(self.Peak)
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
347 b = self.BitwiseAnd()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
348 a = Binop(a, op.typ, b, op.loc)
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
349 return a
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
350
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
351 def BitwiseAnd(self):
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
352 a = self.CastExpression()
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
353 while self.Peak == '&':
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
354 op = self.Consume(self.Peak)
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
355 b = self.CastExpression()
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
356 a = Binop(a, op.typ, b, op.loc)
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
357 return a
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
358
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
359 # Domain of unary expressions:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
360
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
361 def CastExpression(self):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
362 """
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
363 the C-style type cast conflicts with '(' expr ')'
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
364 so introduce extra keyword 'cast'
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
365 """
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
366 if self.Peak == 'cast':
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
367 loc = self.Consume('cast').loc
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
368 self.Consume('<')
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
369 t = self.parse_type_spec()
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
370 self.Consume('>')
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
371 self.Consume('(')
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
372 ce = self.Expression()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
373 self.Consume(')')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
374 return TypeCast(t, ce, loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
375 else:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
376 return self.UnaryExpression()
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
377
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
378 def UnaryExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
379 if self.Peak in ['&', '*']:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
380 op = self.Consume(self.Peak)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
381 ce = self.CastExpression()
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
382 if op.val == '*':
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
383 return Deref(ce, op.loc)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
384 else:
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
385 return Unop(op.typ, ce, op.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
386 else:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
387 return self.PostFixExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
388
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
389 def PostFixExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
390 pfe = self.PrimaryExpression()
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
391 while self.Peak in ['[', '.', '->', '(', '++']:
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
392 if self.hasConsumed('['):
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
393 i = self.Expression()
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
394 self.Consume(']')
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
395 pfe = Index(pfe, i, i.loc)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
396 elif self.hasConsumed('->'):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
397 field = self.Consume('ID')
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
398 pfe = Deref(pfe, pfe.loc)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
399 pfe = Member(pfe, field.val, field.loc)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
400 elif self.hasConsumed('.'):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
401 field = self.Consume('ID')
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
402 pfe = Member(pfe, field.val, field.loc)
315
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
403 elif self.Peak == '++':
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
404 loc = self.Consume('++').loc
084cccaa5deb Added console and screen
Windel Bouwman
parents: 313
diff changeset
405 pfe = Unop('++', pfe, loc)
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
406 elif self.hasConsumed('('):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
407 # Function call
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
408 args = []
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
409 if not self.hasConsumed(')'):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
410 args.append(self.Expression())
308
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
411 while self.hasConsumed(','):
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
412 args.append(self.Expression())
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
413 self.Consume(')')
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
414 pfe = FunctionCall(pfe, args, pfe.loc)
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
415 else:
2e7f55319858 Merged analyse into codegenerator
Windel Bouwman
parents: 307
diff changeset
416 raise Exception()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
417 return pfe
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
418
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
419 def PrimaryExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
420 if self.hasConsumed('('):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
421 e = self.Expression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
422 self.Consume(')')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
423 return e
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
424 elif self.Peak == 'NUMBER':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
425 val = self.Consume('NUMBER')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
426 return Literal(val.val, val.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
427 elif self.Peak == 'REAL':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
428 val = self.Consume('REAL')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
429 return Literal(val.val, val.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
430 elif self.Peak == 'true':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
431 val = self.Consume('true')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
432 return Literal(True, val.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
433 elif self.Peak == 'false':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
434 val = self.Consume('false')
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 293
diff changeset
435 return Literal(False, val.loc)
311
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 308
diff changeset
436 elif self.Peak == 'STRING':
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 308
diff changeset
437 val = self.Consume('STRING')
ff665880a6b0 Added testcase for kernel and userspace
Windel Bouwman
parents: 308
diff changeset
438 return Literal(val.val, val.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
439 elif self.Peak == 'ID':
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 301
diff changeset
440 return self.parseDesignator()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
441 self.Error('Expected NUM, ID or (expr), got {0}'.format(self.Peak))
354
5477e499b039 Added some sort of string functionality
Windel Bouwman
parents: 336
diff changeset
442