Exemplo n.º 1
0
    public static string Evaluate(string[] instructions)
    {
        var stack = new ForthStack();

        foreach (var instruction in instructions)
        {
            stack.Evaluate(instruction);
        }

        return(stack.ToString());
    }
Exemplo n.º 2
0
        public Forth()
        {
            dStack = new ForthStack<object>();
            aStack = new ForthStack<object>();
            rStack = new ForthStack<int>();

            searchOrder = new ForthStack<Vocabulary>();
            definitions = new Vocabulary("forth");
            definitions.Dict.Add("forth", new ForthDictionaryEntry(definitions));
            searchOrder.Push(definitions);

            compileStack = new ForthStack<CompileState>();

            loopStack = new ForthStack<LoopDeDoo>();
            numericBase = 10;

            memory = new object[1024];
            here = 0;

            isCompiling = false;

            // populate initial dictionary

            Dictionary<string, ForthDictionaryEntry> dict = definitions.Dict;

            foreach (MethodInfo mi in typeof(Forth).GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static))
            {
                object[] obj = mi.GetCustomAttributes(typeof(ForthWordAttribute), false);
                if (obj == null || obj.Length != 1 || !(obj[0] is ForthWordAttribute)) continue;
                ParameterInfo[] pInfo = mi.GetParameters();
                if (pInfo.Length != 1) continue;
                if (pInfo[0].ParameterType != typeof(Forth)) continue;
                if (mi.ReturnType != typeof(void)) continue;
                ForthWordAttribute fwa = obj[0] as ForthWordAttribute;
                ForthDictionaryEntry fde = new ForthDictionaryEntry
                (
                    fwa.Name,
                    Delegate.CreateDelegate
                    (
                        typeof(ExecutionToken),
                        mi
                    ) as ExecutionToken,
                    fwa.IsImmediate
                );
                dict.Add(fde.Name, fde);
            }
        }