1
0

Implement &&

This commit is contained in:
otya128
2016-12-31 09:45:27 +09:00
parent 17b5ba0f49
commit e6d0a49c24
2 changed files with 57 additions and 0 deletions

View File

@@ -2561,3 +2561,50 @@ class Linput : Code
vm.push(Value(vm.petitcomputer.input(guide.castDString, false, true)));
}
}
class LogicalAnd : Code
{
int addr;
override void execute(VM vm)
{
if (vm.stacki == 0)
{
throw new StackUnderFlow();
}
//TRUE->POP
//FALSE->JUMP
if (!vm.stack[vm.stacki - 1].boolValue)
{
vm.pc = addr - 1;
}
else
{
vm.decSP;
}
}
}
class ConvertBool : Code
{
//0 => 0(int)
//-1,-2,-3... => 1(int)
//1,2,3... => 1(int)
//"string" => 3(int)
override void execute(VM vm)
{
if (vm.stacki == 0)
{
throw new StackUnderFlow();
}
auto top = &vm.stack[vm.stacki - 1];
if (top.type == ValueType.String)
{
top.type = ValueType.Integer;
top.integerValue = 3;
}
else if (top.isNumber)
{
top.type = ValueType.Integer;
top.integerValue = top.boolValue;
}
}
}

View File

@@ -502,6 +502,16 @@ class Compiler
case NodeType.BinaryOperator:
{
auto binop = cast(BinaryOperator)exp;
if(binop.operator == TokenType.LogicalAnd)
{
compileExpression(binop.item1, sc);
auto jmp = new LogicalAnd();
genCode(jmp);
compileExpression(binop.item2, sc);
genCode(new ConvertBool());
jmp.addr = cast(int)code.length;
break;
}
compileExpression(binop.item1, sc);
compileExpression(binop.item2, sc);
if(binop.operator == TokenType.LBracket)