Beispiel #1
0
            internal override ExitType Eval(Tokenizer _ = null, WordListBuilder wlb = null)
            {
                if (Stack.Pop() != 0)
                {
                    var retThen = _wlThen.Eval();

                    if (retThen != ExitType.Okay)
                    {
                        return(retThen);
                    }
                }
                else
                {
                    if (_wlElse != null)
                    {
                        var retElse = _wlElse.Eval();

                        if (retElse != ExitType.Okay)
                        {
                            return(retElse);
                        }
                    }
                }

                return(ExitType.Okay);
            }
Beispiel #2
0
            internal override ExitType Eval(Tokenizer _ = null, WordListBuilder wlb = null)
            {
                while (true)
                {
                    ExitType et;
                    if (_evalCond != null)
                    {
                        et = _evalCond.Eval();

                        if (et != ExitType.Okay)
                        {
                            return(et);
                        }
                        if (Stack.Pop() == 0)
                        {
                            return(ExitType.Okay);
                        }
                    }
                    et = _wlBegin.Eval();
                    if (et != ExitType.Okay)
                    {
                        return(et);
                    }

                    if (_isUntil)
                    {
                        if (Stack.Pop() != 0)
                        {
                            return(ExitType.Okay);
                        }
                    }
                }
            }
Beispiel #3
0
            internal override ExitType Eval(Tokenizer tokenizer = null, WordListBuilder _ = null)
            {
                _iLoop = LoopIndices.Count;
                ExitType et;
                var      iStart = Stack.Pop();
                var      iEnd   = Stack.Pop();

                if (_isQuestDo && iStart == iEnd)
                {
                    return(ExitType.Okay);
                }

                LoopIndices.Push(iStart);

                // Break into two nearly identical cases for speed
                if (iEnd > iStart)
                {
                    do
                    {
                        et = _wlDo.Eval();
                        if (et != ExitType.Okay)
                        {
                            return(et == ExitType.Leave ? ExitType.Okay : et);
                        }

                        if (_plusLoop)
                        {
                            LoopIndices[_iLoop] += Stack.Pop();
                        }
                        else
                        {
                            ++LoopIndices[_iLoop];
                        }
                    } while (LoopIndices[_iLoop] < iEnd);
                }
                else
                {
                    do
                    {
                        et = _wlDo.Eval();
                        if (et != ExitType.Okay)
                        {
                            return(et == ExitType.Leave ? ExitType.Okay : et);
                        }

                        if (_plusLoop)
                        {
                            LoopIndices[_iLoop] += Stack.Pop();
                        }
                        else
                        {
                            --LoopIndices[_iLoop];
                        }
                    } while (LoopIndices[_iLoop] > iEnd);
                }

                LoopIndices.Pop();
                return(ExitType.Okay);
            }
Beispiel #4
0
        internal override ExitType Eval(Tokenizer tokenizer = null, WordListBuilder wlb = null)
        {
            var passedParms = GetPassedParms();
            var result      = _constructor.Invoke(passedParms);

            CallAction.HandleResult(TRet, result);
            return(ExitType.Okay);
        }
Beispiel #5
0
        internal override ExitType Eval(Tokenizer tokenizer = null, WordListBuilder wlb = null)
        {
            var obj         = _isStatic ? null : Session.GetManagedObject(Session.Stack.Pop());
            var passedParms = GetPassedParms();
            var result      = _method.Invoke(obj, passedParms);

            CallAction.HandleResult(TRet, result);
            return(ExitType.Okay);
        }
Beispiel #6
0
        internal static void Definition(Tokenizer tokenizer)
        {
            var       wlb            = new WordListBuilder();
            string    currentDefWord = tokenizer.NextToken().ToLower();
            Evaluable execute        = null;

            while (true)
            {
                var word = tokenizer.NextToken().ToLower();

                if (word == "does>")
                {
                    execute = wlb.Realize(true, currentDefWord);
                    wlb.Clear();
                    continue;
                }

                if (word == ";")
                {
                    if (execute == null)
                    {
                        Vocabulary.CurrentVocabulary.AddDefinition(currentDefWord, wlb.Realize(true, currentDefWord));
                    }
                    else
                    {
                        var run     = wlb.Realize(true, currentDefWord);
                        var compile = execute;
                        Vocabulary.CurrentVocabulary.AddDefinition(currentDefWord, new DoesWord(compile, run));
                    }

                    Session.LastDefinedWord = null;
                    return;
                }

                var evaluable = Interpreter.ParseWord(word);
                if (evaluable == null)
                {
                    // TODO: get more robust error handling
                    throw new NfException($"Couldn't locate word {word}");
                }

                if (evaluable.IsImmediate)
                {
                    evaluable.Eval(tokenizer, wlb);
                    continue;
                }

                wlb.Add(evaluable);
            }
        }
