Exemple #1
0
        public ClSymbol Read(ISource source)
        {
            if (source.Eof())
            {
                return(null);
            }
            if (TryCheckSpecialBuiltInFunction(source, out var fun))
            {
                return(new ClSymbol(fun));
            }
            var ch = (char)source.Peek();

            if (!char.IsLetter(ch))
            {
                return(null);
            }
            string loop(string acc)
            {
                if (source.Eof())
                {
                    return(acc);
                }
                var ch = (char)source.Peek();

                if (!char.IsLetterOrDigit(ch) && ch != '-' && ch != '?' && ch != '!')
                {
                    return(acc);
                }
                return(loop($"{acc}{(char) source.Read()}"));
            }

            return(new ClSymbol(loop($"{(char) source.Read()}")));
        }
Exemple #2
0
        public ClObj Read(ISource source)
        {
            var items = new List <ClObj> {
                ClSymbol.Begin
            };

            while (!source.Eof())
            {
                source.RewindSpacesAndComments();
                items.Add(_reader.Read(source));
                source.RewindSpacesAndComments();
            }
            return(BuiltIn.ListOf(items));
        }
 public ClObj Read(ISource source)
 {
     if (source.Eof())
     {
         return(ClCell.Nil);
     }
     foreach (var reader in _readers)
     {
         if (reader.Read(source) is ClObj obj)
         {
             return(obj);
         }
     }
     throw new SyntaxError("Unknown literal");
 }
Exemple #4
0
        protected bool TryReadAtLeastOneNumber(ISource source, out string nums)
        {
            string loop(string acc = "")
            {
                if (source.Eof())
                {
                    return(acc);
                }
                if (!char.IsDigit((char)source.Peek()))
                {
                    return(acc);
                }
                return(loop($"{acc}{(char) source.Read()}"));
            }

            nums = loop();
            return(!string.IsNullOrEmpty(nums));
        }
 public ClChar Read(ISource source)
 {
     if (!source.Rewind(@"#\"))
     {
         return(null);
     }
     foreach (var(word, ch) in SpecialChars)
     {
         if (!source.Rewind(word))
         {
             continue;
         }
         return(new ClChar(ch));
     }
     if (source.Eof())
     {
         throw new SyntaxError($"Invalid format of the {nameof(ClChar)} literal");
     }
     return(new ClChar((char)source.Read()));
 }
Exemple #6
0
        public static bool RewindSpaces(this ISource source)
        {
            bool loop(bool atLeastOne = false)
            {
                if (source.Eof())
                {
                    return(atLeastOne);
                }
                var code = source.Read();

                if (char.IsWhiteSpace((char)code))
                {
                    return(loop(true));
                }
                source.Buffer(code);
                return(atLeastOne);
            }

            return(loop());
        }
        public ClString Read(ISource source)
        {
            if (!source.Rewind("'"))
            {
                return(null);
            }
            string loop(string acc)
            {
                if (source.Eof())
                {
                    throw new SyntaxError($"Invalid format of the {nameof(ClString)} literal");
                }
                var ch = (char)source.Read();

                if (ch == '\'')
                {
                    return(acc);
                }
                return(loop($"{acc}{ch}"));
            }

            return(new ClString(loop(string.Empty)));
        }