34-Assignment-7-Little-Schemer - CS106X Autumn 2010 Handout...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 34 Autumn 2010 November 17 th , 2010 Assignment 7: Little Schemer Assignment written by Jerry Cain, with help from Michael Gummelt. For your last assignment, you’re going to take working program and extend it into a full- blown Little Scheme interpreter. The program as it’s handed to you doesn’t do much beyond respond to a subset of core Scheme functions and reduce fractions for you. You’ll start off by adding a few more core built-in functions, and once you have those working, you’ll add code that implements function definition and general function call evaluation. Handout 33 discusses our reduced version of Scheme and how one programs using it. Due: Friday, December 3 rd at 5:00 p.m. The Expression Class Hierarchy Fundamentally, all Scheme expressions are Booleans, rational numbers, string constants, symbols, lists, or functions, and only the last two of the six have anything interesting going on. Think in abstract terms. All expressions—and I mean all expression—know how to evaluate themselves, present a string version of themselves, and clarify whether or not they’re true or false when evaluated in a context where a Boolean value is expected. The following is a cannibalization of the Expression class that appears in Chapter 14, adapted to mitigate a new domain: class Expression { public: virtual Expression *eval(EvalState& state) { return this; } virtual string toString() = 0; virtual bool isTrue() { return true; } }; This says that a good default implementation of eval is to just return this , because a good number of the subclasses evaluate to themselves. It also says that most expressions should be interpreted as true whenever they’re evaluated in a setting where some Expression Boolean Rational String Symbol List Callable
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Boolean value is expected. And toString is marked as pure virtual aka abstract, because no single implementation works for more than one of them. You can peruse the implementation of the BooleanExpression , RationalExpression , and StringExpression classes. I’m fairly confident there’s very little in there that will surprise you. In fact, it’s unlikely you’ll need to change them much. The implementation of these three classes, coupled with the implementation of the read-eval-print loop in the main function, is enough to account for the following test run: little-schemer 1> "welcome to assignment 7" "welcome to assignment 7" little-schemer 2> "" "" little-schemer 3> 55 55 little-schemer 4> 1234567890 1234567890 little-schemer 5> 3/4 3/4 little-schemer 6> 9/4 9/4 little-schemer 7> 24/10 12/5 little-schemer 8> 2404/3005 4/5 little-schemer 9> true true little-schemer 10> false false All instances of these three classes evaluate to themselves and know how to print themselves out. If you’ve been following lecture and read through Chapter 14, then you can easily intuit how these classes must work if they contribute to the sample session above. SymbolExpression
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2011 for the course CS 106X taught by Professor Cain,g during the Fall '08 term at Stanford.

Page1 / 7

34-Assignment-7-Little-Schemer - CS106X Autumn 2010 Handout...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online