-
Notifications
You must be signed in to change notification settings - Fork 259
Internals: the D language
Work in progress. Contributors: Gwyn Whieldon, (your-name-here)
Files for internals workshop are available at Internals. First exercise of the day is available here. A walkthrough of the syntax in the Makefile referenced by the exercise is given below.
Note that a successfully built version of M2 is a prerequisite for today's exercises.
A sample Makefile for illustrating syntax is here
Be sure that scc1 is on your path for running the exercises. Run make in the D language directory and examine the resulting files from translating foo.d into foo-tmp.c via the Makefile above.
- Do not have to declare variable names ahead of time. Have both an
=(assignment) and:=(assignment and creation), along with::=for definition of macros. - foo-tmp.c is translated into foo.d by the compiler
- In the translated C file, b is declared (
staticmeans it is not exported) near the top, andb:=1later in the file (the C version ofb:=true). - The variable c is exported (so not declared static), and
foo_cindicates that it taken from foo.d - Ccode keyword gives a macro that literally translates the arguments into C code. In this case, we have defined the meaning of
+via this macro. -
if __ then __ elseis an expression, rather than a statement. The linei := if b then 11 else 22was translated into a control statement that assigns a temporary variable_tmp, which is then assigned toi.- This has no impact on performance/speed.
- The variable X is defining a struct with two variables (both integers). This variable doesn't appear in foo-tmp.c. What happened? Let's go look in foo-exports.h to see what happened.
- Looking at the
\* typedefs *\section, we can see the variableX_struct. - Looking down into the
\* struct types *\though, we see thatX_structdoesn't have a type code assigned. - The
+sign included in the struct is what adds the type code assignment.
- Looking at the
- Variables Y and Z are also both structs, where Y has two
intand acharas inputs, and Z just has twoint.- U is defined to be Y or Z (these are different types, can create functions that pass arguments into different functions based on type), declared to be a tagged union.
- Three keywords (
when,if,do) here worth noting in D language:-
whentakes variable u, and checks: -
ify is of type Y, then it returns (viado) y.a -
ifz is of type Z, then it returns (viado) z.d
-
- This definition of U is turned into a switch statement based on type with cases in the C code.
The exercises go through using tags-search in emacs. Returns answers in logical order (rather than, say, alphabetical.)
- Using
tags-search, finding details onsinfunction. Note thewhen,if,do,elsesyntax based on type, returning appropriate answers based on type (complex, real, integer, rational). - Exercises show how to run the version of M2 that you're building in emacs.
- Using meta-x compile in emacs, we recompile the file (in d directory) but didn't produce a new version of the program.
- Have to go back to the [build-directory]/Macaulay2/bin and recompile the program with
make. - Next exercise is to add a new function to the file actors3.d and expose it at the top level (add it to the
exports.m2file). -
Ctrl-X v =will show you differences between your file and last git commit.
Homepage | Projects | Packages | Documentation | Events | Google Group