📄 codedomvisitor.cs
字号:
}
public override object Visit(LocalVariableDeclaration localVariableDeclaration, object data)
{
foreach (VariableDeclaration var in localVariableDeclaration.Variables) {
CodeTypeReference type = new CodeTypeReference(Convert(var.Type));
if (var.Initializer != null) {
currentMethod.Statements.Add(new CodeVariableDeclarationStatement(type,
var.Name,
(CodeExpression)((INode)var.Initializer).AcceptVisitor(this, data)));
} else {
currentMethod.Statements.Add(new CodeVariableDeclarationStatement(type,
var.Name));
}
}
return null;
}
public override object Visit(ReturnStatement returnStatement, object data)
{
return null;
}
public override object Visit(IfStatement ifStatement, object data)
{
return null;
}
public override object Visit(WhileStatement doWhileStatement, object data)
{
return null;
}
public override object Visit(DoLoopStatement doWhileStatement, object data)
{
return null;
}
public override object Visit(ForStatement forStatement, object data)
{
return null;
}
public override object Visit(LabelStatement labelStatement, object data)
{
return null;
}
public override object Visit(GoToStatement gotoStatement, object data)
{
return null;
}
public override object Visit(SelectStatement switchStatement, object data)
{
return null;
}
public override object Visit(ForeachStatement foreachStatement, object data)
{
return null;
}
public override object Visit(LockStatement lockStatement, object data)
{
return null;
}
public override object Visit(TryCatchStatement tryCatchStatement, object data)
{
return null;
}
public override object Visit(AddHandlerStatement addHandlerStatement, object data)
{
CodeExpression methodInvoker = (CodeExpression)addHandlerStatement.HandlerExpression.AcceptVisitor(this, null);
if (addHandlerStatement.EventExpression is IdentifierExpression) {
currentMethod.Statements.Add(new CodeAttachEventStatement(new CodeEventReferenceExpression(new CodeThisReferenceExpression(), ((IdentifierExpression)addHandlerStatement.EventExpression).Identifier),
methodInvoker));
} else {
FieldReferenceOrInvocationExpression fr = (FieldReferenceOrInvocationExpression)addHandlerStatement.EventExpression;
currentMethod.Statements.Add(new CodeAttachEventStatement(new CodeEventReferenceExpression((CodeExpression)fr.TargetObject.AcceptVisitor(this, data), fr.FieldName),
methodInvoker));
}
return null;
}
public override object Visit(RemoveHandlerStatement removeHandlerStatement, object data)
{
return null;
}
public override object Visit(EndStatement endStatement, object data)
{
return null;
}
public override object Visit(ExitStatement exitStatement, object data)
{
return null;
}
public override object Visit(StopStatement stopStatement, object data)
{
return null;
}
public override object Visit(ResumeStatement resumeStatement, object data)
{
return null;
}
public override object Visit(EraseStatement eraseStatement, object data)
{
return null;
}
public override object Visit(ErrorStatement errorStatement, object data)
{
return null;
}
public override object Visit(OnErrorStatement onErrorStatement, object data)
{
return null;
}
public override object Visit(RaiseEventStatement raiseEventStatement, object data)
{
return null;
}
public override object Visit(ReDimStatement reDimStatement, object data)
{
return null;
}
public override object Visit(ThrowStatement throwStatement, object data)
{
return new CodeThrowExceptionStatement((CodeExpression)throwStatement.ThrowExpression.AcceptVisitor(this, data));
}
public override object Visit(PrimitiveExpression expression, object data)
{
// if (expression.Value is string) {
// return new CodePrimitiveExpression(expression.Value);
// } else if (expression.Value is char) {
// return new CodePrimitiveExpression((char)expression.Value);
// } else if (expression.Value == null) {
// return new CodePrimitiveExpression(null);
// }
return new CodePrimitiveExpression(expression.Value);
}
public override object Visit(BinaryOperatorExpression expression, object data)
{
CodeBinaryOperatorType op = CodeBinaryOperatorType.Add;
switch (expression.Op) {
case BinaryOperatorType.Add:
op = CodeBinaryOperatorType.Add;
break;
case BinaryOperatorType.BitwiseAnd:
op = CodeBinaryOperatorType.BitwiseAnd;
break;
case BinaryOperatorType.BitwiseOr:
op = CodeBinaryOperatorType.BitwiseOr;
break;
case BinaryOperatorType.Concat:
// CodeDOM suxx
op = CodeBinaryOperatorType.Add;
break;
case BinaryOperatorType.BooleanAnd:
op = CodeBinaryOperatorType.BooleanAnd;
break;
case BinaryOperatorType.BooleanOr:
op = CodeBinaryOperatorType.BooleanOr;
break;
case BinaryOperatorType.Divide:
op = CodeBinaryOperatorType.Divide;
break;
case BinaryOperatorType.DivideInteger:
// CodeDOM suxx
op = CodeBinaryOperatorType.Divide;
break;
case BinaryOperatorType.GreaterThan:
op = CodeBinaryOperatorType.GreaterThan;
break;
case BinaryOperatorType.GreaterThanOrEqual:
op = CodeBinaryOperatorType.GreaterThanOrEqual;
break;
case BinaryOperatorType.Equality:
op = CodeBinaryOperatorType.IdentityEquality;
break;
case BinaryOperatorType.InEquality:
op = CodeBinaryOperatorType.IdentityInequality;
break;
case BinaryOperatorType.LessThan:
op = CodeBinaryOperatorType.LessThan;
break;
case BinaryOperatorType.LessThanOrEqual:
op = CodeBinaryOperatorType.LessThanOrEqual;
break;
case BinaryOperatorType.Modulus:
op = CodeBinaryOperatorType.Modulus;
break;
case BinaryOperatorType.Multiply:
op = CodeBinaryOperatorType.Multiply;
break;
case BinaryOperatorType.Subtract:
op = CodeBinaryOperatorType.Subtract;
break;
case BinaryOperatorType.ShiftLeft:
// CodeDOM suxx
op = CodeBinaryOperatorType.Multiply;
break;
case BinaryOperatorType.ShiftRight:
// CodeDOM suxx
op = CodeBinaryOperatorType.Multiply;
break;
case BinaryOperatorType.IS:
op = CodeBinaryOperatorType.IdentityEquality;
break;
case BinaryOperatorType.Like:
// CodeDOM suxx
op = CodeBinaryOperatorType.IdentityEquality;
break;
case BinaryOperatorType.ExclusiveOr:
// CodeDOM suxx
op = CodeBinaryOperatorType.BitwiseOr;
break;
}
return new CodeBinaryOperatorExpression((CodeExpression)expression.Left.AcceptVisitor(this, data),
op,
(CodeExpression)expression.Right.AcceptVisitor(this, data));
}
public override object Visit(ParenthesizedExpression expression, object data)
{
return expression.Expression.AcceptVisitor(this, data);
}
public override object Visit(InvocationExpression invocationExpression, object data)
{
Expression target = invocationExpression.TargetObject;
CodeExpression targetExpr;
string methodName = null;
if (target == null) {
targetExpr = new CodeThisReferenceExpression();
} else if (target is FieldReferenceOrInvocationExpression) {
FieldReferenceOrInvocationExpression fRef = (FieldReferenceOrInvocationExpression)target;
targetExpr = (CodeExpression)fRef.TargetObject.AcceptVisitor(this, data);
if (fRef.TargetObject is FieldReferenceOrInvocationExpression) {
FieldReferenceOrInvocationExpression fRef2 = (FieldReferenceOrInvocationExpression)fRef.TargetObject;
if (fRef2.FieldName != null && Char.IsUpper(fRef2.FieldName[0])) {
// an exception is thrown if it doesn't end in an indentifier exception
// for example for : this.MyObject.MyMethod() leads to an exception, which
// is correct in this case ... I know this is really HACKY :)
try {
CodeExpression tExpr = ConvertToIdentifier(fRef2);
if (tExpr != null) {
targetExpr = tExpr;
}
} catch (Exception) {}
}
}
methodName = fRef.FieldName;
// HACK for : Microsoft.VisualBasic.ChrW(NUMBER)
Console.WriteLine(methodName);
if (methodName == "ChrW") {
Console.WriteLine("Return CAST EXPRESSION" + GetExpressionList(invocationExpression.Parameters)[0]);
return new CodeCastExpression("System.Char", GetExpressionList(invocationExpression.Parameters)[0]);
}
} else {
targetExpr = (CodeExpression)target.AcceptVisitor(this, data);
}
return new CodeMethodInvokeExpression(targetExpr, methodName, GetExpressionList(invocationExpression.Parameters));
}
public override object Visit(IdentifierExpression expression, object data)
{
if (IsField(expression.Identifier)) {
return new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),
expression.Identifier);
}
return new CodeVariableReferenceExpression(expression.Identifier);
}
public override object Visit(GetTypeExpression getTypeExpression, object data)
{
// TODO
return null;
}
public override object Visit(TypeReferenceExpression typeReferenceExpression, object data)
{
return null;
}
public override object Visit(ClassReferenceExpression classReferenceExpression, object data)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -