1
0
Files
otyaSMILEBASIC/SMILEBASIC/program.d

147 lines
3.1 KiB
D
Raw Normal View History

2016-12-27 17:16:54 +09:00
module otya.smilebasic.program;
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;
import std.algorithm;
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-27 23:01:28 +09:00
this(T)(T)
{
program = make!(DList!wstring);
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)
{
range.put(l ~ "\n");
}
else
{
if (range.empty)
{
program.insertAfter(range, l ~ "\n");
}
else
{
program.insertBefore(range, l ~ "\n");
}
}
i++;
}
}
2016-12-27 17:16:54 +09:00
}
class Program
{
Slot[] slot;
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)
{
s = Slot(slot);
}
2016-12-27 17:16:54 +09:00
}
2016-12-27 21:03:48 +09:00
void edit(int slot, int line)
{
PRGEDITused = true;
2016-12-27 21:03:48 +09:00
this.currentSlot = slot;
this.slot[slot].currentLine = line;
}
wstring get()
{
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)
{
if (!PRGEDITused)
throw new UsePRGEDITBeforeAnyPRGFunction("PRGSET");
2016-12-27 23:01:28 +09:00
this.slot[currentSlot].set = v;
}
2016-12-27 17:16:54 +09:00
}