Method functions now work.

This commit is contained in:
Christoffer Lerno
2020-05-27 12:26:38 +02:00
parent 28f307d467
commit 1b69423f45
10 changed files with 115 additions and 48 deletions

View File

@@ -1,6 +1,6 @@
module bigint;
macro @max(a, b)
macro max(a, b)
{
return a > b ? a : b;
}
@@ -8,12 +8,12 @@ macro @max(a, b)
// Horribly bad implementation of BigInt with add/sub.
public struct BigInt @opaque
{
byte& number;
byte* number;
uint length;
char sign;
}
public func void BigInt.init(BigInt& bigInt)
public func void BigInt.init(BigInt* bigInt)
{
bigInt.number = malloc(1);
bigInt.number[0] = 0;
@@ -21,7 +21,7 @@ public func void BigInt.init(BigInt& bigInt)
bigInt.sign = 1;
}
public func void BigInt.initFromString(BigInt& bigInt, char& str)
public func void BigInt.initFromString(BigInt* bigInt, char* str)
{
uint size = strlen(str);
bigInt.sign = 1;
@@ -46,14 +46,7 @@ public func void BigInt.initFromString(BigInt& bigInt, char& str)
bigInt.length = size;
}
public func BigInt& BigInt.newFromString(char& str)
{
BigInt& bigInt = malloc(sizeof(BigInt));
bigInt.initFromString(str);
return bigInt;
}
public func void BigInt.copyTo(BigInt& source, BigInt& target)
public func void BigInt.copyTo(BigInt* source, BigInt* target)
{
target.number = realloc(target.number, source.length);
target.sign = source.sign;
@@ -61,12 +54,12 @@ public func void BigInt.copyTo(BigInt& source, BigInt& target)
for (uint i = 0; i < target.length; i++) target.number[i] = source.number[i];
}
public func void BigInt.destroy(BigInt& bigInt)
public func void BigInt.destroy(BigInt* bigInt)
{
free(bigInt.number);
}
func void BigInt.addIgnoreSign(BigInt& a, BigInt& b, BigInt& result)
func void BigInt.addIgnoreSign(BigInt* a, BigInt* b, BigInt* result)
{
uint length = @max(a.length, b.length) + 1;
byte* res = malloc(length);
@@ -105,7 +98,7 @@ func void BigInt.addIgnoreSign(BigInt& a, BigInt& b, BigInt& result)
result.length = length;
}
public func void BigInt.getMaxVal(BigInt &bigInt, uint& pos, int& val)
public func void BigInt.getMaxVal(BigInt* bigInt, uint* pos, int* val)
{
for (uint i = bigInt.length; i > 0; i++)
{
@@ -120,7 +113,7 @@ public func void BigInt.getMaxVal(BigInt &bigInt, uint& pos, int& val)
*val = 0;
}
public func char BigInt.compare(BigInt& a, BigInt& b)
public func char BigInt.compare(BigInt* a, BigInt* b)
{
if (a.sign != b.sign) return a.sign;
byte aMax;
@@ -137,7 +130,7 @@ public func char BigInt.compare(BigInt& a, BigInt& b)
return 0;
}
public func char BigInt.compareNoSign(BigInt& a, BigInt& b)
public func char BigInt.compareNoSign(BigInt* a, BigInt* b)
{
byte aMax;
uint aMaxPos;
@@ -153,13 +146,13 @@ public func char BigInt.compareNoSign(BigInt& a, BigInt& b)
return 0;
}
func void BigInt.subIgnoreSign(BigInt& a, BigInt& b, BigInt& result)
func void BigInt.subIgnoreSign(BigInt* a, BigInt* b, BigInt* result)
{
uint length = @max(a.length, b.length);
byte& res = malloc(length);
byte* res = malloc(length);
BigInt& x;
BigInt& y;
BigInt* x;
BigInt* y;
if (a.compareNoSign(b) < 0)
{
result.sign = -1;
@@ -194,7 +187,7 @@ func void BigInt.subIgnoreSign(BigInt& a, BigInt& b, BigInt& result)
result.length = length;
}
public func void BigInt.add(BigInt& a, BigInt& b, BigInt& result)
public func void BigInt.add(BigInt* a, BigInt* b, BigInt* result)
{
if (a.sign == b.sign)
{
@@ -234,7 +227,7 @@ public func char* BigInt.toCharArray(BigInt* bigInt)
return out;
}
public func void BigInt.print(BigInt& bigInt)
public func void BigInt.print(BigInt* bigInt)
{
char* chars = bigInt.toCharArray();
puts(chars);
@@ -251,7 +244,7 @@ public func void BigInt.fprint(BigInt* bigInt, FILE* file)
}*/
public func int main(int size, char*& args)
public func int main(int size, char** args)
{
BigInt minus2;
BigInt minus1;

View File

@@ -943,6 +943,37 @@ func int eokf(int x)
return x * x + 1;
}
func void Big.init(Big* big)
{
big.x = 1;
big.y = 2;
big.z = 3;
}
func void Big.mult(Big* big, int x)
{
big.x *= x;
big.y *= x;
big.z *= x;
}
struct Bork
{
Big x;
Big y;
}
func void testMethodFunctions()
{
Big b;
b.init();
printf("%d %d %d\n", b.x, b.y, b.z);
b.mult(3);
printf("%d %d %d\n", b.x, b.y, b.z);
Bork x;
x.x.init();
x.x.mult(4);
printf("%d %d %d\n", x.x.x, x.x.y, x.x.z);
}
func int main(int x)
{
printf("Helo!\n");
@@ -958,6 +989,7 @@ func int main(int x)
testType();
testExprBlock();
testFuncPointer();
testMethodFunctions();
int efd = 9;
uint fefoek = 1;
printf("Helo: %d\n", efd + cast(fefoek, int));