Ejemplo n.º 1
0
        private void CompileRepeat(RepeatBlock data)
        {
            string name = ".repeat" + repeats.ToString();

            repeats++;
            Write(name);
            CompileStmtList(data.Statements);
            Write("goto " + name);
        }
Ejemplo n.º 2
0
 public void VisitRepeatBlock(RepeatBlock repeatBlock)
 {
     Console.WriteLine($"begin-repeat {repeatBlock.NumRepeats}");
     foreach (BasicNode node in repeatBlock.Nodes)
     {
         node.Accept(this);
     }
     Console.WriteLine("end-repeat");
 }
Ejemplo n.º 3
0
        /// <inheritdoc />
        public override void CreateModel(int numTweets, int numClasses, int numVocab = 0, bool withEvidence = true, bool withGoldLabels = false)
        {
            IfBlock block = null;

            if (withEvidence)
            {
                this.Evidence = Variable.Bernoulli(0.5).Named("evidence");
                block         = Variable.If(this.Evidence);
            }

            // Biased community model
            base.CreateModel(numTweets, numClasses, numVocab, false, withGoldLabels);

            // Add in the word generation conditioned on true label variable.
            this.WordsInVocabulary = new Range(numVocab).Named("wordsInVocabulary");
            this.ProbWords         = Variable.Array <Vector>(this.Labels).Named("probWords");
            this.ProbWords.SetValueRange(this.WordsInVocabulary);
            this.ProbWords.SetSparsity(Sparsity.Sparse);
            this.WordCount = Variable.Array <int>(this.Tweets).Named("wordCount");
            this.Words     = new Range(this.WordCount[this.Tweets]).Named("words");
            this.Word      = Variable.Array(Variable.Array <int>(this.Words), this.Tweets).Named("word");

            this.ProbWordsPrior         = Variable.Array <Dirichlet>(this.Labels).Named("probWordsPrior");
            this.ProbWords[this.Labels] = Variable <Vector> .Random(this.ProbWordsPrior[this.Labels]);

            this.LikelihoodExponent = Variable.Array <double>(this.Tweets).Named(nameof(this.LikelihoodExponent));

            using (Variable.ForEach(this.Tweets))
            {
                RepeatBlock repeatBlock = null;
                if (UseRepeatFactor)
                {
                    repeatBlock = Variable.Repeat(this.LikelihoodExponent[this.Tweets]);
                }

                using (Variable.Switch(this.TrueLabel[this.Tweets]))
                {
                    this.Word[this.Tweets][this.Words] = Variable.Discrete(this.ProbWords[this.TrueLabel[this.Tweets]]).ForEach(this.Words);
                }

                if (UseRepeatFactor)
                {
                    repeatBlock?.CloseBlock();
                }
            }

            if (withEvidence)
            {
                block.CloseBlock();
            }

            this.HasEvidence = withEvidence;
        }
Ejemplo n.º 4
0
        public void VisitRepeatBlock(RepeatBlock repeatBlock)
        {
            int numIterations = 0;

            while (numIterations < repeatBlock.NumRepeats)
            {
                foreach (BasicNode node in repeatBlock.Nodes)
                {
                    node.Accept(this);
                }

                numIterations++;
            }
        }
Ejemplo n.º 5
0
        public void RepeatBlockTestExecute2()
        {
            var signalBlock = new ImportFromTextBlock {
                Text = "1, 9, 0, 1, 2, 5, -4, 4"
            };
            var repeatBlock = new RepeatBlock {
                FrameSize = 4, RepetitionCount = 1
            };

            signalBlock.ConnectTo(repeatBlock);
            signalBlock.Execute();

            Console.WriteLine(repeatBlock.OutputNodes[0].Object[0].ToString(0));
            //Output: 1 9 0 1 1 9 0 1 2 5 -4 4 2 5 -4 4
            Assert.AreEqual("1 9 0 1 1 9 0 1 2 5 -4 4 2 5 -4 4", repeatBlock.OutputNodes[0].Object[0].ToString(0));
        }
