what the c?

38
What the C? Kane Baccigalupi @rubyghetto

Upload: baccigalupi

Post on 19-Jul-2015

112 views

Category:

Software


0 download

TRANSCRIPT

What the

C?Kane Baccigalupi

@rubyghetto

What the

C?What can you do with it, nowadays?

C1998

Coolest Hottest Language was ….

Javabut learning C was like learning ABCs

C1998

Coolest Hottest Language was ….

Javabut learning C was like learning ABCs

And not very hip companies were making websites in it.

Really!

Assembly

C stands on the shoulders

of giants (who were women!)

Ctoday

what is done with it

Ctoday

what is done with it

utilities

git, bash, diff …

Ctoday

what is done with it

utilities

git, bash, diff …

language extensions: JSON, ruby => nokogiri

Ctoday

what is done with it

utilities

git, bash, diff …

language extensions: JSON, ruby => nokogiri

languages!

Dynamic Languages in

CExtends what C programmers have always done

Bend the mind

Allow unknowns at Runtime

to be compiled into extensible ideas

C Building Blocks

Arrays

int counts[3];

char string[6];

C Building Blocks

int counts[3];

char string[6];

Limitations:• variables can’t be asked about size

• variables can’t be asked about type

• variables can’t be increased

by just adding values beyond declaration

C Building Blocks

typedef struct Array {

int length;

int capacity;

void **values;

} Array;

C Building Blocks

typedef struct Array {

int length;

int capacity;

void **values;

} Array;

C Building Blocks

[ _ ]

Dynamic Array

capacity: 4

length: 3

values:

C Building Blocks

[ _ _ ]

Dynamic Array: pop

capacity: 4

length: 2

values:

C Building Blocks

[ _ ]

Dynamic Array: push

capacity: 4

length: 4

values:

C Building Blocks

[ _ … ]

Dynamic Array: push

capacity: 16

length: 5

values:

reallocation

C Building Blocks

[ _ ]

Dynamic Array: shift?

capacity: 4

length: 2

values:

not efficient!

C Building Blocks

Dynamic Array:

Limitation:

• shift/unshift operations costly

• insert in middle inefficient

Benefit:

• efficient modification to end

• easy value extraction from an index

• easy value addition at a particular index

C Building Blocks

Linked Lists:

typedef struct List {

int length;

struct Node *first;

struct Node *last;

} List;

typedef struct Node {

void *value;

struct Node *prev;

struct Node *next;

} Node;

C Building Blocks

Linked Lists (double):

length: 4

first:

last:

C Building Blocks

Linked Lists (double):

Limitation:

• can’t easily traverse to particular index

Benefits:

• insert anywhere

• easy add to beginning

C Building Blocks

Trees:

length: 4

root:

C Building Blocks

Hash (combining lots):

// javascript

obj = {“hello”: “world”};

obj[‘hello’] // returns “world”

Also called dictionary & map

C Building Blocks

Hash (combining lots):

length: 4

capacity: 5

values: [ ]

dynamic array

of

dynamic arrays

C Building Blocks

Given: string key & value to set

[ ]

dynamic array

of

dynamic arrays

• key converted to hash (long int)

• take modulo of hash to find a bucket

(top level location)

• node has ref to

• original key

• and hash

• and value

• node is shoved into bucket

Now that we have

Building Blocks,

let’s build a

language!

How to make a

language

1. Lex: convert string to tokens

2. Parse: build a syntax tree from tokens

3. Interpret: evaluate the syntax tree

LexingConversion: 1d array to 1d array

1 + x

<integer 1> <id ‘+’> <integer 1>

ParsingConverting 1d array into a tree

<integer 1> <id ‘+’> <id ‘x’>

(method_call,

(receiver, (literal, <integer 1>)),

(message, (lookup, <id ‘+’>)),

(arguments, (lookup, <id ‘x’>))

)

Lexing & ParsingThere is a better way!

Interpretatio

nTaking the tree expression

and executing it as code!

(method_call,

(receiver, (literal, <integer 1>)),

(message, (lookup, <id ‘+’>)),

(arguments, (lookup, <id ‘x’>))

)

• Find or build literal integer `1`

• Use Integer class to lookup `+`

method

• lookup in nested contexts id `x`

• Call `+` method on integer object

`1` with lookup value of x as

argument

FAUXY!Watch it happen:

github.com/baccigalupi/fauxy

FAUXY!

Faux Objects

Faux Functional

Concurrent

Dynamic

Statically typed and immutable!

with Pattern Matching/Multiple Dispatch

FAUXY!

Faux Objects

Faux Functional

Concurrent

Dynamic

Statically typed and immutable!

with Pattern Matching/Multiple Dispatch

NumberToWords.Digit: Class.new(n) -> {

to-words: -> { convert(n) }

convert: -> (n: 1) { 'one' }

convert: -> (n: 2) { 'two' }

convert: -> (n: 3) { 'three' }

convert: -> (n: 4) { 'four' }

convert: -> (n: 5) { 'five' }

convert: -> (n: 6) { 'six' }

convert: -> (n: 7) { 'seven' }

convert: -> (n: 8) { 'eight' }

convert: -> (n: 9) { 'nine' }

}

Attribution

• Assembly Example: homepage.cs.uri.edu

• Women using punchcards: en.wikipedia.org

ME!Kane Baccigalupi

@baccigalupi

Ruby/Rails/JS

Rescue Coding