public static void resume(LuaInterop lua) { LuaThread co = lua.Argument <LuaThread>(0); co.BeginResume(lua.ArgumentCount - 1); for (int argument = 0; argument < lua.ArgumentCount - 1; ++argument) { co.ResumeArgument(argument, lua.Argument(argument)); } int resultCount; try { resultCount = co.Resume(LuaThread.VarResult); } catch (Exception e) { lua.BeginReturn(2); lua.ReturnResult(0, false); lua.ReturnResult(1, e.Message); lua.EndReturn(); return; } lua.BeginReturn(resultCount + 1); lua.ReturnResult(0, true); for (int result = 0; result < resultCount; ++result) { lua.ReturnResult(result + 1, co.ResumeResult(result)); } co.EndResume(); lua.EndReturn(); }
public static void print(LuaInterop lua) { for (int argument = 0; argument < lua.ArgumentCount; ++argument) { if (argument > 0) { Console.Out.Write("\t"); } Console.Out.Write(tostring(lua.Argument(argument))); } Console.Out.Write("\n"); }
public void write(LuaInterop lua) { EnsureWriter(); for (int argument = 0; argument < lua.ArgumentCount; ++argument) { string type = lua.ArgumentType(argument); if (type == "string") { writer.Write(lua.Argument <string>(argument)); } else if (type == "number") { writer.Write(lua.Argument <double>(argument).ToString("G14")); } else { throw new ArgumentException("write() only accepts strings or numbers as arguments."); } } lua.Return(); }
public void read(LuaInterop lua) { EnsureReader(); if (lua.ArgumentCount > 0) { List <LuaValue> results = new List <LuaValue>(); for (int argument = 0; argument < lua.ArgumentCount; ++argument) { string type = lua.ArgumentType(argument); if (type == "number") { int number = lua.Argument <int>(argument); if (number > 0) { // Read actual number of characters. char[] s = new char[number]; int count = reader.ReadBlock(s, 0, number); if (count > 0) { results.Add(new String(s, 0, count)); } else { results.Add(null); } } else { // Check for EOF. if (reader.Peek() != -1) { results.Add(""); } else { results.Add(null); } } } else { string format = lua.Argument <string>(argument); if (format == "*n") { StringBuilder s = new StringBuilder(); // Read a number token from the file. while (Char.IsWhiteSpace(GetChar())) { Shift(); } if (GetChar() == '+' || GetChar() == '-') { s.Append(Shift()); } while (Char.IsDigit(GetChar()) || GetChar() == '.') { s.Append(Shift()); } if (GetChar() == 'e' || GetChar() == 'E') { s.Append(Shift()); if (GetChar() == '+' || GetChar() == '-') { s.Append(Shift()); } } while (Char.IsLetter(GetChar()) || Char.IsNumber(GetChar()) || GetChar() == '_') { s.Append(Shift()); } // Parse the number. int integerValue; double doubleValue; string v = s.ToString(); if (v.StartsWith("0x") && Int32.TryParse(v.Substring(2), NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo, out integerValue)) { results.Add(integerValue); } else if (Int32.TryParse(v, NumberStyles.Number, NumberFormatInfo.InvariantInfo, out integerValue)) { results.Add(integerValue); } else if (Double.TryParse(v, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out doubleValue)) { results.Add(doubleValue); } else { results.Add(null); } } else if (format == "*a") { results.Add(reader.ReadToEnd() ?? ""); } else if (format == "*l") { results.Add(reader.ReadLine()); } } } lua.BeginReturn(results.Count); for (int result = 0; result < results.Count; ++result) { lua.ReturnResult(result, results[result]); } lua.EndReturn(); } else { lua.Return(reader.ReadLine()); } }
public static void format(LuaInterop lua) { string format = lua.Argument <string>(0); StringBuilder s = new StringBuilder(); // Iterate through conversion specifications in format. int argument = 1; int start = 0; while (true) { // Find next format specification. int p = format.IndexOf('%', start); if (p == -1) { s.Append(format.Substring(start)); break; } // Add chunk of format string. s.Append(format.Substring(start, p - start)); // Parse conversion specification. char c = format[p++]; // Flags. bool bLeftJustify = false; bool bSignPlus = false; bool bSignSpace = false; bool bAlternateForm = false; bool bPadWithZeroes = false; while (true) { c = format[p++]; switch (c) { case '-': bLeftJustify = true; continue; case '+': bSignPlus = true; continue; case ' ': bSignSpace = true; continue; case '#': bAlternateForm = true; continue; case '0': bPadWithZeroes = true; continue; } break; } if (bSignSpace && bSignPlus) { bSignSpace = false; } if (bPadWithZeroes && bLeftJustify) { bPadWithZeroes = false; } // Field width. int fieldWidth = 0; while (c >= '0' && c <= '9') { int digit = c - '0'; fieldWidth = fieldWidth * 10 + digit; c = format[p++]; } // Precision. int precision = -1; if (c == '.') { precision = 0; c = format[p++]; bool bNegativePrecision = false; if (c == '-') { bNegativePrecision = true; c = format[p++]; } while (c >= '0' && c <= '9') { int digit = c - '0'; precision = precision * 10 + digit; c = format[p++]; } if (bNegativePrecision) { precision = -1; } } // Conversion specifier cheracter. switch (c) { // Integer conversions. case 'd': case 'i': { string value = IntegerFormat(lua.Argument <int>(argument++), "D", precision, ref bPadWithZeroes); value = AddSign(value, bSignPlus, bSignSpace); if (bPadWithZeroes) { AppendPadded(s, value, fieldWidth); } else { AppendJustified(s, value, fieldWidth, bLeftJustify); } break; } case 'o': { StringBuilder o = new StringBuilder(); uint number = lua.Argument <uint>(argument++); for (uint remainder = number; remainder > 0; remainder >>= 3) { o.Insert(0, (char)('0' + (remainder & 7))); } if (bAlternateForm) { precision = o.Length + 1; } if (precision >= 0) { bPadWithZeroes = false; for (int zero = 0; zero < precision; ++zero) { o.Insert(0, '0'); } } else { if (number == 0) { o.Append('0'); } } string value = AddSign(o.ToString(), bSignPlus, bSignSpace); if (bPadWithZeroes) { AppendPadded(s, value, fieldWidth); } else { AppendJustified(s, value, fieldWidth, bLeftJustify); } break; } case 'u': { string value = IntegerFormat(lua.Argument <uint>(argument++), "D", precision, ref bPadWithZeroes); if (bPadWithZeroes) { AppendPadded(s, value, fieldWidth); } else { AppendJustified(s, value, fieldWidth, bLeftJustify); } break; } case 'x': case 'X': { string f = c.ToString(); string value = IntegerFormat(lua.Argument <int>(argument++), f, precision, ref bPadWithZeroes); if (bAlternateForm) { value = "0" + f + value; } if (bPadWithZeroes) { AppendPadded(s, value, fieldWidth); } else { AppendJustified(s, value, fieldWidth, bLeftJustify); } break; } // Floating-point conversions. case 'f': case 'F': { double v = lua.Argument <double>(argument++); if (precision == -1) { precision = 6; } string value = v.ToString("F" + precision.ToString()); if (!Double.IsInfinity(v) && !!Double.IsNaN(v)) { // Add decimal point if forced. if (bAlternateForm && precision == 0) { value += "."; } // Print. value = AddSign(value, bSignPlus, bSignSpace); if (bPadWithZeroes) { AppendPadded(s, value, fieldWidth); break; } } AppendJustified(s, value, fieldWidth, bLeftJustify); break; } case 'e': case 'E': { string f = c.ToString(); double v = lua.Argument <double>(argument++); if (precision == -1) { precision = 6; } string value = v.ToString(f + precision.ToString()); if (!Double.IsInfinity(v) && !Double.IsNaN(v)) { // Add decimal point if forced. if (bAlternateForm) { int e = value.IndexOf(Char.IsUpper(c) ? 'E' : 'e'); if (e == -1) { e = value.Length; } if (value.IndexOf(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator) == -1) { value = value.Insert(e, NumberFormatInfo.CurrentInfo.NumberDecimalSeparator); } } // Print. value = AddSign(value, bSignPlus, bSignSpace); if (bPadWithZeroes) { AppendPadded(s, value, fieldWidth); break; } } AppendJustified(s, value, fieldWidth, bLeftJustify); break; } case 'g': case 'G': { string f = c.ToString(); double v = lua.Argument <double>(argument++); if (precision == -1) { precision = 6; } if (precision == 0) { precision = 1; } string value = v.ToString(f + precision.ToString()); if (!Double.IsInfinity(v) && !Double.IsNaN(v)) { // Add in decimal point and trailing zeroes if alternate form. if (bAlternateForm) { int e = value.IndexOf(Char.IsUpper(c) ? 'E' : 'e'); if (e == -1) { e = value.Length; } if (value.IndexOf(NumberFormatInfo.CurrentInfo.NumberDecimalSeparator) == -1) { value = value.Insert(e, NumberFormatInfo.CurrentInfo.NumberDecimalSeparator); e += 1; } int length = precision + 1; if (value.StartsWith(NumberFormatInfo.CurrentInfo.NegativeSign)) { length += NumberFormatInfo.CurrentInfo.NegativeSign.Length; } while (e < length) { value = value.Insert(e, "0"); e += 1; } } // Print. value = AddSign(value, bSignPlus, bSignSpace); if (bPadWithZeroes) { AppendPadded(s, value, fieldWidth); break; } } AppendJustified(s, value, fieldWidth, bLeftJustify); break; } // String conversions. case 'c': { int value = lua.Argument <int>(argument++); AppendJustified(s, Char.ConvertFromUtf32(value), fieldWidth, bLeftJustify); break; } case 's': { string value = lua.Argument <string>(argument++); if (precision != -1 && value.Length > precision) { value = value.Remove(precision); } AppendJustified(s, value, fieldWidth, bLeftJustify); break; } case 'q': { string value = lua.Argument <string>(argument++); StringBuilder q = new StringBuilder(); q.Append('"'); start = 0; for (int i = 0; i < value.Length; ++i) { string escape = null; switch (value[i]) { case '"': escape = "\\\""; break; case '\n': escape = "\\n"; break; case '\0': escape = "\\0"; break; case '\\': escape = "\\\\"; break; } if (escape != null) { q.Append(value, start, i - start); q.Append(escape); start = i + 1; } } q.Append(value, start, value.Length - start); q.Append('"'); value = q.ToString(); if (precision != -1 && value.Length > precision) { value = value.Remove(precision); } AppendJustified(s, value, fieldWidth, bLeftJustify); break; } case '%': { s.Append('%'); break; } // Invalid conversion. default: throw new FormatException("Invalid format conversion specifier '%" + c.ToString() + "'."); } start = p; } lua.Return(s.ToString()); }