From 573b7a24584e691b770bafdde82918f970743831 Mon Sep 17 00:00:00 2001 From: ddanilyuk Date: Wed, 14 Oct 2020 21:33:36 +0300 Subject: [PATCH] lab3 --- README.md | 11 + clangCompiler/AppDelegate.swift | 19 +- clangCompiler/Main/Block.swift | 3 +- clangCompiler/Main/Extensions.swift | 2 +- .../Main/Nodes/BinaryOperationNode.swift | 2 +- .../Main/Nodes/FunctionDefinitionNode.swift | 2 + clangCompiler/Main/Nodes/VariableNode.swift | 6 +- clangCompiler/Testers.swift | 295 ++++++++++-------- 8 files changed, 203 insertions(+), 137 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..74ca60c --- /dev/null +++ b/README.md @@ -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. \ No newline at end of file diff --git a/clangCompiler/AppDelegate.swift b/clangCompiler/AppDelegate.swift index c5a9dc0..28b741a 100644 --- a/clangCompiler/AppDelegate.swift +++ b/clangCompiler/AppDelegate.swift @@ -23,8 +23,10 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { let testCode = """ int main() { - int a; - return a; + int some() { + return 3; + } + return 5; } """ @@ -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") @@ -51,12 +53,14 @@ 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: ") @@ -64,7 +68,6 @@ final class AppDelegate: UIResponder, UIApplicationDelegate { 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 diff --git a/clangCompiler/Main/Block.swift b/clangCompiler/Main/Block.swift index 416a9d2..be28e74 100644 --- a/clangCompiler/Main/Block.swift +++ b/clangCompiler/Main/Block.swift @@ -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 { diff --git a/clangCompiler/Main/Extensions.swift b/clangCompiler/Main/Extensions.swift index 2aba990..3b1f144 100644 --- a/clangCompiler/Main/Extensions.swift +++ b/clangCompiler/Main/Extensions.swift @@ -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" } diff --git a/clangCompiler/Main/Nodes/BinaryOperationNode.swift b/clangCompiler/Main/Nodes/BinaryOperationNode.swift index 1ff39d0..aea9bad 100644 --- a/clangCompiler/Main/Nodes/BinaryOperationNode.swift +++ b/clangCompiler/Main/Nodes/BinaryOperationNode.swift @@ -1,5 +1,5 @@ // -// InfixOperation.swift +// BinaryOperationNode.swift // clangCompiler // // Created by Денис Данилюк on 25.09.2020. diff --git a/clangCompiler/Main/Nodes/FunctionDefinitionNode.swift b/clangCompiler/Main/Nodes/FunctionDefinitionNode.swift index a2d91d4..722e7dd 100644 --- a/clangCompiler/Main/Nodes/FunctionDefinitionNode.swift +++ b/clangCompiler/Main/Nodes/FunctionDefinitionNode.swift @@ -9,6 +9,7 @@ import Foundation struct FunctionDefinitionNode: Node { + // Funciton name let identifier: String @@ -24,6 +25,7 @@ struct FunctionDefinitionNode: Node { extension FunctionDefinitionNode: TreeRepresentable { + var name: String { return identifier } diff --git a/clangCompiler/Main/Nodes/VariableNode.swift b/clangCompiler/Main/Nodes/VariableNode.swift index 9c17c01..71ec481 100644 --- a/clangCompiler/Main/Nodes/VariableNode.swift +++ b/clangCompiler/Main/Nodes/VariableNode.swift @@ -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 diff --git a/clangCompiler/Testers.swift b/clangCompiler/Testers.swift index 3cf319a..1dc586c 100644 --- a/clangCompiler/Testers.swift +++ b/clangCompiler/Testers.swift @@ -8,117 +8,143 @@ import Foundation +// MARK:- no indent in multiline is normal struct Testers { - // MARK:- LAB 2 tests - - // Errors - let lab2error1 = """ - int main() { - return -(3 - 2 / 8)g - } - """ - - let lab2error2 = """ - int main() { - return -(3 + 2 / 8); - } - """ + // MARK:- LAB 1 tests + + let lab1test1 = """ +int main() { + return 2; +} +""" - let lab2error3 = """ - int main() { - return 3 s 2 / 8; - } - """ + + let lab1test2 = """ +int main() { + return 012; +} +""" - let lab2error4 = """ - int main() { - return --3 - 2 / 8; - } - """ + let lab1test3 = """ +float main() { + return 3.4; +} +""" - let lab2error5 = """ - in main() { - return (-3 - 2 / 8); - } - """ + let lab1error1 = """ +float main( { + return 3.4; +} +""" - let lab2error6 = """ - int main() - return -16 / 7 / 8; - } - """ - // end errors + // MARK:- LAB 2 tests // -2 let lab2test1 = """ - float main() { - return -2.4; - } - """ +float main() { + return -2.4; +} +""" // -3 / -1 = 3 let lab2test2 = """ - int main() { - return (-9 / -03) / (-1.8); - } - """ +int main() { + return (-9 / -03) / (-1.8); +} +""" // -16 / - (-2) / 2 = - 16 / 2 / 2 = -4 let lab2test3 = """ - int main() { - return -(32 / 2.2) / -(-4 / 2) / 2; - } - """ +int main() { + return -(32 / 2.2) / -(-4 / 2) / 2; +} +""" let lab2test4 = """ - int main() { - return 16.3 / 2 / 02 / 2; - } - """ +int main() { + return 16.3 / 2 / 02 / 2; +} +""" let lab2test5 = """ - int main() { - return (-16 - (-2) - 4); - } - """ - +int main() { + return (-16 / (-2) / 2); +} +""" + + let lab2error1 = """ +int main() { + return -(3 - 2 / 8)g +} +""" + + let lab2error2 = """ +int main() { + return -(3 + 2 / 8); +} +""" + + let lab2error3 = """ +int main() { + return 3 s 2 / 8; +} +""" + + let lab2error4 = """ +int main() { + return --3 - 2 / 8; +} +""" + let lab2error5 = """ +in main() { + return (-3 - 2 / 8); +} +""" + + let lab2error6 = """ +int main() + return -16 / 7 / 8; +} +""" + // MARK:- LAB 3 tests /* Result: 9 */ - let lab3test1 = """ - int main() { - float answ = -9; - return -answ; - } - """ + let lab3test1 = +""" +float main() { + float answ = -9; + return -answ; +} +""" /* Result: 3 */ let lab3test2 = """ - float main() { - float a = 10 / 5; - float b = a * 3; - return b / a; - } - """ +float main() { + float a = 10 / 5; + int b = a * 3; + return b / a; +} +""" /* 010 = 8 - Result: 128 + Result: 0 */ let lab3test3 = """ - int main() { - int deadline; - deadline = 16; - int time = 010; - return time * deadline; - } - """ +int main() { + int deadline; + deadline = 16; + int time = 010; + return -(deadline > time); +} +""" /* a = -4 @@ -126,80 +152,103 @@ struct Testers { Result: 48 */ let lab3test4 = """ - int main() { - int a; - int b; - a = (14 * 2) / -7; - b = a * (12.0 / 4); - return a * b; - } - """ +int main() { + int a; + int b; + a = (14 * 2) / -7 * (2 / 02); + b = a * (12.0 / 4); + return a * b; +} +""" + + /* + a = -4 + b = -12 + Result: 48 + */ + let lab3test5 = """ +int main() { + int a = 6 > 8; + int b = 10 > 010; + return a * b; +} +""" /* c not defind */ let lab3error1 = """ - int main() { - int a = 2; - int b = c * a; - return a * b; - } - """ +int main() { + int a = 2; + int b = c * a; + return a * b; +} +""" /* Missing return */ let lab3error2 = """ - int main() { - int a = -12.3; - int b = a * a; - int c = b * b; - } - """ +int main() { + float a = -12.3; + int b = a * a; + int c = b * b; +} +""" /* Invalid value number */ let lab3error3 = """ - float main() { - int somevariable; - somevariable = 2; - float number = somevariable = 4; - return number; - } - """ +float main() { + int a; + int b; + a = 2 * (b = 2); + return a; +} +""" /* Missing semicolon */ let lab3error4 = """ - int main() { - int value = 3 - return value; - } - """ +int main() { + int value = 3 + return value; +} +""" /* - + is not defind + с is not defind */ let lab3error5 = """ - int main() { - int a; - int b = c + a; - return a * b; - } - """ +int main() { + int a; + c = a * 3; + return a * c; +} +""" /* - + is not defind + value 2 already defind */ let lab3error6 = """ - int main() { - float value1 = 3.14; - int value2 = 2; - int value2 = 4; - return value2; - } - """ +int main() { + int value2; + value2 = 2; + int value2 = 4; + return value2; +} +""" + + /* + < is not defind + */ + let lab3error7 = """ +int main() { + float value1 = 3.14 < 2; + return value1; +} +""" }