Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8753393
commit f99c199
Showing
9 changed files
with
110 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Copyright (c) 2012 Erik Svedäng, Johannes Gotlén | ||
|
||
This software is provided 'as-is', without any express or implied | ||
warranty. In no event will the authors be held liable for any damages | ||
arising from the use of this software. | ||
Permission is granted to anyone to use this software for any purpose, | ||
including commercial applications, and to alter it and redistribute it | ||
freely, subject to the following restrictions: | ||
1. The origin of this software must not be misrepresented; you must not | ||
claim that you wrote the original software. If you use this software | ||
in a product, an acknowledgment in the product documentation would be | ||
appreciated but is not required. | ||
2. Altered source versions must be plainly marked as such, and must not be | ||
misrepresented as being the original software. | ||
3. This notice may not be removed or altered from any source distribution. |
Binary file not shown.
Binary file not shown.
Binary file not shown.
189 changes: 95 additions & 94 deletions
189
ProgrammingLanguageNr1/src/FunctionDefinitionCreator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,95 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Reflection; | ||
namespace ProgrammingLanguageNr1 | ||
{ | ||
public class SprakAPI : Attribute | ||
{ | ||
public string[] Values { get; set; } | ||
public SprakAPI(params string[] values) | ||
{ | ||
this.Values = values; | ||
} | ||
} | ||
|
||
public class FunctionDefinitionCreator | ||
{ | ||
public static FunctionDefinition[] CreateDefinitions(object o, Type t) | ||
{ | ||
List<FunctionDefinition> methods = new List<FunctionDefinition>(); | ||
Dictionary<string, FunctionDocumentation> HelpInfo = new Dictionary<string, FunctionDocumentation>(); | ||
MethodInfo[] methodInfos = t.GetMethods(); | ||
foreach (MethodInfo methodI in methodInfos) | ||
{ | ||
if (methodI.Name.StartsWith("API_")) | ||
{ | ||
SprakAPI[] help = (SprakAPI[])methodI.GetCustomAttributes(typeof(SprakAPI), true); | ||
if (help.Length > 0) | ||
{ | ||
Console.WriteLine("found " + String.Join( ",", help[0].Values)); | ||
List<string> parameterHelp = new List<string>(); | ||
for (int i = 1; i < help[0].Values.Length; i++) | ||
parameterHelp.Add(help[0].Values[i]); | ||
string shortname = methodI.Name.Substring(4); | ||
FunctionDocumentation fd = new FunctionDocumentation(help[0].Values[0], parameterHelp.ToArray()); | ||
HelpInfo.Add(shortname, fd); | ||
} | ||
} | ||
} | ||
foreach (MethodInfo mi in methodInfos) | ||
{ | ||
if (mi.Name.StartsWith("API_")) | ||
{ | ||
Console.WriteLine("parsing " + mi.Name + " return Type " + mi.ReturnType.Name); | ||
string shortname = mi.Name.Substring(4); | ||
if(mi.ReturnType.IsArray) | ||
throw new Exception("FunctionDefinitionCreator can't handle array return value!"); | ||
List<ReturnValueType> parameterTypes = new List<ReturnValueType>(); | ||
List<string> parameterNames = new List<string>(); | ||
List<string> parameterTypeNames = new List<string>(); | ||
foreach (ParameterInfo pi in mi.GetParameters()) | ||
{ | ||
if (pi.ParameterType.IsArray) | ||
throw new Exception("FunctionDefinitionCreator can't handle array parameters!"); | ||
|
||
parameterNames.Add(pi.Name); | ||
parameterTypes.Add(ReturnValue.SystemTypeToReturnValueType(pi.ParameterType)); | ||
parameterTypeNames.Add(ReturnValue.SystemTypeToReturnValueType(pi.ParameterType).ToString().ToLower()); | ||
} | ||
MethodInfo lamdaMethodInfo = mi; | ||
ExternalFunctionCreator.OnFunctionCall function = (retvals) => | ||
{ | ||
int i = 0; | ||
ParameterInfo[] realParamInfo = lamdaMethodInfo.GetParameters(); | ||
List<object> parameters = new List<object>(); | ||
foreach (ReturnValue r in retvals) | ||
{ | ||
if (realParamInfo[i++].ParameterType == typeof(int)) | ||
parameters.Add(Convert.ToInt32(r.Unpack())); | ||
else | ||
parameters.Add(r.Unpack()); | ||
} | ||
//Console.WriteLine("supplied parameter count" + parameters.Count + " neededParamter count " + lamdaMethodInfo.GetParameters().Length); | ||
object result = lamdaMethodInfo.Invoke(o, parameters.ToArray()); | ||
if (lamdaMethodInfo.ReturnType == typeof(void)) | ||
return new ReturnValue(ReturnValueType.VOID); | ||
else | ||
{ | ||
return new ReturnValue(ReturnValue.SystemTypeToReturnValueType(lamdaMethodInfo.ReturnType), result); | ||
} | ||
}; | ||
ReturnValueType returnValueType = ReturnValue.SystemTypeToReturnValueType(mi.ReturnType); | ||
FunctionDocumentation doc; | ||
if (HelpInfo.TryGetValue(shortname, out doc)) | ||
methods.Add(new FunctionDefinition(returnValueType.ToString(), shortname, parameterTypeNames.ToArray(), parameterNames.ToArray(), function, doc)); | ||
else | ||
methods.Add(new FunctionDefinition(returnValueType.ToString(), shortname, parameterTypeNames.ToArray(), parameterNames.ToArray(), function, FunctionDocumentation.Default())); | ||
} | ||
} | ||
return methods.ToArray(); | ||
} | ||
} | ||
} | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Reflection; | ||
|
||
namespace ProgrammingLanguageNr1 | ||
{ | ||
public class SprakAPI : Attribute | ||
{ | ||
public string[] Values { get; set; } | ||
public SprakAPI(params string[] values) | ||
{ | ||
this.Values = values; | ||
} | ||
} | ||
|
||
public class FunctionDefinitionCreator | ||
{ | ||
public static FunctionDefinition[] CreateDefinitions(object o, Type t) | ||
{ | ||
List<FunctionDefinition> methods = new List<FunctionDefinition>(); | ||
Dictionary<string, FunctionDocumentation> HelpInfo = new Dictionary<string, FunctionDocumentation>(); | ||
MethodInfo[] methodInfos = t.GetMethods(); | ||
foreach (MethodInfo methodI in methodInfos) | ||
{ | ||
if (methodI.Name.StartsWith("API_")) | ||
{ | ||
SprakAPI[] help = (SprakAPI[])methodI.GetCustomAttributes(typeof(SprakAPI), true); | ||
if (help.Length > 0) | ||
{ | ||
//Console.WriteLine("found " + String.Join( ",", help[0].Values)); | ||
List<string> parameterHelp = new List<string>(); | ||
for (int i = 1; i < help[0].Values.Length; i++) | ||
parameterHelp.Add(help[0].Values[i]); | ||
string shortname = methodI.Name.Substring(4); | ||
FunctionDocumentation fd = new FunctionDocumentation(help[0].Values[0], parameterHelp.ToArray()); | ||
HelpInfo.Add(shortname, fd); | ||
} | ||
} | ||
} | ||
foreach (MethodInfo mi in methodInfos) | ||
{ | ||
if (mi.Name.StartsWith("API_")) | ||
{ | ||
//Console.WriteLine("parsing " + mi.Name + " return Type " + mi.ReturnType.Name); | ||
string shortname = mi.Name.Substring(4); | ||
if(mi.ReturnType.IsArray) | ||
throw new Exception("FunctionDefinitionCreator can't handle array return value!"); | ||
List<ReturnValueType> parameterTypes = new List<ReturnValueType>(); | ||
List<string> parameterNames = new List<string>(); | ||
List<string> parameterTypeNames = new List<string>(); | ||
foreach (ParameterInfo pi in mi.GetParameters()) | ||
{ | ||
if (pi.ParameterType.IsArray) | ||
throw new Exception("FunctionDefinitionCreator can't handle array parameters!"); | ||
|
||
parameterNames.Add(pi.Name); | ||
parameterTypes.Add(ReturnValue.SystemTypeToReturnValueType(pi.ParameterType)); | ||
parameterTypeNames.Add(ReturnValue.SystemTypeToReturnValueType(pi.ParameterType).ToString().ToLower()); | ||
} | ||
MethodInfo lamdaMethodInfo = mi; | ||
ExternalFunctionCreator.OnFunctionCall function = (retvals) => | ||
{ | ||
int i = 0; | ||
ParameterInfo[] realParamInfo = lamdaMethodInfo.GetParameters(); | ||
List<object> parameters = new List<object>(); | ||
foreach (ReturnValue r in retvals) | ||
{ | ||
if (realParamInfo[i++].ParameterType == typeof(int)) | ||
parameters.Add(Convert.ToInt32(r.Unpack())); | ||
else | ||
parameters.Add(r.Unpack()); | ||
} | ||
//Console.WriteLine("supplied parameter count" + parameters.Count + " neededParamter count " + lamdaMethodInfo.GetParameters().Length); | ||
object result = lamdaMethodInfo.Invoke(o, parameters.ToArray()); | ||
if (lamdaMethodInfo.ReturnType == typeof(void)) | ||
return new ReturnValue(ReturnValueType.VOID); | ||
else | ||
{ | ||
return new ReturnValue(ReturnValue.SystemTypeToReturnValueType(lamdaMethodInfo.ReturnType), result); | ||
} | ||
}; | ||
ReturnValueType returnValueType = ReturnValue.SystemTypeToReturnValueType(mi.ReturnType); | ||
FunctionDocumentation doc; | ||
if (HelpInfo.TryGetValue(shortname, out doc)) | ||
methods.Add(new FunctionDefinition(returnValueType.ToString(), shortname, parameterTypeNames.ToArray(), parameterNames.ToArray(), function, doc)); | ||
else | ||
methods.Add(new FunctionDefinition(returnValueType.ToString(), shortname, parameterTypeNames.ToArray(), parameterNames.ToArray(), function, FunctionDocumentation.Default())); | ||
} | ||
} | ||
return methods.ToArray(); | ||
} | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.