Skip to content
This repository was archived by the owner on Jul 6, 2019. It is now read-only.

Commit 76cfbc6

Browse files
committed
Merge pull request #100 from farcaller/cleanups
Cleanups and documentation fixes Reviewed-by: bharrisau
2 parents a461d72 + d02d7d9 commit 76cfbc6

30 files changed

+288
-281
lines changed

Rakefile

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ load 'support/rake.rb'
33
TOOLCHAIN = 'arm-none-eabi-'
44
RUSTC = 'rustc'
55

6-
features = [:tft_lcd]
7-
8-
Context.create(__FILE__, ENV['PLATFORM'], features)
6+
Context.create(__FILE__, ENV['PLATFORM'])
97

108
provide_stdlibs
119

@@ -34,7 +32,7 @@ compile_rust :zinc_crate, {
3432
deps: :core_crate,
3533
produce: 'main.rs'.in_source.as_rlib.in_build,
3634
out_dir: true,
37-
recompile_on: [:triple, :platform, :features],
35+
recompile_on: [:triple, :platform],
3836
}
3937

4038
# zinc runtime support lib
@@ -51,18 +49,16 @@ compile_rust :zinc_isr, {
5149
source: 'hal/isr.rs'.in_source,
5250
deps: :core_crate,
5351
produce: 'isr.o'.in_intermediate,
54-
recompile_on: [:triple, :features],
52+
recompile_on: [:triple],
5553
}
5654

5755
# zinc scheduler assembly
58-
# TODO(farcaller): make platform-specific
59-
if features.include?(:multitasking)
60-
compile_c :zinc_isr_sched, {
61-
source: 'hal/cortex_m3/sched.S'.in_source,
62-
produce: 'isr_sched.o'.in_intermediate,
63-
recompile_on: [:triple, :features],
64-
}
65-
end
56+
# TODO(farcaller): broken until implemented in PT.
57+
# compile_c :zinc_isr_sched, {
58+
# source: 'hal/cortex_m3/sched.S'.in_source,
59+
# produce: 'isr_sched.o'.in_intermediate,
60+
# recompile_on: [:triple],
61+
# }
6662

6763
# platform tree
6864
compile_rust :platformtree_crate, {
@@ -82,7 +78,7 @@ rust_tests :zinc_test, {
8278
source: 'main.rs'.in_source,
8379
deps: [:core_crate],
8480
produce: 'zinc_test'.in_build,
85-
recompile_on: [:platform, :features],
81+
recompile_on: [:platform],
8682
build_for: :host,
8783
}
8884

@@ -95,6 +91,7 @@ compile_rust :macro_platformtree, {
9591
build_for: :host,
9692
}
9793

94+
desc "Build API documentation"
9895
task build_docs: [:build_docs_html]
9996

10097
task build_docs_html: [] do |t|
@@ -112,13 +109,14 @@ app_tasks = Context.instance.applications.map do |a|
112109
:macro_platformtree,
113110
],
114111
produce: "app_#{a}.o".in_intermediate(a),
115-
recompile_on: [:triple, :platform, :features],
112+
recompile_on: [:triple, :platform],
116113
}
117114

118115
link_binary "app_#{a}_elf".to_sym, {
119116
script: 'layout.ld'.in_platform,
120-
deps: ["app_#{a}".to_sym, :zinc_isr, :zinc_support] +
121-
(features.include?(:multitasking) ? [:zinc_isr_sched] : []),
117+
deps: ["app_#{a}".to_sym, :zinc_isr, :zinc_support],
118+
# TODO(farcaller): broken until implemented in PT.
119+
# (features.include?(:multitasking) ? [:zinc_isr_sched] : []),
122120
produce: "app_#{a}.elf".in_build,
123121
}
124122

File renamed without changes.
File renamed without changes.
File renamed without changes.

platformtree/node.rs

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ pub struct Attribute {
5454
}
5555

