65 lines
2.1 KiB
C#
65 lines
2.1 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(new Symbol("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 (item is not null)
|
||
|
{
|
||
|
list.Add(item);
|
||
|
item = _parser.NextExpressionCC(listVisitor);
|
||
|
}
|
||
|
Expression listExpression = ListFactory.MakeList(list);
|
||
|
List<Expression> quotedList = new List<Expression>
|
||
|
{
|
||
|
new SymbolExpression(new Symbol("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(new Symbol("quote")),
|
||
|
str.ToExpression()
|
||
|
};
|
||
|
return new ProperListExpression(quotedString);
|
||
|
}
|
||
|
}
|
||
|
}
|