Skip to content

Instantly share code, notes, and snippets.

@antonijn
antonijn / lang.txt
Last active August 29, 2015 14:05
Concept for a more readable functional programming language
# declaring putc as an external function with implicit io
int putchar(io $, char c):
implio cimport "putchar"
# print function
none print(io $, char ch):
putchar($, ch); nothing
none print(io $, [char] []): nothing
none print(io $, [char] ch:str):
@antonijn
antonijn / infix.c
Last active January 7, 2016 20:05
Infix evaluator
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
typedef int bool;
#define TRUE 1
#define FALSE 0
/* Returns the operator precedence for an operator */
@antonijn
antonijn / polish.c
Last active August 29, 2015 14:05
Simple polish notation solver
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
double expr(FILE *f)
{
double acc = 0.0;
int nxt = fgetc(f);
if (isspace(nxt))
return expr(f);
/*
* Part of the C AIR instruction to file writer
*/
static void air_instr_to_string(FILE * f, struct air_expr * e)
{
void * it;
int j;
struct air_expr * ex;
struct air_instr * i = e;
@antonijn
antonijn / main.air
Last active August 29, 2015 14:04
Sublte AIR differences
hidden constant char[4] @0 = char[4] { 102,111,111,0 }
global char* @str1 = inline {
%0 = char* getptr char[4]* @0, int 0, int 0
} char* %0
global char[4] @str2 = char[4] { 98,97,114,0 }
@antonijn
antonijn / main.air
Last active August 29, 2015 14:04
Antonijn Intermediate Representation (AIR)
global int @i = inline {
%0 = add int 5, int 3
%1 = add int %0, int 1
} int %1
global int @main(int %argc, char** %argv) {
%0 = alloca int
%1 = alloca char**
%2 = alloca int
@antonijn
antonijn / c.ebnf
Last active November 16, 2015 01:48
The C language in a nutshell. Any questions?
element = declaration | "typedef", declaration | implementation
declaration = pending type, finisher, { ",", finisher }, ";"
implementation = pending type, finisher, "(", param list, ")", scope
pending type = primitive | structure | enumeration | defined type
finisher = ["*", finisher | "(", finisher, ")", "(", param list, ")" | identifier]
param list = ["void" | parameter, { ",", parameter }]
parameter = pending type, finisher
primitive = integer | floating
integer =
@antonijn
antonijn / fib.hs
Last active August 29, 2015 14:03
fibs :: [Integer]
fibs = fibseed 0 1
where fibseed a b = a : fibseed b (a + b)
fib :: Int -> Integer
fib n = fibs !! n
@antonijn
antonijn / readType.hs
Last active August 29, 2015 14:02
Read any C type from a [Token] token stream
-- File parsing hub, providing an interface to the parsers and other
-- helper functions to be used by other parsers.
-- Copyright (C) 2014 Antonie Blom
--
-- This library is free software; you can redistribute it and/or
-- modify it under the terms of the GNU Lesser General Public
-- License as published by the Free Software Foundation; either
-- version 2.1 of the License, or (at your option) any later version.
module Compiler.Parser where
@antonijn
antonijn / euler15.hs
Created May 23, 2014 21:03
Euler problem 15
import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (Maybe)
import qualified Data.Maybe as Maybe
lattice :: Int -> Int -> Int
lattice x y = fst (latticeI Map.empty x y)
where
latticeI m a 1 = (a, m)