Skip to content

Commit

Permalink
lab3
Browse files Browse the repository at this point in the history
  • Loading branch information
ddanilyuk committed Oct 14, 2020
1 parent d950ea5 commit 573b7a2
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 137 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# **C Complier to ASM written on Swift**

### Compiler:

- Lexing and parsing C code.
- Generating Cpp file with Inline assembler

### You can:

- Compile functions with local variables.
19 changes: 11 additions & 8 deletions clangCompiler/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {

let testCode = """
int main() {
int a;
return a;
int some() {
return 3;
}
return 5;
}
"""

Expand All @@ -37,7 +39,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
code = try String(contentsOfFile: "3-07-Swift-IV-82-Danyliuk.txt", encoding: String.Encoding.windowsCP1251)
#endif

code = testers.lab3test1
code = testers.lab3test4
// code = testCode

print("File text:\n\(code)\n")
Expand All @@ -51,20 +53,21 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
let node = try parser.parseBlock(blockType: .startPoint)
print("Tree:\n\(TreePrinter.printTree(root: node))")

// print("ASM code: ")
// let asmCode = try node.interpret(isCPPCode: false)
// print(asmCode)
/*
print("ASM code: ")
let asmCode = try node.interpret(isCPPCode: false)
print(asmCode)
*/

#if !DEBUG
try asmCode.write(toFile: "Sources/3-07-Swift-IV-82-Danyliuk.asm", atomically: false, encoding: String.Encoding.utf8)
try asmCode.write(toFile: "Source/3-07-Swift-IV-82-Danyliuk.asm", atomically: false, encoding: String.Encoding.utf8)
#endif

print("\nC++ code: ")
let cppCode = try node.interpret(isCPPCode: true)
print(cppCode)

#if !DEBUG
// "\" may cause error
try cppCode.write(toFile: "3-07-Swift-IV-82-Danyliuk.cpp", atomically: false, encoding: String.Encoding.utf8)
#endif

Expand Down
3 changes: 2 additions & 1 deletion clangCompiler/Main/Block.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ struct Block: Node {
}

case .function:
let esp = Parser.identifiers.isEmpty ? "" : "\nsub esp, \(Parser.currentVariablePosition)\n"
result += """
; Start function header
push ebp
mov ebp, esp
mov ebp, esp \(esp)
; End function header\n\n
"""
for line in nodes {
Expand Down
2 changes: 1 addition & 1 deletion clangCompiler/Main/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public extension String {
var result = String()

for subString in subStrings {
let arrayOfTabs = Array(repeating: "\t", count: numberOfTabs)
let arrayOfTabs = Array(repeating: " ", count: numberOfTabs)
let stringOfTabs = arrayOfTabs.reduce("", { "\($0)\($1)"})
result += "\(stringOfTabs)\(subString)\n"
}
Expand Down
2 changes: 1 addition & 1 deletion clangCompiler/Main/Nodes/BinaryOperationNode.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// InfixOperation.swift
// BinaryOperationNode.swift
// clangCompiler
//
// Created by Денис Данилюк on 25.09.2020.
Expand Down
2 changes: 2 additions & 0 deletions clangCompiler/Main/Nodes/FunctionDefinitionNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Foundation


struct FunctionDefinitionNode: Node {

// Funciton name
let identifier: String

Expand All @@ -24,6 +25,7 @@ struct FunctionDefinitionNode: Node {


extension FunctionDefinitionNode: TreeRepresentable {

var name: String {
return identifier
}
Expand Down
6 changes: 3 additions & 3 deletions clangCompiler/Main/Nodes/VariableNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ struct VariableNode: PositionNode {
if let value = value {
result += try value.interpret(isCPPCode: isCPPCode)
result.deleteSufix("push eax\n")
result += "mov [\(address) + ebp], eax\n"
result += "mov [ebp - \(address)], eax\n"
}
case .onlyDeclaration:
result += "mov [\(address) + ebp], 0\n"
result += "mov [ebp - \(address)], 0\n"
case .getting:
result += "mov \(register), [\(address) + ebp]\n"
result += "mov \(register), [ebp - \(address)]\n"
}

return result
Expand Down
Loading

0 comments on commit 573b7a2

Please sign in to comment.