aboutsummaryrefslogtreecommitdiff
path: root/code/lib/io.felan
blob: c4869025b6fb70b945aeb8a1179b0e22f62d6b5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
@import("types.felan");
@import("operator.felan");

libc :: @c_library("/usr/lib/libc.so.6");
puts :: @c_function(libc,"puts",(*u8)->i32);
putchar :: @c_function(libc,"putchar",(i32)->void);

println :: () -> void {
  print_char('\n');
};

println :: (value:anytype) -> void {
  print(value);
  print_char('\n');
};

print :: (value:string) -> void {
  i := 0;
  while i < @cast(value.length,i64) {
    print_char(value[i]);
    i += 1;
  }
};

print_char :: (value:u8) -> void {
  putchar(@cast(value,i32));
};

print :: (value:i8) -> void {
  _print_signed(@type_of(value),value);
};

print :: (value:u8) -> void {
  _print_unsigned(@type_of(value),value);
};

print :: (value:i16) -> void {
  _print_signed(@type_of(value),value);
};

print :: (value:u16) -> void {
  _print_unsigned(@type_of(value),value);
};

print :: (value:i32) -> void {
  _print_signed(@type_of(value),value);
};

print :: (value:u32) -> void {
  _print_unsigned(@type_of(value),value);
};

print :: (value:i64) -> void {
  _print_signed(@type_of(value),value);
};

print :: (value:u64) -> void {
  _print_unsigned(@type_of(value),value);
};

_print_signed :: (comptime t:type, value:t) -> i32 {
  if value < @cast(0,t) {
    putchar(@cast('-',i32));
    value = -value;
  }
  _print_unsigned(t,value);
};

_print_unsigned :: (comptime t:type, value:t) -> void {
  NUMBERS_SIZE :: 21;
  numbers := @stack_alloc(NUMBERS_SIZE,u8);
  i := NUMBERS_SIZE - 1;
  numbers[i] = '\0';

  while {
    i -= 1;
    numbers[i] = @cast(value%@cast(10,t),u8)+'0';
    value /= @cast(10,t);
    value != @cast(0,t);
  } {}

  while i < NUMBERS_SIZE - 1 {
    @putc(numbers[i]);
    i += 1;
  }
  return;

  puts(&numbers[i]);
};