2024-01-26 05:37:51 +00:00
|
|
|
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>
|
|
|
|
{
|
2024-07-12 00:17:00 +00:00
|
|
|
new SymbolExpression(SymbolTable.GetInstance().FromString("quote")),
|
2024-01-26 05:37:51 +00:00
|
|
|
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);
|
2024-07-12 00:17:00 +00:00
|
|
|
while (!ReferenceEquals(item, FakeExpression.GetInstance()))
|
2024-01-26 05:37:51 +00:00
|
|
|
{
|
|
|
|
list.Add(item);
|
|
|
|
item = _parser.NextExpressionCC(listVisitor);
|
|
|
|
}
|
|
|
|
Expression listExpression = ListFactory.MakeList(list);
|
|
|
|
List<Expression> quotedList = new List<Expression>
|
|
|
|
{
|
2024-07-12 00:17:00 +00:00
|
|
|
new SymbolExpression(SymbolTable.GetInstance().FromString("quote")),
|
2024-01-26 05:37:51 +00:00
|
|
|
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>
|
|
|
|
{
|
2024-07-12 00:17:00 +00:00
|
|
|
new SymbolExpression(SymbolTable.GetInstance().FromString("quote")),
|
2024-01-26 05:37:51 +00:00
|
|
|
str.ToExpression()
|
|
|
|
};
|
|
|
|
return new ProperListExpression(quotedString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|