annotate python/c3/parser.py @ 233:d3dccf12ca88

Added hexfile tests
author Windel Bouwman
date Sun, 14 Jul 2013 12:28:23 +0200
parents e621e3ba78d2
children 6ed3d3a82a63
rev   line source
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
1 from . import astnodes, lexer
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 150
diff changeset
2 from ppci import CompilerError
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
3
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
4 class Parser:
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
5 """ Parses sourcecode into an abstract syntax tree (AST) """
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
6 def __init__(self, diag):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
7 self.diag = diag
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
8
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
9 def parseSource(self, source):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
10 self.initLex(source)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
11 try:
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
12 self.parsePackage()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
13 return self.mod
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 150
diff changeset
14 except CompilerError as e:
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 150
diff changeset
15 self.diag.addDiag(e)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
16
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
17 def Error(self, msg):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
18 raise CompilerError(msg, self.token.loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
19
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
20 # Lexer helpers:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
21 def Consume(self, typ):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
22 if self.Peak == typ:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
23 return self.NextToken()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
24 else:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
25 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
26
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
27 @property
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
28 def Peak(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
29 return self.token.typ
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
30
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
31 @property
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
32 def CurLoc(self):
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
33 return self.token.loc
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
34
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
35 def hasConsumed(self, typ):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
36 if self.Peak == typ:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
37 self.Consume(typ)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
38 return True
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
39 return False
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
40
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
41 def NextToken(self):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
42 t = self.token
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
43 if t.typ != 'END':
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
44 self.token = self.tokens.__next__()
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
45 return t
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
46
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
47 def initLex(self, source):
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
48 self.tokens = lexer.tokenize(source) # Lexical stage
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
49 self.token = self.tokens.__next__()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
50
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
51 def addDeclaration(self, decl):
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
52 self.currentPart.declarations.append(decl)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
53
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
54 def parseUses(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
55 # TODO: parse uses
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
56 pass
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
57
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
58 def parsePackage(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
59 self.Consume('package')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
60 name = self.Consume('ID')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
61 self.Consume(';')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
62 self.mod = astnodes.Package(name.val, name.loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
63 self.currentPart = self.mod
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
64 self.parseUses()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
65 while self.Peak != 'END':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
66 self.parseTopLevel()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
67 self.Consume('END')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
68
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
69 def parseTopLevel(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
70 if self.Peak == 'function':
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
71 self.parseFunctionDef()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
72 elif self.Peak == 'var':
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
73 self.parseVarDef()
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
74 elif self.Peak == 'const':
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
75 self.parseConstDef()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
76 elif self.Peak == 'type':
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
77 self.parseTypeDef()
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
78 else:
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
79 self.Error('Expected function, var, const or type')
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 parseDesignator(self):
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
82 """ A designator designates an object """
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
83 name = self.Consume('ID')
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
84 return astnodes.Designator(name.val, name.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
85
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
86 # Type system
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
87 def parseTypeSpec(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
88 # For now, do simple type spec, just parse an ID:
219
1fa3e0050b49 Expanded ad hoc code generator
Windel Bouwman
parents: 215
diff changeset
89 #return self.parseDesignator()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
90 if self.Peak == 'struct':
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
91 self.Consume('struct')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
92 self.Consume('{')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
93 mems = []
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
94 while self.Peak != '}':
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
95 mem_t = self.parseTypeSpec()
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
96 mem_n = self.Consume('ID').val
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
97 mems.append(astnodes.StructField(mem_n, mem_t))
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
98 while self.hasConsumed(','):
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
99 mem_n = self.Consume('ID').val
227
82dfe6a32717 Fixed tests
Windel Bouwman
parents: 226
diff changeset
100 mems.append(astnodes.StructField(mem_n, mem_t))
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
101 self.Consume(';')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
102 self.Consume('}')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
103 theT = astnodes.StructureType(mems)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
104 else:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
105 theT = self.parseDesignator()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
106 # Check for pointer suffix:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
107 while self.hasConsumed('*'):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
108 theT = astnodes.PointerType(theT)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
109 return theT
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
110
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
111 def parseTypeDef(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
112 self.Consume('type')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
113 newtype = self.parseTypeSpec()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
114 typename = self.Consume('ID')
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
115 self.Consume(';')
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
116 df = astnodes.DefinedType(typename.val, newtype, typename.loc)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
117 self.addDeclaration(df)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
118
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
119 # Variable declarations:
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
120 def parseVarDef(self):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
121 self.Consume('var')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
122 t = self.parseTypeSpec()
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
123 def parseVar():
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
124 name = self.Consume('ID')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
125 v = astnodes.Variable(name.val, t)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
126 v.loc = name.loc
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
127 if self.hasConsumed('='):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
128 v.ival = self.Expression()
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
129 self.addDeclaration(v)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
130 parseVar()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
131 while self.hasConsumed(','):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
132 parseVar()
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
133 self.Consume(';')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
134
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
135 def parseConstDef(self):
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
136 self.Consume('const')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
137 t = self.parseTypeSpec()
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
138 def parseConst():
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
139 name = self.Consume('ID')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
140 self.Consume('=')
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
141 val = self.Expression()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
142 c = astnodes.Constant(name.val, t, val)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
143 c.loc = name.loc
163
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
144 parseConst()
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
145 while self.hasConsumed(','):
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
146 parseConst()
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
147 self.Consume(';')
8104fc8b5e90 Added visitor to c3
Windel Bouwman
parents: 158
diff changeset
148
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
149 # Procedures
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
150 def parseFunctionDef(self):
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
151 loc = self.Consume('function').loc
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
152 returntype = self.parseTypeSpec()
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
153 fname = self.Consume('ID').val
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
154 f = astnodes.Function(fname, loc)
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
155 self.addDeclaration(f)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
156 savePart = self.currentPart
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
157 self.currentPart = f
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
158 self.Consume('(')
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
159 parameters = []
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
160 if not self.hasConsumed(')'):
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
161 def parseParameter():
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
162 typ = self.parseTypeSpec()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
163 name = self.Consume('ID')
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
164 param = astnodes.Variable(name.val, typ)
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
165 param.loc = name.loc
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
166 self.addDeclaration(param)
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
167 parameters.append(param)
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
168 parseParameter()
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
169 while self.hasConsumed(','):
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
170 parseParameter()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
171 self.Consume(')')
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
172 paramtypes = [p.typ for p in parameters]
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
173 f.typ = astnodes.FunctionType(paramtypes, returntype)
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
174 f.body = self.parseCompoundStatement()
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
175 self.currentPart = savePart
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 # Statements:
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
178
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
179 def parseIfStatement(self):
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
180 loc = self.Consume('if').loc
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
181 self.Consume('(')
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
182 condition = self.Expression()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
183 self.Consume(')')
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
184 yes = self.parseCompoundStatement()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
185 if self.hasConsumed('else'):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
186 no = self.parseCompoundStatement()
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents: 164
diff changeset
187 else:
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
188 no = None
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
189 return astnodes.IfStatement(condition, yes, no, loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
190
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
191 def parseWhileStatement(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
192 loc = self.Consume('while').loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
193 self.Consume('(')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
194 condition = self.Expression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
195 self.Consume(')')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
196 statements = self.parseCompoundStatement()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
197 return astnodes.WhileStatement(condition, statements, loc)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
198
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
199 def parseReturnStatement(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
200 loc = self.Consume('return').loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
201 expr = self.Expression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
202 self.Consume(';')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
203 return astnodes.ReturnStatement(expr, loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
204
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
205 def parseCompoundStatement(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
206 self.Consume('{')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
207 statements = []
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
208 while not self.hasConsumed('}'):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
209 s = self.Statement()
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
210 if s is None:
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
211 continue
166
da0087b82fbe Improved type checking
Windel Bouwman
parents: 165
diff changeset
212 statements.append(s)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
213 return astnodes.CompoundStatement(statements)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
214
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
215 def Statement(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
216 # 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
217 if self.Peak == 'if':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
218 return self.parseIfStatement()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
219 elif self.Peak == 'while':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
220 return self.parseWhileStatement()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
221 elif self.Peak == '{':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
222 return self.parseCompoundStatement()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
223 elif self.hasConsumed(';'):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
224 pass
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
225 elif self.Peak == 'var':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
226 self.parseVarDef()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
227 elif self.Peak == 'return':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
228 return self.parseReturnStatement()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
229 else:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
230 return self.AssignmentOrCall()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
231
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
232 def AssignmentOrCall(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
233 x = self.UnaryExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
234 if self.Peak == '=':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
235 # We enter assignment mode here.
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
236 loc = self.Consume('=').loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
237 rhs = self.Expression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
238 return astnodes.Assignment(x, rhs, loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
239 else:
222
c3f1ce8b638f Fixup of parser
Windel Bouwman
parents: 221
diff changeset
240 return astnodes.ExpressionStatement(x, x.loc)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
241
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
242 # Expression section:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
243 # We not implement these C constructs:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
244 # a(2), f = 2
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
245 # and this:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
246 # a = 2 < x : 4 ? 1;
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
247
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
248 def Expression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
249 exp = self.LogicalAndExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
250 while self.Peak == 'or':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
251 loc = self.Consume('or').loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
252 e2 = self.LogicalAndExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
253 exp = astnodes.Binop(exp, 'or', e2, loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
254 return exp
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
255
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
256 def LogicalAndExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
257 o = self.EqualityExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
258 while self.Peak == 'and':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
259 loc = self.Consume('and').loc
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
260 o2 = self.EqualityExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
261 o = astnodes.Binop(o, 'and', o2, loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
262 return o
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
263
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
264 def EqualityExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
265 ee = self.SimpleExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
266 while self.Peak in ['<', '==', '>']:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
267 op = self.Consume(self.Peak)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
268 ee2 = self.SimpleExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
269 ee = astnodes.Binop(ee, op.typ, ee2, op.loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
270 return ee
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
271
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
272 def SimpleExpression(self):
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
273 """ Shift operations before + and - ? """
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
274 e = self.AddExpression()
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
275 while self.Peak in ['>>', '<<']:
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
276 op = self.Consume(self.Peak)
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
277 e2 = self.AddExpression()
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
278 e = astnodes.Binop(e, op.typ, e2, op.loc)
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
279 return e
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
280
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
281 def AddExpression(self):
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
282 e = self.Term()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
283 while self.Peak in ['+', '-']:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
284 op = self.Consume(self.Peak)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
285 e2 = self.Term()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
286 e = astnodes.Binop(e, op.typ, e2, op.loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
287 return e
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
288
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
289 def Term(self):
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
290 t = self.BitwiseOr()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
291 while self.Peak in ['*', '/']:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
292 op = self.Consume(self.Peak)
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
293 t2 = self.BitwiseOr()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
294 t = astnodes.Binop(t, op.typ, t2, op.loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
295 return t
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
296
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
297 def BitwiseOr(self):
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
298 a = self.BitwiseAnd()
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
299 while self.Peak in ['|']:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
300 op = self.Consume(self.Peak)
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
301 b = self.BitwiseAnd()
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
302 a = astnodes.Binop(a, op.typ, b, op.loc)
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
303 return a
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
304
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
305 def BitwiseAnd(self):
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
306 a = self.CastExpression()
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
307 while self.Peak in ['&']:
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
308 op = self.Consume(self.Peak)
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
309 b = self.CastExpression()
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
310 a = astnodes.Binop(a, op.typ, b, op.loc)
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
311 return a
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
312
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
313 # Domain of unary expressions:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
314
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
315 def CastExpression(self):
228
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
316 """
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
317 the C-style type cast conflicts with '(' expr ')'
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
318 so introduce extra keyword 'cast'
7f18ed9b6b7e Removal of emptystatement class
Windel Bouwman
parents: 227
diff changeset
319 """
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
320 if self.Peak == 'cast':
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
321 loc = self.Consume('cast').loc
221
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
322 self.Consume('<')
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
323 t = self.parseTypeSpec()
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
324 self.Consume('>')
848c4b15fd0b pointers
Windel Bouwman
parents: 220
diff changeset
325 self.Consume('(')
232
e621e3ba78d2 Added left shift instruction
Windel Bouwman
parents: 230
diff changeset
326 ce = self.Expression()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
327 self.Consume(')')
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
328 return astnodes.TypeCast(t, ce, loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
329 else:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
330 return self.UnaryExpression()
230
88a1e0baef65 Added some tests for IR-code
Windel Bouwman
parents: 228
diff changeset
331
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
332 def UnaryExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
333 if self.Peak in ['&', '*']:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
334 op = self.Consume(self.Peak)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
335 ce = self.CastExpression()
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
336 if op.val == '*':
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
337 return astnodes.Deref(ce, op.loc)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
338 else:
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
339 return astnodes.Unop(op.typ, ce, op.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
340 else:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
341 return self.PostFixExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
342
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
343 def PostFixExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
344 pfe = self.PrimaryExpression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
345 while self.Peak in ['[', '(', '.', '->']:
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
346 if self.hasConsumed('['):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
347 pass
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
348 elif self.hasConsumed('('):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
349 # Function call
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
350 args = []
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
351 if not self.hasConsumed(')'):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
352 args.append(self.Expression())
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
353 while self.hasConsumed(','):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
354 args.append(self.Expression())
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
355 self.Consume(')')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
356 pfe = astnodes.FunctionCall(pfe, args, pfe.loc)
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
357 elif self.hasConsumed('->'):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
358 field = self.Consume('ID')
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
359 pfe = astnodes.Deref(pfe, pfe.loc)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
360 pfe = astnodes.FieldRef(pfe, field.val, field.loc)
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
361 elif self.hasConsumed('.'):
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
362 field = self.Consume('ID')
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
363 pfe = astnodes.FieldRef(pfe, field.val, field.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
364 else:
225
1c7364bd74c7 Fixed pointer deref
Windel Bouwman
parents: 222
diff changeset
365 raise Exception()
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
366 return pfe
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
367
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
368 def PrimaryExpression(self):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
369 if self.hasConsumed('('):
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
370 e = self.Expression()
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
371 self.Consume(')')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
372 return e
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
373 elif self.Peak == 'NUMBER':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
374 val = self.Consume('NUMBER')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
375 return astnodes.Literal(val.val, val.loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
376 elif self.Peak == 'REAL':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
377 val = self.Consume('REAL')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
378 return astnodes.Literal(val.val, val.loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
379 elif self.Peak == 'true':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
380 val = self.Consume('true')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
381 return astnodes.Literal(True, val.loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
382 elif self.Peak == 'false':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
383 val = self.Consume('false')
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
384 return astnodes.Literal(False, val.loc)
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
385 elif self.Peak == 'ID':
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
386 d = self.parseDesignator()
213
003c8a976fff Merge of semantics and parser again ..
Windel Bouwman
parents: 186
diff changeset
387 return astnodes.VariableUse(d, d.loc)
220
3f6c30a5d234 Major change in expression parsing to enable pointers and structs
Windel Bouwman
parents: 219
diff changeset
388 self.Error('Expected NUM, ID or (expr), got {0}'.format(self.Peak))
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
389
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
390