Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Swift5.10 #19

Merged
merged 9 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 12 additions & 13 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
{
"object": {
"pins": [
{
"package": "SwiftSyntax",
"repositoryURL": "https://github.com/apple/swift-syntax.git",
"state": {
"branch": "swift-DEVELOPMENT-SNAPSHOT-2019-07-10-m",
"revision": "dd07c607c04a3adb61e6f973b3db88a7062c9f3c",
"version": null
}
"originHash" : "d22c1569a556f84df3013bad6f3a9eae59dbb9229b55e9a0b5b5b163999c1e47",
"pins" : [
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "fa8f95c2d536d6620cc2f504ebe8a6167c9fc2dd",
"version" : "510.0.1"
}
]
},
"version": 1
}
],
"version" : 3
}
18 changes: 13 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// swift-tools-version:5.1
// swift-tools-version:5.10

import PackageDescription

let rpath = "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これもいらないはず
リンカーに -rpath で渡している部分と一緒に削除してもらえますか

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自分でやりました #20


let package = Package(
name: "SwiftTypeInference",
platforms: [
.macOS(.v10_15),
.iOS(.v13),
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git",
.revision("swift-DEVELOPMENT-SNAPSHOT-2019-07-10-m")),
.package(url: "https://github.com/apple/swift-syntax.git", exact: "510.0.1"),
],
targets: [
.target(
Expand All @@ -21,7 +24,12 @@ let package = Package(
),
.target(
name: "SwiftcAST",
dependencies: ["SwiftSyntax", "SwiftcBasic", "SwiftcType"]
dependencies: [
.product(name: "SwiftSyntax", package: "swift-syntax"),
.product(name: "SwiftParser", package: "swift-syntax"),
"SwiftcBasic",
"SwiftcType"
]
),
.target(
name: "SwiftcSema",
Expand All @@ -35,7 +43,7 @@ let package = Package(
name: "SwiftCompiler",
dependencies: ["SwiftcAST", "SwiftcSema"]
),
.target(
.executableTarget(
name: "swsc",
dependencies: ["SwiftcAST", "SwiftcSema"],
linkerSettings: [
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftcAST/ASTNode/ASTNode.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import SwiftcBasic
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

元々なくてよかったの謎ですね


public protocol ASTNode : AnyObject, CustomStringConvertible {
// break retain cycle
func dispose()
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftcAST/ASTNode/BindOptionalExpr.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftcBasic
import SwiftcType

public final class BindOptionalExpr : Expr {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftcAST/ASTNode/CallExpr.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftcBasic
import SwiftcType

public final class CallExpr : Expr {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftcAST/ASTNode/ClosureExpr.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftcBasic
import SwiftcType

public final class ClosureExpr : Expr, DeclContext {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftcAST/ASTNode/DeclRefExpr.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftcBasic
import SwiftcType

public final class DeclRefExpr : Expr {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftcAST/ASTNode/FunctionDecl.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftcBasic
import SwiftcType

public final class FunctionDecl : ValueDecl {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftcAST/ASTNode/InjectIntoOptionalExpr.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftcBasic
import SwiftcType

public final class InjectIntoOptionalExpr : Expr {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftcAST/ASTNode/IntegerLiteralExpr.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftcBasic
import SwiftcType

public final class IntegerLiteralExpr : Expr {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftcAST/ASTNode/OptionalEvaluationExpr.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftcBasic
import SwiftcType

public final class OptionalEvaluationExpr : Expr {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftcAST/ASTNode/OverloadedDeclRefExpr.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftcBasic
import SwiftcType

public final class OverloadedDeclRefExpr : Expr {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftcAST/ASTNode/SourceFile.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftcBasic
import SwiftcType

public final class SourceFile : Decl {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftcAST/ASTNode/UnresolvedDeclRefExpr.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftcBasic
import SwiftcType

public final class UnresolvedDeclRefExpr : Expr {
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftcAST/ASTNode/VariableDecl.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SwiftcBasic
import SwiftcType

public final class VariableDecl : ValueDecl {
Expand Down
124 changes: 60 additions & 64 deletions Sources/SwiftcAST/Parser.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import SwiftParser
import SwiftSyntax
import SwiftcBasic
import SwiftcType
Expand Down Expand Up @@ -31,7 +32,7 @@ public final class Parser {
}

public func parse() throws -> SourceFile {
let syn = try SyntaxParser.parse(source: sourceString)
let syn = SwiftParser.Parser.parse(source: sourceString)

var data = sourceString.data(using: .utf8)!
let size = data.count
Expand Down Expand Up @@ -60,17 +61,19 @@ public final class Parser {

for syn in synStmts {
switch syn.item {
case let syn as VariableDeclSyntax:
for decl in try parse(syn) {
case let .decl(syn):
if let syn = syn.as(VariableDeclSyntax.self) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あれ、昔は VariableDeclSyntax って言語の as でキャストできたのか、そうか。(ただの感想です)

for decl in try parse(syn) {
stmts.append(decl)
}
} else if let syn = syn.as(FunctionDeclSyntax.self) {
let decl = try parse(syn)
stmts.append(decl)
}
case let syn as FunctionDeclSyntax:
let decl = try parse(syn)
stmts.append(decl)
case let syn as ExprSyntax:
case let .expr(syn):
let expr = try parse(expr: syn)
stmts.append(expr)
default:
case .stmt:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default いらないんですね。このenum知らなかったかも。(ただ感)

break
}
}
Expand All @@ -82,37 +85,36 @@ public final class Parser {
var decls: [VariableDecl] = []

for binding in varDecl.bindings {
switch binding.pattern {
case let ident as IdentifierPatternSyntax:
let name = ident.identifier.text
let initializer: Expr? = try binding.initializer.map {
try parse(expr: $0.value)
} as? Expr
let type: Type? = try binding.typeAnnotation.map {
try parse(type: $0.type)
}
let decl = VariableDecl(source: source,
sourceRange: SourceRange(syntax: binding),
parentContext: currentContext,
name: name,
initializer: initializer,
typeAnnotation: type)
decls.append(decl)
default:
break
guard let ident = binding.pattern.as(IdentifierPatternSyntax.self) else {
continue
}

let name = ident.identifier.text
let initializer: Expr? = try binding.initializer.map {
try parse(expr: $0.value)
} as? Expr
let type: Type? = try binding.typeAnnotation.map {
try parse(type: $0.type)
}
let decl = VariableDecl(source: source,
sourceRange: SourceRange(syntax: Syntax(binding)),
parentContext: currentContext,
name: name,
initializer: initializer,
typeAnnotation: type)
decls.append(decl)
}

return decls
}

private func parse(_ synFunc: FunctionDeclSyntax) throws -> FunctionDecl {
let name = synFunc.identifier.text
let name = synFunc.name.text

let sig = try parse(synFunc.signature)

let funcDecl = FunctionDecl(source: source,
sourceRange: SourceRange(syntax: synFunc),
sourceRange: SourceRange(syntax: Syntax(synFunc)),
parentContext: currentContext,
name: name,
parameterType: sig.0,
Expand All @@ -124,39 +126,36 @@ public final class Parser {
}

private func parse(_ synSig: FunctionSignatureSyntax) throws -> (Type, Type) {
let synParamList = synSig.input.parameterList.map { $0 }
let synParamList = synSig.parameterClause.parameters.map { $0 }
guard synParamList.count == 1 else {
throw MessageError("param num must be 1")
}
let synParam = synParamList[0]
guard let synType = synParam.type else {
throw MessageError("no param type")
}
let synType = synParam.type

let param: Type = try parse(type: synType)

let result: Type = try synSig.output
.map { try parse(type: $0.returnType) }
let result: Type = try synSig.returnClause
.map { try parse(type: $0.type) }
?? PrimitiveType.void

return (param, result)
}

private func parse(expr: ExprSyntax) throws -> ASTNode {
let sourceRange = SourceRange(syntax: expr)
switch expr {
case let expr as IdentifierExprSyntax:
let name = expr.identifier.text
let sourceRange = SourceRange(syntax: Syntax(expr))
if let expr = expr.as(DeclReferenceExprSyntax.self) {
let name = expr.baseName.text
return UnresolvedDeclRefExpr(source: source,
sourceRange: sourceRange,
name: name)
case let expr as IntegerLiteralExprSyntax:
} else if let expr = expr.as(IntegerLiteralExprSyntax.self) {
_ = expr
return IntegerLiteralExpr(source: source,
sourceRange: sourceRange)
case let expr as FunctionCallExprSyntax:
} else if let expr = expr.as(FunctionCallExprSyntax.self) {
let callee = try parse(expr: expr.calledExpression) as! Expr
let synArgList = expr.argumentList.map { $0 }
let synArgList = expr.arguments.map { $0 }
guard synArgList.count == 1 else {
throw MessageError("arg num must be 1")
}
Expand All @@ -165,10 +164,10 @@ public final class Parser {
sourceRange: sourceRange,
callee: callee,
argument: arg)
case let expr as ClosureExprSyntax:
} else if let expr = expr.as(ClosureExprSyntax.self) {
return try parse(expr)
default:
throw unsupportedSyntaxError(expr)
} else {
throw unsupportedSyntaxError(Syntax(expr))
}
}

Expand All @@ -180,7 +179,7 @@ public final class Parser {
let (param, ret) = try parse(synSig)

let closure = ClosureExpr(source: source,
sourceRange: SourceRange(syntax: expr),
sourceRange: SourceRange(syntax: Syntax(expr)),
parentContext: currentContext,
parameter: param,
returnType: ret)
Expand All @@ -197,26 +196,24 @@ public final class Parser {
}

private func parse(_ synSig: ClosureSignatureSyntax) throws -> (VariableDecl, Type?) {
guard let synParamClause = synSig.input as? ParameterClauseSyntax else {
guard let synParamClause = synSig.parameterClause?.as(ClosureParameterClauseSyntax.self) else {
throw MessageError("param num must be 1")
}

let synParamList = synParamClause.parameterList.map { $0 }
let synParamList = synParamClause.parameters.map { $0 }
guard synParamList.count == 1 else {
throw MessageError("param num must be 1")
}
let synParam = synParamList[0]
guard let name = synParam.firstName?.text else {
throw MessageError("no param name")
}
let name = synParam.firstName.text

let paramType: Type? = try synParam.type.map { try parse(type: $0) }
let result: Type? = try synSig.output
.map { try parse(type: $0.returnType) }

let result: Type? = try synSig.returnClause
.map { try parse(type: $0.type) }

let param = VariableDecl(source: source,
sourceRange: SourceRange(syntax: synParam),
sourceRange: SourceRange(syntax: Syntax(synParam)),
parentContext: currentContext,
name: name,
initializer: nil,
Expand All @@ -225,23 +222,22 @@ public final class Parser {
}

private func parse(type: TypeSyntax) throws -> Type {
switch type {
case let type as SimpleTypeIdentifierSyntax:
if let type = type.as(IdentifierTypeSyntax.self) {
let name = type.name.text
return PrimitiveType(name: name)
case let type as OptionalTypeSyntax:
} else if let type = type.as(OptionalTypeSyntax.self) {
let wrapped = try parse(type: type.wrappedType)
return OptionalType(wrapped)
case let type as FunctionTypeSyntax:
let synParamList = type.arguments.map { $0 }
} else if let type = type.as(FunctionTypeSyntax.self) {
let synParamList = type.parameters.map { $0 }
guard synParamList.count == 1 else {
throw MessageError("param num must be 1")
}
let param = try parse(type: synParamList[0].type)
let result = try parse(type: type.returnType)
let result = try parse(type: type.returnClause.type)
return FunctionType(parameter: param, result: result)
default:
throw unsupportedSyntaxError(type)
} else {
throw unsupportedSyntaxError(Syntax(type))
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftcType/FunctionType.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import SwiftcBasic

public struct FunctionType : _EquatableType {
private struct Eq : Hashable {
public var parameter: AnyType
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftcType/OptionalType.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import SwiftcBasic

public struct OptionalType : _EquatableType {
public struct Eq : Hashable {
public var wrapped: AnyType
Expand Down
2 changes: 2 additions & 0 deletions Sources/SwiftcType/PrimitiveType.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import SwiftcBasic

public struct PrimitiveType : _EquatableType {
public var name: String

Expand Down