public static LuaValue Next(LuaValue[] values) { LuaTable table = values[0] as LuaTable; LuaValue index = values.Length > 1 ? values[1] : LuaNil.Nil; LuaValue prevKey = LuaNil.Nil; LuaValue nextIndex = LuaNil.Nil; foreach (var key in table.Keys) { if (prevKey.Equals(index)) { nextIndex = key; break; } prevKey = key; } return(new LuaMultiValue(new LuaValue[] { nextIndex, table.GetValue(nextIndex) })); }
private LuaValue InfixBinaryOperation(Term LeftOperand, string Operator, Term RightOperand, LuaTable enviroment) { LuaValue leftValue = LeftOperand.Evaluate(enviroment); LuaValue rightValue = RightOperand.Evaluate(enviroment); switch (Operator) { case "+": var left = leftValue as LuaNumber; var right = rightValue as LuaNumber; if (left != null && right != null) { return(new LuaNumber(left.Number + right.Number)); } else { LuaFunction func = GetMetaFunction("__add", leftValue, rightValue); if (func != null) { return(func.Invoke(new LuaValue[] { leftValue, rightValue })); } } break; case "-": left = leftValue as LuaNumber; right = rightValue as LuaNumber; if (left != null && right != null) { return(new LuaNumber(left.Number - right.Number)); } else { LuaFunction func = GetMetaFunction("__sub", leftValue, rightValue); if (func != null) { return(func.Invoke(new LuaValue[] { leftValue, rightValue })); } } break; case "*": left = leftValue as LuaNumber; right = rightValue as LuaNumber; if (left != null && right != null) { return(new LuaNumber(left.Number * right.Number)); } else { LuaFunction func = GetMetaFunction("__mul", leftValue, rightValue); if (func != null) { return(func.Invoke(new LuaValue[] { leftValue, rightValue })); } } break; case "/": left = leftValue as LuaNumber; right = rightValue as LuaNumber; if (left != null && right != null) { return(new LuaNumber(left.Number / right.Number)); } else { LuaFunction func = GetMetaFunction("__div", leftValue, rightValue); if (func != null) { return(func.Invoke(new LuaValue[] { leftValue, rightValue })); } } break; case "%": left = leftValue as LuaNumber; right = rightValue as LuaNumber; if (left != null && right != null) { return(new LuaNumber(left.Number % right.Number)); } else { LuaFunction func = GetMetaFunction("__mod", leftValue, rightValue); if (func != null) { return(func.Invoke(new LuaValue[] { leftValue, rightValue })); } } break; case "^": left = leftValue as LuaNumber; right = rightValue as LuaNumber; if (left != null && right != null) { return(new LuaNumber(Math.Pow(left.Number, right.Number))); } else { LuaFunction func = GetMetaFunction("__pow", leftValue, rightValue); if (func != null) { return(func.Invoke(new LuaValue[] { leftValue, rightValue })); } } break; case "==": return(LuaBoolean.From(leftValue.Equals(rightValue))); case "~=": return(LuaBoolean.From(leftValue.Equals(rightValue) == false)); case "<": int?compare = Compare(leftValue, rightValue); if (compare != null) { return(LuaBoolean.From(compare < 0)); } else { LuaFunction func = GetMetaFunction("__lt", leftValue, rightValue); if (func != null) { return(func.Invoke(new LuaValue[] { leftValue, rightValue })); } } break; case ">": compare = Compare(leftValue, rightValue); if (compare != null) { return(LuaBoolean.From(compare > 0)); } else { LuaFunction func = GetMetaFunction("__gt", leftValue, rightValue); if (func != null) { return(func.Invoke(new LuaValue[] { leftValue, rightValue })); } } break; case "<=": compare = Compare(leftValue, rightValue); if (compare != null) { return(LuaBoolean.From(compare <= 0)); } else { LuaFunction func = GetMetaFunction("__le", leftValue, rightValue); if (func != null) { return(func.Invoke(new LuaValue[] { leftValue, rightValue })); } } break; case ">=": compare = Compare(leftValue, rightValue); if (compare != null) { return(LuaBoolean.From(compare >= 0)); } else { LuaFunction func = GetMetaFunction("__ge", leftValue, rightValue); if (func != null) { return(func.Invoke(new LuaValue[] { leftValue, rightValue })); } } break; case "..": if ((leftValue is LuaString || leftValue is LuaNumber) && (rightValue is LuaString || rightValue is LuaNumber)) { return(new LuaString(string.Concat(leftValue, rightValue))); } else { LuaFunction func = GetMetaFunction("__concat", leftValue, rightValue); if (func != null) { return(func.Invoke(new LuaValue[] { leftValue, rightValue })); } } break; case "and": bool leftBool = leftValue.GetBooleanValue(); bool rightBool = rightValue.GetBooleanValue(); if (leftBool == false) { return(leftValue); } else { return(rightValue); } case "or": leftBool = leftValue.GetBooleanValue(); rightBool = rightValue.GetBooleanValue(); if (leftBool == true) { return(leftValue); } else { return(rightValue); } } return(null); }