Kaynağa Gözat

Initial commit.

Stan Hebben 6 yıl önce
ebeveyn
işleme
d5adc13307

+ 1
- 1
LICENSE Dosyayı Görüntüle

@@ -1,6 +1,6 @@
1 1
 MIT License
2 2
 
3
-Copyright (c) <year> <copyright holders>
3
+Copyright (c) 2018 Stan Hebben
4 4
 
5 5
 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 6
 

+ 4
- 0
collections/module.json Dosyayı Görüntüle

@@ -0,0 +1,4 @@
1
+{
2
+	"package": "collections",
3
+	"host": "universal"
4
+}

+ 11
- 0
collections/src/HashSet.zs Dosyayı Görüntüle

@@ -0,0 +1,11 @@
1
+export class HashSet<T> {
2
+	public implements Set<T> {
3
+		add(value as T) as bool;
4
+		remove(value as T) as bool;
5
+		
6
+		get size as int;
7
+		
8
+		in(value as T) as bool;
9
+		for(x as T);
10
+	}
11
+}

+ 73
- 0
collections/src/LinkedList.zs Dosyayı Görüntüle

@@ -0,0 +1,73 @@
1
+export class LinkedList<T> {
2
+	var first as Node?;
3
+	var last as Node?;
4
+	var size as int : get;
5
+	
6
+	public get empty as bool
7
+		=> first == null;
8
+	
9
+	public add(value as T) as void {
10
+		if first == null {
11
+			first = last = new Node(value);
12
+		} else {
13
+			val node = new Node(value);
14
+			last.next = node;
15
+			node.prev = last;
16
+			last = node;
17
+		}
18
+		size++;
19
+	}
20
+	
21
+	public clear() as void {
22
+		first = last = null;
23
+		size = 0;
24
+	}
25
+	
26
+	public [](index as int) as T {
27
+		var node = first;
28
+		while index > 0 {
29
+			if node == null
30
+				return panic<T>("index out of bounds");
31
+			
32
+			node = node.next;
33
+		}
34
+		
35
+		if node == null
36
+			return panic<T>("index out of bounds");
37
+		
38
+		return node.value;
39
+	}
40
+	
41
+	public implements Queue<T> {
42
+		poll() as T {
43
+			if first == null
44
+				return panic<T>("Cannot poll an empty queue");
45
+			
46
+			val result = first.value;
47
+			first = first.next;
48
+			if first == null
49
+				last = null;
50
+			else
51
+				first.prev = null;
52
+				
53
+			size--;
54
+		}
55
+		
56
+		peek() as T? {
57
+			return first.value;
58
+		}
59
+		
60
+		offer(value as T) as void
61
+			=> add(value);
62
+	}
63
+	
64
+	private struct Node {
65
+		var next as Node?;
66
+		var prev as Node?;
67
+		val value as T;
68
+		
69
+		this(value as T) {
70
+			this.value = value;
71
+		}
72
+	}
73
+}

+ 5
- 0
collections/src/NoSuchElementException.zs Dosyayı Görüntüle

@@ -0,0 +1,5 @@
1
+export class NoSuchElementException : Exception {
2
+	public this(message as string) {
3
+		super(message);
4
+	}
5
+}

+ 7
- 0
collections/src/Queue.zs Dosyayı Görüntüle

@@ -0,0 +1,7 @@
1
+export interface Queue<T> {
2
+	get empty as bool;
3
+	
4
+	poll() as T;
5
+	peek() as T;
6
+	push(value as T) as void;
7
+}

+ 12
- 0
collections/src/Set.zs Dosyayı Görüntüle

@@ -0,0 +1,12 @@
1
+export interface Set<T> {
2
+	add(value as T) as bool;
3
+	remove(value as T) as bool;
4
+	
5
+	get size as int;
6
+	
7
+	toArray();
8
+	toArray(comparator as function(a as T, b as T) as int);
9
+	
10
+	in(value as T) as bool;
11
+	for(x as T);
12
+}

+ 21
- 0
collections/src/Stack.zs Dosyayı Görüntüle

