Skip to content

Instantly share code, notes, and snippets.

View paulbuis's full-sized avatar
💭
Teaching a Programming Language class at Ball State University

Paul Buis paulbuis

💭
Teaching a Programming Language class at Ball State University
View GitHub Profile
@paulbuis
paulbuis / stacks.d
Created May 21, 2014 17:05
Example code in D for imlementing a Stack template with a non-GC'ed linked list that minimizes invocations of "new"
module stacks;
class Stack(T) {
private StackNode!T* front = null;
~this() {
while (!empty()) {
pop();
}
}
@paulbuis
paulbuis / foo.c
Last active August 29, 2015 14:17
Simple C language separate compilation example
#include "foo.h"
int fooSum(struct foo* pFoo) {
return pFoo->a + pFoo->b;
}
@paulbuis
paulbuis / Calling C from C.md
Last active August 29, 2015 14:28
Compile & Link: Calling C from C

The hello.h header file contains function prototypes that serve as declarations of the functions.

The hello.c file contains definitions of the functions after a preprocessor directive to include the contents of the header file. This ensures that the definitions are consistent with the declarations. Note the name of the header file is in quotes, indicating the header file should be looked for in the project source directory. The name of the standard I/O header is enclosed in angle brackets, indicating it should be looked for in the system header directory.

The main.c file also includes the hello.h header so that the use of the function hello() can be type-checked against the prototype. main() returns 0, indicating success, rather than an error code. The process that invoked fork() to create the process that called main, will be able to check this status result by invoking some variant of the wait() system call. The formal arguments to main() allow main() to access command line arguments. argc i

@paulbuis
paulbuis / Calling C from Fortran.md
Created August 24, 2015 13:09
Calling C from Fortran

main.f contains the function invocation and hello.c contains the function being invoked.

Using GNU's "gfortran" compiler and GNU's C compiler, one needs to add an underscore to the C function name to match the symbol generated by the Fortran compiler. Fortran is "call by reference" so the formal argument is a pointer to the actual argument. The calling function allocates an anonymous variable to store the literal integer 3 in. If the actual argument had been a variable, the called function would be allowed to alter it. Note also, that the caller has no way of knowing the number or types of aguments to hello. Since it is invoked with the call keyword, it is assumed to be a Fortran "procedure" which is essentially a function with no return value.

Again, Fortran passes function arguments "by reference" rather than "by value," so the C code calling the Fortran code in adapt.c needs to take the address of the actual argument.

In the case of the GNU Fortran compiler, the Fortran hello() procedure has an underscore appended to its name when assembly code is generated.

@paulbuis
paulbuis / hello.d
Last active August 29, 2015 14:28
D version of 2 source file hello world example
import std.stdio;
void hello(int repeatCount) nothrow {
try {
foreach (i; 0..repeatCount) {
printf("hello from D\n");
}
}
catch (Throwable t) {
@paulbuis
paulbuis / hello.c
Created August 24, 2015 13:33
Calling C from C++
#include <stdio.h>
#include "hello.h"
void hello(int repeatCount) {
int i;
for (i = 0; i < repeatCount; i++) {
printf("hello from C\n");
}
}
@paulbuis
paulbuis / hello.cpp
Created August 24, 2015 13:37
Calling C++ from C
using namespace std;
#include <iostream>
extern "C" void hello(int repeatCount) {
for (int i=0; i<repeatCount; i++) {
cout << "hello from C++" << endl;
}
}
@paulbuis
paulbuis / Makefile
Last active September 3, 2015 17:04
Makefile for multilingal compile and link
CC = gcc
FC = gfortran
DC = gdc-4.9
CPP = g++
CFLAGS = -O
FFLAGS = -O
DFLAGS = -O
%_c.o: %.c
@paulbuis
paulbuis / .Simple Programming in Go
Last active February 1, 2016 19:53
Simple programming in Go
Simple example of a Go program