Skip to content

Instantly share code, notes, and snippets.

"""
{
"type": "sub" | "unsub",
"fields": [ "all" | <one or more field_list separated by ,> ]
}
field_list:
- "magnitudo"
- "depth"
- "location"
@n0tknowing
n0tknowing / README.md
Last active February 27, 2024 06:49
How `const` and pointer works in C
  1. const can be applied to type specifier (or base type).
  2. const can be applied to pointer.
  3. const, depends on where it's placed, forbids you to do assignment.

Semantic of const int * or int const * (point no. 1)

const int *p = &amp;x; // or int const *p = &amp;x;
@n0tknowing
n0tknowing / tree_traverse.c
Last active April 2, 2023 04:29
DFS Tree traversal
// DFS Tree traversal
// I always forgot about this even though they are simple
// For use in AST, post-order is used
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
struct Node {
struct Node *left;
@n0tknowing
n0tknowing / pratt.cc
Last active April 6, 2023 06:37
Pratt parser in C++
// Operator Associativity Description
// -------- ------------- -----------
// x! right to left Factorial
// +x -x ~x !x right to left Positive, negative, bitwise NOT, logical NOT
// * / % left to right Multiplication, division, modulo
// + - left to right Addition, subtraction
// << >> left to right Bitwise shifts
// & left to right Bitwise AND
// ^ left to right Bitwise XOR
// | left to right Bitwise OR
#ifndef MY_CTYPE_H
#define MY_CTYPE_H
#define U_ 0x01
#define L_ 0x02
#define D_ 0x04
#define S_ 0x08
#define P_ 0x10
#define X_ 0x20
#define C_ 0x40
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC poison strncpy
#endif
#include <stdio.h>
#include <string.h>
int main(void)
{
char buf[20];
print("/**\n * Branchless <ctype.h> replacement")
print(" *\n * GENERATED BY tools/gen.py, DO NOT EDIT!\n */\n")
print("#ifndef BRANCHLESS_CTYPE_H")
print("#define BRANCHLESS_CTYPE_H\n")
print("#include <stdint.h>\n")
print("#define is_alnum(c) (alnum_tab[(uint8_t)(c)])")
print("#define is_alpha(c) (alpha_tab[(uint8_t)(c)])")
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define unlikely(x) (__builtin_expect(!!(x), 0))
#ifndef N
#define N 1000000
#endif
#include <stdio.h>
#include <stdint.h>
#include <string.h>
static uint32_t hexstr_to_u32(const char *str)
{
uint32_t hex = 0;
for (uint32_t i = 0; i < 4; i++) {
if (str[i] >= '0' && str[i] <= '9')
#include <stdlib.h>
#include <string.h>
static char *astrncpy__(char **dst, const char *src, size_t size, int check)
{
if (dst == NULL) {
return NULL;
} else if (src == NULL) {
*dst = NULL;
return NULL;