From: Brendan Hansen Date: Thu, 28 Mar 2024 01:49:41 +0000 (-0500) Subject: updated: examples X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=e2a90f02e0d3de89dabfb54b490d80addbf5cc91;p=onyxlang.io.git updated: examples --- diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..03a1d40 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "examples"] + path = examples + url = https://github.com/onyx-lang/onyx-examples diff --git a/examples b/examples index 1df16a3..45ff12e 160000 --- a/examples +++ b/examples @@ -1 +1 @@ -Subproject commit 1df16a331e64f15a5095d2e79dbc2dcece461382 +Subproject commit 45ff12e45a39d7b0f6afff99cf8101e1a35d6c73 diff --git a/src/app.onyx b/src/app.onyx index 389ff7a..f82e43b 100644 --- a/src/app.onyx +++ b/src/app.onyx @@ -12,21 +12,18 @@ use core.encoding.json } reg: otmp.TemplateRegistry; -#inject Res { - render :: (r: &Res, template: str, vars: any) { - m := misc.any_to_map(vars) ?? make(Map(str, any)); - defer delete(&m); +Res.render :: (r: &Res, template: str, vars: any) { + use m := misc.any_to_map(vars) ?? make(Map(str, any)); - s := reg->render_template(template, &r.writer, &m); + s := reg->render_template(template, &r.writer, &m); - if s != .None { - log(.Warning, "Template Renderer", tprintf("{}", s)); - } - - r.headers["Content-Type"] = "text/html"; - r->status(200 if s == .None else 400); - r->end(); + if s != .None { + log(.Warning, "Template Renderer", tprintf("{}", s)); } + + r.headers->put("Content-Type", "text/html"); + r->status(200 if s == .None else 400); + r->end(); } main :: () { diff --git a/src/cached_resource.onyx b/src/cached_resource.onyx index c364989..d9d98e5 100644 --- a/src/cached_resource.onyx +++ b/src/cached_resource.onyx @@ -12,18 +12,16 @@ Cached_Resource :: struct (Res: type_expr) { release_resource: (res: &Res) -> void; } -#inject Cached_Resource { - get :: (use self: &#Self) -> ? self.Res { - if !resource { - update_resource(self); - } - - if time.duration(time.now(), last_retrieved) > max_age { - update_resource(self); - } +Cached_Resource.get :: (use self: &#Self) -> ? self.Res { + if !resource { + update_resource(self); + } - return self.resource; + if time.duration(time.now(), last_retrieved) > max_age { + update_resource(self); } + + return self.resource; } #local @@ -39,11 +37,9 @@ update_resource :: (self: &Cached_Resource($T)) { -#inject time { - duration :: (t2, t1: time.Timestamp) -> i32 { - t1_ := t1; - t2_ := t2; - return ~~(t2_->to_epoch() - t1_->to_epoch()); - } +time.duration :: (t2, t1: time.Timestamp) -> i32 { + t1_ := t1; + t2_ := t2; + return ~~(t2_->to_epoch() - t1_->to_epoch()); } diff --git a/www/examples/files.html b/www/examples/files.html index 1f54a50..bf2895a 100644 --- a/www/examples/files.html +++ b/www/examples/files.html @@ -20,41 +20,18 @@ write_data_into_file :: (filename: str) { // Also, free it later by defering io.writer_free. This also flushes // the internal buffer of io.Writer to make sure everything is written // to the file. - file_writer := io.writer_make(&file); - defer io.writer_free(&file_writer); + file_writer := io.Writer.make(&file); + defer io.Writer.free(&file_writer); // The simplest way of writing a string to the file. - io.write(&file_writer, "This is the first line of text.\n"); + file_writer->write("This is the first line of text.\n"); // io.write_format can be used to "printf" into a file. // printf can be thought of as io.write_format(&stdio.stream, ...). - io.write_format(&file_writer, "This is a {} line of text.\n", "formatted"); + file_writer->write_format("This is a {} line of text.\n", "formatted"); for i in 0 .. 5 { - io.write_format(&file_writer, "Another line numbered {}.\n", i); - } -} - -#doc "Reads example text from a file." -read_data_from_file :: (filename: str) { - // This is another way of opening a file. Because of the semantics - // of `for` loops over Iterators, they can behave like `with` - // statements in Python or `using` statements in C#. - for file in os.with_file(filename, .Read) { - // Create a io.Reader over the file stream. - file_reader := io.reader_make(file); - defer io.reader_free(&file_reader); - - // Read a single line. - first_line := io.read_line(&file_reader); - printf("First line: {}\n", first_line); - - // Use io.lines to create an iterator over the remaining - // lines in the file. - printf("Remaining lines:\n"); - for line, index in io.lines(&file_reader) { - printf("{}: {}\n", index, line); - } + file_writer->write_format("Another line numbered {}.\n", i); } } @@ -73,6 +50,5 @@ main :: () { filename := "test.txt"; write_data_into_file(filename); - read_data_from_file(filename); read_whole_file(filename); } diff --git a/www/examples/stdin.html b/www/examples/stdin.html index e9ad9dc..f90b53f 100644 --- a/www/examples/stdin.html +++ b/www/examples/stdin.html @@ -2,29 +2,28 @@

Brendan Hansen

This example reads a single line of input from standard input, splits it in half on the first space using string.bisect, converts both parts to integers using conv.parse, then prints the results using printf for formatted printing.
// Use the necessary core libraries
-use core.io
-use core.string
+use core.io { Reader }
 use core.conv
 
 // Use the printf function that lives in the core package.
 // This cannot be `use core.printf`, because that will look
 // for a package called `printf` in `core`.
 use core {
-    printf
+    printf, stdio
 }
 
 main :: () {
     // Create a io.Reader over the stdio.stream to be able scan
     // the input in parts. Also, defer freeing the reader until
     // the end of `main`.
-    stdin_reader := io.reader_make(&stdio.stream);
-    defer io.reader_free(&stdin_reader);
+    stdin_reader := Reader.make(&stdio.stream);
+    defer Reader.free(&stdin_reader);
 
     // Read a single line of input.
-    line := io.read_line(&stdin_reader);
+    line := stdin_reader->read_line();
 
     // Split the line on the first space.
-    a_str, b_str := string.bisect(line, " ");
+    a_str, b_str := line->bisect(" ");
 
     // Parse and convert both parts to i32s, with a default value
     // of 0 if it fails to parse as an i32.