Permalink
Browse files

Add doc comment, change while loop to recursion.

  • Loading branch information...
1 parent 4f54ffb commit 8614185ff62e5a7cbbf656125b086fc568d6e996 @CraigStuntz committed Jan 31, 2012
Showing with 13 additions and 10 deletions.
  1. +1 −0 Lbac.Compiler/Part01-Introduction.fs
  2. +12 −10 Lbac.Compiler/Part02-ExpressionParsing.fs
@@ -20,6 +20,7 @@
error(s + " Expected")
/// <summary>
+ /// Returns argument c if lookahead equals c, throws otherwise.
/// Crenshaw calls this "Match", but match is reserved in F#
/// </summary>
member x.matchChar(c : char) =
@@ -114,16 +114,18 @@
member x.expression() =
// <expression> ::= [<addop>] <term> [<addop> <term>]*
- let mutable result = if isAddop x.look then
- [IL.Ldc_I4_0]
- else
- x.term()
- while isAddop x.look do
- match x.look with
- | '+' -> result <- result @ x.add()
- | '-' -> result <- result @ x.subtract()
- | _ -> x.expected("Addop")
- result
+ let head = if isAddop x.look then
+ [IL.Ldc_I4_0]
+ else
+ x.term()
+ // rest of expression is evaluated recurively for forms like 1+2-3+4...
+ x.expressionTail head
+
+ member private x.expressionTail head =
+ match x.look with
+ | '+' -> x.expressionTail( head @ x.add() )
+ | '-' -> x.expressionTail( head @ x.subtract() )
+ | _ -> head
override x.compile() =
x.expression()

0 comments on commit 8614185

Please sign in to comment.