@@ -0,0 +1,21 @@
1
+export class Stack<T> {
2
+	var values as T[] = new T[](8);
3
+	var size as int : get = 0;
4
+	
5
+	public push(value as T) as void {
6
+		if size == values.length
7
+			values = values.copy(values.length * 2);
8
+			
9
+		values[size++] = value;
10
+	}
11
+	
12
+	public pop() as T {
13
+		if size == 0
14
+			throw new NoSuchElementException("Stack is empty!");
15
+		
16
+		return values[--size];
17
+	}
18
+	
19
+	public get empty as bool
20
+		=> size == 0;
21
+}

+ 4
- 0
io/module.json Dosyayı Görüntüle

@@ -0,0 +1,4 @@
1
+{
2
+	"package": "collections",
3
+	"host": "universal"
4
+}

+ 9
- 0
io/src/IOException.zs Dosyayı Görüntüle

@@ -0,0 +1,9 @@
1
+public class IOException : Exception {
2
+	public this(message as string) {
3
+		super(message);
4
+	}
5
+	
6
+	/*public this(message as string, cause as Exception) {
7
+		super(message, cause);
8
+	}*/
9
+}

+ 10
- 0
io/src/Reader.zs Dosyayı Görüntüle

@@ -0,0 +1,10 @@
1
+export interface Reader {
2
+	~this;
3
+	
4
+	read() as int throws IOException;
5
+	
6
+	read(buffer as char[]) as int throws IOException
7
+		=> read(buffer, 0, buffer.length);
8
+	
9
+	read(buffer as char[], offset as int, length as int) as int throws IOException;
10
+}

+ 22
- 0
io/src/StringReader.zs Dosyayı Görüntüle

@@ -0,0 +1,22 @@
1
+export class StringReader {
2
+	val data as char[];
3
+	var offset as int;
4
+	
5
+	public this(value as string) {
6
+		data = value.characters;
7
+	}
8
+	
9
+	public implements Reader {
10
+		~this {}
11
+	
12
+		read() as int
13
+			=> offset == data.length ? -1 : data[offset++];
14
+	
15
+		read(buffer as char[], offset as int, length as int) as int {
16
+			length = int.min(data.length - this.offset, length);
17
+			data.copyTo(buffer, this.offset, offset, length);
18
+			this.offset += length;
19
+			return length;
20
+		}
21
+	}
22
+}

+ 14
- 0
stdlib/module.json Dosyayı Görüntüle

@@ -0,0 +1,14 @@
1
+{
2
+	"package": "stdlib",
3
+	"host": "universal",
4
+	"globals": {
5
+		"Comparable": {"type": "Definition", "definition": "Comparable"},
6
+		"Exception": {"type": "Definition", "definition": "Exception"},
7
+		"Hashable": {"type": "Definition", "definition": "Hashable"},
8
+		"IllegalArgumentException": {"type": "Definition", "definition": "IllegalArgumentException"},
9
+		"StringBuilder": {"type": "Definition", "definition": "StringBuilder"},
10
+		"StringBuildable": {"type": "Definition", "definition": "StringBuildable"},
11
+		"panic": {"type": "Definition", "definition": "panic"},
12
+		"Result": {"type": "Definition", "definition": "Result"}
13
+	}
14
+}

+ 124
- 0
stdlib/src/Arrays.zs Dosyayı Görüntüle

