Piethon  1.0
A Python-like interpreter using flex and bison.
Public Member Functions | List of all members
Function Class Reference

#include <function.h>

Collaboration diagram for Function:
Collaboration graph

Public Member Functions

 Function (ParameterList *pList, StatementList *sList)
 
 ~Function ()
 
Number apply (const SymbolTable &, const FunctionTable &, ExpressionList *) const
 
Number apply (const SymbolTable &st, const FunctionTable &ft) const
 

Detailed Description

Definition at line 29 of file function.h.

Constructor & Destructor Documentation

◆ Function()

Function::Function ( ParameterList pList,
StatementList sList 
)
inline

Definition at line 31 of file function.h.

31 : paramList(pList), stmtList(sList) { }

◆ ~Function()

Function::~Function ( )

Definition at line 24 of file function.cpp.

24  {
25  delete paramList;
26  delete stmtList;
27 }

Member Function Documentation

◆ apply() [1/2]

Number Function::apply ( const SymbolTable st,
const FunctionTable ft,
ExpressionList argList 
) const

Definition at line 39 of file function.cpp.

39  {
40 
41  // copy existing symbol and function tables into local tables
42  SymbolTable localST(st);
43  FunctionTable localFT(ft);
44 
45  // ensure the correct number of args were passed
46  if (paramList->size() != argList->size()) {
47  throw ParameterMismatch();
48  }
49 
50  // bind passed arguments to formal parameters in local symbol table
51  list<string>::const_iterator formalIt = paramList->begin();
52  list<Expression*>::const_iterator argIt = argList->begin();
53 
54  while (formalIt != paramList->end()) {
55  localST[*formalIt] = (*argIt)->eval(st, ft);
56  ++formalIt;
57  ++argIt;
58  }
59 
60  // execute all statements in function. If the function throws a return value, return it
61  try {
62  stmtList->eval(localST, localFT);
63  } catch (ReturnValue r) {
64  return r.getValue();
65  }
66 
67  // else, the function had no return statement. Return default 0.
68  return Number(0);
69 }
list< string >::const_iterator begin() const
Definition: function.cpp:16
unsigned int size() const
Definition: expression.cpp:87
Number getValue() const
Definition: exception.cpp:15
list< Expression * >::const_iterator begin() const
Definition: expression.cpp:91
void eval(SymbolTable &, FunctionTable &) const
Definition: statement.cpp:103
list< string >::const_iterator end() const
Definition: function.cpp:20
unsigned int size() const
Definition: function.cpp:8
Definition: number.h:21

◆ apply() [2/2]

Number Function::apply ( const SymbolTable st,
const FunctionTable ft 
) const

Definition at line 29 of file function.cpp.

29  {
30  // create dummy empty expressionList to pass to no-arg function
31  ExpressionList* el = new ExpressionList();
32 
33  // evaluate the function
34  Number n = apply(st, ft, el);
35  delete el;
36  return n;
37 }
Number apply(const SymbolTable &, const FunctionTable &, ExpressionList *) const
Definition: function.cpp:39
Definition: number.h:21

The documentation for this class was generated from the following files: