void AddExpression(StringBuilder luaSB, int indent, SPAGS.Expression expression)
{
switch (expression.Type)
{
case SPAGS.ExpressionType.AllocateArray:
SPAGS.Expression.AllocateArray allocArray = (SPAGS.Expression.AllocateArray)expression;
switch (allocArray.ElementType.Category)
{
case SPAGS.ValueTypeCategory.StringValue:
luaSB.Append("{}");
break;
case SPAGS.ValueTypeCategory.Struct:
SPAGS.ValueType.Struct structType = (SPAGS.ValueType.Struct)allocArray.ElementType;
if (structType.IsManaged)
{
luaSB.Append("{}");
break;
}
else
{
goto default;
}
default:
luaSB.Append("ags.array(--[[ TODO ]])");
break;
}
break;
case SPAGS.ExpressionType.AllocStringBuffer:
luaSB.Append("ags.string()");
break;
case SPAGS.ExpressionType.AllocStruct:
SPAGS.Expression.AllocateStruct allocStruct = (SPAGS.Expression.AllocateStruct)expression;
luaSB.Append(GetName(allocStruct.TheStructType) + "()");
break;
case SPAGS.ExpressionType.ArrayIndex:
SPAGS.Expression.ArrayIndex arrayIndex = (SPAGS.Expression.ArrayIndex)expression;
AddExpression(luaSB, 0, arrayIndex.Target);
luaSB.Append("[");
AddExpression(luaSB, 0, arrayIndex.Index);
luaSB.Append("]");
break;
case SPAGS.ExpressionType.Attribute:
SPAGS.Expression.Attribute attr = (SPAGS.Expression.Attribute)expression;
switch (attr.TheAttribute.Getter.Name)
{
case "Maths::get_Pi":
luaSB.Append("math.pi");
break;
default:
if (attr.Target != null)
{
AddExpression(luaSB, indent, attr.Target);
}
else
{
luaSB.Append(GetName(attr.TheStructType));
}
luaSB.Append("." + attr.TheAttribute.Name);
break;
}
break;
case SPAGS.ExpressionType.BinaryOperator:
SPAGS.Expression.BinaryOperator binop = (SPAGS.Expression.BinaryOperator)expression;
switch (binop.Token.Type)
{
case SPAGS.TokenType.Add:
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(" + ");
AddExpression(luaSB, indent, binop.Right);
break;
case SPAGS.TokenType.BitwiseAnd:
luaSB.Append("bit.band(");
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(", ");
AddExpression(luaSB, indent, binop.Right);
luaSB.Append(")");
break;
case SPAGS.TokenType.BitwiseLeftShift:
luaSB.Append("bit.lshift(");
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(", ");
AddExpression(luaSB, indent, binop.Right);
luaSB.Append(")");
break;
case SPAGS.TokenType.BitwiseOr:
luaSB.Append("bit.bor(");
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(", ");
AddExpression(luaSB, indent, binop.Right);
luaSB.Append(")");
break;
case SPAGS.TokenType.BitwiseRightShift:
luaSB.Append("bit.rshift(");
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(", ");
AddExpression(luaSB, indent, binop.Right);
luaSB.Append(")");
break;
case SPAGS.TokenType.BitwiseXor:
luaSB.Append("bit.bxor(");
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(", ");
AddExpression(luaSB, indent, binop.Right);
luaSB.Append(")");
break;
case SPAGS.TokenType.Divide:
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(" / ");
AddExpression(luaSB, indent, binop.Right);
break;
case SPAGS.TokenType.IsEqualTo:
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(" == ");
AddExpression(luaSB, indent, binop.Right);
break;
case SPAGS.TokenType.IsGreaterThan:
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(" > ");
AddExpression(luaSB, indent, binop.Right);
break;
case SPAGS.TokenType.IsGreaterThanOrEqualTo:
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(" >= ");
AddExpression(luaSB, indent, binop.Right);
break;
case SPAGS.TokenType.IsLessThan:
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(" < ");
AddExpression(luaSB, indent, binop.Right);
break;
case SPAGS.TokenType.IsLessThanOrEqualTo:
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(" <= ");
AddExpression(luaSB, indent, binop.Right);
break;
case SPAGS.TokenType.IsNotEqualTo:
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(" ~= ");
AddExpression(luaSB, indent, binop.Right);
break;
case SPAGS.TokenType.LogicalAnd:
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(" and ");
AddExpression(luaSB, indent, binop.Right);
break;
case SPAGS.TokenType.LogicalOr:
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(" or ");
AddExpression(luaSB, indent, binop.Right);
break;
case SPAGS.TokenType.Modulus:
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(" % ");
AddExpression(luaSB, indent, binop.Right);
break;
case SPAGS.TokenType.Multiply:
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(" * ");
AddExpression(luaSB, indent, binop.Right);
break;
case SPAGS.TokenType.Subtract:
AddExpression(luaSB, indent, binop.Left);
luaSB.Append(" - ");
AddExpression(luaSB, indent, binop.Right);
break;
}
break;
case SPAGS.ExpressionType.Call:
SPAGS.Expression.Call call = (SPAGS.Expression.Call)expression;
AddExpression(luaSB, indent, call.CallingOn);
luaSB.Append("(");
for (int j = 0; j < call.Parameters.Count; j++)
{
if (j > 0) luaSB.Append(", ");
AddExpression(luaSB, indent, call.Parameters[j]);
}
luaSB.Append(")");
break;
case SPAGS.ExpressionType.Method:
SPAGS.Expression.Method methodExpr = (SPAGS.Expression.Method)expression;
switch (methodExpr.TheMethod.Function.Name)
{
case "String::Format": luaSB.Append("string.format"); break;
case "Maths::ArcCos": luaSB.Append("math.acos"); break;
case "Maths::ArcSin": luaSB.Append("math.asin"); break;
case "Maths::ArcTan": luaSB.Append("math.atan"); break;
case "Maths::ArcTan2": luaSB.Append("math.atan2"); break;
case "Maths::Cos": luaSB.Append("math.cos"); break;
case "Maths::Cosh": luaSB.Append("math.cosh"); break;
case "Maths::DegreesToRadians": luaSB.Append("math.rad"); break;
case "Maths::Exp": luaSB.Append("math.exp"); break;
case "Maths::Log": luaSB.Append("math.log"); break;
case "Maths::Log10": luaSB.Append("math.log10"); break;
case "Maths::RadiansToDegrees": luaSB.Append("math.deg"); break;
case "Maths::RaiseToPower": luaSB.Append("math.pow"); break; // todo: binop?
case "Maths::Sin": luaSB.Append("math.sin"); break;
case "Maths::Sinh": luaSB.Append("math.sinh"); break;
case "Maths::Sqrt": luaSB.Append("math.sqrt"); break;
case "Maths::Tan": luaSB.Append("math.tan"); break;
case "Maths::Tanh": luaSB.Append("maths.tanh"); break;
default:
if (methodExpr.TheMethod.IsStatic)
{
luaSB.Append(GetName(methodExpr.TheStructType));
luaSB.Append(".");
}
else
{
AddExpression(luaSB, indent, methodExpr.Target);
luaSB.Append(":");
}
luaSB.Append(methodExpr.TheMethod.Name);
break;
}
break;
case SPAGS.ExpressionType.Function:
SPAGS.Expression.Function funcExpr = (SPAGS.Expression.Function)expression;
switch (funcExpr.TheFunction.Name)
{
case "IntToFloat": luaSB.Append("tonumber"); break; // todo: completely eliminate
default:
luaSB.Append(GetName(funcExpr.TheFunction));
break;
}
break;
case SPAGS.ExpressionType.CharLiteral:
SPAGS.Expression.CharLiteral charLiteral = (SPAGS.Expression.CharLiteral)expression;
luaSB.Append("string.byte('" + charLiteral.Value + "')");
break;
case SPAGS.ExpressionType.Constant:
SPAGS.Expression.Constant constant = (SPAGS.Expression.Constant)expression;
luaSB.Append(GetName(constant.TheConstant));
break;
case SPAGS.ExpressionType.EnumValue:
SPAGS.Expression.EnumValue enumValue = (SPAGS.Expression.EnumValue)expression;
luaSB.Append(GetName(enumValue.TheValue));
break;
case SPAGS.ExpressionType.Field:
SPAGS.Expression.Field field = (SPAGS.Expression.Field)expression;
AddExpression(luaSB, indent, field.Target);
luaSB.Append("." + field.TheField.Name);
break;
case SPAGS.ExpressionType.FloatLiteral:
SPAGS.Expression.FloatLiteral floatLiteral = (SPAGS.Expression.FloatLiteral)expression;
luaSB.Append(floatLiteral.Value.ToString());
break;
case SPAGS.ExpressionType.IntegerLiteral:
luaSB.Append(((SPAGS.Expression.IntegerLiteral)expression).Value.ToString());
break;
case SPAGS.ExpressionType.Null:
luaSB.Append("nil");
break;
case SPAGS.ExpressionType.StringLiteral:
SPAGS.Expression.StringLiteral stringLiteral = (SPAGS.Expression.StringLiteral)expression;
luaSB.Append(InvokeLua.util_quotestring(stringLiteral.Value));
break;
case SPAGS.ExpressionType.UnaryOperator:
SPAGS.Expression.UnaryOperator unop = (SPAGS.Expression.UnaryOperator)expression;
switch (unop.Token.Type)
{
case SPAGS.TokenType.LogicalNot:
luaSB.Append("not ");
AddExpression(luaSB, indent, unop.Operand);
break;
case SPAGS.TokenType.Subtract:
luaSB.Append("-");
AddExpression(luaSB, indent, unop.Operand);
break;
}
break;
case SPAGS.ExpressionType.Variable:
SPAGS.Expression.Variable variable = (SPAGS.Expression.Variable)expression;
luaSB.Append(GetName(variable.TheVariable));
break;
default:
luaSB.Append("--[[" + expression.Type.ToString() + "]] nil");
break;
}
}