Beispiel #7
0
        internal static void Begin(Tokenizer tokenizer, WordListBuilder wlbParent)
        {
            var       wlbBegin = new WordListBuilder();
            Evaluable evalCond = null;

            while (true)
            {
                var word = tokenizer.NextToken().ToLower();

                switch (word)
                {
                case "again":
                    wlbParent.Add(new BeginPrim(wlbBegin.Realize()));
                    return;

                case "until":
                    wlbParent.Add(new BeginPrim(wlbBegin.Realize(), true));
                    return;

                case "while":
                    evalCond = wlbBegin.Realize();
                    wlbBegin.Clear();
                    continue;

                case "repeat" when evalCond == null:
                    throw new NfException("\"begin...repeat\" with no while");

                case "repeat":
                    wlbParent.Add(new BeginPrim(wlbBegin.Realize(), false, evalCond));
                    return;
                }

                var evaluable = Interpreter.ParseWord(word);
                if (evaluable == null)
                {
                    // TODO: get more robust error handling
                    throw new NfException($"Couldn't locate word {word}");
                }

                if (evaluable.IsImmediate)
                {
                    // Only runs at compile time so no need to supply a parent here.
                    evaluable.Eval(tokenizer, wlbBegin);
                    continue;
                }
                wlbBegin.Add(evaluable);
            }
        }
Beispiel #8
0
        private void OnRefreshWords(object sender, EventArgs e)
        {
            _comboBoxSearchValue.DataSource = null;
            StringCollection indexDirs = GetIndexDirectories(_textBoxSearchIndexPath.Text, true);

            if (indexDirs == null)
            {
                return;
            }

            string searchWord = _comboBoxSearchValue.Text;

            if (string.IsNullOrEmpty(searchWord))
            {
                searchWord = "a";
            }

            List <string> words = new List <string>();

            using (WordListBuilder wordsBuilder = new WordListBuilder())
            {
                Dictionary <string, int> wordsMap = new Dictionary <string, int>();
                foreach (string dir in indexDirs)
                {
                    if (wordsBuilder.OpenIndex(dir))
                    {
                        wordsBuilder.ListWords(searchWord, 5);
                        for (int i = 0; i < wordsBuilder.Count; ++i)
                        {
                            wordsMap[wordsBuilder.GetNthWord(i)] = 1;
                        }
                    }
                }

                foreach (KeyValuePair <string, int> kv in wordsMap)
                {
                    words.Add(kv.Key);
                }
            }

            _comboBoxSearchValue.DataSource = words;
        }
Beispiel #9
0
        public List <WordItem> GetResults(string indexPath, string segment)
        {
            var wordListBuilder = new WordListBuilder();

            wordListBuilder.OpenIndex(indexPath);
            wordListBuilder.ListWords(segment, 4);

            var words = new List <WordItem>();

            for (int i = 0; i < wordListBuilder.Count; ++i)
            {
                var item = new WordItem();
                item.MakeFromWordListBuilder(wordListBuilder, i);
                if (item.HitCount > 0)
                {
                    words.Add(item);
                }
            }
            return(words);
        }
Beispiel #10
0
            internal override ExitType Eval(Tokenizer tokenizer = null, WordListBuilder wlb = null)
            {
                Session.LastDefinedWord = null;
                _compile.Eval(tokenizer, wlb);
                if (Session.LastDefinedWord != null)
                {
                    var intPrim = Vocabulary.Lookup(Session.LastDefinedWord) as IntPrim;
                    if (intPrim == null)
                    {
                        throw new NfException("LastDefinedWord was not defined as an IntPrim");
                    }

                    List <Evaluable> newDefWords = new List <Evaluable>()
                    {
                        intPrim, _run
                    };
                    Vocabulary.CurrentVocabulary.AddDefinition(Session.LastDefinedWord, new WordList(newDefWords));
                    Session.LastDefinedWord = null;
                }
                return(ExitType.Okay);
            }
        internal override ExitType Eval(Tokenizer tokenizer = null, WordListBuilder wlb = null)
        {
            var passedParms = new object[_tParms.Length];

            for (var i = _tParms.Length - 1; i >= 0; i--)
            {
                var val = Stack.Pop();
                if (_tParms[i] == typeof(int))
                {
                    passedParms[i] = val;
                }
                else
                {
                    passedParms[i] = DotNetObjects[val];
                }
            }

            var result = _method.Invoke(_obj, passedParms);

            CallAction.HandleResult(_tRet, result);
            return(ExitType.Okay);
        }
Beispiel #12
0
        internal static void If(Tokenizer tokenizer, WordListBuilder wlbParent)
        {
            var wlbThenClause = new WordListBuilder();
            var wlbElseClause = (WordListBuilder)null;

            while (true)
            {
                var word = tokenizer.NextToken().ToLower();

                // ReSharper disable once SwitchStatementMissingSomeCases
                switch (word)
                {
                case "then":
                    wlbParent.Add(new IfPrim(wlbThenClause.Realize(), wlbElseClause?.Realize()));
                    return;

                case "else":
                    wlbElseClause = wlbThenClause;
                    wlbThenClause = new WordListBuilder();
                    continue;
                }

                var evaluable = Interpreter.ParseWord(word);
                if (evaluable == null)
                {
                    // TODO: get more robust error handling
                    throw new NfException($"Couldn't locate word {word}");
                }

                if (evaluable.IsImmediate)
                {
                    evaluable.Eval(tokenizer, wlbThenClause);
                    continue;
                }

                wlbThenClause.Add(evaluable);
            }
        }
