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 / 0 - JavaScript (ES6) Singly Linked List Class.md
Last active December 13, 2016 18:18
Basic Linked List Class in JavaScript to be used to support Stack operations (push/pop), later versions to support Queue operations

The file 1-list.js below shows how to declare a OO-style class in JavaScript.

Note that the only things assigned to properties of this are anonymous functions. These are the "methods" of the class that access "private" properties. JavaScript doesn't have the keyword private like some other languages. Instead, the "private data members" of a class are the local variables and parameters of the class constructor. Most JavaScript code in the wild doesn't attempt to make anything "private" which is just bad OO design. Here, the "methods" are functions nested inside the constructor to give them access to the constructors local variables.

Typical JavaScript code you see in the wild would have placed the methods as properties of List.prototype which allows for

@paulbuis
paulbuis / .Simple Programming in Go
Last active February 1, 2016 19:53
Simple programming in Go
Simple example of a Go program
@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 / 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 / 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.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) {

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 / 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.

@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 / 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;
}