parent
734f288290
commit
7a5474ec28
|
@ -7,7 +7,7 @@ using System.CodeDom.Compiler;
|
||||||
|
|
||||||
namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
|
namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
|
||||||
{
|
{
|
||||||
class Compiler
|
public class Compiler
|
||||||
{
|
{
|
||||||
private LSL2CS.Converter.LSL2CSConverter LSL_Converter = new LSL2CS.Converter.LSL2CSConverter();
|
private LSL2CS.Converter.LSL2CSConverter LSL_Converter = new LSL2CS.Converter.LSL2CSConverter();
|
||||||
private CSharpCodeProvider codeProvider = new CSharpCodeProvider();
|
private CSharpCodeProvider codeProvider = new CSharpCodeProvider();
|
||||||
|
@ -15,7 +15,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
|
||||||
public string Compile(string LSOFileName)
|
public string Compile(string LSOFileName)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
string OutFile = Path.GetFileNameWithoutExtension(LSOFileName);
|
string OutFile = Path.GetFileNameWithoutExtension(LSOFileName);
|
||||||
|
|
||||||
// TODO: Add error handling
|
// TODO: Add error handling
|
||||||
|
@ -37,7 +37,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return OutFile;
|
return OutFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ namespace LSL2CS.Converter
|
||||||
//private Regex rnw = new Regex(@"[a-zA-Z0-9_\-]", RegexOptions.Compiled);
|
//private Regex rnw = new Regex(@"[a-zA-Z0-9_\-]", RegexOptions.Compiled);
|
||||||
private Dictionary<string, string> DataTypes = new Dictionary<string, string>();
|
private Dictionary<string, string> DataTypes = new Dictionary<string, string>();
|
||||||
private Dictionary<string, string> QUOTES = new Dictionary<string, string>();
|
private Dictionary<string, string> QUOTES = new Dictionary<string, string>();
|
||||||
|
|
||||||
public LSL2CSConverter()
|
public LSL2CSConverter()
|
||||||
{
|
{
|
||||||
DataTypes.Add("void", "void");
|
DataTypes.Add("void", "void");
|
||||||
|
@ -29,8 +29,6 @@ namespace LSL2CS.Converter
|
||||||
public string Convert(string Script)
|
public string Convert(string Script)
|
||||||
{
|
{
|
||||||
string Return = "";
|
string Return = "";
|
||||||
QUOTES.Clear();
|
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Prepare script for processing
|
// Prepare script for processing
|
||||||
|
@ -64,10 +62,6 @@ namespace LSL2CS.Converter
|
||||||
// Return += count + ". " + translate(line) + "\r\n";
|
// Return += count + ". " + translate(line) + "\r\n";
|
||||||
//}
|
//}
|
||||||
|
|
||||||
int level = 0;
|
|
||||||
bool Level0Set = false;
|
|
||||||
bool Level1Set = false;
|
|
||||||
|
|
||||||
|
|
||||||
// QUOTE REPLACEMENT
|
// QUOTE REPLACEMENT
|
||||||
//char[] SA = Script.ToCharArray();
|
//char[] SA = Script.ToCharArray();
|
||||||
|
@ -77,31 +71,41 @@ namespace LSL2CS.Converter
|
||||||
bool quote_replaced = false;
|
bool quote_replaced = false;
|
||||||
string quote_replacement_string = "Q_U_O_T_E_REPLACEMENT_";
|
string quote_replacement_string = "Q_U_O_T_E_REPLACEMENT_";
|
||||||
string quote = "";
|
string quote = "";
|
||||||
|
bool last_was_escape = false;
|
||||||
int quote_replaced_count = 0;
|
int quote_replaced_count = 0;
|
||||||
for (int p = 0; p < Script.Length; p++)
|
for (int p = 0; p < Script.Length; p++)
|
||||||
{
|
{
|
||||||
|
|
||||||
while (true) {
|
C = Script.Substring(p, 1);
|
||||||
C = Script.Substring(p,1);
|
while (true)
|
||||||
if (C == "\"") {
|
{
|
||||||
|
if (C == "\"" && last_was_escape == false)
|
||||||
|
{
|
||||||
// Toggle inside/outside quote
|
// Toggle inside/outside quote
|
||||||
in_quote = ! in_quote;
|
in_quote = !in_quote;
|
||||||
if(in_quote) {
|
if (in_quote)
|
||||||
|
{
|
||||||
quote_replaced_count++;
|
quote_replaced_count++;
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
// We just left a quote
|
// We just left a quote
|
||||||
QUOTES.Add(quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]), quote);
|
QUOTES.Add(quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]), quote);
|
||||||
quote = "";
|
quote = "";
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!in_quote) {
|
if (!in_quote)
|
||||||
|
{
|
||||||
// We are not inside a quote
|
// We are not inside a quote
|
||||||
quote_replaced = false;
|
quote_replaced = false;
|
||||||
|
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
// We are inside a quote
|
// We are inside a quote
|
||||||
if (!quote_replaced) {
|
if (!quote_replaced)
|
||||||
|
{
|
||||||
// Replace quote
|
// Replace quote
|
||||||
_Script += quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
|
_Script += quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
|
||||||
quote_replaced = true;
|
quote_replaced = true;
|
||||||
|
@ -110,35 +114,135 @@ namespace LSL2CS.Converter
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_Script += C;
|
_Script += C;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
last_was_escape = false;
|
||||||
|
if (C == @"\")
|
||||||
|
{
|
||||||
|
last_was_escape = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Script = _Script;
|
||||||
//
|
//
|
||||||
// END OF QUOTE REPLACEMENT
|
// END OF QUOTE REPLACEMENT
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// PROCESS STATES
|
||||||
|
int ilevel = 0;
|
||||||
|
int lastlevel = 0;
|
||||||
|
string ret = "";
|
||||||
|
string cache = "";
|
||||||
|
bool in_state = false;
|
||||||
|
string current_statename = "";
|
||||||
|
for (int p = 0; p < Script.Length; p++)
|
||||||
|
{
|
||||||
|
C = Script.Substring(p, 1);
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
// inc / dec level
|
||||||
|
if (C == @"{")
|
||||||
|
ilevel++;
|
||||||
|
if (C == @"}")
|
||||||
|
ilevel--;
|
||||||
|
if (ilevel < 0)
|
||||||
|
ilevel = 0;
|
||||||
|
cache += C;
|
||||||
|
|
||||||
|
// if level == 0, add to return
|
||||||
|
if (ilevel == 1 && lastlevel == 0)
|
||||||
|
{
|
||||||
|
// 0 => 1: Get last
|
||||||
|
Match m = Regex.Match(cache, @"(?![a-zA-Z_]+)\s*([a-zA-Z_]+)[^a-zA-Z_\(\)]*{", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||||
|
|
||||||
|
in_state = false;
|
||||||
|
if (m.Success)
|
||||||
|
{
|
||||||
|
// Go back to level 0, this is not a state
|
||||||
|
in_state = true;
|
||||||
|
current_statename = m.Groups[1].Captures[0].Value;
|
||||||
|
Console.WriteLine("Current statename: " + current_statename);
|
||||||
|
cache = Regex.Replace(cache, @"(?![a-zA-Z_]+)\s*([a-zA-Z_]+)[^a-zA-Z_\(\)]*{", "", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||||
|
}
|
||||||
|
ret += cache;
|
||||||
|
cache = "";
|
||||||
|
}
|
||||||
|
if (ilevel == 0 && lastlevel == 1)
|
||||||
|
{
|
||||||
|
// 1 => 0: Remove last }
|
||||||
|
if (in_state == true)
|
||||||
|
{
|
||||||
|
cache = cache.Remove(cache.Length - 1, 1);
|
||||||
|
//cache = Regex.Replace(cache, "}$", "", RegexOptions.Multiline | RegexOptions.Singleline);
|
||||||
|
|
||||||
|
//Replace function names
|
||||||
|
// void dataserver(key query_id, string data) {
|
||||||
|
//cache = Regex.Replace(cache, @"([^a-zA-Z_]\s*)((?!if|switch|for)[a-zA-Z_]+\s*\([^\)]*\)[^{]*{)", "$1" + "<STATE>" + "$2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||||
|
Console.WriteLine("Replacing using statename: " + current_statename);
|
||||||
|
cache = Regex.Replace(cache, @"^(\s*)((?!if|switch|for)[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)", @"$1" + current_statename + "_$2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||||
|
}
|
||||||
|
|
||||||
|
ret += cache;
|
||||||
|
cache = "";
|
||||||
|
in_state = true;
|
||||||
|
current_statename = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// if we moved from level 0 to level 1 don't add last word + {
|
||||||
|
// if we moved from level 1 to level 0 don't add last }
|
||||||
|
// if level > 0 cache all data so we can run regex on it when going to level 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
lastlevel = ilevel;
|
||||||
|
}
|
||||||
|
ret += cache;
|
||||||
|
cache = "";
|
||||||
|
|
||||||
|
Script = ret;
|
||||||
|
ret = "";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Replace CAST - (integer) with (int)
|
// Replace CAST - (integer) with (int)
|
||||||
Script = Regex.Replace(_Script, @"\(integer\)", @"(int)", RegexOptions.Compiled | RegexOptions.Multiline);
|
Script = Regex.Replace(Script, @"\(integer\)", @"(int)", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||||
// Replace return types and function variables - integer a() and f(integer a, integer a)
|
// Replace return types and function variables - integer a() and f(integer a, integer a)
|
||||||
_Script = Regex.Replace(Script, @"(^|[\(,])(\s*int)eger(\s*)", @"$1$2$3", RegexOptions.Compiled | RegexOptions.Multiline);
|
Script = Regex.Replace(Script, @"(^|;|}|[\(,])(\s*int)eger(\s*)", @"$1$2$3", RegexOptions.Compiled | RegexOptions.Multiline);
|
||||||
|
|
||||||
// Add void to functions not having that
|
// Add "void" in front of functions that needs it
|
||||||
Script = Regex.Replace(_Script, @"^(\s*)((?!if|switch|for|foreach)[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)", @"$1void $2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
Script = Regex.Replace(Script, @"^(\s*)((?!if|switch|for)[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)", @"$1void $2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||||
|
|
||||||
|
// Replace <x,y,z> and <x,y,z,r>
|
||||||
|
Script = Regex.Replace(Script, @"<([^,>]*,[^,>]*,[^,>]*,[^,>]*)>", @"Rotation.Parse($1)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||||
|
Script = Regex.Replace(Script, @"<([^,>]*,[^,>]*,[^,>]*)>", @"Vector.Parse($1)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||||
|
|
||||||
|
// Replace List []'s
|
||||||
|
Script = Regex.Replace(Script, @"\[([^\]]*)\]", @"List.Parse($1)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||||
|
|
||||||
|
|
||||||
// Replace int (TEMPORARY QUICKFIX)
|
// Replace (string) to .ToString() //
|
||||||
//_Script = Regex.Replace(Script, "integer", "int", RegexOptions.Compiled);
|
Script = Regex.Replace(Script, @"\(string\)\s*([a-zA-Z0-9_]+(\s*\([^\)]*\))?)", @"$1.ToString()", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||||
|
Script = Regex.Replace(Script, @"\((float|int)\)\s*([a-zA-Z0-9_]+(\s*\([^\)]*\))?)", @"$1.Parse($2)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
|
||||||
|
|
||||||
|
|
||||||
|
// REPLACE BACK QUOTES
|
||||||
foreach (string key in QUOTES.Keys)
|
foreach (string key in QUOTES.Keys)
|
||||||
{
|
{
|
||||||
string val;
|
string val;
|
||||||
QUOTES.TryGetValue(key, out val);
|
QUOTES.TryGetValue(key, out val);
|
||||||
_Script = Script.Replace(key, "\"" + val + "\"");
|
Script = Script.Replace(key, "\"" + val + "\"");
|
||||||
Script = _Script;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Return = Script;
|
Return = "namespace SecondLife {" + Environment.NewLine;
|
||||||
|
Return += "public class Script : LSL_BaseClass {" + Environment.NewLine;
|
||||||
|
Return += Script;
|
||||||
|
Return += "} }" + Environment.NewLine;
|
||||||
|
|
||||||
return Return;
|
return Return;
|
||||||
}
|
}
|
||||||
|
@ -154,5 +258,7 @@ namespace LSL2CS.Converter
|
||||||
// return ret;
|
// return ret;
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL
|
||||||
|
{
|
||||||
|
class LSL_BaseClass
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue