@@ -42,6 +42,9 @@ public void Dispose( )
4242 Context . Dispose ( ) ;
4343 }
4444
45+ public BitcodeModule GeneratedModule => Module ;
46+
47+ // <Generate>
4548 public Value Generate ( Parser parser , IParseTree tree , DiagnosticRepresentations additionalDiagnostics )
4649 {
4750 if ( parser . NumberOfSyntaxErrors > 0 )
@@ -51,22 +54,21 @@ public Value Generate( Parser parser, IParseTree tree, DiagnosticRepresentations
5154
5255 return Visit ( tree ) ;
5356 }
57+ // </Generate>
5458
5559 public override Value VisitParenExpression ( [ NotNull ] ParenExpressionContext context )
5660 {
5761 return context . Expression . Accept ( this ) ;
5862 }
5963
64+ // <VisitConstExpression>
6065 public override Value VisitConstExpression ( [ NotNull ] ConstExpressionContext context )
6166 {
6267 return Context . CreateConstant ( context . Value ) ;
6368 }
69+ // </VisitConstExpression>
6470
65- public override Value VisitExternalDeclaration ( [ NotNull ] ExternalDeclarationContext context )
66- {
67- return context . Signature . Accept ( this ) ;
68- }
69-
71+ // <VisitVariableExpression>
7072 public override Value VisitVariableExpression ( [ NotNull ] VariableExpressionContext context )
7173 {
7274 string varName = context . Name ;
@@ -77,6 +79,7 @@ public override Value VisitVariableExpression( [NotNull] VariableExpressionConte
7779
7880 return value ;
7981 }
82+ // </VisitVariableExpression>
8083
8184 public override Value VisitFunctionCallExpression ( [ NotNull ] FunctionCallExpressionContext context )
8285 {
@@ -90,41 +93,55 @@ public override Value VisitFunctionCallExpression( [NotNull] FunctionCallExpress
9093 return InstructionBuilder . Call ( function , args ) . RegisterName ( "calltmp" ) ;
9194 }
9295
96+ // <FunctionDeclarations>
97+ public override Value VisitExternalDeclaration ( [ NotNull ] ExternalDeclarationContext context )
98+ {
99+ return context . Signature . Accept ( this ) ;
100+ }
101+
93102 public override Value VisitFunctionPrototype ( [ NotNull ] FunctionPrototypeContext context )
94103 {
95104 return GetOrDeclareFunction ( new Prototype ( context ) ) ;
96105 }
106+ // </FunctionDeclarations>
97107
108+ // <VisitFunctionDefinition>
98109 public override Value VisitFunctionDefinition ( [ NotNull ] FunctionDefinitionContext context )
99110 {
100111 return DefineFunction ( ( Function ) context . Signature . Accept ( this )
101112 , context . BodyExpression
102113 ) ;
103114 }
115+ // </VisitFunctionDefinition>
104116
117+ // <VisitTopLevelExpression>
105118 public override Value VisitTopLevelExpression ( [ NotNull ] TopLevelExpressionContext context )
106119 {
107120 var proto = new Prototype ( $ "anon_expr_{ AnonNameIndex ++ } " ) ;
108121 var function = GetOrDeclareFunction ( proto ) ;
109122
110123 return DefineFunction ( function , context . expression ( ) ) ;
111124 }
125+ // </VisitTopLevelExpression>
112126
127+ // <VisitExpression>
113128 public override Value VisitExpression ( [ NotNull ] ExpressionContext context )
114129 {
115130 // Expression: PrimaryExpression (op expression)*
116131 // the sub-expressions are in evaluation order
117- var lhs = context . primaryExpression ( ) . Accept ( this ) ;
132+ var lhs = context . Atom . Accept ( this ) ;
118133 foreach ( var ( op , rhs ) in context . OperatorExpressions )
119134 {
120135 lhs = EmitBinaryOperator ( lhs , op , rhs ) ;
121136 }
122137
123138 return lhs ;
124139 }
140+ // </VisitExpression>
125141
126142 protected override Value DefaultResult => null ;
127143
144+ // <EmitBinaryOperator>
128145 private Value EmitBinaryOperator ( Value lhs , BinaryopContext op , IParseTree rightTree )
129146 {
130147 var rhs = rightTree . Accept ( this ) ;
@@ -166,12 +183,14 @@ private Value EmitBinaryOperator( Value lhs, BinaryopContext op, IParseTree righ
166183 throw new CodeGeneratorException ( $ "Invalid binary operator { op . Token . Text } " ) ;
167184 }
168185 }
186+ // </EmitBinaryOperator>
169187
170188 private Function GetFunction ( string name )
171189 {
172190 return Module . GetFunction ( name ) ;
173191 }
174192
193+ // <GetOrDeclareFunction>
175194 private Function GetOrDeclareFunction ( Prototype prototype )
176195 {
177196 var function = Module . GetFunction ( prototype . Identifier . Name ) ;
@@ -194,7 +213,9 @@ private Function GetOrDeclareFunction( Prototype prototype )
194213
195214 return retVal ;
196215 }
216+ // </GetOrDeclareFunction>
197217
218+ // <DefineFunction>
198219 private Function DefineFunction ( Function function , ExpressionContext body )
199220 {
200221 if ( ! function . IsDeclaration )
@@ -222,6 +243,7 @@ private Function DefineFunction( Function function, ExpressionContext body )
222243
223244 return function ;
224245 }
246+ // </DefineFunction>
225247
226248 // <PrivateMembers>
227249 private readonly DynamicRuntimeState RuntimeState ;
0 commit comments