Skip to content

Instantly share code, notes, and snippets.

@andydude
andydude / echo.rs
Last active December 17, 2015 22:38
POSIX echo
fn unwords(args: &[~str]) -> ~str {
return str::connect(args, " ");
}
fn echo(args: &[~str]) {
match args.tail() {
[~"-n", ..strs] => print(unwords(strs)),
strs => println(unwords(strs)),
}
}
@andydude
andydude / myVisitor.cpp
Created April 25, 2013 00:39
My attempt at adding a function to help with the bubbling phase of walking the AST.
unsigned clang_visitChildrenWithPost(CXCursor parent,
CXCursorVisitor visitor,
CXPostChildrenVisitor post_visitor,
CXClientData client_data) {
clang::cxcursor::CursorVisitor CursorVis(clang_Cursor_getTranslationUnit(parent), visitor, client_data,
/*VisitPreprocessorLast=*/false,
/*VisitIncludedPreprocessingEntries=*/false,
/*RegionOfInterest=*/clang::SourceRange(),
/*VisitDeclsOnly=*/false,
/*PostChildrenVisitor=*/post_visitor);
use v6;
module C::CAST;
class TranslationUnit {
has ExternalDeclaration @decls;
}
class ExternalDeclaration {
}
「int main(int x) { return x; }」
function-definition => 「int main(int x) { return x; }」
declaration-specifiers => 「int 」
declaration-specifier => 「int 」
type-specifier => 「int 」
sym => 「int」
declarator => 「main(int x) 」
direct-declarator => 「main(int x) 」
direct-declarator-first => 「main」
ident => 「main」
use Grammar::Debugger;
grammar C::CInteger;
token TOP {^ <integer-constant> $}
token integer-constant {
<integer-value> <integer-suffix>*
}
proto token integer-value {*}
token integer-value:sym<8> { <octal-constant> }
$ panda install Grammar::Debugger
==> Grammar::Debugger depends on Term::ANSIColor
==> Fetching Term::ANSIColor
==> Building Term::ANSIColor
Compiling lib/Term/ANSIColor.pm
==> Testing Term::ANSIColor
t/00-load.t .. ok
All tests successful.
Files=1, Tests=1, 1 wallclock secs ( 0.02 usr 0.01 sys + 0.72 cusr 0.19 csys = 0.94 CPU)
Result: PASS
# This file is at C/CIntent.pm6
grammar C::CIdent;
token TOP {^ <ident> $}
# identifier
token ident { <.ident-first> <.ident-rest>* }
# identifier-nondigit
proto token ident-first {*}
token ident-first:sym<under> { '_' }
@andydude
andydude / StdC89Lexer.pm6
Created March 29, 2013 01:31
My first attempt at a C11 parser (ignore the filenames)
# References ISO/IEC 9899:1990 "Information technology - Programming Language C" (C89 for short)
grammar C::StdC89Lexer;
#rule TOP {
# ^ <c-token>+ $
#}
# SS 6.4
proto token c-token {*}
func error1panic(err error) {
if err != nil {
panic(err)
}
return
}
func error2panic(a Any, err error) Any {
if err != nil {
panic(err)
@andydude
andydude / core.clj
Created June 28, 2012 07:28
test-template
;; filename: src/test_template/core.clj
(ns test-template.core
(:require [clojure.java.io :as io]
[clostache.parser :as st]
[cheshire.core :as js]))
(defn render [template-filename json-filename]
(let [temp (str (line-seq (io/reader template-filename)))
data (js/parse-stream (io/reader json-filename) true)]