Ejemplo n.º 6
0
        public void RepeatBlockTestExecute()
        {
            var signalBlock = new ImportFromTextBlock {
                ColumnSeparator = " "
            };
            var block = new RepeatBlock();

            block.Execute();

            signalBlock.ConnectTo(block);
            Assert.IsNotNull(block.Name);
            Assert.IsNotNull(block.Description);
            Assert.IsNotNull(block.ProcessingType);

            block.RepetitionCount = 2;
            block.FrameSize       = 3;
            signalBlock.Text      = "1 2 3 4";
            signalBlock.Execute();
            Assert.AreEqual("1 2 3 1 2 3 1 2 3 4 4 4", block.OutputNodes[0].Object.ToString(0));

            var block2 = (RepeatBlock)block.Clone();

            block2.RepetitionCount = 3;
            signalBlock.Text       = "1 2";
            block.ConnectTo(block2);
            signalBlock.Execute();
            Assert.AreEqual("1 2 1 2 1 2", block.OutputNodes[0].Object.ToString(0));
            Assert.AreEqual("1 2 1 1 2 1 1 2 1 1 2 1 2 1 2 2 1 2 2 1 2 2 1 2", block2.OutputNodes[0].Object.ToString(0));

            block.Cascade = false;
            block2        = (RepeatBlock)block.Clone();
            block.ConnectTo(block2);
            signalBlock.Execute();
            Assert.AreEqual("", block2.OutputNodes[0].Object.ToString(0, " "));
            Assert.AreEqual(0, block2.OutputNodes[0].Object.Count);
        }
Ejemplo n.º 7
0
        public int Parse(string[] lines, Block block, int i)
        {
            Regex tupleRegex = new Regex(this.tupleExpr);
            Regex numRegex   = new Regex("^[0-9]+$");

            for (; i < lines.Length; i++)
            {
                if (lines[i].StartsWith("\n") || lines[i].StartsWith("\r\n") || string.IsNullOrWhiteSpace(lines[i]))
                {
                    continue;
                }
                if (lines[i].StartsWith(" ") || lines[i].StartsWith("\t"))
                {
                    throw new IncorrectCommandException(i);
                }

                Regex  exprRegex = new Regex("(\\s|\\n|\\t|\\r)+");
                string command   = exprRegex.Replace(lines[i], string.Empty);

                if (command.StartsWith("add"))
                {
                    exprRegex = new Regex("add");
                    string argument = exprRegex.Replace(command, string.Empty, 1);
                    if (!tupleRegex.IsMatch(argument))
                    {
                        throw new IncorrectCommandException(i);
                    }
                    block.AddNode(new Add(argument));
                }
                else if (command.StartsWith("read"))
                {
                    exprRegex = new Regex("read");
                    string argument = exprRegex.Replace(command, string.Empty, 1);
                    if (!tupleRegex.IsMatch(argument))
                    {
                        throw new IncorrectCommandException(i);
                    }
                    block.AddNode(new Read(argument));
                }
                else if (command.StartsWith("take"))
                {
                    exprRegex = new Regex("take");
                    string argument = exprRegex.Replace(command, string.Empty, 1);
                    if (!tupleRegex.IsMatch(argument))
                    {
                        throw new IncorrectCommandException(i);
                    }
                    block.AddNode(new Take(argument));
                }
                else if (command.StartsWith("wait"))
                {
                    exprRegex = new Regex("wait");
                    string argument = exprRegex.Replace(command, string.Empty, 1);
                    if (!numRegex.IsMatch(argument))
                    {
                        throw new IncorrectCommandException(i);
                    }
                    int num = Int32.Parse(argument);
                    block.AddNode(new Wait(num));
                }
                else if (command.StartsWith("begin-repeat"))
                {
                    if (!(block is Script))
                    {
                        throw new IncorrectCommandException(i);
                    }
                    exprRegex = new Regex("begin-repeat");
                    string argument = exprRegex.Replace(command, string.Empty, 1);
                    if (!numRegex.IsMatch(argument))
                    {
                        throw new IncorrectCommandException(i);
                    }
                    int num = Int32.Parse(argument);

                    RepeatBlock rb = new RepeatBlock(num);
                    i = rb.Parse(this, lines, i + 1);
                    block.AddNode(rb);

                    //verify if it reached end (and the loop wasn't closed)
                    if (i == lines.Length)
                    {
                        throw new BlockEndMissingException(i);
                    }
                }
                else if (command.StartsWith("end-repeat"))
                {
                    if (!(block is RepeatBlock))
                    {
                        throw new IncorrectCommandException(i);
                    }
                    exprRegex = new Regex("end-repeat");
                    string remainder = exprRegex.Replace(command, string.Empty, 1);
                    if (remainder != string.Empty)
                    {
                        throw new IncorrectCommandException(i);
                    }
                    return(i);
                }
                else
                {
                    throw new IncorrectCommandException(i);
                }
            }
            return(i);
        }
