Advertisement
Guest User

Degu Language Example

a guest
Nov 6th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.38 KB | None | 0 0
  1. ######################################################
  2.  
  3. import "std/io" use std:io
  4.  
  5. # This is a comment!
  6.  
  7. func main() -> i32
  8. {
  9. # Display the text 'Hello, World!'
  10. println("Hello, World!");
  11.  
  12. # Exit the program with a status code of 0
  13. ret 0;
  14. }
  15.  
  16. ######################################################
  17.  
  18. import "std/io" use std:io
  19.  
  20. func main(char[&][&] args) -> i32
  21. {
  22. # In Degu, array references implicitly include their length (this is why they are not referred to as 'pointers')
  23. # C-style loops are extremely powerful, so Degu uses them too.
  24. for (i32 i = 0; i < args.len; i ++)
  25. println("Arg {0} is {1}.", i, args[i]);
  26.  
  27. println("There are {0} arguments.", args.len);
  28.  
  29. ret 0;
  30. }
  31.  
  32. ######################################################
  33.  
  34. import "std/io"
  35.  
  36. func factorial(i32 x) -> i32
  37. {
  38. if (x > 1)
  39. ret factorial(x - 1) * x; # Recursive function call
  40. else
  41. ret 1;
  42. }
  43.  
  44. func main() -> i32
  45. {
  46. for (i32 i = 1; i <= 8; i ++)
  47. std:io:println("{0}! = {1}", i, factorial(i));
  48.  
  49. ret 0;
  50. }
  51.  
  52. ######################################################
  53.  
  54. type vec2 { f32 x, f32 y };
  55.  
  56. func main()
  57. {
  58. # If an instance of a type is initialised without construction, all primitive types within it are implicitly set to '0' (or whatever the equivalent is for that type)
  59. # i32 default: 0
  60. # f32 default: 0.0
  61. # void& default: 0x0
  62. # void[&] default: start = 0x0, length = 0
  63. # i32[n] default: { 0, 0, 0, ... }
  64.  
  65. vec2 v;
  66. }
  67.  
  68. ######################################################
  69.  
  70. import "std/io" use std:io
  71. import "std/str" use std:str
  72.  
  73. type PhonePair { str name, str number };
  74.  
  75. func main()
  76. {
  77. # Arrays can be declared with a C-style brace initializer. When using this method, the length of the array can be inferred at compile-time.
  78. PhonePair[?] phonebook = {
  79. { .name = "Alice", .number = "07345 123456" }, # C-style brace initializers exist for types too!
  80. { .name = "Bob", .number = "07873 248163" },
  81. { .name = "Charlie", .number = "07123 654321" },
  82. };
  83.  
  84. for (usize i = 0; i < phonebook.len; i ++)
  85. println("{0} can be called on {1}", phonebook[i].name, phonebook[i].number);
  86. }
  87.  
  88. ######################################################
  89.  
  90. import "std/io" use std:io
  91.  
  92. # Because 'ptr' is an array reference, we can pass arrays of any size
  93. # If ptr was declared as 'char[?]', the compiler would produce an error since the size of the value array can not be inferred at compile-time.
  94. func print_arr(char[&] ptr)
  95. {
  96. println("The char array reference points to '{0}'.", ptr);
  97. }
  98.  
  99. func main()
  100. {
  101. # Here, we use compile-time size inference
  102. char[?] arr = "The Octodon genus of the Degu is native to South America";
  103.  
  104. # This is an array reference. The length of the array is implicitly an attribute of the reference, and is known only at run-time
  105. char[&] ptr = arr&;
  106.  
  107. # We can print a char array
  108. println("The char array is '{0}'.", arr);
  109.  
  110. # We can print a char array reference
  111. println("The char array reference points to '{0}'.", ptr);
  112.  
  113. # Array pointers can be passed to functions
  114. print_arr(arr);
  115. }
  116.  
  117. ######################################################
  118.  
  119. import "std/io" use std:io
  120. import "std/str" use std:str
  121. import "std/array" use std:array
  122.  
  123. func main()
  124. {
  125. # The standard library contains many useful types that manage memory automatically. 'str' types contain heap-allocated representations of character arrays
  126. str text = str("Hello! These are some words.");
  127.  
  128. # 'array' contains a heap-allocated array of items. This is necessary, since the number of items cannot be determined at compile-time.
  129. array<str> words = text.split(' ');
  130.  
  131. for (usize i = 0; i < words.len; i ++)
  132. {
  133. # Degu does not support operator overloading. To access items in an array type, one must use 'myarray.items[n]'
  134. println("Word {0} is {1}", i, words.items[i]);
  135. }
  136.  
  137. # 'str' types automatically free their internal memory when they go out of scope
  138. str input = readln("How many times? ");
  139. u32 times = input.u32(); # Parse the input string into an i32
  140.  
  141. i32 a, b = 0, 1; # Shortcut declaration and assignment can be used for multiple variables of the same type
  142. for (u32 i = 0; i < times; i ++)
  143. {
  144. # In Degu, sequence points only occur at the start and end of a statement. Therefore, swapping can occur without the use of a temporary variable.
  145. a, b = b, a;
  146. b += a;
  147.  
  148. println("fibonnaci({0}) = {1}", i, a);
  149. }
  150. }
  151.  
  152. ######################################################
  153.  
  154. # Degu supports generic programming
  155. func print<type T>(T obj)
  156. {
  157. # The compiler intuitively understands types, and so can infer a human-readable name for the type
  158. println("Object of type {0} is {1}", typename(T), obj);
  159. }
  160.  
  161. # Degu's generic programming also supports primitive generics
  162. func factorial<u32 X>() -> u32
  163. {
  164. if (X > 1)
  165. ret factorial<X - 1>();
  166. else
  167. ret 1;
  168. }
  169.  
  170. # Since the compiler has all the information it needs, it will evaluate this expression at compile-time, thereby reducing run-time overhead
  171. # Function calls that are not pre-computable at compile-time are not permitted as global variable initializers (TODO: Define 'pre-computable')
  172. u32 fac_10 = factorial<10>();
  173.  
  174. # Degu's generic programming extends to types
  175. type tuple<type T, type U> { T first, U second };
  176.  
  177. # Degu even supports variadic generic programming
  178. func print_objects<type... A>(A arg...)
  179. {
  180. # Statements tailed with '...' are expanded for each variadic argument
  181. print(arg);...
  182.  
  183. # The same applies for code blocks...
  184. {
  185. if (arg > 0)
  186. print(arg);
  187. }...
  188.  
  189. # ...and also parameters
  190. do_nothing(arg...);
  191. }
  192.  
  193. # Degu's variadic generics can be limited to a specific number of additional arguments
  194. type maximum_of_5<type...5 A>(A arg...)
  195. {
  196. do_nothing(arg...);
  197. }
  198.  
  199. ######################################################
  200.  
  201. type vec3 { f32 x, f32 y };
  202.  
  203. # In Degu, methods are defined outside of types to avoid the complexity problems that usually accompany OOP. They use the 'impl' keyword
  204. impl vec3.add(vec3 v)
  205. {
  206. # The 'this' keyword is used to refer to the current instance
  207. # Shortcut operations on multiple variables can be used
  208. this.x, this.y += v.x, v.y;
  209. }
  210.  
  211. # In Degu, the following is illegal: Methods cannot modify data pointed to by any arguments. However, they can modify global variables (although this is discouraged)
  212. # For an object to be modified by a method, it must be passed by value (although the compiler may implicitly decide to pass by reference if the method does not modify the value of the object)
  213. impl vec3.copy_to(vec3& v)
  214. {
  215. v@.x, v@.y = this.x, this.y;
  216. }
  217.  
  218. ######################################################
  219.  
  220. # Degu namespaces are declared with the 'name' keyword
  221. name my_namespace
  222. {
  223. type my_type { i32 a, i32 b };
  224.  
  225. # Pointers to void may exist. However, pointer arithmetic is illegal
  226. # Pointers may not be initialised with integers directly (since doing so is dangerous). A typecast must be explicitly used first
  227. # To perform a typecast, a value must be surrounded with brackets and the '~>' operator used
  228. void& ptr = (0xC0000000 ~> void&);
  229. }
  230.  
  231. ######################################################
  232.  
  233. import "std/mem" use std:mem
  234.  
  235. # In Degu, certain method names are reserved for special purposes
  236. # These names are 'create', 'destroy', 'move' and 'copy'
  237.  
  238. type IntArray { i32[&] items };
  239.  
  240. # 'create' can be called without an instance of the type being created first, and is usually used as a constructor
  241. impl IntArray.create(usize len)
  242. {
  243. this.items = new_array<i32>(len);
  244. }
  245.  
  246. # 'move' is implicitly called when an instance is initialised by transferring the contents of another instance. It can also be used as a way of enabling typecasting instances of different types
  247. impl IntArray.move(IntArray& other)
  248. {
  249. this.items = other@.items;
  250. }
  251.  
  252. # 'copy' is implicitly called when an instance is initialised by copying the contents of another instance. Like 'move', it can also be used as a way of enabling typecasting between different types
  253. impl IntArray.copy(IntArray& other)
  254. {
  255. this.items = copy_array<i32>(other@.items);
  256. }
  257.  
  258. # 'destroy' is implicitly called whenever the instance goes out of scope without first being implicitly copied and is used as a way of clearing up any memory that may be owned by an object. It cannot be called explicitly
  259. impl IntArray.destroy()
  260. {
  261. delete_array<i32>(this.items);
  262. }
  263.  
  264. func main()
  265. {
  266. IntArray arr = IntArray.create(5);
  267. arr.items[0] = 1;
  268. arr.items[1] = 2;
  269. arr.items[2] = 4;
  270. arr.items[3] = 8;
  271. arr.items[4] = 16;
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement