KumiScript/parser/ParserTopLevelVisitor.cs

54 lines
1.5 KiB
C#
Raw Normal View History

2024-01-26 05:37:51 +00:00
using KumiScript.Interpreter;
namespace KumiScript.Reader
{
public class ParserTopLevelVisitor : ITokenVisitor
{
Parser _parser;
public ParserTopLevelVisitor(Parser parser)
{
_parser = parser;
}
public Expression VisitAtom(AtomToken atom)
{
return atom.ToExpression();
}
public Expression VisitString(StringToken str)
{
return str.ToExpression();
}
public Expression VisitEoF(EndOfFileToken eof)
{
throw new ParserEndOfFileException();
}
public Expression VisitParen(ParenthesisToken paren)
{
if (!paren._leftParen)
throw new ParserUnexpectedTokenException("Unexpected Token ')'! Wanted ATOM, '(', or EOF.");
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);
}
return ListFactory.MakeList(list);
}
public Expression VisitSpecial(SpecialToken spec)
{
if (spec._value == '\'')
return _parser.NextExpressionCC(new ParserQuoteVisitor(_parser));
throw new NotImplementedException();
}
}
}