Beispiel #13
0
        internal static void Do(Tokenizer tokenizer, WordListBuilder wlbParent, bool isQuestDo)
        {
            var _wlbDo = new WordListBuilder();

            while (true)
            {
                var word = tokenizer.NextToken().ToLower();

                if (word == "loop")
                {
                    wlbParent.Add(new DoPrim(_wlbDo.Realize(), false, isQuestDo));
                    return;
                }

                if (word == "+loop")
                {
                    wlbParent.Add(new DoPrim(_wlbDo.Realize(), true, isQuestDo));
                    return;
                }

                var evaluable = Interpreter.ParseWord(word);
                if (evaluable == null)
                {
                    // TODO: get more robust error handling
                    throw new NfException($"Couldn't locate word {word}");
                }

                if (evaluable.IsImmediate)
                {
                    evaluable.Eval(tokenizer, _wlbDo);
                    continue;
                }

                _wlbDo.Add(evaluable);
            }
        }
Beispiel #14
0
 public WordListBuilderTest()
 {
     this.wordListPathConfigurationProvider = A.Fake <IWordListPathConfigurationProvider>();
     this.wordFileReader = A.Fake <IWordFileReader>();
     this.builder        = new WordListBuilder(this.wordListPathConfigurationProvider, this.wordFileReader);
 }
Beispiel #15
0
 internal override ExitType Eval(Tokenizer tokenizer = null, WordListBuilder wlb = null)
 {
     Stack.Push(Value);
     return(ExitType.Okay);
 }
Beispiel #16
0
 // TODO: Consider making tokenizer and WordListBuilder static and taking them out as parameters in Eval for performance
 internal virtual ExitType Eval(Tokenizer tokenizer = null, WordListBuilder wlb = null)
 {
     return(ExitType.Okay);
 }
Beispiel #17
0
 internal override ExitType Eval(Tokenizer _ = null, WordListBuilder wlb = null)
 {
     return(_action());
 }
Beispiel #18
0
        internal static void FString(Tokenizer tokenizer, WordListBuilder wlbParent, StringType st = StringType.Forth)
        {
            var sbText       = new StringBuilder();
            var prependSpace = false;

            var done = false;

            // TODO: read tokenizer char by char to properly emulate white space
            while (!done)
            {
                var word = tokenizer.NextToken();

                if (word.EndsWith("\""))
                {
                    done = true;
                    word = word.Substring(0, word.Length - 1);
                }

                if (prependSpace)
                {
                    sbText.Append(" ");
                }

                prependSpace = true;
                sbText.Append(word);
            }

            var text       = sbText.ToString();
            int finalValue = -1;

            switch (st)
            {
            case StringType.Forth:
                var pCountedString = Memory.Allocate(text.Length * sizeof(char) + Session.StringLengthSize);
                Memory.StoreCString(pCountedString, text);
                finalValue = pCountedString;
                break;

            case StringType.DotNet:
                var index = SaveManagedObject(text);
                finalValue = index;
                break;

            case StringType.Type:
                var type = Type.GetType(text);
                if (type == null)
                {
                    throw new NfException($"Unrecognized .NET type in t\": {text}");
                }

                if (type.IsGenericType)
                {
                    var genericParameterCount = type.GetGenericArguments().Length;
                    var parmTypes             = CallAction.GetParmTypes($"Invalid types for generic type {text}", genericParameterCount);
                    type = type.MakeGenericType(parmTypes);
                }
                finalValue = SaveManagedObject(type);
                break;

            case StringType.Stack:
                var pStackString = Memory.AllocateStackString(text.Length);
                Memory.StoreString(pStackString, text);
                finalValue = pStackString;
                break;
            }
            if (wlbParent == null)
            {
                Stack.Push(finalValue);
                if (st == StringType.Stack)
                {
                    Stack.Push(text.Length);
                }
            }
            else
            {
                wlbParent.Add(new IntPrim(finalValue));
                if (st == StringType.Stack)
                {
                    wlbParent.Add(new IntPrim(text.Length));
                }
            }
        }
Beispiel #19
0
 public void MakeFromWordListBuilder(WordListBuilder wordListBuilder, int iItem)
 {
     Word     = wordListBuilder.GetNthWord(iItem);
     HitCount = wordListBuilder.GetNthWordCount(iItem);
     Detail   = " " + wordListBuilder.GetNthWordCount(iItem) + " hits in " + wordListBuilder.GetNthWordDocCount(iItem) + " documents";
 }
Beispiel #20
0
 internal override ExitType Eval(Tokenizer tokenizer = null, WordListBuilder wlb = null)
 {
     _compileAction(tokenizer, wlb);
     return(ExitType.Okay);
 }
Beispiel #21
0
 internal override ExitType Eval(Tokenizer tokenizer = null, WordListBuilder _ = null)
 {
     _action(tokenizer);
     return(ExitType.Okay);
 }