Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add doc comment, change while loop to recursion.

  • Loading branch information...
commit 8614185ff62e5a7cbbf656125b086fc568d6e996 1 parent 4f54ffb
@CraigStuntz authored
View
1  Lbac.Compiler/Part01-Introduction.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) =
View
22 Lbac.Compiler/Part02-ExpressionParsing.fs
@@ -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()
Please sign in to comment.
Something went wrong with that request. Please try again.