From c3b06ae5e3857e425808b0a83349244ec7eea630 Mon Sep 17 00:00:00 2001 From: otya128 Date: Tue, 27 Dec 2016 21:03:48 +0900 Subject: [PATCH] Implement PRGEDIT,PRGGET$() --- SMILEBASIC/builtinfunctions.d | 16 ++++++++++++ SMILEBASIC/program.d | 48 ++++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/SMILEBASIC/builtinfunctions.d b/SMILEBASIC/builtinfunctions.d index df105c8..c3f4d68 100644 --- a/SMILEBASIC/builtinfunctions.d +++ b/SMILEBASIC/builtinfunctions.d @@ -3158,6 +3158,22 @@ class BuiltinFunction auto dialog = new Dialog(p); p.project.result = cast(DialogResult)dialog.show(text, cast(SelectionType)selType, cast(wstring)cap, cast(int)timeout); } + static void PRGEDIT(PetitComputer p, int slot) + { + PRGEDIT(p, slot, 1); + } + static void PRGEDIT(PetitComputer p, int slot, int line) + { + if (slot < 0 || p.program.slotSize <= slot) + throw new OutOfRange("PRGEDIT", 1); + if (line < -1 || line == 0) + throw new OutOfRange("PRGEDIT"/*, 2*/); + p.program.edit(slot, line); + } + static wstring PRGGET(PetitComputer p) + { + return p.program.get(); + } //alias void function(PetitComputer, Value[], Value[]) BuiltinFunc; static BuiltinFunctions[wstring] builtinFunctions; static wstring getBasicName(BFD)(const wstring def) diff --git a/SMILEBASIC/program.d b/SMILEBASIC/program.d index 12e97de..b7e55a2 100644 --- a/SMILEBASIC/program.d +++ b/SMILEBASIC/program.d @@ -1,9 +1,11 @@ module otya.smilebasic.program; +import std.range; import std.container; struct Slot { DList!wstring program; + DList!wstring.Range range; void load(wstring data) { import std.algorithm; @@ -12,6 +14,11 @@ struct Slot { program.insertBack(l ~ '\n'); } + if (program.back == "\n") + { + program.removeBack(); + } + range = program[]; } wchar[] text() { @@ -40,16 +47,55 @@ struct Slot } return ""; } + void currentLine(int line) + { + if (line == -1) + { + //range = DList!wstring.Range(program._last); + range = program[]; + while (!range.empty) + { + auto old = range.save; + range.popFront(); + if (range.empty) + { + range = old; + break; + } + } + return; + } + range = program[]; + range.popFrontN(line - 1); + } + wstring get() + { + if (range.empty) + { + return ""; + } + auto a = range.front; + range.popFront(); + return a; + } } class Program { Slot[] slot; int currentSlot; - int currentLine = 1; int slotSize = 4; this() { slot = new Slot[5]; } + void edit(int slot, int line) + { + this.currentSlot = slot; + this.slot[slot].currentLine = line; + } + wstring get() + { + return this.slot[currentSlot].get; + } }