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

View File

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

View File

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

View File

@@ -12,6 +12,7 @@ enum NodeType
Variable, Variable,
CallFunction, CallFunction,
VoidExpression, VoidExpression,
UnaryOperator,
Statements, Statements,
FunctionBody, FunctionBody,
@@ -64,6 +65,17 @@ class BinaryOperator : Expression
this.item2 = i2; 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 class Variable : Expression
{ {
wstring name; wstring name;

View File

@@ -747,7 +747,12 @@ class Parser
//TODO:UnaryOperatorの実装 //TODO:UnaryOperatorの実装
//とりあえず0-exprを作成 //とりあえず0-exprを作成
lex.popFront(); 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; return node;
default: default:
return node; return node;