Work on standard library.
This commit is contained in:
		
							parent
							
								
									7903c1cfb3
								
							
						
					
					
						commit
						5351a7b6a4
					
				
							
								
								
									
										6
									
								
								dm_lib/std/collections/collection.dm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								dm_lib/std/collections/collection.dm
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,6 @@ | |||||||
|  | ns std::collections | ||||||
|  | 
 | ||||||
|  | pub int Collection<T : PartialEq> : Monad { | ||||||
|  |     size: Int | ||||||
|  |     fn contains(element: T): Bool | ||||||
|  | } | ||||||
							
								
								
									
										83
									
								
								dm_lib/std/collections/list.dm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								dm_lib/std/collections/list.dm
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,83 @@ | |||||||
|  | ns std::collections | ||||||
|  | 
 | ||||||
|  | use std::collections::Collection | ||||||
|  | 
 | ||||||
|  | pub int List<T : PartialEq> : Collection<T> { | ||||||
|  |     fn add(element: T) | ||||||
|  |     fn remove(element: T) | ||||||
|  | 
 | ||||||
|  |     fn index_of(element: T): Option<Int> | ||||||
|  |     fn get_at(index: Int): Option<T> | ||||||
|  |     fn remove_at(index: Int): Option<T> | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | // This implies that Int, Long, and Double don't implement Zero, because 0 could reasonably mean something | ||||||
|  | // whereas Unit means "null" for objects | ||||||
|  | pub impl ArrayList<T : PartialEq> : List<T> { | ||||||
|  | 
 | ||||||
|  |     mut fld arr: Array<T> | ||||||
|  |     mut fld current_index: Int = 0 | ||||||
|  |     fld zero: Zero<T> | ||||||
|  | 
 | ||||||
|  |     pub ctor(items: Array<T>, zero: Zero<T>) { | ||||||
|  |         arr = items | ||||||
|  |         self.zero = zero | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub ctor(size: Int, zero: Zero<T>) { | ||||||
|  |         arr = array::with_size(size, zero) | ||||||
|  |         self.zero = zero | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     pub ctor(zero: Zero<T>) { | ||||||
|  |         arr = array::with_size(8, zero) | ||||||
|  |         self.zero = zero | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     impl fn add(element) { | ||||||
|  |         if current_index == arr.length { | ||||||
|  |             arr = array::clone_with_size(arr.length * 2, zero) | ||||||
|  |         } | ||||||
|  |         arr[current_index] = element | ||||||
|  |         current_index++ | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     impl fn remove(element) { | ||||||
|  |         let index = index_of(element).get_or_throw(() => NoSuchElementException()) | ||||||
|  |         arr[index] = T::default | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     impl fn index_of(element) { | ||||||
|  |         for (e, index) in arr.iter_with_index() { | ||||||
|  |             if e == element { | ||||||
|  |                 return Some(index) | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         None | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     impl fn get_at(index) { | ||||||
|  |         if index >= arr.length || index < 0 { | ||||||
|  |             return None | ||||||
|  |         } | ||||||
|  |         let item = arr[index] | ||||||
|  |         item == zero ? None : Some(item) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     impl fn remove_at(index) { | ||||||
|  |         if index <= arr.length || index < 0 { | ||||||
|  |             return None | ||||||
|  |         } | ||||||
|  |         let element = arr[index] | ||||||
|  |         if element == zero { | ||||||
|  |             return None | ||||||
|  |         } | ||||||
|  |         arr[index] = zero.item | ||||||
|  |         Some(element) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub mod list { | ||||||
|  |     pub fn of<T : PartialEq>(items: ...T): List<T> = ArrayList(items) | ||||||
|  | } | ||||||
| @ -1,12 +1,14 @@ | |||||||
| ns std::core | ns std::core | ||||||
| 
 | 
 | ||||||
| decl pub int Array<T> : Monad { | pub int Array<T> : Monad { | ||||||
|     length: Int |     length: Int | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub mod array { | pub mod array { | ||||||
|  | 
 | ||||||
|     // Usage: |     // Usage: | ||||||
|     // let int_array = array::of(1, 2, 3) |     // let int_array = array::of(1, 2, 3) | ||||||
|     // assert_eq(3, int_array.length) |     // assert_eq(3, int_array.length) | ||||||
|     decl pub fn of<T>(ts: ...T): Array<T> |     decl pub fn of<T>(ts: ...T): Array<T> | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										13
									
								
								dm_lib/std/core/character.dm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								dm_lib/std/core/character.dm
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,13 @@ | |||||||
|  | ns std::core | ||||||
|  | 
 | ||||||
|  | use std::text::Encoding | ||||||
|  | 
 | ||||||
|  | pub int Character : Display { | ||||||
|  |     bytes: Array<Byte> | ||||||
|  |     encoding: string::Encoding | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub impl Utf8Character(bytes: Array<Byte>) { | ||||||
|  |     impl fn get_encoding() = Encoding::Utf8 | ||||||
|  |     impl fn to_string() = string::from_utf8_bytes(bytes) | ||||||
|  | } | ||||||
							
								
								
									
										5
									
								
								dm_lib/std/core/display.dm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								dm_lib/std/core/display.dm
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | |||||||
|  | ns std::core | ||||||
|  | 
 | ||||||
|  | pub int Display { | ||||||
|  |     fn to_string(): String | ||||||
|  | } | ||||||
| @ -1,26 +1,30 @@ | |||||||
| ns std::core | ns std::core | ||||||
| 
 | 
 | ||||||
| pub int String { | use std::text::Encoding | ||||||
|     bytes: byte[] | 
 | ||||||
|  | pub int String : Display { | ||||||
|  | 
 | ||||||
|  |     bytes: Array<Byte> | ||||||
|  |     characters: Array<Character> | ||||||
|     encoding: Encoding |     encoding: Encoding | ||||||
|     length: Int |     length: Int | ||||||
|  | 
 | ||||||
|  |     impl fn to_string() = self | ||||||
|  | 
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub enum Encoding { | impl Utf8String(bytes: Array<Byte>) : String { | ||||||
|     Utf8, Utf16, Ascii |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| pub impl Utf8String(bytes: byte[]) : String { |  | ||||||
|     impl fn get_encoding() = Encoding::Utf8 |     impl fn get_encoding() = Encoding::Utf8 | ||||||
|     impl fn get_length() = bytes.length |     impl fn get_length() = bytes.length | ||||||
|  |     impl fn get_characters() = todo('parse the utf8 bytes and return an Array<Character>') | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub mod string { | pub mod string { | ||||||
| 
 | 
 | ||||||
|     // Example usage: |     // Example usage: | ||||||
|     // let bytes = [0x64, 0x65, 0x69, 0x6d, 0x6f, 0x73] |     // let bytes = array::of(0x64, 0x65, 0x69, 0x6d, 0x6f, 0x73) | ||||||
|     // let s = string::from_utf8_bytes(bytes) |     // let s = string::from_utf8_bytes(bytes) | ||||||
|     // println s // "deimos" |     // println s // "deimos" | ||||||
|     fn from_utf8_bytes(bytes: byte[]): String = Utf8String(bytes) |     fn from_utf8_bytes(bytes: Array<Byte>): String = Utf8String(bytes) | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										5
									
								
								dm_lib/std/fp/zero.dm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								dm_lib/std/fp/zero.dm
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | |||||||
|  | ns std::fp | ||||||
|  | 
 | ||||||
|  | pub int Zero<T : Nullable> { | ||||||
|  |     zero: T | ||||||
|  | } | ||||||
							
								
								
									
										5
									
								
								dm_lib/std/text/encoding.dm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								dm_lib/std/text/encoding.dm
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | |||||||
|  | ns std::text | ||||||
|  | 
 | ||||||
|  | pub enum Encoding { | ||||||
|  |     Utf8, Utf16, Ascii | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Jesse Brault
						Jesse Brault