Created
March 14, 2017 21:59
-
-
Save oleksii-demedetskyi/42bbb19b18c28bf482b6ec7e07718182 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import Cocoa | |
struct AST { | |
let declarations: [Statement] | |
} | |
enum Statement { | |
case expression(Expression) | |
case declaration(Declaration) | |
case loop(LoopStatement) | |
case branch(BranchStatement) | |
case labeled(LabeledStatement) | |
case controlTransfer(ControlTransferStatement) | |
case `defer`(DeferStatement) | |
case `do`(DoStatement) | |
case compilerControl(CompilerControlStatement) | |
} | |
extension Statement { | |
var isExpression: Bool { | |
if case .expression = self { return true } | |
else { return false } | |
} | |
var isDeclaration: Bool { | |
if case .declaration = self { return true } | |
else { return false } | |
} | |
var isLoopStatement: Bool { | |
if case .loop = self { return true } | |
else { return false } | |
} | |
var isBranchStatement: Bool { | |
if case .branch = self { return true } | |
else { return false } | |
} | |
var isLabeledStatement: Bool { | |
if case .labeled = self { return true } | |
else { return false } | |
} | |
var isControlTransferStatement: Bool { | |
if case .controlTransfer = self { return true } | |
else { return false } | |
} | |
var isDeferStatement: Bool { | |
if case .defer = self { return true } | |
else { return false } | |
} | |
var isDoStatement: Bool { | |
if case .do = self { return true } | |
else { return false } | |
} | |
var isCompilerControlStatement: Bool { | |
if case .compilerControl = self { return true } | |
else { return false } | |
} | |
} | |
struct Expression {} | |
enum Declaration { | |
case `import`(ImportDeclaration) | |
case constant(ConstantDeclaration) | |
case variable(VariableDeclaration) | |
case `typealias`(TypealiasDeclaration) | |
case function(FunctionDeclaration) | |
case `enum`(EnumDeclaration) | |
case `struct`(StructDeclaration) | |
case `class`(ClassDeclaration) | |
case `protocol`(ProtocolDeclaration) | |
case initializer(InitializerDeclaration) | |
case deinitializer(DeinitializerDeclaration) | |
case `extension`(ExtensionDeclaration) | |
case `subscript`(SubscriptDeclaration) | |
case `operator`(OperatorDeclaration) | |
case precedenceGroup(PrecedenceGroupDeclaration) | |
} | |
struct ImportDeclaration { | |
let attributes: [Attribute] | |
let importKind: ImportKind? | |
let importPath: ImportPath | |
} | |
struct Attribute {} | |
enum ImportKind { | |
case `typealias` | |
case `struct` | |
case `class` | |
case `enum` | |
case `protocol` | |
case `var` | |
case `func` | |
} | |
struct ImportPath { | |
let identifiers: [ImportPathIdentifier] | |
init(first: ImportPathIdentifier, rest: [ImportPathIdentifier]) { | |
identifiers = [first] + rest | |
} | |
} | |
struct ImportPathIdentifier {} | |
struct ConstantDeclaration {} | |
struct VariableDeclaration {} | |
struct TypealiasDeclaration { | |
let attributes: [Attribute] | |
let accessLevel: AccessLevelModifier? | |
let name: Identifier | |
let genericArguments: GenericArgumentClause? | |
let type: Type | |
} | |
enum AccessLevelModifier {} | |
struct Identifier {} | |
struct GenericArgumentClause {} | |
struct Type {} | |
struct FunctionDeclaration {} | |
struct EnumDeclaration {} | |
struct StructDeclaration {} | |
struct ClassDeclaration {} | |
struct ProtocolDeclaration {} | |
struct InitializerDeclaration {} | |
struct DeinitializerDeclaration {} | |
struct ExtensionDeclaration {} | |
struct SubscriptDeclaration {} | |
struct OperatorDeclaration {} | |
struct PrecedenceGroupDeclaration {} | |
struct LoopStatement {} | |
struct BranchStatement {} | |
struct LabeledStatement {} | |
struct ControlTransferStatement {} | |
struct DeferStatement {} | |
struct DoStatement {} | |
struct CompilerControlStatement {} | |
func numberOfIfs(in ast: AST) -> Int { | |
return ast.declarations.filter { $0.isBranchStatement }.count | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment