Skip to content
This repository has been archived by the owner on Dec 2, 2020. It is now read-only.

Commit

Permalink
Adding a sample file showing objects/prototypes. Documenting.
Browse files Browse the repository at this point in the history
  • Loading branch information
munificent committed Feb 16, 2010
1 parent 2771e36 commit cc4936a
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 3 deletions.
48 changes: 48 additions & 0 deletions sample/objects.fin
@@ -0,0 +1,48 @@
' define a singleton object for the Car "type". this object will be
' the factory for all other cars.
def Car <- Object copyWith: {
' define the car prototype. all cars will derive from this
def _prototype <- Object copyWith: {
' all cars have accessors for their make and model
.addMethod: "make" body: { _make }
.addMethod: "model" body: { _model }

.addMethod: "toString" body: {
"This car is a " + _make + " " + _model + "."
}
}

' define a constructor method on the factory.
.addMethod: "make:model:" body: {
|make model|

' the constructor simply derives a new car from the car
' prototype and then stores the make and model in it
_prototype copyWith: {
def _make <- make
def _model <- model
}
' like ruby, the last expression in the method is implicitly
' the returned value
}

' expose the prototype. this lets us add features to all cars
.addMethod: "prototype" body: { _prototype }
}

' now lets make a car
def jetta <- Car make: "volkswagen" model: "jetta"

' and call a method on its prototype, which in turn references the
' specific car's state
writeLine: jetta toString

' now let's try adding something to the car prototype. first we'll
' check to see that it isn't there
writeLine: jetta shortName ' this should write Nil

' now we'll add a method to the prototype
Car prototype addMethod: "shortName" body: { _make + " " + _model }

' now lets call it on jetta and see if we can find it
writeLine: jetta shortName
4 changes: 4 additions & 0 deletions src/Repl.h
Expand Up @@ -4,12 +4,16 @@

namespace Finch
{
// A simple read/eval/print loop. Calling Run() will create a new Finch
// environment, read an expression from stdin, evaluate it, and repeat
// until the user quits by calling "Ether quit".
class Repl
{
public:
Repl()
{}

// Runs the REPL. This function will not return until the user quits.
void Run();

private:
Expand Down
5 changes: 5 additions & 0 deletions src/Script.h
Expand Up @@ -10,10 +10,15 @@ namespace Finch
class Environment;
class Object;

// Static class for loading and executing Finch code stored in script files.
class Script
{
public:
// Loads and runs the script at the given path in its own environment.
static Ref<Object> Run(String fileName);

// Loads and runs the script at the given path in the given existing
// environment.
static Ref<Object> Run(String fileName, Environment & env);

private:
Expand Down
2 changes: 2 additions & 0 deletions src/Stack.h
Expand Up @@ -26,13 +26,15 @@ namespace Finch
// Gets the maximum number of items the stack can hold.
int Capacity() const { return Size; }

// Pushes the given item onto the top of the stack.
void Push(const T & item)
{
ASSERT(mCount < Capacity(), "Cannot push onto a full stack.");

mItems[mCount++] = item;
}

// Pops the top item off the stack.
T Pop()
{
ASSERT(mCount > 0, "Cannot pop an empty stack.");
Expand Down
1 change: 1 addition & 0 deletions src/Syntax/FinchParser.h
Expand Up @@ -28,6 +28,7 @@ namespace Finch
Ref<Expr> ParseLine();

private:
// The grammar productions, from lowest to highest precedence.
Ref<Expr> Expression();
Ref<Expr> Sequence();
Ref<Expr> Variable();
Expand Down
5 changes: 3 additions & 2 deletions src/Syntax/ILineReader.h
Expand Up @@ -8,8 +8,9 @@
namespace Finch
{
// Interface for a class that reads a series of lines of text from some
// source. Used to abstract where the Lexer gets it Finch source from so
// that we can use it both for the Repl and for parsing entire source files.
// source. Used to abstract where the Lexer gets it Finch source code from
// so that we can use it both for the Repl and for parsing entire source
// files.
class ILineReader
{
public:
Expand Down
1 change: 1 addition & 0 deletions src/Syntax/Parser.h
Expand Up @@ -40,6 +40,7 @@ namespace Finch

ITokenSource & mTokens;

// The 2 here is the maximum number of lookahead tokens.
Queue<Ref<Token>, 2> mRead;

NO_COPY(Parser);
Expand Down
3 changes: 2 additions & 1 deletion src/Syntax/Token.h
Expand Up @@ -40,7 +40,8 @@ namespace Finch
class Token
{
public:
// virtual constructors
// Factory methods to construct tokens of different types with
// different data.
static Ref<Token> New(TokenType type);
static Ref<Token> New(TokenType type, const String & text);
static Ref<Token> New(TokenType type, double number);
Expand Down

0 comments on commit cc4936a

Please sign in to comment.