Skip to content

Instantly share code, notes, and snippets.

@swilly22
swilly22 / calc.t
Created November 26, 2017 09:33
Calculator grammar
exp: exp op exp
exp: '(' exp ')'
exp: number
op: +
-
*
/
@swilly22
swilly22 / calc.y
Created November 26, 2017 09:34
Calculator grammar
exp: exp op exp
exp: '(' exp ')'
exp: number
op: +
-
*
/
@swilly22
swilly22 / eval.h
Created November 26, 2017 09:38
expression_eval
Evaluate(root) {
if(node_is_const(root)) {
return root.value;
}
for(child in root) {
evaluate(child);
}
return root.op(root.children);
@swilly22
swilly22 / arithmetic_expression.y
Created November 26, 2017 09:40
Arithmetic expression grammar
// (exp)
arithmetic_expression ::= LEFT_PARENTHESIS arithmetic_expression RIGHT_PARENTHESIS.
// exp + exp
arithmetic_expression ::= arithmetic_expression binary_operator arithmetic_expression.
// func(exp)
arithmetic_expression ::= STRING LEFT_PARENTHESIS arithmetic_expression_list RIGHT_PARENTHESIS.
// 9
arithmetic_expression ::= value.
// friend.age
arithmetic_expression ::= variable.
@swilly22
swilly22 / dictInit.c
Created February 15, 2018 06:02
redis dict init
uint64_t dictStringHash(const void *key) {
return dictGenHashFunction(key, strlen(key));
}
void *dictStringDup(void *privdata, const void *key) {
DICT_NOTUSED(privdata);
size_t key_len = strlen(key);
char *copy = malloc((key_len + 1) * sizeof(char));
strcpy(copy, key);
copy[key_len] = 0;
void f(void) {
void *a = NULL;
void *b = NULL;
void *c = NULL;
a = malloc(32);
//...
if(cond1) {
free(a)
return;
@swilly22
swilly22 / WithGoTo.c
Last active September 15, 2019 10:30
void f(void) {
void *a = NULL;
void *b = NULL;
void *c = NULL;
a = malloc(32);
//...
if(cond1) goto cleanup;
b = malloc(64);
@swilly22
swilly22 / toUpper.c
Last active September 16, 2019 07:13
SIValue toUpper(SIValue v) {
SIType actual_type = SI_TYPE(v);
if(actual_type != SI_STRING) {
const char *actual_type_str = SIType_ToString(actual_type);
raise("Type mismatch: expected string but was %s", actual_type_str);
}
}
if(cond) {
raise Exception("something went wrong")
}
try {
// Perform work which might throw an exception
work();
} catch (error *e) {
reportError(e);
}