macro (a: Allocator) => a
}
-// This is similar to alloca in C.
+#doc """
+ Allocates memory from the stack. This is similar to `alloca` in C.
+
+ **DO NOT USE THIS IN A LOOP! You cannot free memory allocated off the stack.**
+"""
from_stack :: macro (size: u32) -> rawptr {
// This should do something about the alignment...
// Everything so far has assume that the stack is aligned to 16 bytes.
return __stack_top;
}
+#doc """
+ Allocates memory from the stack to form a slice of `T` with length `size`.
+
+ **DO NOT USE THIS IN A LOOP! You cannot free memory allocated off the stack.**
+"""
array_from_stack :: macro ($T: type_expr, size: u32) -> [] T {
defer __stack_top = cast([&]u8, __stack_top) + size * sizeof T;
return (cast([&]T) __stack_top)[0 .. size];
}
+#doc """
+ Moves a value on to the heap. Useful for cases like this:
+
+ f :: () -> &Foo {
+ return alloc.on_heap(Foo.{
+ name = "...",
+ age = 42
+ });
+ }
+"""
on_heap :: macro (v: $V) -> &V {
out := cast(&V) raw_alloc(context.allocator, sizeof V);
core.memory.set(out, 0, sizeof V);
return out;
}
+#doc """
+ Like `alloc.on_heap`, but allocates on the temporary allocator.
+"""
on_temp :: macro (v: $V) -> &V {
out := cast(&V) raw_alloc(context.temp_allocator, sizeof V);
core.memory.set(out, 0, sizeof V);
Type_Info_Struct
}
-//
-// Represents data from a CSV file of a particular type.
+#doc "Represents data from a CSV file of a particular type."
CSV :: struct (Output_Type: type_expr) {
entries: [..] Output_Type;
}
-//
-// Tag-type used to tell the ingress and egress methods what
-// the column name of a particular data element should be.
-//
-// Data :: struct {
-// @CSV_Column.{"Actual Column Name"}
-// variable_name: str;
-// }
+#doc """
+ Tag-type used to tell the ingress and egress methods what
+ the column name of a particular data element should be.
+
+ Data :: struct {
+ @CSV_Column.{"Actual Column Name"}
+ variable_name: str;
+ }
+"""
CSV_Column :: struct {
name: str;
}
-//
-// Define methods used with the CSV structure.
+#doc "Define methods used with the CSV structure."
#inject CSV {
- //
- // Create and initialize a CSV with no elements.
+ #doc "Create and initialize a CSV with no elements."
make :: ($T: type_expr) => {
r := CSV(T).{};
r.entries = make(typeof r.entries);
return r;
}
- //
- // Frees all data in a CSV.
+ #doc "Frees all data in a CSV."
delete :: (csv: &CSV) {
delete(&csv.entries);
}
- //
- // Ingests data from a string representing CSV data.
- // Uses the type of the CSV to know what columns should be expected.
- // If `headers_presents` is true, the first line will be treated as
- // headers, and cross checked with the CSV_Column tag information.
- // Use this when the columns from your CSV have a different order
- // from the order of fields in the structure.
+ #doc """
+ Ingests data from a string representing CSV data.
+ Uses the type of the CSV to know what columns should be expected.
+ If `headers_presents` is true, the first line will be treated as
+ headers, and cross checked with the CSV_Column tag information.
+ Use this when the columns from your CSV have a different order
+ from the order of fields in the structure.
+ """
ingress_string :: (csv: &CSV, contents: str, headers_present := true) -> bool {
reader, stream := io.reader_from_string(contents);
defer cfree(stream);
return csv->ingress(&reader, headers_present);
}
- //
- // Ingests data from an Reader containing CSV data.
- // Uses the type of the CSV to know what columns should be expectd.
+ #doc """
+ Ingests data from a Reader containing CSV data.
+ Uses the type of the CSV to know what columns should be expectd.
+ """
ingress :: (csv: &CSV, reader: &io.Reader, headers_present := true) -> bool {
Header :: struct {
type: type_expr;
}
}
- //
- // Outputs data from a CSV into a Writer.
- // When `include_headers` is true, the first line outputted will be
- // the headers of the CSV, according to the CSV_Column tag information.
+ #doc """
+ Outputs data from a CSV into a Writer.
+ When `include_headers` is true, the first line outputted will be
+ the headers of the CSV, according to the CSV_Column tag information.
+ """
egress :: (csv: &CSV, writer: &io.Writer, include_headers := true) {
output_type_info: &Type_Info_Struct = ~~ get_type_info(csv.Output_Type);