Skip to content

Instantly share code, notes, and snippets.

@SimonDanisch
Created April 23, 2014 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimonDanisch/11228385 to your computer and use it in GitHub Desktop.
Save SimonDanisch/11228385 to your computer and use it in GitHub Desktop.
#Parses header files for the GLDefinitions
using Match, DictUtils
function writeDictFunctions(fs, dict)
write(fs, "#function bodies\n")
for elem in dict
expr = parse(elem[2])
if expr.args[1].head == :tuple
argTypes = "(" *join(map(arg-> string(arg.args[2]), expr.args[1].args), ',')* ")"
argNames = join(map(arg->string(arg.args[1]), expr.args[1].args), ',')
returnType = string(expr.args[2])
jArguments = string(expr.args[1])
else
argTypes = "(" *string(expr.args[1].args[2])* ",)"
argNames = string(expr.args[1].args[1])
returnType = string(expr.args[2])
jArguments = "(" *string(expr.args[1])* ")"
end
ccallFunc = "ccall(@getFuncPointer \"" *elem[1]* "\" , " *argTypes* ", " *returnType* ", " *argNames* ")"
jFunc = elem[1] *jArguments* " = " *ccallFunc* "::" *returnType* "\n"
write(fs, jFunc)
end
end
function writeDictConstants(fs, dict)
write(fs, "#constants\n")
for elem in dict
spaces = 64 - length(elem[1])
write(fs, "const " * elem[1] * " "^spaces *"= " *elem[2]*"\n")
end
end
function writeDictTypes(fs, dict)
write(fs, "#types\n")
for elem in dict
spaces = 60 - length(elem[1])
write(fs,"typealias " * elem[1] * " "^spaces * elem[2]*"\n")
end
end
sourceLocation = "deprecated/"
openGLFolder = "../"
GL_FILES = ["gl33", "gl42", "gl43"]
types = Dict{ASCIIString, Dict{ASCIIString, ASCIIString}}()
functions = Dict{ASCIIString, Dict{ASCIIString, ASCIIString}}()
constants = Dict{ASCIIString, Dict{ASCIIString, ASCIIString}}()
for file in GL_FILES
t, f, c = parseJLGLFile(sourceLocation*file*"/"*file*".jl")
types[file] = t
functions[file] = f
constants[file] = c
println("parsed: $(file)")
end
#We just create one file with all type definitions...
#Types need to be defined in the right order
mergedTypes = Dict{ASCIIString, ASCIIString}()
for elem in types
merge!(mergedTypes, elem[2])
end
typeValues = collect(values(mergedTypes))
allKeys = collect(keys(mergedTypes))
orderedKeys = ASCIIString[]
index = 0
# find out if value is defined, if yes it can be pushed into the ordered key Array
while length(orderedKeys) < length(typeValues)
for elem in mergedTypes
a = parse(elem[2])
if isa(a, Symbol)
if isdefined(a) && !in(elem[1], orderedKeys)
eval(parse("typealias $(elem[1]) $(elem[2])"))
push!(orderedKeys, elem[1])
end
elseif isa(a, Expr)
if isdefined(a.args[2]) && !in(elem[1], orderedKeys)
eval(parse("typealias $(elem[1]) $(elem[2])"))
push!(orderedKeys, elem[1])
end
end
end
end
# now we can write the files
fs = open(openGLFolder*"types.jl", "w")
write(fs, "#all OpenGL Types\n")
for elem in orderedKeys
spaces = 60 - length(elem)
write(fs,"typealias " * elem * " "^spaces * get(mergedTypes, elem, error)*"\n")
end
exports = join(orderedKeys, ", ")
write(fs, "export "*exports*"\n")
close(fs)
#Writing all the other files
for file in GL_FILES
_types = get(types, file, error)
_functions = get(functions, file, error)
_constants = get(constants, file, error)
exports = join([collect(keys(_functions)), collect(keys(_constants))], ", ")
fs = open(openGLFolder*"$(file).jl", "w")
writeDictFunctions(fs, _functions)
writeDictConstants(fs, _constants)
write(fs, "# Export everything!\n")
write(fs, "export "*exports*"\n")
close(fs)
println("written: $(openGLFolder)$(file).jl")
end
global const jlConstantRegex = r"const (GL_)?([[:print:]]+)=([[:print:]]+)"
global const jlTypeAliasRegex = r"typealias ([[:graph:]]+) ([[:graph:]]+)"
global const jlFunctionRegex = r"@getCFun \"[[:graph:]]+\" [[:graph:]]+ ([[:graph:]]+)(\([[:print:]]*\)::[[:graph:]]+)"
function parseJLGLFile(path::ASCIIString)
types = Dict{ASCIIString, ASCIIString}()
functions = Dict{ASCIIString, ASCIIString}()
constants = Dict{ASCIIString, ASCIIString}()
fstream = open(path)
lines = readlines(fstream)
close(fstream)
for line in lines
@match line begin
jlTypeAliasRegex(alias, _type) => types[strip(alias)] = strip(_type)
jlFunctionRegex(name, func) => functions[strip(name)] = strip(func)
jlConstantRegex(GL_, name, value) => constants["GL_"*strip(name)] = strip(value)
end
end
lines = 0
types, functions, constants
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment