Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
Summary Activity SCM

SCM Repository

[sml3d] View of /trunk/sml3d/gen/gen-from-xml/glgen/gen-consts.sml
ViewVC logotype

View of /trunk/sml3d/gen/gen-from-xml/glgen/gen-consts.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1369 - (download) (annotate)
Tue Apr 1 23:00:51 2014 UTC (5 years ago) by jhr
File size: 2425 byte(s)
  working on generation tools
(* gen-consts.sml
 *
 * COPYRIGHT (c) 2014 The SML3d Project (http://sml3d.cs.uchicago.edu)
 * All rights reserved.
 *
 *)

structure GenConsts : sig

  (* Generate a structure containing all of the constant (i.e., enum) definitions
   * in the database.
   *)
    val gen : {db : DBXMLRep.db, outFile : string, structName : string} -> unit

    val test : string -> unit

  end = struct

    structure X = DBXMLRep
    structure F = Format

  (* header and footer for the output file *)
    val header = "\
	  \(* %s\n\
	  \ *\n\
	  \ * COPYRIGHT (c) %4d The SML3d Project (http://sml3d.cs.uchicago.edu)\n\
	  \ * All rights reserved.\n\
	  \ *\n\
	  \ * These are the OpenGL constants taken from %s.\n\
          \ *\n\
          \ * WARNING: this file is generated; do not edit!!!\n\
	  \ *)\n\
	  \\n\
	  \structure %s =\n\
	  \  struct\n\
	  \\n\
	  \    type glenum = GLTypes.glenum\n\
	  \\n\
          \"
    val footer = "\
	  \\n\
	  \  end (* %s *)\n\
          \"

    val a_GLint = Atom.atom "GLint"
    val a_GLuint = Atom.atom "GLuint"
    val a_GLuint64 = Atom.atom "GLuint64"

    fun gen {db, outFile, structName} = let
          val outS = TextIO.openOut outFile
          fun pr s = TextIO.output(outS, s)
          fun prf (fmt, items) = pr(F.format fmt items)
          val X.DB{registry, enums, ...} = db
	  fun genEnum {name, ty, value} = let
		val (isSigned, mlty) = (case ty
		       of NONE => (true, "glint")
			| SOME(CType.NamedTy ty) =>
			    if Atom.same(ty, a_GLint) then (true, "glint")
			    else if Atom.same(ty, a_GLuint) then (false, "glenum")
			    else if Atom.same(ty, a_GLuint64) then (false, "glint64")
			    else raise Fail "unknown C type"
			| _ => raise Fail "unknown C type"
		      (* end case *))
		in
		  if isSigned
		    then prf("    val %s : %s = %d\n", [F.ATOM name, F.STR mlty, F.LINT value])
		    else prf("    val %s : %s = 0wx%08x\n", [F.ATOM name, F.STR mlty, F.LINT value])
		end
          in
            prf (header, [
		F.STR(OS.Path.file outFile), F.INT(Date.year(Date.fromTimeLocal(Time.now()))),
		F.STR registry, F.STR structName
	      ]);
            List.app genEnum enums;
            prf (footer, [F.STR structName]);
            TextIO.closeOut outS
          end

    fun test dbFile = let
          val db = DBXMLParser.parseFile dbFile
          in
            gen {db = db, outFile = "consts.sml", structName = "Consts"}
          end

  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0