Exemplo n.º 1
0
 private static bool CapsInfer(Interpreter interpreter, Source source, Stringe tagname, TagArg[] args)
 {
     // TODO: Make capsinfer properly infer "first" capitalization given multiple sentences. Currently, it mistakes it for "word" mode.
     var words = Regex.Matches(args[0].GetString(), @"\w+").OfType<Match>().Select(m => m.Value).ToArray();
     int wCount = words.Length;
     int uCount = 0;
     int fwCount = 0;
     bool firstCharIsUpper = false;
     for (int i = 0; i < wCount; i++)
     {
         if (words[i].All(Char.IsUpper))
         {
             uCount++;
         }
         if (Char.IsUpper(words[i][0]))
         {
             fwCount++;
             if (i == 0) firstCharIsUpper = true;
         }
     }
     if (uCount == wCount)
     {
         interpreter.CurrentState.Output.SetCaps(Capitalization.Upper);
     }
     else if (wCount > 1 && fwCount == wCount)
     {
         interpreter.CurrentState.Output.SetCaps(Capitalization.Word);
     }
     else if (firstCharIsUpper)
     {
         interpreter.CurrentState.Output.SetCaps(Capitalization.First);
     }
     return false;
 }
Exemplo n.º 2
0
        public TagBlueprint(Interpreter interpreter, Source source, Stringe name, IEnumerable<Token<TokenType>>[] args = null)
            : base(interpreter)
        {
            Source = source;
            Name = name;

            if (!Interpreter.TagFuncs.TryGetValue(Name.Value.ToLower().Trim(), out _tagDef))
            {
                throw new ManhoodException(Source, Name, "The tag '" + Name.Value + "' does not exist.");
            }

            _tagDef.ValidateArgCount(source, name, args != null ? args.Length : 0);

            if (args == null)
            {
                _args = Enumerable.Empty<TagArg>().ToArray();
            }
            else
            {
                // Insert token arguments into the array, set string args to null.
                _args = args.Select((a, i) => _tagDef.ArgTypes[i] == TagArgType.Tokens ? TagArg.FromTokens(a) : null).ToArray();

                // Queue string arguments on the stack.
                for (int i = 0; i < _tagDef.ArgTypes.Length; i++)
                {
                    if (_tagDef.ArgTypes[i] == TagArgType.Result)
                    {
                        interpreter.PushState(Interpreter.State.CreateDerivedDistinct(source, args[i], interpreter));
                    }
                }
            }
        }
Exemplo n.º 3
0
 public override bool Use()
 {
     var srcstr = I.PopResultString();
     var src = new Source("Meta_" + String.Format("{0:X16}", srcstr.Hash()), SourceType.Metapattern, srcstr);
     I.PushState(Interpreter.State.Create(src, I));
     return true;
 }
Exemplo n.º 4
0
 public State(Interpreter ii, Source source, ChannelStack output)
 {
     _finished = false;
     _preBlueprints = new Stack<Blueprint>();
     _postBlueprints = new Stack<Blueprint>();
     _interpreter = ii;
     _output = output;
     _reader = new SourceReader(source);
     _sharesOutput = (output == _interpreter._output && _interpreter.PrevState != null) || (_interpreter._stateStack.Any() && output == _interpreter._stateStack.Peek().Output);
 }
Exemplo n.º 5
0
 private State(Interpreter ii, Source derivedSource, IEnumerable<Token<TokenType>> tokens,
     ChannelStack output)
 {
     _finished = false;
     _preBlueprints = new Stack<Blueprint>();
     _postBlueprints = new Stack<Blueprint>();
     _interpreter = ii;
     _output = output;
     _reader = new SourceReader(new Source(derivedSource.Name, derivedSource.Type, tokens, derivedSource.Code));
     _sharesOutput = (output == _interpreter._output && _interpreter.PrevState != null) || (_interpreter._stateStack.Any() && output == _interpreter._stateStack.Peek().Output);
 }
Exemplo n.º 6
0
 internal ManhoodException(Source source, Stringe token, string message = "A generic syntax error was encountered.") : base((token != null ? ("(Ln " + token.Line + ", Col " + token.Column + ") - ") : "") + message)
 {
     _source = source.Code;
     if (token != null)
     {
         _line = token.Line;
         _col = token.Column;
         _index = token.Offset;
         _length = token.Length;
     }
     else
     {
         _line = _col = 1;
         _index = 0;
         _length = 0;
     }
 }
