annotate python/c3/parser.py @ 225:1c7364bd74c7

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