Skip to content

Commit

Permalink
More slides.
Browse files Browse the repository at this point in the history
  • Loading branch information
Amos Wenger committed Jun 20, 2012
1 parent 2ff576c commit 74bffc1
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 20 deletions.
6 changes: 6 additions & 0 deletions excerpts/generics-001-with-types.ooc
@@ -0,0 +1,6 @@

// primitive type
answer := identity(42)

// object type
identity("foobar") println()
7 changes: 7 additions & 0 deletions excerpts/identity-generic2.c
@@ -0,0 +1,7 @@
typedef uint8_t *Any;

void identity(Class *T, Any value, Any ret) {
if (ret) {
memcpy(ret, value, T->size)
}
}
4 changes: 4 additions & 0 deletions excerpts/identity-int2.c
@@ -0,0 +1,4 @@
/* what is the generic version of this? */
int identity(int value) {
return value;
}
6 changes: 3 additions & 3 deletions excerpts/logger.ooc
@@ -1,13 +1,13 @@
Logger: class {
Logger: class extends Formatter {
level: Level
out: Stream

init: func (=out) {
level = Level INFO
}

log: func (msg, level = Level INFO) {
log: func (msg: String, level: Level) {
if (level <= this level)
format(msg)
out print(format(msg))
}
}
2 changes: 1 addition & 1 deletion excerpts/pointer-dance.c
@@ -1,4 +1,4 @@
void somefunc(uint8_t value) {
void somefunc(uint8_t *value) {
int i = *((int*) value);
// do something with i
}
100 changes: 84 additions & 16 deletions slides.md
Expand Up @@ -4,7 +4,7 @@

# The ooc programming language

ooc is a general-purpose programming language. It was created in 2009 for an EPFL school project, and is now self-hosting, currently in v0.9.4
ooc is a general-purpose programming language. It was created in 2009 for an EPFL school project, and is now self-hosting, currently in v0.9.4. It produces clean, portable C code, its SDK works on Windows, OSX, Linux, Haiku, FreeBSD, and probably more.

Has been used to create games, power live streaming backend architecture (in production), write compilers, IRC servers, IRC bots, torrent clients, implement Lisp, JIT assemblers, package managers, and more.

Expand All @@ -18,10 +18,6 @@ Has been used to create games, power live streaming backend architecture (in pro

\input{excerpts/logger-use.ooc.tex}

# Enums

\input{excerpts/level.ooc.tex}

# Covers (C side)

\input{excerpts/cover-struct.c.tex}
Expand All @@ -30,22 +26,94 @@ Has been used to create games, power live streaming backend architecture (in pro

\input{excerpts/cover-struct.ooc.tex}

# C++
# Features not covered here

Well outside the scope of this presentation:

* Operator overloading
* Implicit conversions
* Cover inheritance, compound covers,
structured initializers
* Version blocks
* Interfaces
* Custom memory management
* Enums
* Pattern matching

# Meta-programming in other languages

C only allows macros, not generic programming. While this
doesn't prevent the creation of generic containers, type
safety is not guaranteed.

C++ meta-programming is done via templates: compile-time
instanciation, type safety, significant cost in compilation time
and binary size.

JVM-based languages (Java, Scala, Groovy, etc.) have generic
classes, with type erasure because of backwards-compatibility.
Limited compile-time type-safety (can be overriden) and no
introspection possible at runtime.

# Types

A type can either be:

* A complex type: object, interface. e.g. `String`, `Logger`, etc.
* A primitive type: cover, cover from. e.g. `Int`, `Boolean`

Java has a similar distinction (`int` vs `Integer`).

In ooc, instead of boxing and unboxing, primitive types are allowed
as generic type parameters.

# Generics - Functions

\input{excerpts/generics-001.ooc.tex}

\input{excerpts/generics-001-with-types.ooc.tex}

# Generics - Classes

\input{excerpts/generics-container.ooc.tex}

Number of generic parameters is not limited:

\input{excerpts/generics-kv.ooc.tex}

# The problem

Non-generic code generates straightforward C code, but generic types
add to the semantics of the language and have no natural C translation.

\input{excerpts/identity-int2.c.tex}

Generic type sizes can vary: operations on generic values must work
whatever their actual size is at runtime. So must operations on arrays
of generic values.

# The solution

All types in ooc have runtime type information, returned by the
`TypeName_class()` function. This structure contains the width of the
type.

\input{excerpts/identity-generic2.c.tex}

# The solution

Calls like this one:

* C++ doesn't compile to C anymore
* C++ has more features
* C++ has templates
\input{excerpts/identity-call.ooc.tex}

# JVM
are translated as:

* Java runs on a VM (mostly)
* Java tries to stay away from platform-specific code
* Java has type erasure in Generic
* Scala also limited by the JVM, no reification
\input{excerpts/identity-call.c.tex}

# Identity specialized
# The solution's problem

\input{excerpts/identity-int.c.tex}
Passing the address of generic values instead of their value directly
is an extra indirection (dereference)

# Source size

Expand Down

0 comments on commit 74bffc1

Please sign in to comment.