Ejemplo n.º 8
0
        public List <Stmt> GenerateAST()
        {
            Block currentBlock = new Block();
            var   blockstack   = new Stack <Block>();
            Token tok          = null;
            var   tree         = new List <Stmt>();
            var   running      = true;

            while (running)
            {
                try
                {
                    tok = CurrentToken;
                    CurrentIndex++;
                    if (tok == null)
                    {
                        break;
                    }
                }
                catch { }
                if (currentBlock is IfBlock ifb)
                {
                    if (IfEndsAtThisLine != -1 && tok.Line > IfEndsAtThisLine)
                    {
                        currentBlock.AddStmt(new EndIf());
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                    }
                }

                switch (tok.TokenName)
                {
                case Lexer.Tokens.Call:
                    currentBlock.AddStmt(ParseCall());
                    break;

                case Lexer.Tokens.Label:
                    currentBlock.AddStmt(new Label(tok.TokenValue.TrimEnd(new[] { ':' }).ToLowerInvariant())
                    {
                        Line = tok.Line
                    });
                    CurrentIndex++;
                    break;

                case Lexer.Tokens.Goto:
                {
                    currentBlock.AddStmt(new Goto(CurrentToken.TokenValue.TrimEnd(new[] { ':' }).ToLowerInvariant())
                        {
                            Line = tok.Line
                        });
                    CurrentIndex++;

                    Block block = currentBlock;

                    /* if ( blockstack.Count > 0 && block is Func )
                     * {
                     *   currentBlock = blockstack.Pop();
                     *   currentBlock.AddStmt( block );
                     * }*/
                }
                break;

                case Lexer.Tokens.Event:
                    currentBlock.AddStmt(ParseEvent());
                    break;

                case Lexer.Tokens.FindItem:
                    currentBlock.AddStmt(ParseFindItem());
                    break;
                }

                if (tok.TokenName == Lexer.Tokens.Import)
                {
                    //  Program.imports.Add( ParseImport() );
                }

                else if (tok.TokenName == Lexer.Tokens.Function)
                {
                    Block block = currentBlock;
                    if (blockstack.Count > 0 && block is Func)
                    {
                        currentBlock = blockstack.Pop();
                        currentBlock.AddStmt(block);
                    }

                    Func func = ParseFunc();

                    if (currentBlock != null)
                    {
                        blockstack.Push(currentBlock);
                        currentBlock = func;
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.If)
                {
                    IfBlock ifblock = ParseIf();

                    if (currentBlock != null)
                    {
                        blockstack.Push(currentBlock);
                        currentBlock = ifblock;
                    }
                }

                /* else if ( tok.TokenName == Lexer.Tokens.ElseIf )
                 * {
                 *   ElseIfBlock elseifblock = ParseElseIf();
                 *
                 *   if ( currentBlock != null )
                 *   {
                 *       blockstack.Push( currentBlock );
                 *       currentBlock = elseifblock;
                 *   }
                 * }*/
                else if (tok.TokenName == Lexer.Tokens.Else)
                {
                    if (currentBlock != null)
                    {
                        blockstack.Push(currentBlock);
                        currentBlock = new ElseBlock()
                        {
                            Line = tok.Line
                        };
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.Repeat)
                {
                    if (currentBlock != null)
                    {
                        blockstack.Push(currentBlock);
                        currentBlock = new RepeatBlock()
                        {
                            Line = tok.Line
                        };
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.Set)
                {
                    Assign a = ParseAssign();
                    currentBlock.AddStmt(a);
                }

                /*else if ( tok.TokenName == Lexer.Tokens.Ident )
                 * {
                 *  if ( tokens.Peek().TokenName == Lexer.Tokens.Equal )
                 *  {
                 *      tokens.pos--;
                 *      Assign a = ParseAssign();
                 *      currentBlock.AddStmt( a );
                 *  }
                 *  else if ( tokens.Peek().TokenName == Lexer.Tokens.LeftParan )
                 *  {
                 *      tokens.pos--;
                 *      Call c = ParseCall();
                 *      currentBlock.AddStmt( c );
                 *  }
                 * }*/
                else if (tok.TokenName == Lexer.Tokens.Return)
                {
                    Return r = ParseReturn();
                    currentBlock.AddStmt(r);
                    Block block = currentBlock;

                    if (blockstack.Count > 0 && block is Func)
                    {
                        currentBlock = blockstack.Pop();
                        currentBlock.AddStmt(block);
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.RightBrace)
                {
                    if (currentBlock is Func)
                    {
                        currentBlock.AddStmt(new Return(null));
                        //tree.Add( currentBlock );
                        //currentBlock = null;
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                    }
                    else if (currentBlock is IfBlock || currentBlock is ElseIfBlock || currentBlock is ElseBlock)
                    {
                        currentBlock.AddStmt(new EndIf());
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                    }
                    else if (currentBlock is RepeatBlock)
                    {
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.EOF)
                {
                    if (currentBlock is Func)
                    {
                        currentBlock.AddStmt(new Return(null));
                        //tree.Add( currentBlock );
                        //currentBlock = null;
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                    }
                    tree.Add(currentBlock);
                    running = false;
                }
            }
            return(tree);
        }
Ejemplo n.º 9
0
        public List <Stmt> GenerateAST()
        {
            Block currentBlock = new Block();
            var   blockstack   = new Stack <Block>();
            Token tok          = null;
            var   tree         = new List <Stmt>();
            var   running      = true;

            while (running)
            {
                try
                {
                    tok = CurrentToken;
                    CurrentIndex++;
                    if (tok == null)
                    {
                        break;
                    }
                }
                catch { }
                if (currentBlock is IfBlock ifb)
                {
                    if (BlockEndsAtThisLine != -1 && tok.Line > BlockEndsAtThisLine)
                    {
                        // currentBlock.AddStmt( new EndIf() );
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                        BlockEndsAtThisLine = -1;
                    }
                }
                if (currentBlock is ForBlock || currentBlock is WhileBlock)
                {
                    if (BlockEndsAtThisLine != -1 && tok.Line > BlockEndsAtThisLine)
                    {
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                        BlockEndsAtThisLine = -1;
                    }
                }

                switch (tok.TokenName)
                {
                case Lexer.Tokens.LeftBrace:
                case Lexer.Tokens.Comment:
                case Lexer.Tokens.NewLine:
                    continue;

                case Lexer.Tokens.Call:
                    currentBlock.AddStmt(ParseCall());
                    continue;

                case Lexer.Tokens.Tile:
                    currentBlock.AddStmt(ParseTile());
                    continue;

                case Lexer.Tokens.Scanjournal:
                    currentBlock.AddStmt(ParseScanJournal());
                    continue;

                case Lexer.Tokens.ExEvent:
                    currentBlock.AddStmt(ParseExEvent());
                    continue;

                case Lexer.Tokens.Target:
                    currentBlock.AddStmt(ParseTarget());
                    continue;

                case Lexer.Tokens.Msg:
                    currentBlock.AddStmt(ParseMessage());
                    continue;

                case Lexer.Tokens.Move:
                    currentBlock.AddStmt(ParseMove());
                    continue;

                case Lexer.Tokens.Menu:
                    currentBlock.AddStmt(ParseMenu());
                    continue;

                case Lexer.Tokens.Click:
                    currentBlock.AddStmt(ParseClick());
                    continue;

                case Lexer.Tokens.Pause:
                    currentBlock.AddStmt(new PauseStmt()
                    {
                        Line = tok.Line
                    });
                    continue;

                case Lexer.Tokens.Continue:
                    currentBlock.AddStmt(new Continue()
                    {
                        Line = tok.Line
                    });
                    continue;

                case Lexer.Tokens.IgnoreItem:
                    currentBlock.AddStmt(ParseIgnoreItem());
                    continue;

                case Lexer.Tokens.LinesPerCycle:
                    currentBlock.AddStmt(new LinesPerCycle(ParseExpr())
                    {
                        Line = tok.Line
                    });
                    continue;

                case Lexer.Tokens.Wait:
                    currentBlock.AddStmt(new WaitStmt(ParseExpr())
                    {
                        Line = tok.Line
                    });
                    continue;

                case Lexer.Tokens.Label:
                    currentBlock.AddStmt(new Label(tok.TokenValue.TrimEnd(new[] { ':' }).ToLowerInvariant())
                    {
                        Line = tok.Line
                    });
                    CurrentIndex++;
                    continue;

                case Lexer.Tokens.Break:
                    currentBlock.AddStmt(new Break()
                    {
                        Line = tok.Line
                    });
                    CurrentIndex++;
                    continue;

                case Lexer.Tokens.Goto:
                {
                    currentBlock.AddStmt(new Goto(CurrentToken.TokenValue.TrimEnd(new[] { ':' }).ToLowerInvariant())
                        {
                            Line = tok.Line
                        });
                    CurrentIndex++;

                    Block block = currentBlock;

                    /* if ( blockstack.Count > 0 && block is Func )
                     * {
                     *   currentBlock = blockstack.Pop();
                     *   currentBlock.AddStmt( block );
                     * }*/
                }
                    continue;

                case Lexer.Tokens.Event:
                    currentBlock.AddStmt(ParseEvent());
                    continue;

                case Lexer.Tokens.FindItem:
                    currentBlock.AddStmt(ParseFindItem());
                    continue;
                }

                if (tok.TokenName == Lexer.Tokens.Import)
                {
                    //  Program.imports.Add( ParseImport() );
                }

                else if (tok.TokenName == Lexer.Tokens.Function)
                {
                    Block block = currentBlock;
                    if (blockstack.Count > 0 && block is Func)
                    {
                        currentBlock = blockstack.Pop();
                        currentBlock.AddStmt(block);
                    }

                    Func func = ParseFunc();

                    if (currentBlock != null)
                    {
                        blockstack.Push(currentBlock);
                        currentBlock = func;
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.If)
                {
                    IfBlock ifblock = ParseIf();

                    if (currentBlock != null)
                    {
                        blockstack.Push(currentBlock);
                        currentBlock = ifblock;
                    }
                }

                /* else if ( tok.TokenName == Lexer.Tokens.ElseIf )
                 * {
                 *   ElseIfBlock elseifblock = ParseElseIf();
                 *
                 *   if ( currentBlock != null )
                 *   {
                 *       blockstack.Push( currentBlock );
                 *       currentBlock = elseifblock;
                 *   }
                 * }*/
                else if (tok.TokenName == Lexer.Tokens.Else)
                {
                    if (currentBlock != null)
                    {
                        blockstack.Push(currentBlock);
                        currentBlock = new ElseBlock()
                        {
                            Line = tok.Line
                        };
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.For)
                {
                    blockstack.Push(currentBlock);
                    currentBlock = ParseFor();
                }
                else if (tok.TokenName == Lexer.Tokens.While)
                {
                    blockstack.Push(currentBlock);
                    currentBlock = ParseWhile();
                }
                else if (tok.TokenName == Lexer.Tokens.Repeat)
                {
                    if (currentBlock != null)
                    {
                        blockstack.Push(currentBlock);
                        currentBlock = new RepeatBlock()
                        {
                            Line = tok.Line
                        };
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.Set)
                {
                    Assign a = ParseAssign();
                    currentBlock.AddStmt(a);
                }

                /*else if ( tok.TokenName == Lexer.Tokens.Ident )
                 * {
                 *  if ( tokens.Peek().TokenName == Lexer.Tokens.Equal )
                 *  {
                 *      tokens.pos--;
                 *      Assign a = ParseAssign();
                 *      currentBlock.AddStmt( a );
                 *  }
                 *  else if ( tokens.Peek().TokenName == Lexer.Tokens.LeftParan )
                 *  {
                 *      tokens.pos--;
                 *      Call c = ParseCall();
                 *      currentBlock.AddStmt( c );
                 *  }
                 * }*/
                else if (tok.TokenName == Lexer.Tokens.Return)
                {
                    Return r = ParseReturn();
                    currentBlock.AddStmt(r);
                    Block block = currentBlock;

                    if (blockstack.Count > 0 && block is Func)
                    {
                        currentBlock = blockstack.Pop();
                        currentBlock.AddStmt(block);
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.RightBrace)
                {
                    if (currentBlock is Func)
                    {
                        currentBlock.AddStmt(new Return(null));
                        //tree.Add( currentBlock );
                        //currentBlock = null;
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                    }
                    else if (currentBlock is IfBlock || currentBlock is ElseIfBlock || currentBlock is ElseBlock)
                    {
                        //currentBlock.AddStmt( new EndIf() );
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                    }
                    else if (currentBlock is RepeatBlock)
                    {
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                    }
                    else if (currentBlock is ForBlock || currentBlock is WhileBlock)
                    {
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                    }
                }
                else if (tok.TokenName == Lexer.Tokens.EOF)
                {
                    if (currentBlock is Func)
                    {
                        currentBlock.AddStmt(new Return(null));
                        //tree.Add( currentBlock );
                        //currentBlock = null;
                        Block block = currentBlock;

                        if (blockstack.Count > 0)
                        {
                            currentBlock = blockstack.Pop();
                            currentBlock.AddStmt(block);
                        }
                    }
                    tree.Add(currentBlock);
                    running = false;
                }
                else
                {
                    Console.WriteLine($"Unhandled Token at Line: {tok.Line} " + tok.TokenName + " " + tok.TokenValue);
                }
            }
            return(tree);
        }