1
0

単項演算子を実�

This commit is contained in:
otya128
2015-06-07 19:33:51 +09:00
parent 978a7a6ee7
commit 628b259caf
5 changed files with 37 additions and 8 deletions

View File

@@ -115,6 +115,7 @@ class PrintCode : Code
throw new TypeMismatch();
}
}
stdout.flush();
}
}
/*
@@ -197,6 +198,7 @@ class Operate : Code
vm.pop(r);
int ri = r.integerValue;
double rd = r.integerValue;
bool numf = r.type == ValueType.Double || r.type == ValueType.Integer;
if(r.type == ValueType.Double)
{
ri = cast(int)r.doubleValue;
@@ -206,10 +208,16 @@ class Operate : Code
{
//単項演算子
case TokenType.Not:
vm.push(Value(~ri));
if(numf)
vm.push(Value(~ri));
else
throw new TypeMismatch();
return;
case TokenType.LogicalNot:
vm.push(Value(!ri));
if(numf)
vm.push(Value(!ri));
else
throw new TypeMismatch();
return;
default:
break;
@@ -256,7 +264,7 @@ class Operate : Code
//数値 * 文字列だとエラー
case TokenType.Mul:
{
wstring delegate(wstring x, wstring y, int num) mul;
wstring delegate(wstring, wstring, int) mul;
mul = (x, y, z) => z > 0 ? x ~ mul(x , y, z - 1) : "";
vm.push(Value(mul(ls, ls, cast(int)rd)));
}

View File

@@ -107,6 +107,13 @@ class Compiler
genCodeOP(binop.operator);
}
break;
case NodeType.UnaryOperator:
{
auto una = cast(UnaryOperator)exp;
compileExpression(una.item);
genCodeOP(una.operator);
}
break;
case NodeType.Variable:
auto var = cast(Variable)exp;
genCodePushGlobal(getGlobalVarIndex(var.name));

View File

@@ -44,10 +44,7 @@ FOR I=0 TO 100
ENDIF
ENDIF
NEXT
A$=\"A\"
?A$
A$=2
?A$
?NOT 1,!!\"a\"
");
version(none) auto parser = new Parser(readText("FIZZBUZZ.TXT").to!wstring);
auto vm = parser.compile();

View File

@@ -12,6 +12,7 @@ enum NodeType
Variable,
CallFunction,
VoidExpression,
UnaryOperator,
Statements,
FunctionBody,
@@ -64,6 +65,17 @@ class BinaryOperator : Expression
this.item2 = i2;
}
}
class UnaryOperator : Expression
{
TokenType operator;
Expression item;
this(TokenType o, Expression i)
{
this.type = NodeType.UnaryOperator;
this.operator = o;
this.item = i;
}
}
class Variable : Expression
{
wstring name;

View File

@@ -747,7 +747,12 @@ class Parser
//TODO:UnaryOperatorの実装
//とりあえず0-exprを作成
lex.popFront();
node = new BinaryOperator(new Constant(Value(0)), TokenType.Minus, expression());
node = new BinaryOperator(new Constant(Value(0)), TokenType.Minus, factor());
return node;
case TokenType.LogicalNot:
case TokenType.Not:
lex.popFront();
node = new UnaryOperator(token.type, factor());
return node;
default:
return node;