KumiScript/reader/ParserQuoteVisitor.cs

65 lines
No EOL
2.2 KiB
C#

using KumiScript.Interpreter;
namespace KumiScript.Reader
{
public class ParserQuoteVisitor : ITokenVisitor
{
Parser _parser;
public ParserQuoteVisitor(Parser parser)
{
_parser = parser;
}
public Expression VisitAtom(AtomToken atom)
{
List<Expression> quotedAtom = new List<Expression>
{
new SymbolExpression(SymbolTable.GetInstance().FromString("quote")),
atom.ToExpression()
};
return new ProperListExpression(quotedAtom);
}
public Expression VisitEoF(EndOfFileToken eof)
{
throw new ParserUnexpectedEndOfFileException("Expected <atom>, <list>, got EOF.");
}
public Expression VisitParen(ParenthesisToken paren)
{
if (!paren._leftParen)
throw new ParserUnexpectedTokenException("Unexpected Token ')'! Wanted <atom>, '('.");
List<Expression> list = new List<Expression>();
ParserListVisitor listVisitor = new ParserListVisitor(_parser);
Expression item = _parser.NextExpressionCC(listVisitor);
while (!ReferenceEquals(item, FakeExpression.GetInstance()))
{
list.Add(item);
item = _parser.NextExpressionCC(listVisitor);
}
Expression listExpression = ListFactory.MakeList(list);
List<Expression> quotedList = new List<Expression>
{
new SymbolExpression(SymbolTable.GetInstance().FromString("quote")),
listExpression
};
return new ProperListExpression(quotedList);
}
public Expression VisitSpecial(SpecialToken spec)
{
throw new ParserUnexpectedTokenException("Expected <atom>, <list>, got SPECIAL.");
}
public Expression VisitString(StringToken str)
{
List<Expression> quotedString = new List<Expression>
{
new SymbolExpression(SymbolTable.GetInstance().FromString("quote")),
str.ToExpression()
};
return new ProperListExpression(quotedString);
}
}
}