Skip to content
This repository was archived by the owner on Jun 17, 2022. It is now read-only.

Commit e46f3da

Browse files
committed
fix rust emitter
1 parent 014be6f commit e46f3da

File tree

13 files changed

+97
-37
lines changed

13 files changed

+97
-37
lines changed

modules/ast/src/lib.zz

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ fn parse_v(err::Err+et mut *e, json::Parser+pt mut* p, void mut * user, char *k,
2323
switch v.t {
2424
json::ValueType::String => {
2525
static_attest(safe(arg->pool));
26-
static_attest(safe(v.buffer));
27-
static_attest(nullterm(v.buffer));
28-
char mut * dup = arg->pool->malloc(buffer::strlen(v.buffer));
29-
memcpy(dup, v.buffer, buffer::strlen(v.buffer) + 1);
26+
static_attest(safe(v.string));
27+
static_attest(nullterm(v.string));
28+
char mut * dup = arg->pool->malloc(buffer::strlen(v.string));
29+
memcpy(dup, v.string, buffer::strlen(v.string) + 1);
3030
arg->v.string = dup;
3131
}
3232
default => {
@@ -84,10 +84,10 @@ fn parse_t_other_value(err::Err+et mut *e, json::Parser+pt mut* p, void mut * us
8484
switch v.t {
8585
json::ValueType::String => {
8686
static_attest(safe(arg->pool));
87-
static_attest(safe(v.buffer));
88-
static_attest(nullterm(v.buffer));
89-
char mut * dup = arg->pool->malloc(buffer::strlen(v.buffer));
90-
memcpy(dup, v.buffer, buffer::strlen(v.buffer) + 1);
87+
static_attest(safe(v.string));
88+
static_attest(nullterm(v.string));
89+
char mut * dup = arg->pool->malloc(buffer::strlen(v.string));
90+
memcpy(dup, v.string, buffer::strlen(v.string) + 1);
9191
arg->v.string = dup;
9292
}
9393
default => {

modules/err/src/lib.zz

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ export struct Err+ {
2424

2525
/// create a new error
2626
export fn make(Err+tail mut new*self)
27-
where tail > 0
2827
model checked(*self)
2928
{
29+
assert(tail > 0);
3030
memset(self, 0, sizeof(Err));
3131
buffer::clear(&(self->trace));
3232
static_attest(checked(*self));
3333
}
3434

3535
/// ignore any previous errors and reset error state
3636
export fn ignore(Err+tail mut *self)
37-
where tail > 0
3837
model checked(*self)
3938
{
39+
assert(tail > 0);
4040
memset(self, 0, sizeof(Err));
4141
buffer::clear(&self->trace);
4242
static_attest(checked(*self));

modules/hex/src/lib.zz

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ export fn print(u8 *data, usize l)
4848
}
4949

5050

51-
export fn dump_slice(slice::Slice *s)
51+
export fn dump_slice(slice::Slice s)
52+
where slice::slice::integrity(&s)
5253
{
53-
static_attest(s->size <= len(s->mem));
54-
for (usize mut i = 0; i < s->size; i++) {
55-
printf("%02x ", s->mem[i]);
54+
for (usize mut i = 0; i < s.size; i++) {
55+
printf("%02x ", s.mem[i]);
5656
if i % 16 == 15 {
5757
printf("\n");
5858
}

modules/json/src/lib.zz

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub enum ValueType {
1919

2020
export struct Value {
2121
ValueType t;
22-
char* buffer;
22+
char* string;
2323
int integer;
2424
usize index;
2525
}
@@ -332,7 +332,7 @@ fn advance(Parser+tail mut *self, err::Err+et mut *e, char token)
332332
Value val = Value{
333333
index: stack->index,
334334
t: ValueType::String,
335-
buffer: ((self->capture).mem + self->keylen),
335+
string: ((self->capture).mem + self->keylen),
336336
};
337337
de(e, self, stack->user, (self->capture).mem, val);
338338
if err::check(e) {

modules/json/tests/b.zz

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ symbol InvalidValue;
2626
fn deserialize_charge(err::Err+et mut *e, json::Parser+pt mut* p, void mut *user, char *k, json::Value v)
2727
where err::checked(*e)
2828
{
29-
printf("charge.>%s< == >%s< %u, %d [%zu]\n", k, v.buffer, v.t, v.integer, v.index);
29+
printf("charge.>%s< == >%s< %u, %d [%zu]\n", k, v.string, v.t, v.integer, v.index);
3030
}
3131

3232
fn deserialize_engine(err::Err+et mut *e, json::Parser+pt mut* p, void mut *user, char *k, json::Value v)
3333
where err::checked(*e)
3434
where nullterm(k)
35-
where nullterm(v.buffer)
35+
where nullterm(v.string)
3636
{
3737
let into = (Engine mut*)user;
38-
printf("engine.>%s< == >%s< %d\n", k, v.buffer, v.integer);
38+
printf("engine.>%s< == >%s< %d\n", k, v.string, v.integer);
3939
if buffer::cstr_eq("fuel", k) && v.t == json::ValueType::String{
40-
if buffer::cstr_eq("dinosaurs", v.buffer) {
40+
if buffer::cstr_eq("dinosaurs", v.string) {
4141
into->fuel = Fuel::Dinosaurs;
42-
} else if buffer::cstr_eq("electric", v.buffer) {
42+
} else if buffer::cstr_eq("electric", v.string) {
4343
into->fuel = Fuel::Electric;
4444
} else {
4545
err::fail(e, InvalidValue , "invalid fuel value %s", v);
@@ -57,7 +57,7 @@ fn deserialize_vehicle(err::Err+et mut *e, json::Parser+pt mut* p, void mut *use
5757
where nullterm(k)
5858
{
5959
let into = (Vehicle mut*)user;
60-
printf("vehicle.>%s< == >%s< %d\n", k, v.buffer, v.integer);
60+
printf("vehicle.>%s< == >%s< %d\n", k, v.string, v.integer);
6161

6262
if buffer::cstr_eq("height", k) {
6363
into->height = v.integer;

modules/json/tests/print.zz

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn pretty(err::Err+et mut *e, json::Parser+pt mut* p, void mut * user, char *k,
114114
}
115115
}
116116
json::ValueType::String => {
117-
printf("%s : %s\n", k, v.buffer);
117+
printf("%s : %s\n", k, v.string);
118118
}
119119
json::ValueType::Integer => {
120120
printf("%s : %d\n", k, v.integer);

modules/slice/src/mut_slice.zz

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using <string.h> as c_string;
22
using slice::{Slice};
3+
using slice as roslice;
34

45
export struct MutSlice {
56
u8 mut* mem;
@@ -9,6 +10,7 @@ export struct MutSlice {
910

1011
pub theory integrity(MutSlice mut*self) -> bool (
1112
safe(self->at)
13+
&& safe(self->mem)
1214
&& len(self->mem) >= self->size
1315
&& *self->at <= self->size
1416
&& *self->at <= len(self->mem)
@@ -29,6 +31,19 @@ export fn make(MutSlice new mut*self, u8 mut*mem, usize size, usize mut *at)
2931
static_attest(self->size == len(self->mem));
3032
}
3133

34+
export fn as_slice(MutSlice * self) -> Slice
35+
where integrity(self)
36+
model roslice::integrity(&return)
37+
{
38+
let r = Slice {
39+
mem: self->mem,
40+
size: *self->at,
41+
};
42+
static_attest(roslice::integrity(&r));
43+
return r;
44+
}
45+
46+
3247
export fn append_slice(MutSlice mut * self, Slice *other) -> bool
3348
where integrity(self)
3449
model integrity(self)

src/emitter_common.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use std::path::PathBuf;
2+
use std::path::Path;
3+
4+
pub fn path_rel<A: AsRef<Path>, B: AsRef<Path>> (base: A, src: B) -> PathBuf {
5+
let mut src : PathBuf = src.as_ref().into();
6+
if !src.is_absolute() {
7+
src = src.canonicalize().expect(&format!("canonicalize {:?}", src));
8+
}
9+
return pathdiff::diff_paths(&src, base.as_ref()).expect(&format!("pathdiff {:?} ", src));
10+
}

src/emitter_rs.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
use super::ast;
44
use super::flatten;
55
use super::make;
6+
use super::emitter_common;
67
use super::name::Name;
78
use super::parser::{self, emit_error};
89
use super::project::Project;
10+
use super::project;
911
use std::collections::HashSet;
1012
use std::fs;
1113
use std::io::Write;
@@ -21,12 +23,14 @@ pub struct Emitter {
2123
}
2224

2325
pub fn outname(_project: &Project, stage: &make::Stage, module: &flatten::Module) -> String {
24-
format!("target/rust/{}.rs", module.name.0[1..].join("_"))
26+
let td = project::target_dir();
27+
td.join("rust").join(format!("{}.rs", module.name.0[1..].join("_"))).to_string_lossy().to_string()
2528
}
2629

2730
pub fn make_module(make: &super::make::Make) {
28-
let pdir_ = format!("target/rust/{}/", make.artifact.name);
29-
let pdir = std::path::Path::new(&pdir_);
31+
let td = project::target_dir();
32+
let pdir_ = td.join("rust").join(&make.artifact.name);
33+
let pdir = std::path::Path::new(&pdir_);
3034
std::fs::create_dir_all(&pdir).unwrap();
3135

3236

@@ -39,7 +43,14 @@ pub fn make_module(make: &super::make::Make) {
3943
write!(f, " cc::Build::new()\n").unwrap();
4044

4145
for step in &make.steps {
42-
write!(f," .file(\"../../../{}\")\n", step.source.to_string_lossy()).unwrap();
46+
write!(f," .file(\"{}\")\n",
47+
emitter_common::path_rel(&pdir, &step.source).to_string_lossy().to_string()
48+
).unwrap();
49+
}
50+
for flag in &make.cincludes {
51+
write!(f," .include(\"{}\")\n",
52+
emitter_common::path_rel(&pdir, flag).to_string_lossy().to_string()
53+
).unwrap();
4354
}
4455
write!(f, " .compile(\"{}\");\n", make.artifact.name).unwrap();
4556
write!(f, "}}\n").unwrap();
@@ -365,6 +376,7 @@ impl Emitter {
365376
_ => unreachable!(),
366377
};
367378
let shortname = Name::from(&ast.name).0.last().unwrap().clone();
379+
write!(self.f, "#[derive(Clone)]\n").unwrap();
368380
write!(self.f, "#[repr(C)]\n").unwrap();
369381
write!(self.f, "pub enum {} {{\n", shortname).unwrap();
370382
for (name, literal) in names {
@@ -580,7 +592,7 @@ impl {name} {{
580592
}
581593
}
582594

583-
write!(self.f, "\n\n#[repr(C)]\npub struct {} {{\n", shortname).unwrap();
595+
write!(self.f, "\n#[derive(Clone)]\n#[repr(C)]\npub struct {} {{\n", shortname).unwrap();
584596
for i in 0..fields.len() {
585597
let field = &fields[i];
586598

@@ -666,7 +678,7 @@ impl {name} {{
666678

667679
let shortname = Name::from(&ast.name).0.last().unwrap().clone();
668680

669-
write!(self.f, "#[repr(C)]\npub struct {sn} {{\n pub ctx: *mut std::ffi::c_void,\n",
681+
write!(self.f, "#[derive(Clone)]\n#[repr(C)]\npub struct {sn} {{\n pub ctx: *mut std::ffi::c_void,\n",
670682
sn = shortname,
671683
).unwrap();
672684

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod emitter_js;
1515
pub mod emitter_py;
1616
pub mod emitter_rs;
1717
pub mod emitter_go;
18+
pub mod emitter_common;
1819
pub mod expand;
1920
pub mod export_make;
2021
pub mod export_cmake;
@@ -46,11 +47,11 @@ impl Error {
4647
}
4748
}
4849

49-
#[derive(PartialEq)]
50+
#[derive(PartialEq, Debug)]
5051
pub enum BuildSet {
5152
Tests,
5253
Run,
53-
Check,
54+
Check(Option<std::path::PathBuf>),
5455
All,
5556
Export,
5657
Named(String),

0 commit comments

Comments
 (0)