New in Wolfram
Mathematica
8: SymbolicC
◄
previous
|
next
►
Software Development
Create Domain-Specific Language for APIs
SymbolicC can create domain-specific languages in
Mathematica
and translate them to C. This example defines a simple language that converts
Mathematica
points and polygons into calls to the OpenGL API.
In[1]:=
X
Needs["SymbolicC`"] generateOpenGLCode[x_List] := CCall[If[Length[x] == 2, "glVertex2d", "glVertex3d"], x] generateOpenGLCode[Point[pts_]] := {CCall["glBegin", {"GL_POINTS"}], generateOpenGLCode /@ If[ListQ[First[pts]], N[pts], N[{pts}]], CCall["glEnd", {}]} generateOpenGLCode[ Polygon[pts_]] := {CCall["glBegin", {"GL_POLYGON"}], generateOpenGLCode /@ N[pts], CCall["glEnd", {}]} generateOpenGLCode[x__] := generateOpenGLCode /@ {x} { generateOpenGLCode[Polygon[{{1, 0}, {0, Sqrt[3]}, {-1, 0}}]], generateOpenGLCode[Point[{1, 1}]] } // ToCCodeString
Out[1]=