5656
impl Attribute {
57+
/// Creates a new attribute with given span.
5758
pub fn new(value: AttributeValue, key_span: Span, value_span: Span)
5859
-> Attribute {
5960
Attribute {
@@ -63,6 +64,7 @@ impl Attribute {
6364
}
6465
}
6566

67+
/// Creates a new attribute with DUMMY_SP for key and value.
6668
pub fn new_nosp(value: AttributeValue) -> Attribute {
6769
Attribute {
6870
value: value,
@@ -72,8 +74,14 @@ impl Attribute {
7274
}
7375
}
7476

77+
/// Node builder is a function that generates code based on the node content or
78+
/// mutates other nodes.
7579
pub type NodeBuilderFn = fn(&mut Builder, &mut ExtCtxt, Rc<Node>);
7680

81+
/// Subnodes is effectively an ordered map.
82+
///
83+
/// We still address nodes by path for most of use cases, but we need to know
84+
/// the original order of appearance (it makes things deterministic).
7785
pub struct Subnodes {
7886
by_index: Vec<Rc<Node>>,
7987
by_path: HashMap<String, Weak<Node>>,
@@ -87,20 +95,28 @@ impl Subnodes {
8795
}
8896
}
8997

98+
/// Adds a node to subnodes.
99+
///
100+
/// The node must not be present in the subnodes.
90101
pub fn push(&mut self, node: Rc<Node>) {
91102
let weak = node.downgrade();
92103
self.by_path.insert(node.path.clone(), weak);
93104
self.by_index.push(node);
94105
}
95106

107+
/// Returns a vector representation of subnodes.
96108
pub fn as_vec<'a>(&'a self) -> &'a Vec<Rc<Node>> {
97109
&self.by_index
98110
}
99111

112+
/// Returns a map representation of subnodes.
100113
pub fn as_map<'a>(&'a self) -> &'a HashMap<String, Weak<Node>> {
101114
&self.by_path
102115
}
103116

