work on string, bug fixes, overloaded assignment operator. Still need to get overloaded copy_construct for declaration assignment

This commit is contained in:
Nathan Braswell
2015-06-01 01:43:23 -04:00
parent bbcebf7c17
commit 6f9ceaa717
10 changed files with 124 additions and 55 deletions

View File

@@ -1,4 +1,5 @@
import string:*;
import mem:*
__if_comp__ __C__ simple_passthrough """
#include <stdio.h>
@@ -8,6 +9,11 @@ fun println() : void {
print("\n");
}
fun println<T>(toPrint: T) : void {
print(toPrint)
print("\n")
}
fun print(toPrint: char*) : void {
__if_comp__ __C__ {
simple_passthrough(toPrint = toPrint::) """
@@ -17,17 +23,10 @@ fun print(toPrint: char*) : void {
return;
}
fun println(toPrint: char*) : void {
print(toPrint);
println();
}
fun print(toPrint: string) : void {
print(toPrint.toCharArray());
}
fun println(toPrint: string): void {
println(toPrint.toCharArray());
var charArr = toPrint.toCharArray()
defer delete(charArr)
print(charArr);
}
fun print(toPrint: int): void {
@@ -39,11 +38,6 @@ fun print(toPrint: int): void {
return;
}
fun println(toPrint: int): void {
print(toPrint);
println();
}
fun print(toPrint: float): void {
__if_comp__ __C__ {
simple_passthrough(toPrint = toPrint::) """
@@ -62,15 +56,3 @@ fun print(toPrint: double) : void{
return;
}
fun println(toPrint: float): void {
print(toPrint);
println();
}
fun println(toPrint: double): void {
print(toPrint);
println();
}

View File

@@ -13,15 +13,45 @@ obj string (Destructable) {
data.addEnd(*str);
str += 1;
}
// no null terminator
return this;
}
fun construct(vec: vector::vector<char>): string* {
data.copy_construct(&vec);
return this;
}
fun copy_construct(old: string*): void {
data.copy_construct(&old->data)
}
fun destruct():void {
data.destruct()
}
fun operator=(str: char*): void {
destruct();
construct(str)
}
fun operator+(str: char*): string {
var newStr.construct(str):string
var ret.construct(data + newStr.data):string
return ret
}
fun operator+=(str: char*): void {
var newStr.construct(str):string
data += newStr.data
}
fun toCharArray(): char* {
var out: char* = mem::new<char>(data.size);
var out: char* = mem::new<char>(data.size+1);
for (var i: int = 0; i < data.size; i++;)
out[i] = data.get(i);
// null terminator
out[data.size] = 0
return out;
}
};

View File

@@ -29,7 +29,29 @@ obj vector<T> (Destructable) {
}
fun destruct(): void {
delete<T>(data);
if (data)
delete<T>(data);
data = 0
}
fun operator=(other:vector<T>):void {
resize(other.size)
for (var i = 0; i < other.size; i++;)
set(i, other.get(i))
}
fun operator+(other:vector<T>):vector<T> {
var newVec.construct(size + other.size):vector<T>
for (var i = 0; i < size; i++;)
newVec.set(i, get(i))
for (var i = 0; i < other.size; i++;)
newVec.set(i+size, other.get(i))
return newVec
}
fun operator+=(other:vector<T>):void {
for (var i = 0; i < other.size; i++;)
addEnd(other.get(i))
}
fun clone(): vector<T> {
@@ -48,6 +70,7 @@ obj vector<T> (Destructable) {
delete<T>(data, 0);
data = newData;
available = newSize;
size = lesser(size, newSize)
return true;
}