summaryrefslogtreecommitdiffstats
path: root/source/code/preproc.cpp
blob: 05624f34570cc43f43477905f504df6566187c18 (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
internal String8 handle_all_comments(String8 buffer) {
		b32 found = false;
		while(!found) {
			u64 comment_position = 0;
			found = str8_find(&buffer, 0, str8("//"), &comment_position);
			{
				// TODO: I dont know if this check works?
				// TODO: What if we are at the beginning of the file.
				// if(start_pos != 0) { // TODO: this logic is inverted i think
				// if(buffer->data[start_pos - 1] == '\n') { // is the current line the start of the line
				
				u64 start_pos = 0;
				
				if(false) {
					Log("not the beginning of the line");
				} else {
					u64 new_line = 0;
					str8_find(&buffer, start_pos, str8("\n"), &new_line);
					str8_chop_left_ptr(&buffer,  new_line);
				}
			}
		}

return buffer;
	}
	

// NOTE: we return a list a of  all pound signs that we found  within the current bufferr
// when find any 1..n we return the next tokens in a String8List.
internal String8List *handle_directives(MemArena *arena, String8 buffer, String8 seperator)
{
 Assert(buffer.size);
String8List *list = PushStruct(arena, String8List);
 TokenNodeList *list_pound_token_values = PushStruct(arena, TokenNodeList);
 u64 offset_to_first_pound              = 0;
 b32 found                              = str8_find(&buffer, 0, seperator, &offset_to_first_pound);
 if(!found){ return buffer; }
 // NOTE: do +1 to remove the pound sign and enable us to start parsing the directive
 buffewr = str8_chop_left(buffer, offset_to_first_pound + 1);

 //-
 u64 idx           = 0;
 b32 is_whitespace = 0;
 u64 end_offset    = 0;
 String8 substr    = {};

 while (!is_whitespace && idx < buffer.size) {
  is_whitespace = cstring_is_whitespace(buffer.data[idx]);
  end_offset    = idx;
  ++idx;
 }

 if (end_offset != 0) {
  substr = str8_create_substr(&buffer, 0, end_offset);
  buffer = str8_chop_left(buffer, end_offset);
 }

 //- handle directives

 if (str8_is_match(substr, str8("import"))) {

  substr = str8_chop_left(buffer, substr.size);
  while (cstring_is_whitespace(*buffer.data)) { buffer = str8_chop_left(buffer, 1); }

  u64 end_pos = 0;
  b32 found_first_newline_or_whitespace = false;
  for(u64 substr_index_to_find_newline_or_whitespace = 0; substr_index_to_find_newline_or_whitespace < substr.size; ++substr_index_to_find_newline_or_whitespace) {

   if(cstring_is_whitespace(buffer.data[substr_index_to_find_newline_or_whitespace]) ||
      cstring_is_newline(buffer.data[substr_index_to_find_newline_or_whitespace])) {
    end_pos = substr_index_to_find_newline_or_whitespace;
    found_first_newline_or_whitespace = true;
    continue;
   }

   if(found_first_newline_or_whitespace) {
    break;
   }

  }

  // TODO: we count one to many for some reason
  end_pos-= 1;

  substr = str8_create_substr(&buffer, 0, end_pos);

 } else {
  if (str8_is_empty(&substr)) {

   Log("failed substring: " Str8Fmt, Str8Arg(substr));
   PANIC("directive not defined yet");

// TODO: exit

  }

  PANIC("Invalid import");
 }

 // NOTE: before there was a pice of code here that said that we should handle module code the
 // code here but this should stay a generic function. do the matrching on the returned struct
 // instead

 Token *new_token = PushStruct(arena, Token);
 new_token->type         = IdentToken;
 new_token->line         = 0;
 new_token->value.as_str = substr;

return {};
}

internal String8 handle_all_modules_into_buffer(MemArena *arena, ModuleList *modules)
{
	String8 buffer = PushString8(arena, modules.count * 100);
ScratchArena scratch  = scratch_start(arena);
	FOR_STR8_NODE(files, current)
	{
		if(str8_is_match(current->value, token->value.as_str))
{
			str8_append(&temp, FileLoad(arena, current->value));
		}
	}
scratch_end(&scratch);
return buffer;
}