INC/DECを実装(配列除く)
This commit is contained in:
@@ -888,3 +888,79 @@ class CallBuiltinFunction : Code
|
|||||||
vm.stacki -= func.argments.length - outcount;
|
vm.stacki -= func.argments.length - outcount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
class IncCodeG : Code
|
||||||
|
{
|
||||||
|
int var;
|
||||||
|
this(int var)
|
||||||
|
{
|
||||||
|
this.var = var;
|
||||||
|
}
|
||||||
|
//TODO:文字列INCの挙動
|
||||||
|
override void execute(VM vm)
|
||||||
|
{
|
||||||
|
Value v;
|
||||||
|
Value g = vm.global[var];
|
||||||
|
vm.pop(v);
|
||||||
|
if(!g.isNumber() && g.type != ValueType.String)
|
||||||
|
{
|
||||||
|
throw new TypeMismatch();
|
||||||
|
}
|
||||||
|
if((!g.isNumber() || !v.isNumber()) && g.type != v.type)
|
||||||
|
{
|
||||||
|
throw new TypeMismatch();
|
||||||
|
}
|
||||||
|
if(g.isNumber())
|
||||||
|
{
|
||||||
|
double l = g.castDouble;
|
||||||
|
double r = v.castDouble;
|
||||||
|
if(v.type == ValueType.Double)
|
||||||
|
vm.global[var] = Value(l + r);
|
||||||
|
else
|
||||||
|
vm.global[var] = Value(cast(int)(l + r));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
wstring l = g.stringValue;
|
||||||
|
wstring r = v.stringValue;
|
||||||
|
vm.global[var] = Value(l ~ r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class IncCodeL : Code
|
||||||
|
{
|
||||||
|
int var;
|
||||||
|
this(int var)
|
||||||
|
{
|
||||||
|
this.var = var;
|
||||||
|
}
|
||||||
|
//TODO:文字列INCの挙動
|
||||||
|
override void execute(VM vm)
|
||||||
|
{
|
||||||
|
Value v;
|
||||||
|
Value* g = &vm.stack[vm.bp + var];
|
||||||
|
vm.pop(v);
|
||||||
|
if(!g.isNumber() && g.type != ValueType.String)
|
||||||
|
{
|
||||||
|
throw new TypeMismatch();
|
||||||
|
}
|
||||||
|
if((!g.isNumber() || !v.isNumber()) && g.type != v.type)
|
||||||
|
{
|
||||||
|
throw new TypeMismatch();
|
||||||
|
}
|
||||||
|
if(g.isNumber())
|
||||||
|
{
|
||||||
|
double l = g.castDouble;
|
||||||
|
double r = v.castDouble;
|
||||||
|
if(v.type == ValueType.Double)
|
||||||
|
*g = Value(l + r);
|
||||||
|
else
|
||||||
|
*g = Value(cast(int)(l + r));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
wstring l = g.stringValue;
|
||||||
|
wstring r = v.stringValue;
|
||||||
|
*g = Value(l ~ r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -277,8 +277,6 @@ class Compiler
|
|||||||
{
|
{
|
||||||
if(sc.func)
|
if(sc.func)
|
||||||
{
|
{
|
||||||
int global = hasGlobalVarIndex(name);
|
|
||||||
if(global) return global;
|
|
||||||
int local = sc.func.getLocalVarIndex(name, this);
|
int local = sc.func.getLocalVarIndex(name, this);
|
||||||
return local;
|
return local;
|
||||||
}
|
}
|
||||||
@@ -477,6 +475,19 @@ class Compiler
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void compileInc(Inc node, Scope sc)
|
||||||
|
{
|
||||||
|
compileExpression(node.expression, sc);
|
||||||
|
int index = sc.func ? sc.func.hasLocalVarIndex(node.name) : 0;
|
||||||
|
if(index)
|
||||||
|
{
|
||||||
|
genCode(new IncCodeL(index));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
genCode(new IncCodeG(this.getGlobalVarIndex(node.name)));
|
||||||
|
}
|
||||||
|
}
|
||||||
void compileStatements(Statements statements, Scope sc)
|
void compileStatements(Statements statements, Scope sc)
|
||||||
{
|
{
|
||||||
foreach(Statement s ; statements.statements)
|
foreach(Statement s ; statements.statements)
|
||||||
@@ -652,6 +663,9 @@ class Compiler
|
|||||||
case NodeType.While:
|
case NodeType.While:
|
||||||
compileWhile(cast(While)i, s);
|
compileWhile(cast(While)i, s);
|
||||||
break;
|
break;
|
||||||
|
case NodeType.Inc:
|
||||||
|
compileInc(cast(Inc)i, s);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
stderr.writeln("Compile:NotImpl ", i.type);
|
stderr.writeln("Compile:NotImpl ", i.type);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -427,6 +427,7 @@ class Inc : Statement
|
|||||||
Expression expression;
|
Expression expression;
|
||||||
this(wstring name, Expression expr)
|
this(wstring name, Expression expr)
|
||||||
{
|
{
|
||||||
|
this.type = NodeType.Inc;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.expression = expr;
|
this.expression = expr;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -360,8 +360,8 @@ DEF FACT(N)
|
|||||||
END"*/
|
END"*/
|
||||||
`CLS : CL=0 : Z=0
|
`CLS : CL=0 : Z=0
|
||||||
WHILE 1
|
WHILE 1
|
||||||
Z=Z+1
|
INC Z
|
||||||
?Z;
|
?Z,
|
||||||
WEND
|
WEND
|
||||||
WHILE 1
|
WHILE 1
|
||||||
INC Z : IF Z>200 THEN Z=0
|
INC Z : IF Z>200 THEN Z=0
|
||||||
|
|||||||
Reference in New Issue
Block a user