Exemplo n.º 7
0
        public SubCallBlueprint(Interpreter interpreter, Source source, Subroutine subroutine, IEnumerable<Token<TokenType>>[] args) : base(interpreter)
        {
            _subroutine = subroutine;
            if (args == null)
            {
                _args = Enumerable.Empty<TagArg>().ToArray();
            }
            else
            {
                // Insert token arguments into the array, set string args to null.
                _args = args.Select((a, i) => _subroutine.Parameters[i].Item2 == TagArgType.Tokens ? TagArg.FromTokens(a) : null).ToArray();

                // Queue string arguments on the stack.
                for (int i = 0; i < _subroutine.ParamCount; i++)
                {
                    if (_subroutine.Parameters[i].Item2 == TagArgType.Result)
                    {
                        interpreter.PushState(Interpreter.State.CreateDerivedDistinct(source, args[i], interpreter));
                    }
                }
            }
        }
Exemplo n.º 8
0
 public static State CreateDerivedShared(Source derivedSource, IEnumerable<Token<TokenType>> tokens,
     Interpreter interpreter)
 {
     return new State(interpreter, derivedSource, tokens, interpreter._output);
 }
Exemplo n.º 9
0
 public static State Create(Source source, Interpreter interpreter)
 {
     return new State(interpreter, source, interpreter._output);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Executes the specified source using a custom random number generator and returns the resulting output.
 /// </summary>
 /// <param name="input">The source to execute.</param>
 /// <param name="rng">The random number generator to use when generating output.</param>
 /// <param name="charLimit">The maximum number of characters that can be printed. An exception will be thrown if the limit is exceeded. Set to zero or below for unlimited characters.</param>
 /// <returns></returns>
 public Output Do(Source input, RNG rng, int charLimit = 0)
 {
     return new Interpreter(this, input, rng, charLimit).Run();
 }
Exemplo n.º 11
0
 private static bool First(Interpreter interpreter, Source source, Stringe tagname, TagArg[] args)
 {
     if (interpreter.CurrentRepeater == null || !interpreter.CurrentRepeater.IsFirst) return false;
     interpreter.PushState(State.CreateDerivedDistinct(source, args[0].GetTokens(), interpreter, interpreter.CurrentState.Output));
     return true;
 }
Exemplo n.º 12
0
 private static bool Sync(Interpreter interpreter, Source source, Stringe tagname, TagArg[] args)
 {
     var typeStr = args[1].GetString();
     SyncType type;
     if (!Enum.TryParse(typeStr, true, out type))
     {
         throw new ManhoodException(source, tagname, "Invalid synchronizer type: '" + typeStr + "'");
     }
     interpreter.Sync(args[0].GetString(), type);
     return false;
 }
Exemplo n.º 13
0
 private static bool Number(Interpreter interpreter, Source source, Stringe tagName, TagArg[] args)
 {
     int a, b;
     if (!Int32.TryParse(args[0].GetString(), out a) || !Int32.TryParse(args[1].GetString(), out b))
     {
         throw new ManhoodException(source, tagName, "Range values could not be parsed. They must be numbers.");
     }
     interpreter.Print(interpreter.FormatNumber(interpreter.RNG.Next(a, b + 1)));
     return false;
 }
Exemplo n.º 14
0
 public bool Invoke(Interpreter interpreter, Source source, Stringe tagName, TagArg[] args)
 {
     return _func(interpreter, source, tagName, args);
 }
Exemplo n.º 15
0
 private static bool Reset(Interpreter interpreter, Source source, Stringe tagName, TagArg[] args)
 {
     interpreter.Reset(args[0].GetString());
     return false;
 }
Exemplo n.º 16
0
 public void ValidateArgCount(Source source, Stringe tagName, int count)
 {
     if (count != _paramCount)
         throw new ManhoodException(source, tagName, "Tag '" + tagName.Value + "' expected " + _paramCount + " " + (_paramCount == 1 ? "argument" : "arguments") + " but got " + count + ".");
 }
Exemplo n.º 17
0
        private static bool Repeat(Interpreter interpreter, Source source, Stringe tagName, TagArg[] args)
        {
            var reps = args[0].GetString().ToLower().Trim();
            if (reps == "each")
            {
                interpreter.NextAttribs.Repetitons = Repeater.Each;
                return false;
            }

            int num;
            if (!Int32.TryParse(reps, out num))
            {
                throw new ManhoodException(source, tagName, "Invalid repetition value '" + reps + "' - must be a number.");
            }
            if (num < 0)
            {
                throw new ManhoodException(source, tagName, "Repetition value cannot be negative.");
            }

            interpreter.NextAttribs.Repetitons = num;
            return false;
        }
Exemplo n.º 18
0
 private static bool Separator(Interpreter interpreter, Source source, Stringe tagName, TagArg[] args)
 {
     interpreter.NextAttribs.Separator = args[0].GetTokens();
     return false;
 }
Exemplo n.º 19
0
 public static State CreateDerivedDistinct(Source derivedSource, IEnumerable<Token<TokenType>> tokens,
     Interpreter interpreter, ChannelStack output = null)
 {
     return new State(interpreter, derivedSource, tokens, output ?? new ChannelStack(interpreter.CharLimit));
 }
Exemplo n.º 20
0
 private static bool Desync(Interpreter interpreter, Source source, Stringe tagname, TagArg[] args)
 {
     interpreter.Desync();
     return false;
 }
Exemplo n.º 21
0
 private Subroutine(Source source, Tuple<string, TagArgType>[] parameters)
 {
     _source = source;
     _parameters = parameters;
     _argCount = _parameters.Length;
 }
Exemplo n.º 22
0
 private static bool Chance(Interpreter interpreter, Source source, Stringe tagname, TagArg[] args)
 {
     int a;
     if (!Int32.TryParse(args[0].GetString(), out a))
     {
         throw new ManhoodException(source, tagname, "Invalid chance number.");
     }
     interpreter.SetChance(a);
     return false;
 }
Exemplo n.º 23
0
 /// <summary>
 /// Executes the specified source and returns the resulting output.
 /// </summary>
 /// <param name="input">The source to execute.</param>
 /// <param name="charLimit">The maximum number of characters that can be printed. An exception will be thrown if the limit is exceeded. Set to zero or below for unlimited characters.</param>
 /// <returns></returns>
 public Output Do(Source input, int charLimit = 0)
 {
     return new Interpreter(this, input, new RNG(Seeds.NextRaw()), charLimit).Run();
 }
Exemplo n.º 24
0
 private static bool Pin(Interpreter interpreter, Source source, Stringe tagname, TagArg[] args)
 {
     interpreter.Pin(args[0].GetString());
     return false;
 }
Exemplo n.º 25
0
 public static Subroutine FromTokens(string name, Source derivedSource, IEnumerable<Token<TokenType>> tokens, Tuple<string, TagArgType>[] parameters)
 {
     return new Subroutine(Source.Derived(name, derivedSource, tokens), parameters);
 }
Exemplo n.º 26
0
 private static bool Close(Interpreter interpreter, Source source, Stringe tagname, TagArg[] args)
 {
     interpreter.CurrentState.Output.PopChannel(args[0].GetString());
     return false;
 }
Exemplo n.º 27
0
 /// <summary>
 /// Executes the specified source using a custom seed and returns the resulting output.
 /// </summary>
 /// <param name="input">The source to execute.</param>
 /// <param name="seed">The seed to generate output with.</param>
 /// <param name="charLimit">The maximum number of characters that can be printed. An exception will be thrown if the limit is exceeded. Set to zero or below for unlimited characters.</param>
 /// <returns></returns>
 public Output Do(Source input, long seed, int charLimit = 0)
 {
     return new Interpreter(this, input, new RNG(seed), charLimit).Run();
 }
Exemplo n.º 28
0
 internal static Source Derived(string name, Source source, IEnumerable<Token<TokenType>> tokens)
 {
     return new Source(name, source._type, tokens, source._code);
 }
Exemplo n.º 29
0
 public SourceReader(Source source)
 {
     _source = source;
     _tokens = source.Tokens.ToArray();
     _pos = 0;
 }
Exemplo n.º 30
0
        private static bool Nth(Interpreter interpreter, Source source, Stringe tagname, TagArg[] args)
        {
            int offset, interval;
            if (!Int32.TryParse(args[0].GetString(), out interval))
            {
                throw new ManhoodException(source, tagname, "Invalid interval value.");
            }

            if (interval <= 0)
            {
                throw new ManhoodException(source, tagname, "Interval must be greater than zero.");
            }

            if (!Int32.TryParse(args[1].GetString(), out offset))
            {
                throw new ManhoodException(source, tagname, "Invalid offset value.");
            }

            if (interpreter.CurrentRepeater == null || !interpreter.CurrentRepeater.Nth(offset, interval)) return false;
            interpreter.PushState(State.CreateDerivedDistinct(source, args[2].GetTokens(), interpreter, interpreter.CurrentState.Output));
            return true;
        }