Skip to content

Instantly share code, notes, and snippets.

View harlanhaskins's full-sized avatar
🦅
Swifting

Harlan Haskins harlanhaskins

🦅
Swifting
View GitHub Profile
@harlanhaskins
harlanhaskins / Parser.swift
Last active July 18, 2021 02:47
Building a Compiler in Swift with LLVM, Part 2: AST and the Parser
#if os(macOS)
import Darwin
#elseif os(Linux)
import Glibc
#endif
// MARK: Tokens
enum BinaryOperator: Character {
case plus = "+"
@harlanhaskins
harlanhaskins / Lexer.swift
Last active March 4, 2023 20:10
Building a Compiler in Swift with LLVM, Part 1: Introduction and the Lexer
#if os(macOS)
import Darwin
#elseif os(Linux)
import Glibc
#endif
enum BinaryOperator: Character {
case plus = "+"
case minus = "-"
case times = "*"
@harlanhaskins
harlanhaskins / bf.tr
Last active January 3, 2017 21:52
BF interpreter for the Trill iOS App
foreign type String {
foreign init(_: *Int8) -> String
foreign var length: Int
foreign func charCodeAt(_: Int) -> Int8
foreign static func fromCharCode(_: Int8) -> String
}
type Op {
let opcode: Int8
var operand: Int
@harlanhaskins
harlanhaskins / shuffle.tr
Created January 3, 2017 19:50
Shuffle program for Trill in the iOS app
foreign type Array {
foreign init() -> Array
foreign func push(_: Any)
foreign subscript(_: Int) -> Any
foreign var length: Int
}
foreign type Math {
foreign static func random() -> Double
foreign static func floor(_: Double) -> Int
(source_file
(import_decl 'Foundation')
(top_level_code_decl
(brace_stmt
(pattern_binding_decl
(pattern_named type='CGPoint' 'a')
(call_expr type='CGPoint' location=point.swift:2:9 range=[point.swift:2:9 - line:2:38] nothrow arg_labels=x:y:
(constructor_ref_call_expr type='(Int, Int) -> CGPoint' location=point.swift:2:9 range=[point.swift:2:9 - line:2:9] nothrow
(declref_expr implicit type='(CGPoint.Type) -> (Int, Int) -> CGPoint' location=point.swift:2:9 range=[point.swift:2:9 - line:2:9] decl=CoreGraphics.(file).CGPoint.init(x:y:) function_ref=single specialized=no)
(type_expr type='CGPoint.Type' location=point.swift:2:9 range=[point.swift:2:9 - line:2:9] typerepr='CGPoint'))
@harlanhaskins
harlanhaskins / test.ll
Created October 21, 2016 18:26
Swift static dispatch
; ModuleID = '-'
source_filename = "-"
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.9"
define hidden void @_TFV4test3Foo8printBarfT_T_(i64) #0 {
entry:
%1 = call %swift.type* @_TMaP_() #6
%2 = call { %swift.bridge*, i8* } @_TFs27_allocateUninitializedArrayurFBwTGSax_Bp_(i64 1, %swift.type* %1)
%3 = extractvalue { %swift.bridge*, i8* } %2, 0
import Foundation
enum Relation<T> where T: Strideable {
case equal(T)
case greater(T)
case greaterOrEqual(T)
case less(T)
case lessOrEqual(T)
case between(T, T)
@harlanhaskins
harlanhaskins / trace.swift
Created October 6, 2016 17:50
Pure Swift stack trace
struct StackFrame {
let symbol: String
let file: String
let address: UInt64
let symbolAddress: UInt64
var demangledSymbol: String {
return _stdlib_demangleName(symbol)
}
}
@harlanhaskins
harlanhaskins / se-000-disallow-optionals-in-string-interpolation.md
Last active October 4, 2016 05:29
Disallow Optionals in String Interpolation