Skip to content

Commit

Permalink
Revert 8_custom_functions.swift file contents
Browse files Browse the repository at this point in the history
  • Loading branch information
loumadev committed Dec 5, 2023
1 parent 92c59f0 commit dbdcc89
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 129 deletions.
149 changes: 20 additions & 129 deletions test/compiler/codegen/swift_samples/8_custom_functions.swift
Original file line number Diff line number Diff line change
@@ -1,131 +1,22 @@
// func __stringify__(_ n: Double?) -> String {
// if(n == nil) { return "nil" }
// if(n == 0) { return "0" }

// var num = n!

// var isNegative = false
// if(num < 0) {
// isNegative = true
// num = 0 - num
// }

// var integerPart = Int2Double(Double2Int(num))
// var fractionalPart = num - integerPart
// var hasFractionalPart = fractionalPart > 0

// var integerResult = ""
// var divisor = 1.0

// // Find the appropriate divisor to get the most significant digit of the integer part
// while(integerPart / divisor >= 10) {
// divisor = divisor * 10
// }

// // Extract digits for the integer part
// while(divisor >= 1) {
// let digit = Double2Int(integerPart / divisor)
// integerResult = integerResult + chr(digit + 48) // Convert digit to character code
// integerPart = Int2Double(__modulo__(integerPart, divisor))
// divisor = divisor / 10
// }

// let precision = 15
// var position = 0
// var fractionalResult = ""

// var floatOffset = length(integerResult) + 1 // +1 for the decimal point
// var zeroIndex = 0

// // Extract digits for the fractional part
// while(precision > position && fractionalPart > 0) {
// fractionalPart = fractionalPart * 10
// let digit = Double2Int(fractionalPart)
// fractionalResult = fractionalResult + chr(digit + 48) // Convert digit to character code
// fractionalPart = fractionalPart - Int2Double(digit)
// position = position + 1

// if(digit == 0) {
// if(zeroIndex == 0) {
// zeroIndex = position
// }
// } else {
// zeroIndex = 0
// }
// }

// // Add fractional part if it exists
// if(hasFractionalPart) {
// integerResult = integerResult + "." + fractionalResult
// }

// // Remove trailing zeros
// if(zeroIndex > 0) {
// integerResult = substring(of: integerResult, startingAt: 0, endingBefore: floatOffset + zeroIndex - 1)!
// }

// // Add negative sign if needed
// if(isNegative) {
// integerResult = "-" + integerResult
// }

// return integerResult
// }

// func __stringify__(_ n: Int?) -> String {
// if(n == nil) { return "nil" }

// return __stringify__(Int2Double(n!))
// }

// func __stringify__(_ b: Bool?) -> String {
// if(b == nil) { return "nil" }

// if(b!) {
// return "true"
// } else {
// return "false"
// }
// func foo() -> Void {
// write("Hello, World!")
// }

// func __stringify__(_ s: String?) -> String {
// if(s == nil) { return "nil" }

// return s!
// }

// func __modulo__(_ a: Double, _ b: Double) -> Int {
// return Double2Int(a - Int2Double(Double2Int(a / b)) * b)
//
// foo()
func bar(_ n : Int) -> Int {
if (n == 0) {
return 0
} else {
return n + bar(n - 1)
}
}

let a = bar(3)
write("Result of bar: ", a, "\n")

// func sum(_ a: Int, _ b: Int) -> Int {
// return a + b
// }


// var int = 123
// var float = 456.789
// var bool = true
// var str = "Hello World!"
// var nilVal: Int? = nil
// var result = "My int is " + __stringify__(int)
// + ", float is " + __stringify__(float)
// + ", boolean is " + __stringify__(bool)
// + ", string is " + __stringify__(str)
// + ", and nil is " + __stringify__(nilVal)
// write(result, "\n")


func f() -> Int {return 1}
func f() -> Double {return 2.0}
func f() -> String {return "3"}
func f() -> Bool {return true}

var int = 123
var float = 456.789
var bool = true
var str = "Hello World!"
var nilVal: Int? = nil

var result = "My int is \(int), float is \(float), boolean is \(bool), string is \(str), and nil is \(nilVal)"
var expr = "Expression = \((2 + f()) * (f() - 3.5) * 5)"
var nested = "Nested = \("Nested = \("Nested = \(1 + 1)")")"

write("\n", result, "\n", expr, "\n", nested, "\n")

//
// let resultOfSum = sum(1, 2)
// write("Result of sum: ", resultOfSum, "\n")

0 comments on commit dbdcc89

Please sign in to comment.