@@ -0,0 +1,124 @@
1
+export expand <T : Comparable<T>> T[] {
2
+	public extern sort() as void;
3
+	public extern sorted() as T[];
4
+}
5
+
6
+export expand <T : Hashable<T>> T[] {
7
+	public implements Hashable<T[]> {
8
+		public extern hashCode() as int;
9
+		public extern == (other as T) as bool;
10
+	}
11
+}
12
+
13
+export expand <T> T[] {
14
+	public extern sort(comparator as function(a as T, b as T) as int) as void;
15
+	public extern sorted(comparator as function(a as T, b as T) as int) as T[];
16
+	public extern copy() as T[];
17
+	public extern copy(newSize as int) as T[];
18
+	public extern copyTo(target as T[], sourceOffset as int, targetOffset as int, length as int) as void;
19
+	
20
+	public get first as T?;
21
+	public get last as T?;
22
+	public get reversed as T[];
23
+	
24
+	public map<U>(projection as function(value as T) as U) as U[] {
25
+		return new U[]<T>(this, projection);
26
+	}
27
+	
28
+	public map<U>(projection as function(index as int, value as T) as U) as U[] {
29
+		return new U[]<T>(this, projection);
30
+	}
31
+	
32
+	public extern filter(predicate as function(value as T) as bool) as T[];
33
+	public extern filter(predicate as function(index as int, value as T) as bool) as T[];
34
+	
35
+	public each(consumer as function(value as T) as void) as void {
36
+		for value in this
37
+			consumer(value);
38
+	}
39
+	
40
+	public each(consumer as function(index as int, value as T) as void) as void {
41
+		for i, value in this
42
+			consumer(i, value);
43
+	}
44
+	
45
+	public contains(predicate as function(value as T) as bool) as bool {
46
+		for value in this
47
+			if predicate(value)
48
+				return true;
49
+		
50
+		return false;
51
+	}
52
+	
53
+	public contains(predicate as function(index as int, value as T) as bool) as bool {
54
+		for i, value in this
55
+			if predicate(i, value)
56
+				return true;
57
+		
58
+		return false;
59
+	}
60
+	
61
+	public all(predicate as function(value as T) as bool) as bool {
62
+		for value in this
63
+			if !predicate(value)
64
+				return false;
65
+		
66
+		return true;
67
+	}
68
+	
69
+	public all(predicate as function(i as int, value as T) as bool) as bool {
70
+		for i, value in this
71
+			if !predicate(i, value)
72
+				return false;
73
+		
74
+		return true;
75
+	}
76
+	
77
+	public first(predicate as function(value as T) as bool) as T? {
78
+		for value in this
79
+			if predicate(value)
80
+				return value;
81
+		
82
+		return null;
83
+	}
84
+	
85
+	public first(predicate as function(i as int, value as T) as bool) as T? {
86
+		for i, value in this
87
+			if predicate(i, value)
88
+				return value;
89
+		
90
+		return null;
91
+	}
92
+	
93
+	public last(predicate as function(value as T) as bool) as T? {
94
+		for i, value in this.reversed
95
+			if predicate(value)
96
+				return value;
97
+		
98
+		return null;
99
+	}
100
+	
101
+	public last(predicate as function(index as int, value as T) as bool) as T? {
102
+		for i, value in this.reversed
103
+			if predicate(i, value)
104
+				return value;
105
+		
106
+		return null;
107
+	}
108
+	
109
+	public count(predicate as function(value as T) as bool) as int {
110
+		var result = 0;
111
+		for value in this
112
+			if predicate(value)
113
+				result++;
114
+		return result;
115
+	}
116
+	
117
+	public count(predicate as function(index as int, value as T) as bool) as int {
118
+		var result = 0;
119
+		for i, value in this
120
+			if predicate(i, value)
121
+				result++;
122
+		return result;
123
+	}
124
+}

+ 8
- 0
stdlib/src/Assoc.zs Dosyayı Görüntüle

@@ -0,0 +1,8 @@
1
+export expand <K, V> V[K] {
2
+	mapValues<W>(projection as function(value as V) as W) as W[K] {
3
+		val result = new W[K];
4
+		for k, v in this
5
+			result[k] = projection(v);
6
+		return result;
7
+	}
8
+}

+ 5
- 0
stdlib/src/Chars.zs Dosyayı Görüntüle

@@ -0,0 +1,5 @@
1
+export expand char {
2
+	public times(number as int) as string {
3
+		return new string(new char[](number, this));
4
+	}
5
+}

+ 3
- 0
stdlib/src/Comparable.zs Dosyayı Görüntüle

@@ -0,0 +1,3 @@
1
+export interface Comparable<T> {
2
+	compareTo(other as T) as int;
3
+}

+ 5
- 0
stdlib/src/EnforcementLevel.zs Dosyayı Görüntüle

@@ -0,0 +1,5 @@
1
+public enum EnforcementLevel {
2
+	INFO,
3
+	ENFORCE,
4
+	PROVE
5
+}

+ 4
- 0
stdlib/src/Exception.zs Dosyayı Görüntüle

