From 3ab1c8e6c8fcc124b52c885d854411a883dd3b59 Mon Sep 17 00:00:00 2001 From: otya128 Date: Tue, 27 Dec 2016 17:16:54 +0900 Subject: [PATCH] Add program.d --- SMILEBASIC/VM.d | 6 ++-- SMILEBASIC/builtinfunctions.d | 10 +++---- SMILEBASIC/petitcomputer.d | 20 ++++++------- SMILEBASIC/program.d | 55 +++++++++++++++++++++++++++++++++++ dub.json | 2 +- 5 files changed, 74 insertions(+), 19 deletions(-) create mode 100644 SMILEBASIC/program.d diff --git a/SMILEBASIC/VM.d b/SMILEBASIC/VM.d index c743503..977ff4f 100644 --- a/SMILEBASIC/VM.d +++ b/SMILEBASIC/VM.d @@ -2334,7 +2334,7 @@ class Exec : Code } wstring content; if (vm.petitcomputer.project.loadFile(file.project, "TXT", file.name, content)) - vm.petitcomputer.slot[slot].load(content); + vm.petitcomputer.program.slot[slot].load(content); else throw new LoadFailed();//TODO:DIALOG? } @@ -2347,7 +2347,7 @@ class Exec : Code throw new TypeMismatch(); } import otya.smilebasic.parser; - auto parser = new Parser(cast(immutable)vm.petitcomputer.slot[slot].text); + auto parser = new Parser(cast(immutable)vm.petitcomputer.program.slot[slot].text); auto compiler = parser.compiler; VMAddress retaddr; if (slot != vm.currentSlotNumber) @@ -2381,7 +2381,7 @@ class Use : Code throw new TypeMismatch(); } import otya.smilebasic.parser; - auto parser = new Parser(cast(immutable)vm.petitcomputer.slot[slot].text); + auto parser = new Parser(cast(immutable)vm.petitcomputer.program.slot[slot].text); auto compiler = parser.compiler; compiler.compile(vm, slot); } diff --git a/SMILEBASIC/builtinfunctions.d b/SMILEBASIC/builtinfunctions.d index 537b0f9..df105c8 100644 --- a/SMILEBASIC/builtinfunctions.d +++ b/SMILEBASIC/builtinfunctions.d @@ -2269,7 +2269,7 @@ class BuiltinFunction } if(resname == "" || resname.indexOf("PRG") == 0) { - int lot = p.currentSlot; + int lot = p.program.currentSlot; if(resname != "" && resname != "PRG") { auto num = resname[3..$]; @@ -2280,7 +2280,7 @@ class BuiltinFunction //not exist return; } - p.slot[lot].load(txt); + p.program.slot[lot].load(txt); return; } if(resname.indexOf("GRP") == 0) @@ -2301,16 +2301,16 @@ class BuiltinFunction { throw new IllegalFunctionCall("SAVE"/*, 1*/); } - auto slot = p.currentSlot; + auto slot = p.program.currentSlot; if (file.hasResourceNumber) { slot = file.resourceNumber; } - if (slot >= p.slotSize) + if (slot >= p.program.slotSize) { throw new IllegalFunctionCall("SAVE"/*, 1*/); } - auto str = cast(immutable)p.slot[slot].text; + auto str = cast(immutable)p.program.slot[slot].text; p.project.saveTextFile(file, str); } static void SAVE(PetitComputer p, wstring name, Value data) diff --git a/SMILEBASIC/petitcomputer.d b/SMILEBASIC/petitcomputer.d index a2e9ed1..76e9040 100644 --- a/SMILEBASIC/petitcomputer.d +++ b/SMILEBASIC/petitcomputer.d @@ -20,6 +20,7 @@ import otya.smilebasic.project; import otya.smilebasic.console; import otya.smilebasic.graphic; import otya.smilebasic.dialog; +import otya.smilebasic.program; const static rot_test_deg = 45f; const static rot_test_x = 0f; const static rot_test_y = 1f; @@ -1067,7 +1068,6 @@ class PetitComputer } Console console; Object buttonLock; - Slot[] slot; bool isRunningDirectMode = false; GLenum textureScaleMode; int maincnt; @@ -1082,7 +1082,7 @@ class PetitComputer textureScaleMode = GL_NEAREST; } buttonLock = new Object(); - slot = new Slot[5]; + program = new Program(); keybuffer = new Key[128]; init(); graphic = new Graphic2(this); @@ -1109,7 +1109,7 @@ class PetitComputer int oldpc; void runSlot(int lot) { - parser = new Parser(cast(wstring)slot[lot].text); + parser = new Parser(cast(wstring)program.slot[lot].text); try { vm = parser.compile(); @@ -1133,7 +1133,7 @@ class PetitComputer nodirectmode = true; if (nodirectmode) { - slot[0].load(readText(inputfile).to!wstring); + program.slot[0].load(readText(inputfile).to!wstring); runSlot(0); } else @@ -1209,7 +1209,7 @@ class PetitComputer writeln(sbe.to!string); writeln(sbe.getErrorMessage2); auto loc = vm.currentLocation; - console.print(vm.currentSlotNumber, ":", loc.line, ":", loc.pos, ":", slot[vm.currentSlotNumber].getLine(loc)); + console.print(vm.currentSlotNumber, ":", loc.line, ":", loc.pos, ":", program.slot[vm.currentSlotNumber].getLine(loc)); } catch(Throwable t) { @@ -1225,7 +1225,7 @@ class PetitComputer console.print("\n"); writeln(t); auto loc = vm.currentLocation; - console.print(vm.currentSlotNumber, ":", loc.line, ":", loc.pos, ":", slot[vm.currentSlotNumber].getLine(loc)); + console.print(vm.currentSlotNumber, ":", loc.line, ":", loc.pos, ":", program.slot[vm.currentSlotNumber].getLine(loc)); } catch(Throwable t) { @@ -1278,7 +1278,7 @@ class PetitComputer writeln(sbe.getErrorMessage2); writeln(sbe.func); loc = vm.currentLocation; - console.print(vm.currentSlotNumber, ":", loc.line, ":", loc.pos, ":", slot[vm.currentSlotNumber].getLine(loc)); + console.print(vm.currentSlotNumber, ":", loc.line, ":", loc.pos, ":", program.slot[vm.currentSlotNumber].getLine(loc)); } catch(Throwable t) { @@ -1293,7 +1293,7 @@ class PetitComputer console.print(t.to!string); console.print("\n"); writeln(t); - console.print(vm.currentSlotNumber, ":", loc.line, ":", loc.pos, ":", slot[vm.currentSlotNumber].getLine(vm.currentLocation)); + console.print(vm.currentSlotNumber, ":", loc.line, ":", loc.pos, ":", program.slot[vm.currentSlotNumber].getLine(vm.currentLocation)); } catch(Throwable t) { @@ -1672,8 +1672,8 @@ class PetitComputer updateWindowSize(); } - int currentSlot; - int slotSize = 4; + Program program; + //プチコン内部表現はRGB5_A1 static uint toGLColor(GLenum format, ubyte r, ubyte g, ubyte b, ubyte a) { diff --git a/SMILEBASIC/program.d b/SMILEBASIC/program.d new file mode 100644 index 0000000..12e97de --- /dev/null +++ b/SMILEBASIC/program.d @@ -0,0 +1,55 @@ +module otya.smilebasic.program; +import std.container; + +struct Slot +{ + DList!wstring program; + void load(wstring data) + { + import std.algorithm; + program.clear(); + foreach(l; splitter(data, "\n")) + { + program.insertBack(l ~ '\n'); + } + } + 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 ""; + } +} + +class Program +{ + Slot[] slot; + int currentSlot; + int currentLine = 1; + int slotSize = 4; + this() + { + slot = new Slot[5]; + } +} diff --git a/dub.json b/dub.json index ce247d6..fff667c 100644 --- a/dub.json +++ b/dub.json @@ -4,7 +4,7 @@ "derelict-sdl2": "~>1.9.7", "derelict-gl3": "*" }, - "sourceFiles": ["SMILEBASIC/bg.d","SMILEBASIC/builtinfunctions.d","SMILEBASIC/compiler.d","SMILEBASIC/error.d","SMILEBASIC/main.d","SMILEBASIC/node.d","SMILEBASIC/parser.d","SMILEBASIC/petitcomputer.d","SMILEBASIC/sprite.d","SMILEBASIC/systemvariable.d","SMILEBASIC/token.d","SMILEBASIC/type.d","SMILEBASIC/VM.d","SMILEBASIC/project.d","SMILEBASIC/console.d","SMILEBASIC/graphic.d","SMILEBASIC/dialog.d","SMILEBASIC/data.d"], + "sourceFiles": ["SMILEBASIC/bg.d","SMILEBASIC/builtinfunctions.d","SMILEBASIC/compiler.d","SMILEBASIC/error.d","SMILEBASIC/main.d","SMILEBASIC/node.d","SMILEBASIC/parser.d","SMILEBASIC/petitcomputer.d","SMILEBASIC/sprite.d","SMILEBASIC/systemvariable.d","SMILEBASIC/token.d","SMILEBASIC/type.d","SMILEBASIC/VM.d","SMILEBASIC/project.d","SMILEBASIC/console.d","SMILEBASIC/graphic.d","SMILEBASIC/dialog.d","SMILEBASIC/data.d","SMILEBASIC/program.d"], "targetType": "executable", "buildRequirements": ["allowWarnings"], "targetPath": "out",