Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions cspell-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ newtype
noexcept
nmake
ntdll
nullopt
nullptr
pmut
primitve
println
Expand Down
2 changes: 1 addition & 1 deletion examples/char/NMakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CXX = cl.exe
# MSVC doesn't support earlier that c++14
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++14

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just add a || !defined(__cplusplus) to our guards, to not requiring a new flag for windows people, and supporting other broken compilers.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it’s defined, it’s just set to 1997. we could check for _MSVC_LANG, which should work regardless of flags, although it’s really annoying that we have to do this.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't make any sense. So windows people deserve this misery I guess and we shouldn't help them then.

CXXFLAGS = /W4 /DEBUG /EHsc /std:c++14 /Zc:__cplusplus
WINLIBS = ntdll.lib

EXAMPLE_NAME = char
Expand Down
2 changes: 1 addition & 1 deletion examples/conditional/NMakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CXX = cl.exe
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++20
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++20 /Zc:__cplusplus
WINLIBS = ntdll.lib Userenv.lib Ws2_32.lib

EXAMPLE_NAME = conditional
Expand Down
4 changes: 2 additions & 2 deletions examples/conditional/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ int main() {
s.push(KVPair{key.to_owned(), static_cast<KVPairValue_T>(value)});
}
auto it = s.iter();
for (auto next = it.next(); next.matches_Some(); next = it.next()) {
auto pair = next.unwrap();
for (auto next = it.next(); auto some = Option<rust::Ref<KVPair>>::Some::match(next); next = it.next()) {
rust::Ref<KVPair> pair = some->f0;
rust::Ref<rust::std::string::String> key = pair.key;
KVPairValue_T value = pair.value;
std::cout << "KVPair(size = " << KVPair::self_size()
Expand Down
24 changes: 12 additions & 12 deletions examples/conditional/main.zng
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,29 @@ mod ::std {
type option::Option<usize> {
#layout(size = 16, align = 8);

constructor None;
constructor Some(usize);

fn unwrap(self) -> usize;
variant None {}
variant Some {
field 0 (offset = auto, type = usize);
}
}

type option::Option<crate::KeyValuePair> {
#layout(size = 32, align = 8);

constructor None;
constructor Some(crate::KeyValuePair);

fn unwrap(self) -> crate::KeyValuePair;
variant None {}
variant Some {
field 0 (offset = auto, type = crate::KeyValuePair);
}
}

type option::Option<&crate::KeyValuePair> {
#layout(size = 8, align = 8);
wellknown_traits(Copy);

constructor None;
constructor Some(&crate::KeyValuePair);

fn unwrap(self) -> &crate::KeyValuePair;
variant None {}
variant Some {
field 0 (offset = auto, type = &crate::KeyValuePair);
}
}


Expand Down
3 changes: 3 additions & 0 deletions examples/enums/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
generated.h
generated.rs
generated.cpp
14 changes: 14 additions & 0 deletions examples/enums/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "example-enums"
version = "0.10.0"
edition.workspace = true
rust-version.workspace = true
license.workspace = true
publish = false

[lib]
crate-type = ["staticlib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
13 changes: 13 additions & 0 deletions examples/enums/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
a.out: main.cpp generated.h src/generated.rs src/lib.rs ../../target/release/libexample_enums.a
${CXX} -std=c++17 -Werror main.cpp -g -L ../../target/release/ -l example_enums

../../target/release/libexample_enums.a:
cargo build --release

generated.h ./src/generated.rs: main.zng
cd ../../zngur-cli && cargo run g -i ../examples/enums/main.zng --crate-name "crate"

.PHONY: ../../target/release/libexample_enums.a generated.h clean

clean:
rm -f generated.h src/generated.rs a.out actual_output.txt
23 changes: 23 additions & 0 deletions examples/enums/NMakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CXX = cl.exe
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++20 /Zc:__cplusplus
WINLIBS = ntdll.lib

EXAMPLE_NAME = enums

GENERATED = generated.h src/generated.rs

RUSTLIB_PATH = ../../target/release/

RUSTLIB = example_$(EXAMPLE_NAME).lib

a.exe : main.cpp src/lib.rs $(GENERATED) $(RUSTLIB_PATH)/$(RUSTLIB)
$(CXX) $(CXXFLAGS) main.cpp /Fe:a.exe /link $(WINLIBS) $(RUSTLIB) /LIBPATH:$(RUSTLIB_PATH)

$(RUSTLIB_PATH)/$(RUSTLIB) :
cargo build --release

$(GENERATED) : main.zng
cd ../../zngur-cli && cargo run g -i ../examples/$(EXAMPLE_NAME)/main.zng --crate-name "crate"

clean :
- del /f /q generated.h src\generated.rs a.exe main.obj generated.obj actual_output.txt 2>nul
10 changes: 10 additions & 0 deletions examples/enums/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Example: Simple

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is copy paste.


A simple example, used as a demo in the main README file.

To run this example:

```
make
./a.out
```
2 changes: 2 additions & 0 deletions examples/enums/expected_output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
some 42
67!!
26 changes: 26 additions & 0 deletions examples/enums/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <cstdio>
#include <iostream>
#include <vector>

#include "./generated.h"

template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

template<class T> using Option = rust::std::option::Option<T>;

int main() {
Option<int> v = Option<int>::Some(42);
std::visit(overloaded{
[](Option<int>::None) { std::cout << "none" << std::endl; },
[](Option<int>::Some s) { std::cout << "some " << s.f0 << std::endl; },
}, v.match());

if (auto r = Option<int>::Some::match_mut(v)) {
*rust::RefMut(r->f0) = 67;
}

if (auto r = Option<int>::Some::match(v)) {
std::cout << r->f0 << "!!" << std::endl;
}
}
13 changes: 13 additions & 0 deletions examples/enums/main.zng
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#convert_panic_to_exception

mod ::std {
type option::Option<i32> {
#layout(size = 8, align = 4);
wellknown_traits(Debug, Copy);

variant None {}
variant Some {
field 0 (offset = auto, type = i32);
}
}
}
2 changes: 2 additions & 0 deletions examples/enums/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[rustfmt::skip]
mod generated;
2 changes: 1 addition & 1 deletion examples/impl_trait/NMakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CXX = cl.exe
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++20
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++20 /Zc:__cplusplus
WINLIBS = ntdll.lib

EXAMPLE_NAME = impl_trait
Expand Down
2 changes: 1 addition & 1 deletion examples/memory_management/NMakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CXX = cl.exe
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++20
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++20 /Zc:__cplusplus
WINLIBS = ntdll.lib

EXAMPLE_NAME = memory_management
Expand Down
6 changes: 3 additions & 3 deletions examples/memory_management/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ int main() {
}
std::cout << "Checkpoint 35" << std::endl;
{
auto p1 = Option<PrintOnDrop>::Some(
Option<PrintOnDrop> p1 = Option<PrintOnDrop>::Some(
PrintOnDrop("option_A"_rs));
std::cout << "Checkpoint 36" << std::endl;
auto p2 = Option<PrintOnDrop>::Some(
Option<PrintOnDrop> p2 = Option<PrintOnDrop>::Some(
PrintOnDrop("option_B"_rs));
std::cout << "Checkpoint 37" << std::endl;
p2.take();
Expand All @@ -135,7 +135,7 @@ int main() {
rust::Ref<rust::Str> elems[3] = {"elem1"_rs, "elem2"_rs, "elem3"_rs};
int i = 0;
auto iter = rust::std::iter::from_fn(
rust::Box<rust::Dyn<rust::Fn<Option<PrintOnDrop>>>>::make_box([&] {
rust::Box<rust::Dyn<rust::Fn<Option<PrintOnDrop>>>>::make_box([&]() -> Option<PrintOnDrop> {
if (i == 3) {
return Option<PrintOnDrop>::None();
}
Expand Down
6 changes: 4 additions & 2 deletions examples/memory_management/main.zng
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ mod ::std {
type Option<crate::PrintOnDrop> {
#layout(size = 16, align = 8);

constructor Some(crate::PrintOnDrop);
constructor None;
variant None {}
variant Some {
field 0 (offset = auto, type = crate::PrintOnDrop);
}

fn take(&mut self) -> Option<crate::PrintOnDrop>;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/multiple_modules/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include <receiver.zng.h>

int main() {
auto processor = rust::std::option::Option<rust::processor::Processor>::Some(
rust::std::option::Option<rust::processor::Processor> processor = rust::std::option::Option<rust::processor::Processor>::Some(
rust::processor::Processor::new_());
auto receiver = rust::std::option::Option<rust::receiver::Receiver>::Some(
rust::std::option::Option<rust::receiver::Receiver> receiver = rust::std::option::Option<rust::receiver::Receiver>::Some(
rust::receiver::Receiver::new_());
auto stats =
rust::Impl<rust::aggregation::StatsAccumulator, rust::Inherent>::create();
Expand Down
8 changes: 5 additions & 3 deletions examples/multiple_modules/processor/processor.zng
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ type crate::Processor {
type ::std::option::Option<crate::Processor> {
#layout(size = 1, align = 1);

constructor None;
constructor Some(crate::Processor);
fn unwrap(self) -> crate::Processor;
variant None {}
variant Some {
field 0 (offset = auto, type = crate::Processor);
}
fn unwrap(self) -> crate::Processor;
}
8 changes: 5 additions & 3 deletions examples/multiple_modules/receiver/receiver.zng
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ type crate::Receiver {
type ::std::option::Option<crate::Receiver> {
#layout(size = 16, align = 8);

constructor None;
constructor Some(crate::Receiver);
fn unwrap(self) -> crate::Receiver;
variant None {}
variant Some {
field 0 (offset = auto, type = crate::Receiver);
}
fn unwrap(self) -> crate::Receiver;
}
2 changes: 1 addition & 1 deletion examples/raw_pointer/NMakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CXX = cl.exe
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++17 # c++17 is needed for panic to exceptions
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++17 /Zc:__cplusplus # c++17 is needed for panic to exceptions
WINLIBS = ntdll.lib

EXAMPLE_NAME = raw_pointer
Expand Down
2 changes: 1 addition & 1 deletion examples/rayon/NMakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CXX = cl.exe
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++14 # c++14 is as low as msvc goes
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++14 /Zc:__cplusplus # c++14 is as low as msvc goes
WINLIBS = ntdll.lib

EXAMPLE_NAME = rayon
Expand Down
2 changes: 1 addition & 1 deletion examples/rayon_split/NMakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CXX = cl.exe
CXXFLAGS = /W4 /DEBUG /EHsc /external:I . /external:W0 /std:c++14 # c++14 is as low as msvc goes
CXXFLAGS = /W4 /DEBUG /EHsc /external:I . /external:W0 /std:c++14 /Zc:__cplusplus # c++14 is as low as msvc goes
WINLIBS = ntdll.lib

EXAMPLE_NAME = rayon_split
Expand Down
2 changes: 1 addition & 1 deletion examples/regression_test1/NMakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CXX = cl.exe
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++20
CXXFLAGS = /W4 /DEBUG /EHsc /std:c++20 /Zc:__cplusplus
WINLIBS = ntdll.lib

EXAMPLE_NAME = regression_test1
Expand Down
Loading
Loading