C has become pretty old school, but the way C developers now and throughout its history have used C, extended C is the foundation of a amazing dynamic languages. This talk describes some dynamic C data structures and also gives an overview of developing a language on top of these tools.
12. 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
18. C Building Blocks
[ _ … ]
Dynamic Array: push
capacity: 16
length: 5
values:
reallocation
19. C Building Blocks
[ _ ]
Dynamic Array: shift?
capacity: 4
length: 2
values:
not efficient!
20. 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
21. 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;
23. C Building Blocks
Linked Lists (double):
Limitation:
• can’t easily traverse to particular index
Benefits:
• insert anywhere
• easy add to beginning
25. C Building Blocks
Hash (combining lots):
// javascript
obj = {“hello”: “world”};
obj[‘hello’] // returns “world”
Also called dictionary & map
26. C Building Blocks
Hash (combining lots):
length: 4
capacity: 5
values:
[ ]
dynamic array
of
dynamic arrays
27. 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
28. Now that we have
Building Blocks,
let’s build a
language!
29. 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
33. Interpretatio
n
Taking 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