@@ -0,0 +1,4 @@
1
+export virtual class Exception {
2
+	public this(message as string) {}
3
+	public this(message as string, cause as Exception) {}
4
+}

+ 4
- 0
stdlib/src/Hashable.zs Dosyayı Görüntüle

@@ -0,0 +1,4 @@
1
+public interface Hashable<T> {
2
+	hashCode() as int;
3
+	== (other as T) as bool;
4
+}

+ 5
- 0
stdlib/src/IllegalArgumentException.zs Dosyayı Görüntüle

@@ -0,0 +1,5 @@
1
+export class IllegalArgumentException : Exception {
2
+	public this(message as string) {
3
+		super(message);
4
+	}
5
+}

+ 0
- 0
stdlib/src/IllegalOperationException.zs Dosyayı Görüntüle


+ 8
- 0
stdlib/src/Integers.zs Dosyayı Görüntüle

@@ -0,0 +1,8 @@
1
+expand int {
2
+	public extern toHexString() as string;
3
+	
4
+	public static min(a as int, b as int) as int
5
+		=> a < b ? a : b;
6
+	public static max(a as int, b as int) as int
7
+		=> a > b ? a : b;
8
+}

+ 17
- 0
stdlib/src/List.zs Dosyayı Görüntüle

@@ -0,0 +1,17 @@
1
+[Native("stdlib::List")]
2
+export class List<T> {
3
+	[Native("constructor")]
4
+	public this() {}
5
+	[Native("add")]
6
+	public add(value as T) as void;
7
+	[Native("getIndex")]
8
+	public [](index as int) as T;
9
+	[Native("setIndex")]
10
+	public []=(index as int, value as T) as T;
11
+	[Native("toArray")]
12
+	public as T[];
13
+	[Native("length")]
14
+	public get length as int;
15
+	[Native("isEmpty")]
16
+	public get isEmpty as bool;
17
+}

+ 4
- 0
stdlib/src/Panic.zs Dosyayı Görüntüle

@@ -0,0 +1,4 @@
1
+// TODO: function overloading on global functions
2
+//extern function panic() as void;
3
+
4
+extern function panic<T>(message as string) as T;

+ 48
- 0
stdlib/src/Result.zs Dosyayı Görüntüle

@@ -0,0 +1,48 @@
1
+export variant Result<T, E> {
2
+	Ok(T),
3
+	Error(E);
4
+	
5
+	public then<R>(fn as function(result as T) as Result<R, E>) as Result<R, E> {
6
+		return match this {
7
+			Ok(result) => fn(result),
8
+			Error(error) => Error(error)
9
+		};
10
+	}
11
+	
12
+	public handle<X>(handler as function(error as E) as Result<T, X>) as Result<T, X> {
13
+		return match this {
14
+			Ok(result) => Ok(result),
15
+			Error(error) => handler(error)
16
+		};
17
+	}
18
+	
19
+	public expect() as T {
20
+		return match this {
21
+			Ok(result) => result,
22
+			Error(error) => panic<T>("expect() called on an error value")
23
+		};
24
+	}
25
+	
26
+	public orElse(other as T) as T {
27
+		return match this {
28
+			Ok(result) => result,
29
+			Error(error) => other
30
+		};
31
+	}
32
+	
33
+	public orElse(other as function(error as E) as T) as T {
34
+		return match this {
35
+			Ok(result) => result,
36
+			Error(error) => other(error)
37
+		};
38
+	}
39
+}
40
+
41
+export expand <T, E : Exception> Result<T, E> {
42
+	public unwrap() as T {
43
+		return match this {
44
+			Ok(result) => result,
45
+			Error(error) => throw error
46
+		};
47
+	}
48
+}

+ 6
- 0
stdlib/src/StringBuildable.zs Dosyayı Görüntüle

@@ -0,0 +1,6 @@
1
+export interface StringBuildable {
2
+	toString(output as StringBuilder) as void;
3
+	
4
+	as string
5
+		=> new StringBuilder() << this;
6
+}

+ 49
- 0
stdlib/src/StringBuilder.zs Dosyayı Görüntüle

