1
0

COLORを実�

This commit is contained in:
otya128
2015-07-11 22:21:48 +09:00
parent f10437449f
commit 7ad5c9ff3c
4 changed files with 167 additions and 28 deletions

View File

@@ -11,7 +11,7 @@ import otya.smilebasic.type;
import otya.smilebasic.petitcomputer;
//プチコンの引数省略は特殊なので
//LOCATE ,,0のように省略できる
struct DefaultValue(T)
struct DefaultValue(T, bool skippable = true)
{
T value;
bool isDefault;
@@ -29,6 +29,11 @@ struct DefaultValue(T)
{
isDefault = f;
}
void setDefaultValue(T v)
{
if(isDefault)
value = v;
}
mixin Proxy!value;
}
alias ValueType = otya.smilebasic.type.ValueType;
@@ -36,17 +41,24 @@ struct BuiltinFunctionArgument
{
ValueType argType;
bool optionalArg;
bool skipArg;
}
class BuiltinFunction
{
BuiltinFunctionArgument[] argments;
ValueType result;
void function(PetitComputer, Value[], Value[]) func;
this(BuiltinFunctionArgument[] argments, ValueType result, void function(PetitComputer, Value[], Value[]) func)
int startskip;
this(BuiltinFunctionArgument[] argments, ValueType result, void function(PetitComputer, Value[], Value[]) func, int startskip)
{
this.argments = argments;
this.result = result;
this.func = func;
this.startskip = startskip;
}
bool hasSkipArgument()
{
return this.startskip != this.argments.length;
}
import std.math;
/*
@@ -56,8 +68,21 @@ class BuiltinFunction
}*/
static double function(double) ABS = &abs!double;
//static ABS = function double(double x) => abs(this.result == ValueType.Double ? 1 : 0);
static void LOCATE(PetitComputer p, DefaultValue!int x, DefaultValue!int y, DefaultValue!int z)
static void LOCATE(PetitComputer p, DefaultValue!int x, DefaultValue!int y, DefaultValue!(int, false) z)
{
x.setDefaultValue(p.CSRX);
y.setDefaultValue(p.CSRY);
z.setDefaultValue(p.CSRZ);
p.CSRX = cast(int)x;
p.CSRY = cast(int)y;
p.CSRZ = cast(int)z;
}
static void COLOR(PetitComputer p, DefaultValue!int fore, DefaultValue!(int, false) back)
{
fore.setDefaultValue(p.consoleForeColor);
back.setDefaultValue(p.consoleBackColor);
p.consoleForeColor = cast(int)fore;
p.consoleBackColor = cast(int)back;
}
//alias void function(PetitComputer, Value[], Value[]) BuiltinFunc;
static BuiltinFunction[wstring] builtinFunctions;
@@ -71,7 +96,8 @@ class BuiltinFunction
builtinFunctions[name] = new BuiltinFunction(
GetFunctionParamType!(BuiltinFunction, name),
GetFunctionReturnType!(BuiltinFunction, name),
mixin(AddFunc!(BuiltinFunction, name))
mixin(AddFunc!(BuiltinFunction, name)),
GetStartSkip!(BuiltinFunction, name),
);
writeln(AddFunc!(BuiltinFunction, name));
}
@@ -79,6 +105,25 @@ class BuiltinFunction
}
}
template GetStartSkip(T, string N)
{
private template SkipSkip(int I, P...)
{
static if(P.length <= I)
{
enum SkipSkip = I - is(P[0] == PetitComputer);
}
else static if(is(P[I] == DefaultValue!(int, false)))
{
enum SkipSkip = I - is(P[0] : PetitComputer);
}
else
{
enum SkipSkip = SkipSkip!(I + 1, P);
}
}
enum GetStartSkip = SkipSkip!(0, ParameterTypeTuple!(__traits(getMember, T, N)));
}
template GetFunctionReturnType(T, string N)
{
static if(is(ReturnType!(__traits(getMember, T, N)) == double))
@@ -100,12 +145,12 @@ template AddFunc(T, string N)
static if(is(ReturnType!(__traits(getMember, T, N)) == double))
{
const string AddFunc = "function void(PetitComputer p, Value[] arg, Value[] ret){if(ret.length != 1){throw new IllegalFunctionCall();}ret[0] = Value(" ~ N ~ "(" ~
AddFuncArg!(0, 0, ParameterTypeTuple!(__traits(getMember, T, N))) ~ "));}";
AddFuncArg!(ParameterTypeTuple!(__traits(getMember, T, N)).length - 1, 0, 0, ParameterTypeTuple!(__traits(getMember, T, N))) ~ "));}";
}
else static if(is(ReturnType!(__traits(getMember, T, N)) == void))
{
const string AddFunc = "function void(PetitComputer p, Value[] arg, Value[] ret){if(ret.length != 1){throw new IllegalFunctionCall();}" ~ N ~ "(" ~
AddFuncArg!(0, 0, ParameterTypeTuple!(__traits(getMember, T, N))) ~ ");}";
const string AddFunc = "function void(PetitComputer p, Value[] arg, Value[] ret){if(ret.length != 0){throw new IllegalFunctionCall();}" ~ N ~ "(" ~
AddFuncArg!(ParameterTypeTuple!(__traits(getMember, T, N)).length - 1, 0, 0, ParameterTypeTuple!(__traits(getMember, T, N))) ~ ");}";
}
else
{
@@ -120,6 +165,13 @@ DefaultValue!int fromIntToDefault(Value v)
else
return DefaultValue!int(true);
}
DefaultValue!(int, false) fromIntToSkip(Value v)
{
if(v.isNumber)
return DefaultValue!(int, false)(v.castInteger());
else
return DefaultValue!(int, false)(true);
}
template GetFunctionParamType(T, string N)
{
enum GetFunctionParamType = mixin("[" ~ Array!(ParameterTypeTuple!(__traits(getMember, T, N))) ~ "]");
@@ -137,6 +189,10 @@ template GetFunctionParamType(T, string N)
{
const string arg = "ValueType.Integer, true";
}
else static if(is(P[0] == DefaultValue!(int, false)))
{
const string arg = "ValueType.Integer, true";
}
else static if(is(P[0] == PetitComputer))
{
enum Array = Array!(P[1..$]);
@@ -151,12 +207,13 @@ template GetFunctionParamType(T, string N)
}
}
}
template AddFuncArg(int N, int M, P...)
template AddFuncArg(int L, int N, int M, P...)
{
enum I = L - N;
static if(is(P[0] == double))
{
enum add = 1;
const string arg = "arg[" ~ N.to!string ~ "].castDouble";
const string arg = "arg[" ~ I.to!string ~ "].castDouble";
}
else static if(is(P[0] == PetitComputer))
{
@@ -166,12 +223,17 @@ template AddFuncArg(int N, int M, P...)
else static if(is(P[0] == int))
{
enum add = 1;
const string arg = "arg[" ~ N.to!string ~ "].castInteger";
const string arg = "arg[" ~ I.to!string ~ "].castInteger";
}
else static if(is(P[0] == DefaultValue!int))
{
enum add = 1;
const string arg = "fromIntToDefault(arg[" ~ N.to!string ~ "])";
const string arg = "fromIntToDefault(arg[" ~ I.to!string ~ "])";
}
else static if(is(P[0] == DefaultValue!(int, false)))
{
enum add = 1;
const string arg = "fromIntToSkip(arg[" ~ I.to!string ~ "])";
}
else
{
@@ -185,6 +247,6 @@ template AddFuncArg(int N, int M, P...)
}
else
{
const string AddFuncArg = arg ~ ", " ~ AddFuncArg!(N + add, M + 1, P[1..$]);
const string AddFuncArg = arg ~ ", " ~ AddFuncArg!(L - !add, N + add, M + 1, P[1..$]);
}
}