// ( module names -- ) public override void Execute(Interpreter interp) { ArrayItem names = (ArrayItem)interp.StackPop(); Module module = (Module)interp.StackPop(); foreach (StackItem item in names.ArrayValue) { string name, newName; if (item is ArrayItem) { ArrayItem arrayItem = (ArrayItem)item; name = ((StringItem)arrayItem.ArrayValue[0]).StringValue; newName = ((StringItem)arrayItem.ArrayValue[1]).StringValue; } else { name = ((StringItem)item).StringValue; newName = name; } Word word; if (!module.TryFindWord(name, out word)) { string msg = String.Format("Can't find word {0} in module '{1}'", name, module.Name); throw new InvalidOperationException(msg); } word.Text = newName; interp.CurModule().AddWord(word); } }
// ( items n -- items[n] ) public override void Execute(Interpreter interp) { IntItem n = (IntItem)interp.StackPop(); ArrayItem items = (ArrayItem)interp.StackPop(); interp.StackPush(items.ArrayValue[n.IntValue]); }
// ( list-of-lists -- list ) public override void Execute(Interpreter interp) { ArrayItem list_of_lists = (ArrayItem)interp.StackPop(); List <StackItem> result = new List <StackItem>(); flatten(list_of_lists, result); interp.StackPush(new ArrayItem(result)); }
// ( items field -- sorted-items ) public override void Execute(Interpreter interp) { StringItem field = (StringItem)interp.StackPop(); ArrayItem items = (ArrayItem)interp.StackPop(); List <StackItem> item_list = (List <StackItem>)items.ArrayValue; sort(item_list, field.StringValue); interp.StackPush(new ArrayItem(item_list)); }
// ( [a b ...] -- a b ... ) public override void Execute(Interpreter interp) { ArrayItem items = (ArrayItem)interp.StackPop(); foreach (StackItem item in items.ArrayValue) { interp.StackPush(item); } }
// ( names -- ) public override void Execute(Interpreter interp) { ArrayItem names = (ArrayItem)interp.StackPop(); foreach (StringItem item in names.ArrayValue) { interp.CurModule().AddVariableIfMissing(item.StringValue); } }
// ( modules -- ) public override void Execute(Interpreter interp) { ArrayItem modules = (ArrayItem)interp.StackPop(); foreach (Module m in modules.ArrayValue) { interp.UseModule(m); } }
// ( a b -- [ a b ] ) public override void Execute(Interpreter interp) { StackItem b = interp.StackPop(); StackItem a = interp.StackPop(); ArrayItem result = new ArrayItem(); result.Add(a); result.Add(b); interp.StackPush(result); }
// ( items forthic -- ) public override void Execute(Interpreter interp) { StringItem forthic = (StringItem)interp.StackPop(); ArrayItem items = (ArrayItem)interp.StackPop(); foreach (var item in items.ArrayValue) { interp.StackPush(item); interp.Run(forthic.StringValue); } }
void flatten(ArrayItem items, List <StackItem> res) { foreach (StackItem item in items.ArrayValue) { if (item.GetType() == typeof(ArrayItem)) { flatten((ArrayItem)item, res); } else { res.Add(item); } } }
// ( values fields -- record ) public override void Execute(Interpreter interp) { ArrayItem fields = (ArrayItem)interp.StackPop(); ArrayItem values = (ArrayItem)interp.StackPop(); RecordItem result = new RecordItem(); for (int i = 0; i < fields.ArrayValue.Count; i++) { StringItem keyItem = (StringItem)fields.ArrayValue[i]; result.SetValue(keyItem.StringValue, values.ArrayValue[i]); } interp.StackPush(result); }
// ( words -- ) public override void Execute(Interpreter interp) { ArrayItem words = (ArrayItem)interp.StackPop(); foreach (StringItem item in words.ArrayValue) { Word word; if (interp.TryFindWord(item.StringValue, out word)) { interp.CurModulePredecessor().AddWord(word); } else { throw new InvalidOperationException(String.Format("Can't find {0} to publish", item.StringValue)); } } }
// ( array forthic -- ? ) public override void Execute(Interpreter interp) { StringItem forthic = (StringItem)interp.StackPop(); ArrayItem indices = (ArrayItem)interp.StackPop(); if (indices.ArrayValue.Count == 2) { IntItem i_max = (IntItem)indices.ArrayValue[0]; IntItem j_max = (IntItem)indices.ArrayValue[1]; for (var i = 0; i < i_max.IntValue; i++) { for (var j = 0; j < j_max.IntValue; j++) { interp.StackPush(new IntItem(i)); interp.StackPush(new IntItem(j)); interp.Run(forthic.StringValue); } } } }
IntItem length(ArrayItem items) { return(new IntItem(items.ArrayValue.Count)); }
// ( items -- length ) public override void Execute(Interpreter interp) { ArrayItem items = (ArrayItem)interp.StackPop(); interp.StackPush(length(items)); }