@@ -0,0 +1,49 @@
1
+[Native("stdlib::StringBuilder")]
2
+export class StringBuilder {
3
+	[Native("constructor")]
4
+	public extern this();
5
+	[Native("constructorWithCapacity")]
6
+	public extern this(capacity as int);
7
+	[Native("constructorWithValue")]
8
+	public extern this(value as string);
9
+	
10
+	[Native("isEmpty")]
11
+	public extern get isEmpty as bool;
12
+	[Native("length")]
13
+	public extern get length as int;
14
+	
15
+	[Native("appendBool")]
16
+	public extern <<(value as bool) as StringBuilder;
17
+	[Native("appendByte")]
18
+	public extern <<(value as byte) as StringBuilder;
19
+	[Native("appendSByte")]
20
+	public extern <<(value as sbyte) as StringBuilder;
21
+	[Native("appendShort")]
22
+	public extern <<(value as short) as StringBuilder;
23
+	[Native("appendUShort")]
24
+	public extern <<(value as ushort) as StringBuilder;
25
+	[Native("appendInt")]
26
+	public extern <<(value as int) as StringBuilder;
27
+	[Native("appendUInt")]
28
+	public extern <<(value as uint) as StringBuilder;
29
+	[Native("appendLong")]
30
+	public extern <<(value as long) as StringBuilder;
31
+	[Native("appendULong")]
32
+	public extern <<(value as ulong) as StringBuilder;
33
+	[Native("appendFloat")]
34
+	public extern <<(value as float) as StringBuilder;
35
+	[Native("appendDouble")]
36
+	public extern <<(value as double) as StringBuilder;
37
+	[Native("appendChar")]
38
+	public extern <<(value as char) as StringBuilder;
39
+	[Native("appendString")]
40
+	public extern <<(value as string) as StringBuilder;
41
+	
42
+	public <<(value as StringBuildable) as StringBuilder {
43
+		value.toString(this);
44
+		return this;
45
+	}
46
+	
47
+	[Native("asString")]
48
+	public extern implicit as string;
49
+}

+ 74
- 0
stdlib/src/Strings.zs Dosyayı Görüntüle

@@ -0,0 +1,74 @@
1
+export expand string {
2
+	public const in(c as char) as bool
3
+		=> indexOf(c) >= 0;
4
+	
5
+	public const indexOf(c as char) as int {
6
+		for i in 0 .. length {
7
+			if this[i] == c
8
+				return i;
9
+		}
10
+		
11
+		return -1;
12
+	}
13
+	
14
+	public const indexOf(c as char, from as int) as int {
15
+		for i in from .. length {
16
+			if this[i] == c
17
+				return i;
18
+		}
19
+		
20
+		return -1;
21
+	}
22
+	
23
+	public const lastIndexOf(c as char) as int {
24
+		var i = length;
25
+		while i > 0 {
26
+			i--;
27
+			if this[i] == c
28
+				return i;
29
+		}
30
+		
31
+		return -1;
32
+	}
33
+	
34
+	public const lastIndexOf(c as char, until as int) as int {
35
+		var i = until;
36
+		while i > 0 {
37
+			i--;
38
+			if this[i] == c
39
+				return i;
40
+		}
41
+		
42
+		return -1;
43
+	}
44
+	
45
+	public const split(delimiter as char) as string[] {
46
+		val result = new List<string>();
47
+		var start = 0;
48
+		for i in 0 .. this.length {
49
+			if this[i] == delimiter {
50
+				result.add(this[start .. i]);
51
+				start = i + 1;
52
+			}
53
+		}
54
+		result.add(this[start .. $]);
55
+		return result as string[];
56
+	}
57
+	
58
+	public const trim() as string {
59
+		var from = 0;
60
+		while from < this.length && this[from] in [' ', '\t', '\r', '\n']
61
+			from++;
62
+		var to = this.length;
63
+		while to > 0 && this[to - 1] in [' ', '\t', '\r', '\n']
64
+			to--;
65
+		
66
+		return to < from ? "" : this[from .. to];
67
+	}
68
+	
69
+	public const lpad(length as int, c as char) as string
70
+		=> this.length >= length ? this : c.times(length - this.length) + this;
71
+	
72
+	public const rpad(length as int, c as char) as string
73
+		=> this.length >= length ? this : this + c.times(length - this.length);
74
+}

Loading…
İptal
Kaydet