Skip to content

Instantly share code, notes, and snippets.

View H2CO3's full-sized avatar
🦀

Árpád Goretity  H2CO3

🦀
View GitHub Profile
@H2CO3
H2CO3 / mp.c
Created November 4, 2015 15:23
#include <sys/mman.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
void try_mprotect(void *p, size_t len, int prot)
{
if (mprotect(p, len, prot) != 0) {
def read_FASTA_file(fname):
with open(fname, "r") as f:
sequs = filter(len, f.read().split(">"))
return ["".join(seq.split("\n")[1:]) for seq in sequs]
// takes no arguments, and returns a StringLiteral AST node containing the current date formatted as a string
macro CompilationDateAsString() -> std::ASTNode * {
// imagine that we already have date/time support in our standard library
let cur_date = new std::Date();
return std::StringLiteralAST(cur_date.to_string());
}
// So, the following main() function:
fn main() {
print(@CompilationDateAsString());
@H2CO3
H2CO3 / c99.c
Created September 13, 2015 19:44
typedef struct {
int x;
} Foo;
int main()
{
int n = 1;
int VLA_is_not_in_cplusplus[n];
Foo f = { .x = 42 };
f = (Foo){ 42 };
//
// hash_table.hpp
// The Shift-C compiler
//
// Created by Arpad Goretity (H2CO3)
// on 02/06/2015
//
// Licensed under the 2-clause BSD License
//
> Object Oriented Programming (OOP) as an idea has been oversold. […] Extremist languages like Java force you to think of everything in terms of objects.
Yes, OOP has been oversold. But that’s not the problem of OOP; that’s the problem of incompetent programmers who are trying to use one tool for everything. Of course what Java doing is extremism too (also, see the amount of incompetent programmers in each language… Java is considered an “industrial” language, so it’s perhaps only C and C++ that have more utterly incompetent users than Java; people just want to be rich.) But nobody said you have to use Java! In fact, I think you should not use Java. Having to write an entire class just to possess a main function? Stupid! There are better languages that support OO yet don’t *force* you into the OO mindset, e.g. Objective-C or C++.
> State is not your friend, state is your enemy. Changes to state make programs harder to reason about […]
Except when it’s the declarative approach that’s harder to reason about.
'reggelt!
gyors language design kérdés null pointerekkel kapcsolatban:
iOS miatt sokáig Objective-C-ben dolgoztam, és megszoktam (nagyon kényelmes), hogy a null objektumon
való metódushívás nem száll el (segfault/NullPointerException), hanem a visszatérési típus nulla értékét
(pl. int-nél 0, pointernél null, stb.) adja vissza.
nem nagyon futottam még bele olyan hibába, ahol ez a viselkedés bugot okozott volna,
sőt, az esetek többségében kifejezetten kívánatos.
@H2CO3
H2CO3 / inherit.spn
Created August 22, 2015 07:07
prototypal inheritance demo in Sparkling
let base = {
foo: fn (self) {
stdout.printf("base::foo(self.type = %s)\n", self.type);
},
type: "base"
};
let derived = {
super: base,
bar: fn { print("derived::bar"); },
function drawShape(ctx, xoff, yoff) {
ctx.beginPath();
ctx.moveTo(206 + xoff, 86 + yoff);
ctx.bezierCurveTo(263 + xoff, 33 + yoff, 381 + xoff, 65 + yoff, 348 + xoff, 160 + yoff);
ctx.bezierCurveTo(328 + xoff, 218 + yoff, 251 + xoff, 194 + yoff, 206 + xoff, 279 + yoff);
ctx.bezierCurveTo(161 + xoff, 194 + yoff, 84 + xoff, 218 + yoff, 64 + xoff, 160 + yoff);
ctx.bezierCurveTo(31 + xoff, 65 + yoff, 149 + xoff, 33 + yoff, 206 + xoff, 86 + yoff);
ctx.stroke();
}
###### 'C-style' syntax (braces and semicolons) ######
-> return statements needed (except in one-expression lambdas)
// function := 'fn' ['<' Types... '>'] ident '(' Args... ')' ['->' RetType] block-statement
// local-function := function
// lambda := '(' args... ')' '->' statement
// args := [ arg { , arg } ]
// arg := ident [['::'] type]
// if 'statement' is an expression, then automatically 'return' it!
// 'statement' can be a block as well! (-> multiple statements...)