117+
/// A helper method to move data from other subnodes into current instance.
118+
///
119+
/// Used as a helper for wrapping in RefCell.
104120
pub fn clone_from(&mut self, other: Subnodes) {
105121
self.by_index = other.by_index;
106122
self.by_path = other.by_path;
@@ -113,31 +129,52 @@ impl Subnodes {
113129
/// path_span. Attributes are stored by name, subnodes are stored by path.
114130
/// Type_name, if present, must specify the type path for the node's
115131
/// materialized object.
132+
///
133+
/// Two nodes are equal if their full paths are equal.
116134
pub struct Node {
135+
/// Node name, might be optional.
117136
pub name: Option<String>,
137+
138+
/// Name span if name is present or path span otherwise.
118139
pub name_span: Span,
119140

141+
/// Node path, which is unique among all sibling nodes.
120142
pub path: String,
143+
144+
/// Path span.
121145
pub path_span: Span,
122146

147+
/// A map of node's attributes.
123148
pub attributes: RefCell<HashMap<String, Rc<Attribute>>>,
124-
subnodes: RefCell<Subnodes>,
125-
pub parent: Option<Weak<Node>>,
126149

127-
type_name: RefCell<Option<String>>,
128-
type_params: RefCell<Vec<String>>,
150+
/// A weak reference to parent node, None for root nodes.
151+
pub parent: Option<Weak<Node>>,
129152

130-
/// A function that materializes this node.
153+
/// A function that materializes this node, i.e. generates some actionable
154+
/// code.
155+
///
156+
/// Materializers are exectuted in order of dependencies resolution, so having
157+
/// a fully built tree of dependencies is a must.
131158
pub materializer: Cell<Option<NodeBuilderFn>>,
132159

133160
/// Present iff this node will modify state of any other nodes.
161+
///
162+
/// Mutators are executed before materializers in no specific order.
134163
pub mutator: Cell<Option<NodeBuilderFn>>,
135164

136165
/// List of nodes that must be materialized before this node.
166+
///
167+
/// Generally, a node must depend on something to be materialized. The root
168+
/// node that all other nodes must depend on implicitly or explicitly is
169+
/// mcu::clock, which must always be present in PT.
137170
pub depends_on: RefCell<Vec<Weak<Node>>>,
138171

139172
/// List of nodes that may be materialized before this node.
140173
pub rev_depends_on: RefCell<Vec<Weak<Node>>>,
174+
175+
subnodes: RefCell<Subnodes>,
176+
type_name: RefCell<Option<String>>,
177+
type_params: RefCell<Vec<String>>,
141178
}
142179

143180
impl Node {
@@ -160,41 +197,60 @@ impl Node {
160197
}
161198
}
162199

200+
/// Set type name for the generated struct.
201+
///
202+
/// If this node generates an object in main(), type_name references the type
203+
/// of that object, e.g. for DHT22 driver that would be
204+
/// `zinc::drivers::dht22::DHT22`.
163205
pub fn set_type_name(&self, tn: String) {
164206
let mut borrow = self.type_name.borrow_mut();
165207
borrow.deref_mut().clone_from(&Some(tn));
166208
}
167209

210+
/// Get type name for the generated struct.
168211
pub fn type_name(&self) -> Option<String> {
169212
self.type_name.borrow().clone()
170213
}
171214

215+
/// Get type parameters for the generated object.
172216
pub fn type_params(&self) -> Vec<String> {
173217
self.type_params.borrow().clone()
174218
}
175219

220+
/// Set type parameters for the generated object, including lifetimes.
221+
///
222+
/// A default lifetime if this is going to end as a task argument is 'a. Other
223+
/// lifetimes or fully-qualified types may be used as well. DHT22 driver uses
224+
/// this to provide `zinc::hal::timer::Timer` and `zinc::hal::pin::GPIO` for
225+
/// its public struct of `pub struct DHT22<'a, T, P>`.
176226
pub fn set_type_params(&self, params: Vec<String>) {
177227
let mut borrow = self.type_params.borrow_mut();
178228
borrow.deref_mut().clone_from(&params);
179229
}
180230

231+
/// Returns a cloned vec of node's subnodes.
181232
pub fn subnodes(&self) -> Vec<Rc<Node>> {
182233
self.subnodes.borrow().as_vec().clone()
183234
}
184235

236+
/// Invokes the closure for each node from node's subnodes passing a path and
237+
/// weak node reference.
185238
pub fn with_subnodes_map(&self, f: |&HashMap<String, Weak<Node>>|) {
186239
let borrow = self.subnodes.borrow();
187240
f(borrow.as_map());
188241
}
189242

243+
/// Sets the node's subnodes from a passed object.
190244
pub fn set_subnodes(&self, new: Subnodes) {
191245
self.subnodes.borrow_mut().clone_from(new);
192246
}
193247

248+
/// Returns a clones string of current node's path.
194249
pub fn path(&self) -> String {
195250
self.path.clone()
196251
}
197252

253+
/// Returns a fully-qualified path of the current node.
198254
pub fn full_path(&self) -> String {
199255
let pp = match self.parent {
200256
Some(ref parent) => parent.clone().upgrade().unwrap().full_path() + "::",
@@ -372,6 +428,9 @@ impl fmt::Show for Node {
372428
///
373429
/// Root nodes are stored by path in `nodes`, All the nmaed nodes are also
374430
/// stored by name in `named`.
431+
///
432+
/// TODO(farcaller): this could be really refactored into transient root node
433+
/// object that can depend on mcu::clock.
375434
pub struct PlatformTree {
376435
nodes: HashMap<String, Rc<Node>>,
377436
named: HashMap<String, Weak<Node>>,

platformtree/platformtree.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
//! Platform tree operations crate
1717
18+
#![experimental]
1819
#![feature(quote)]
1920
#![crate_name="platformtree"]
2021
#![crate_type="rlib"]

platformtree/test_helpers.rs

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ use syntax::ast;
1919
use syntax::codemap::MacroBang;
2020
use syntax::codemap::{CodeMap, Span, mk_sp, BytePos, ExpnInfo, NameAndSpan};
2121
use syntax::codemap;
22-
use syntax::diagnostic::{Emitter, RenderSpan, Level, mk_span_handler, mk_handler};
22+
use syntax::diagnostic::{Emitter, RenderSpan, Level, mk_span_handler};
23+
use syntax::diagnostic::mk_handler;
2324
use syntax::ext::base::ExtCtxt;
2425
use syntax::ext::expand::ExpansionConfig;
2526
use syntax::ext::quote::rt::ExtParseUtils;
@@ -30,55 +31,28 @@ use builder::Builder;
3031
use node;
3132
use parser::Parser;
3233

33-
use hamcrest::{success,Matcher,MatchResult,SelfDescribing};
34-
use std::fmt::Show;
35-
36-
pub struct EqualToString<T> {
37-
expected: T
38-
}
39-
40-
impl<T: Show> SelfDescribing for EqualToString<T> {
41-
fn describe(&self) -> String {
42-
format!("{}", self.expected)
43-
}
44-
}
45-
46-
impl<T : PartialEq+Show> Matcher<T> for EqualToString<T> {
47-
fn matches(&self, actual: T) -> MatchResult {
48-
if self.expected.eq(&actual) {
49-
success()
50-
}
51-
else {
52-
Err(format!("was {}", actual))
53-
}
54-
}
55-
}
56-
57-
pub fn equal_to<T : PartialEq+Show>(expected: T) -> Box<EqualToString<T>> {
58-
box EqualToString { expected: expected }
59-
}
60-
61-
pub fn equal_to_s(expected: &str) -> Box<EqualToString<String>> {
62-
equal_to(expected.to_string())
63-
}
64-
34+
/// Tests if the source fails to be parsed by PT parser.
6535
pub fn fails_to_parse(src: &str) {
6636
with_parsed_tts(src, |_, failed, pt| {
6737
assert!(unsafe{*failed} == true);
6838
assert!(pt.is_none());
6939
});
7040
}
7141

42+
/// Tests if the source fails to be built by PT builder.
7243
pub fn fails_to_build(src: &str) {
7344
with_parsed(src, |cx, failed, pt| {
45+
assert!(unsafe{*failed} == false);
7446
Builder::build(cx, pt);
75-
assert!(unsafe{*failed} == true);
7647
});
7748
}
7849

79-
pub fn with_parsed(src: &str, block: |&mut ExtCtxt, *mut bool, Rc<node::PlatformTree>|) {
50+
/// Yields an ExtCtxt, parser error state and parsed PT.
51+
///
52+
/// TODO(farcaller): get rid of that bool, it's broken.
53+
pub fn with_parsed(src: &str,
54+
block: |&mut ExtCtxt, *mut bool, Rc<node::PlatformTree>|) {
8055
with_parsed_tts(src, |cx, failed, pt| {
81-
assert!(unsafe{*failed} == false);
8256
block(cx, failed, pt.unwrap());
8357
});
8458
}

src/drivers/dht22_pt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ fn build_dht22(builder: &mut Builder, cx: &mut ExtCtxt, node: Rc<node::Node>) {
7171
#[cfg(test)]
7272
mod test {
7373
use builder::Builder;
74-
use test_helpers::{assert_equal_source, with_parsed, equal_to, equal_to_s};
75-
use hamcrest::{assert_that,is};
74+
use test_helpers::{assert_equal_source, with_parsed};
75+
use hamcrest::{assert_that, is, equal_to};
7676

7777
#[test]
7878
fn builds_lpc17xx_pt() {
@@ -96,7 +96,7 @@ mod test {
9696

9797
let pin_node = pt.get_by_name("pin").unwrap();
9898
assert_that(pin_node.get_string_attr("direction").unwrap(),
99-
is(equal_to_s("out")));
99+
is(equal_to("out".to_string())));
100100
});
101101
}
102102
}

0 commit comments

Comments
 (0)