Skip to content

Commit

Permalink
added csharp meta-example generation and include generated examples i…
Browse files Browse the repository at this point in the history
…n test build and cookbook
  • Loading branch information
karlnapf authored and vigsterkr committed Mar 10, 2016
1 parent f1e95fb commit b652fa3
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 3 deletions.
3 changes: 2 additions & 1 deletion doc/cookbook/source/conf.py
Expand Up @@ -189,7 +189,8 @@
('python', 'py'),
('octave', 'm'),
('r', 'R'),
('java', 'java')
('java', 'java'),
('csharp', 'cs'),
)

generated_examples_path = None
Expand Down
5 changes: 5 additions & 0 deletions examples/meta/CMakeLists.txt
Expand Up @@ -40,4 +40,9 @@ IF(ENABLE_TESTING)
IF (RModular AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/r)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/r)
ENDIF()

# csharp
IF (CSharpModular AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/csharp)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/csharp)
ENDIF()
ENDIF()
27 changes: 27 additions & 0 deletions examples/meta/csharp/CMakeLists.txt
@@ -0,0 +1,27 @@
SET(CSHARP_FLAGS "/lib:${CSHARP_MODULAR_BUILD_DIR};/r:modshogun")

# add test case for each generated example
# (not generated yet so have to fake filenames from META_EXAMPLES list)
FOREACH(META_EXAMPLE ${META_EXAMPLES})
# assume a structure <target_language>/<category>/listing.sg
STRING(REGEX REPLACE ".*/(.*).sg" "\\1" EXAMPLE_NAME ${META_EXAMPLE})
STRING(REGEX REPLACE ".*/(.*/.*).sg" "\\1" EXAMPLE_NAME_WITH_DIR ${META_EXAMPLE})
STRING(REGEX REPLACE "/" "-" EXAMPLE_NAME_WITH_DIR ${EXAMPLE_NAME_WITH_DIR})
STRING(REGEX REPLACE ".*/(.*)/.*.sg" "\\1" EXAMPLE_REL_DIR ${META_EXAMPLE})

ADD_CUSTOM_COMMAND(OUTPUT ${EXAMPLE_NAME_WITH_DIR}.exe
COMMAND ${CSHARP_COMPILER} ${EXAMPLE_NAME}.cs ${CSHARP_FLAGS} -out:${EXAMPLE_NAME}.exe
DEPENDS csharp_modular
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_REL_DIR})
LIST(APPEND CSHARP_EXAMPLES ${EXAMPLE_NAME_WITH_DIR}.exe)

add_test(NAME generated_csharp-${EXAMPLE_NAME_WITH_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${CSHARP_INTERPRETER} ${CMAKE_CURRENT_BINARY_DIR}/${EXAMPLE_REL_DIR}/${EXAMPLE_NAME}.exe)
set_property(TEST generated_csharp-${EXAMPLE_NAME_WITH_DIR} PROPERTY
ENVIRONMENT "MONO_PATH=${CSHARP_MODULAR_BUILD_DIR}")
ENDFOREACH()

add_custom_target(build_csharp_meta_examples ALL
DEPENDS ${CSHARP_EXAMPLES}
COMMENT "C# examples")
2 changes: 1 addition & 1 deletion examples/meta/generator/generate.py
Expand Up @@ -60,7 +60,7 @@ def translateExamples(inputDir, outputDir, targetsDir, includedTargets=None):
parser.add_argument("-o", "--output", help="path to output directory")
parser.add_argument("-i", "--input", help="path to examples directory (input)")
parser.add_argument("-t", "--targetsfolder", help="path to directory with target JSON files")
parser.add_argument('targets', nargs='*', help="Targets to include (one or more of: python java r octave). If not specified all targets are produced.")
parser.add_argument('targets', nargs='*', help="Targets to include (one or more of: python java r octave csharp). If not specified all targets are produced.")

args = parser.parse_args()

Expand Down
60 changes: 60 additions & 0 deletions examples/meta/generator/targets/csharp.json
@@ -0,0 +1,60 @@
{
"Program": "using System;\n\npublic class classifier_knn_modular {\n public static void Main() {\n modshogun.init_shogun_with_defaults();\n\n$program\n }\n}\n",
"Dependencies": {
"AllDependencies": "$enumDependencies\n\n",
"EnumDependencies": "$enums",
"DependencyListElementClass": "$element",
"DependencyListElementEnum": "import static org.shogun.$type.$value;",
"DependencyListSeparator": "\n"
},
"Statement": " $statement;\n",
"Comment": " //$comment\n",
"Init": {
"Construct": "$type $name = new $type($arguments)",
"Copy": "$type $name = $expr"
},
"Assign": "$name = $expr",
"Type": {
"Default": "$type",
"bool": "bool",
"string": "String",
"BoolVector": "bool[]",
"CharVector": "char[]",
"ByteVector": "byte[]",
"WordVector": "ushort[]",
"ShortVector": "short[]",
"IntVector": "int[]",
"LongIntVector": "long[]",
"ULongIntVector": "ulongint[]",
"ShortRealVector": "float[]",
"RealVector": "double[]",
"LongRealVector": "double[]",
"ComplexVector": "???",
"BoolMatrix": "bool[,]",
"CharMatrix": "char[,]",
"ByteMatrix": "byte[,]",
"WordMatrix": "ushort[,]",
"ShortMatrix": "short[,]",
"IntMatrix": "int[,]",
"LongIntMatrix": "long[,]",
"ULongIntMatrix": "ulongint[,]",
"ShortRealMatrix": "float[,]",
"RealMatrix": "float[,]",
"LongRealMatrix": "double[,]",
"ComplexMatrix": "????"
},
"Expr": {
"StringLiteral": "\"$literal\"",
"BoolLiteral": {
"True": "true",
"False": "false"
},
"NumberLiteral": "$number",
"MethodCall": "$object.$method($arguments)",
"Identifier": "$identifier",
"Enum":"$value"
},
"Print": "Console.Write($expr)",
"OutputDirectoryName": "csharp",
"FileExtension": ".cs"
}
2 changes: 1 addition & 1 deletion examples/meta/generator/translate.py
Expand Up @@ -286,7 +286,7 @@ def loadTargetDict(targetJsonPath):
if __name__ == "__main__":
# Parse command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--target", nargs='?', help="Translation target. Possible values: python, java, r, or octave. (default: python)")
parser.add_argument("-t", "--target", nargs='?', help="Translation target. Possible values: python, java, r, octave, csharp. (default: python)")
parser.add_argument("path", nargs='?', help="Path to input file. If not specified input is read from stdin")
args = parser.parse_args()

Expand Down

0 comments on commit b652fa3

Please sign in to comment.