2016-12-27 17:16:54 +09:00
|
|
|
module otya.smilebasic.program;
|
2016-12-27 23:24:33 +09:00
|
|
|
import otya.smilebasic.error;
|
2016-12-27 21:03:48 +09:00
|
|
|
import std.range;
|
2016-12-27 17:16:54 +09:00
|
|
|
import std.container;
|
2016-12-27 23:01:28 +09:00
|
|
|
import std.algorithm;
|
2016-12-27 23:24:33 +09:00
|
|
|
import std.algorithm;
|
2016-12-27 17:16:54 +09:00
|
|
|
|
2016-12-28 15:49:44 +09:00
|
|
|
unittest
|
|
|
|
|
{
|
|
|
|
|
Slot slot;
|
|
|
|
|
slot.init();
|
|
|
|
|
slot.currentLine = 1;
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
slot.currentLine = 1;
|
|
|
|
|
slot.set("1");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
slot.currentLine = 1;
|
|
|
|
|
assert(slot.get() == "1\n");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
slot.currentLine = 2;
|
|
|
|
|
slot.set("2");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
slot.currentLine = 1;
|
|
|
|
|
assert(slot.get() == "1\n");
|
|
|
|
|
assert(slot.get() == "2\n");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
slot.currentLine = 1;
|
|
|
|
|
slot.set("A");
|
|
|
|
|
assert(slot.get() == "2\n");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
slot.currentLine = 1;
|
|
|
|
|
assert(slot.get() == "A\n");
|
|
|
|
|
assert(slot.get() == "2\n");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
slot.currentLine = 2;
|
|
|
|
|
slot.set("I\nJ");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
slot.currentLine = 1;
|
|
|
|
|
assert(slot.get() == "A\n");
|
|
|
|
|
assert(slot.get() == "I\n");
|
|
|
|
|
assert(slot.get() == "J\n");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
|
|
|
|
|
//PRGINS
|
|
|
|
|
slot.currentLine = 1;
|
|
|
|
|
slot.insert("3", false);
|
|
|
|
|
assert(slot.get() == "A\n");
|
|
|
|
|
assert(slot.get() == "I\n");
|
|
|
|
|
assert(slot.get() == "J\n");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
slot.currentLine = 1;
|
|
|
|
|
assert(slot.get() == "3\n");
|
|
|
|
|
assert(slot.get() == "A\n");
|
|
|
|
|
assert(slot.get() == "I\n");
|
|
|
|
|
assert(slot.get() == "J\n");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
|
|
|
|
|
slot.currentLine = 1;
|
|
|
|
|
slot.insert("4", true);
|
|
|
|
|
assert(slot.get() == "4\n");
|
|
|
|
|
assert(slot.get() == "A\n");
|
|
|
|
|
assert(slot.get() == "I\n");
|
|
|
|
|
assert(slot.get() == "J\n");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
slot.currentLine = 1;
|
|
|
|
|
assert(slot.get() == "3\n");
|
|
|
|
|
assert(slot.get() == "4\n");
|
|
|
|
|
assert(slot.get() == "A\n");
|
|
|
|
|
assert(slot.get() == "I\n");
|
|
|
|
|
assert(slot.get() == "J\n");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
|
|
|
|
|
slot.currentLine = 2;
|
|
|
|
|
slot.insert("5", false);
|
|
|
|
|
assert(slot.get() == "4\n");
|
|
|
|
|
assert(slot.get() == "A\n");
|
|
|
|
|
assert(slot.get() == "I\n");
|
|
|
|
|
assert(slot.get() == "J\n");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
slot.currentLine = 1;
|
|
|
|
|
assert(slot.get() == "3\n");
|
|
|
|
|
assert(slot.get() == "5\n");
|
|
|
|
|
assert(slot.get() == "4\n");
|
|
|
|
|
assert(slot.get() == "A\n");
|
|
|
|
|
assert(slot.get() == "I\n");
|
|
|
|
|
assert(slot.get() == "J\n");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
|
|
|
|
|
slot.currentLine = 2;
|
|
|
|
|
slot.insert("6", true);
|
|
|
|
|
assert(slot.get() == "6\n");
|
|
|
|
|
assert(slot.get() == "4\n");
|
|
|
|
|
assert(slot.get() == "A\n");
|
|
|
|
|
assert(slot.get() == "I\n");
|
|
|
|
|
assert(slot.get() == "J\n");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
slot.currentLine = 1;
|
|
|
|
|
assert(slot.get() == "3\n");
|
|
|
|
|
assert(slot.get() == "5\n");
|
|
|
|
|
assert(slot.get() == "6\n");
|
|
|
|
|
assert(slot.get() == "4\n");
|
|
|
|
|
assert(slot.get() == "A\n");
|
|
|
|
|
assert(slot.get() == "I\n");
|
|
|
|
|
assert(slot.get() == "J\n");
|
|
|
|
|
assert(slot.get() == "\n");
|
|
|
|
|
assert(slot.get() == "");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-27 17:16:54 +09:00
|
|
|
struct Slot
|
|
|
|
|
{
|
|
|
|
|
DList!wstring program;
|
2016-12-27 21:03:48 +09:00
|
|
|
DList!wstring.Range range;
|
2016-12-28 15:49:44 +09:00
|
|
|
void init()
|
2016-12-27 23:01:28 +09:00
|
|
|
{
|
2016-12-28 15:49:44 +09:00
|
|
|
program = make!(DList!wstring)(["\n"w]);
|
2016-12-27 23:01:28 +09:00
|
|
|
range = program[];
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-27 17:16:54 +09:00
|
|
|
void load(wstring data)
|
|
|
|
|
{
|
|
|
|
|
program.clear();
|
|
|
|
|
foreach(l; splitter(data, "\n"))
|
|
|
|
|
{
|
|
|
|
|
program.insertBack(l ~ '\n');
|
|
|
|
|
}
|
2016-12-27 21:03:48 +09:00
|
|
|
if (program.back == "\n")
|
|
|
|
|
{
|
|
|
|
|
program.removeBack();
|
|
|
|
|
}
|
|
|
|
|
range = program[];
|
2016-12-27 17:16:54 +09:00
|
|
|
}
|
|
|
|
|
wchar[] text()
|
|
|
|
|
{
|
|
|
|
|
import std.algorithm.iteration, std.outbuffer;
|
|
|
|
|
auto buffer = new OutBuffer();
|
|
|
|
|
buffer.reserve(program.opSlice.map!"(a.length + 1) * 2".sum);
|
|
|
|
|
|
|
|
|
|
foreach(line; program)
|
|
|
|
|
{
|
|
|
|
|
buffer.write(line);
|
|
|
|
|
}
|
|
|
|
|
ubyte[] progbuff = buffer.toBytes;
|
|
|
|
|
wchar[] progbuff2 = (cast(wchar*)progbuff.ptr)[0..progbuff.length / 2];
|
|
|
|
|
return progbuff2;
|
|
|
|
|
}
|
|
|
|
|
import otya.smilebasic.token;
|
|
|
|
|
wstring getLine(SourceLocation loc)
|
|
|
|
|
{
|
|
|
|
|
import std.string;
|
|
|
|
|
int i;
|
|
|
|
|
foreach (line; program)
|
|
|
|
|
{
|
|
|
|
|
i++;
|
|
|
|
|
if (i == loc.line)
|
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
2016-12-27 21:03:48 +09:00
|
|
|
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;
|
|
|
|
|
}
|
2016-12-27 23:01:28 +09:00
|
|
|
void set(wstring v)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
foreach(l; splitter(v, "\n"))
|
|
|
|
|
{
|
|
|
|
|
if (!range.empty && i == 0)
|
|
|
|
|
{
|
2016-12-28 15:49:44 +09:00
|
|
|
auto b = range.save;
|
|
|
|
|
b.popFront();
|
|
|
|
|
if (b.empty)
|
|
|
|
|
{
|
|
|
|
|
program.insertBefore(range, l ~ "\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
range.put(l ~ "\n");
|
|
|
|
|
}
|
2016-12-27 23:01:28 +09:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (range.empty)
|
|
|
|
|
{
|
|
|
|
|
program.insertAfter(range, l ~ "\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
program.insertBefore(range, l ~ "\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-28 15:49:44 +09:00
|
|
|
void insert(wstring line, bool isBack)
|
|
|
|
|
{
|
|
|
|
|
typeof(range) backup;
|
|
|
|
|
if (isBack)
|
|
|
|
|
{
|
|
|
|
|
backup = range.save;
|
|
|
|
|
range.popFront();
|
|
|
|
|
}
|
|
|
|
|
foreach(l; splitter(line, "\n"))
|
|
|
|
|
{
|
|
|
|
|
if (isBack)
|
|
|
|
|
{
|
|
|
|
|
//backup = range.save;
|
|
|
|
|
program.insertBefore(range, l ~ "\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
program.insertBefore(range, l ~ "\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (isBack)
|
|
|
|
|
{
|
|
|
|
|
range = backup;
|
|
|
|
|
range.popFront();
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-27 17:16:54 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Program
|
|
|
|
|
{
|
|
|
|
|
Slot[] slot;
|
2016-12-27 23:24:33 +09:00
|
|
|
bool PRGEDITused;
|
2016-12-27 17:16:54 +09:00
|
|
|
int currentSlot;
|
|
|
|
|
int slotSize = 4;
|
|
|
|
|
this()
|
|
|
|
|
{
|
|
|
|
|
slot = new Slot[5];
|
2016-12-27 23:01:28 +09:00
|
|
|
foreach(ref s; slot)
|
|
|
|
|
{
|
2016-12-28 15:49:44 +09:00
|
|
|
s.init();
|
2016-12-27 23:01:28 +09:00
|
|
|
}
|
2016-12-27 17:16:54 +09:00
|
|
|
}
|
2016-12-27 21:03:48 +09:00
|
|
|
void edit(int slot, int line)
|
|
|
|
|
{
|
2016-12-27 23:24:33 +09:00
|
|
|
PRGEDITused = true;
|
2016-12-27 21:03:48 +09:00
|
|
|
this.currentSlot = slot;
|
|
|
|
|
this.slot[slot].currentLine = line;
|
|
|
|
|
}
|
|
|
|
|
wstring get()
|
|
|
|
|
{
|
2016-12-27 23:24:33 +09:00
|
|
|
if (!PRGEDITused)
|
|
|
|
|
throw new UsePRGEDITBeforeAnyPRGFunction("PRGGET$");
|
2016-12-27 21:03:48 +09:00
|
|
|
return this.slot[currentSlot].get;
|
|
|
|
|
}
|
2016-12-27 23:01:28 +09:00
|
|
|
void set(wstring v)
|
|
|
|
|
{
|
2016-12-27 23:24:33 +09:00
|
|
|
if (!PRGEDITused)
|
|
|
|
|
throw new UsePRGEDITBeforeAnyPRGFunction("PRGSET");
|
2016-12-27 23:01:28 +09:00
|
|
|
this.slot[currentSlot].set = v;
|
|
|
|
|
}
|
2016-12-28 15:49:44 +09:00
|
|
|
void insert(wstring line, bool isBack)
|
|
|
|
|
{
|
|
|
|
|
if (!PRGEDITused)
|
|
|
|
|
throw new UsePRGEDITBeforeAnyPRGFunction("PRGINS");
|
|
|
|
|
this.slot[currentSlot].insert(line, isBack);
|
|
|
|
|
}
|
2016-12-27 17:16:54 +09:00
|
|
|
}
|