Skip to content

Commit

Permalink
ch4
Browse files Browse the repository at this point in the history
  • Loading branch information
slobodin committed Aug 31, 2012
1 parent a824645 commit d470443
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 223 deletions.
162 changes: 57 additions & 105 deletions ch4/interpreter-cpp/schint/expression.cpp
Expand Up @@ -6,124 +6,75 @@
*/

#include "expression.h"
#include <algorithm>
#include "schemelist.h"

#include <stdexcept>

enum ExpressionType
{
Number,
Variable,
Quote,
Assignment,
Definition,
If,
Lambda,
Begin,
Application
};

// REDO!!!!!!!!!!!!!!!!! THIS IS XYNTA

class List
{
std::string m_listString;

static void makeList(std::string &rawStr)
{
for (size_t i = 0; i < rawStr.size(); i++)
{
char &ch = rawStr.at(i);
if (i != std::string::npos && i != 0)
{
if (std::isspace(ch))
{
const char &prevCh = rawStr.at(i - 1);
const char &nextCh = rawStr.at(i + 1);

if (prevCh == '(' || nextCh == ')' || prevCh == ' ' || nextCh == ' ')
{
rawStr.erase(i, 1); i--;
}
}
}
}
}

public:
List(const std::string &str) : m_listString(str)
{
if (str.empty() || str[0] != '(' || str[str.size() - 1] != ')')
throw std::runtime_error("Bad expression.");
makeList(m_listString);
}

ExpressionType type() const
{
std::string tag = car(*this).string();

if (tag == "\'")
return Quote;
if (tag == "set!")
return Assignment;
if (tag == "define")
return Definition;
if (tag == "if")
return If;
if (tag == "lambda")
return Lambda;
if (tag == "begin")
return Begin;
Expression::Expression()
{
}

//Expression::ExpressionType Expression::typeBySExpression(const std::string &sexpr)
//{
// std::string tag = sexpr.substr(1, sexpr.find(' ') - 1);



// throw std::runtime_error(std::string("Unknown expression type ") + tag + " .");
//}

Expression *Expression::analyzeExpression(const std::string &str)
{
SchemeList lst(str);

std::string tag = lst.car();

if (tag == "\'")
return nullptr;
if (tag == "set!")
return nullptr;
if (tag == "define")
return new DefinitionExpression(0 /* analyze (rest) */, 0);
if (tag == "if")
return nullptr;
if (tag == "lambda")
return nullptr;
if (tag == "begin")
return nullptr;
// if (tag == number);
// if (tag == symbol);
// if (tag == pair);

throw std::runtime_error(std::string("Unknown expression type ") + tag + " .");
}

// REDO!!!!!!!!!!!!!!!!! THIS IS XYNTA
throw std::runtime_error(std::string("Unknown expression type ") + tag + " .");
// ExpressionType exprType = typeBySExpression(expr);

static List car(const List &lst)
{
if (lst.string() == "()" || lst.string().at(0) != '(')
throw std::runtime_error("Unable to get car.");
return List(lst.string().substr(1, lst.string().find(' ') - 1));
}
// new DefinitionExpr(cadr(expr), createExpression(caddr(expr)));

static List cdr(const List &lst)
{
std::string res = std::string("(") + lst.string().substr(lst.string().find(' '));
makeList(res);
return List(res);
}
// if (exprType == Quote)
// return new QuotedExpression(List::car(List::cdr(expr)).string());
// if (exprType == Assignment)
// return new AssignmentExpression("");
// if (exprType == Definition)
// return new DefinitionExpression("");
// if (exprType == If)
// return new IfExpression("");
// if (exprType == Lambda)
// return new LambdaExpression("");
// if (exprType == Begin)
// return new BeginExpression("");

std::string string() const { return m_listString; }
};
// return nullptr;
}

Expression::Expression()
Expression *Expression::car() const
{
}

Expression *Expression::createExpression(const std::string &str)
Expression *Expression::cdr() const
{
List expr(str);
ExpressionType exprType = expr.type();

if (exprType == Quote)
return new QuotedExpression(List::car(List::cdr(expr)).string());
if (exprType == Assignment)
return new AssignmentExpression("");
if (exprType == Definition)
return new DefinitionExpression("");
if (exprType == If)
return new IfExpression("");
if (exprType == Lambda)
return new LambdaExpression("");
if (exprType == Begin)
return new BeginExpression("");

return nullptr;
}

/*
VariableExpression::VariableExpression(const std::string &value)
{
}
Expand Down Expand Up @@ -158,9 +109,9 @@ Expression *AssignmentExpression::eval(Environment *env)
std::string AssignmentExpression::toString() const
{
}
}*/

DefinitionExpression::DefinitionExpression(const std::string &value)
DefinitionExpression::DefinitionExpression(Expression *variable, Expression *value)
{
}

Expand All @@ -171,7 +122,7 @@ Expression *DefinitionExpression::eval(Environment *env)
std::string DefinitionExpression::toString() const
{
}

/*
IfExpression::IfExpression(const std::string &value)
{
}
Expand Down Expand Up @@ -219,3 +170,4 @@ Expression *ApplicationExpression::eval(Environment *env)
std::string ApplicationExpression::toString() const
{
}
*/

0 comments on commit d470443

Please sign in to comment.