This commit is contained in:
Michael Zhang 2020-02-07 01:43:24 -06:00
parent 54a61d9865
commit b316508b7f
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
2 changed files with 63 additions and 35 deletions

View file

@ -1,5 +1,7 @@
#[macro_use] extern crate quote;
#[macro_use] extern crate maplit;
#[macro_use]
extern crate quote;
#[macro_use]
extern crate maplit;
extern crate proc_macro;
use std::collections::{HashMap, HashSet};
@ -7,7 +9,7 @@ use std::collections::{HashMap, HashSet};
use petgraph::{dot::Dot, graph::Graph, graphmap::DiGraphMap};
use proc_macro2::{Span, TokenStream, TokenTree};
use quote::ToTokens;
use syn::{Expr, ExprPath, Ident, Path, PathSegment, PathArguments, punctuated::Punctuated};
use syn::{punctuated::Punctuated, Expr, ExprPath, Ident, Path, PathArguments, PathSegment};
#[derive(Debug, PartialEq, Eq, Hash)]
enum TagLhs {
@ -28,7 +30,6 @@ enum Rsx {
Tag(Tag),
Code(Expr),
Text(String),
// For(String, String, Vec<Rsx>),
}
@ -71,11 +72,11 @@ impl Visitor {
model: HashMap::new(),
}
}
fn load_model(&mut self, model: &HashMap<String, String>) {
self.model.extend(model.clone());
}
fn unique_name(&mut self, base: impl AsRef<str>) -> String {
// TODO: normalize the name somehow so it fits in an ident (ex. strip punct)
let base = base.as_ref();
@ -83,7 +84,7 @@ impl Visitor {
self.idx += 1;
format!("{}_{}", base, next)
}
fn unique_idx(&mut self, node: DepNode) -> u32 {
let next = self.idx;
self.idx += 1;
@ -119,13 +120,13 @@ impl Visitor {
fn extract_model_dependencies(expr: &Expr) -> HashSet<String> {
let tokens = expr.to_token_stream();
let mut result = HashSet::new();
for token in tokens.into_iter() {
if let TokenTree::Ident(ident) = token {
result.insert(format!("{}", ident));
}
}
result
}
@ -137,39 +138,27 @@ pub fn example(input_tokens: proc_macro::TokenStream) -> proc_macro::TokenStream
"name".into() => "String".into(),
};
let todomvc_datainit: HashMap<String, String> = hashmap! {
"name".into() => "\"world\".into()".into(),
};
let todomvc_dom = vec![
Rsx::Tag(Tag {
tag: "input".into(),
attrs: hashmap! {
TagLhs::Bind("value".into()) => "name".into(),
TagLhs::Bind("value".into()) => "name".into(),
},
..Tag::default()
}),
Rsx::Text("Hello, ".into()),
Rsx::Code(Expr::Path(ExprPath {
attrs: vec![],
qself: None,
path: Path {
leading_colon: None,
segments: {
let mut segments = Punctuated::new();
let ident = Ident::new("name", Span::call_site());
let arguments = PathArguments::None;
segments.push(PathSegment {
ident,
arguments,
});
segments
},
},
})),
Rsx::Code(syn::parse_str::<Expr>("name").unwrap()),
Rsx::Text("!".into()),
];
let mut visitor = Visitor::new();
visitor.load_model(&todomvc_datamodel);
visitor.visit(&todomvc_dom);
println!("{:?}", visitor);
println!("DOT:");
let graph: Graph<_, _, _> = visitor.deps.0.clone().into_graph();
@ -177,19 +166,34 @@ pub fn example(input_tokens: proc_macro::TokenStream) -> proc_macro::TokenStream
let name = format_ident!("{}", todomvc_name);
let mut model = TokenStream::new();
let mut init = TokenStream::new();
for (name, ty) in visitor.model {
let name = format_ident!("{}", name);
// TODO: parse this into an actual expression tree for Vec<T>
let ty = format_ident!("{}", ty);
model.extend(quote!{ #name : #ty , });
model.extend(quote! { #name : #ty , });
}
for (name, value) in todomvc_datainit {
let name = format_ident!("{}", name);
let value = syn::parse_str::<Expr>(&value).unwrap();
init.extend(quote! { #name : #value , });
}
let result = quote! {
struct #name {
#model
}
impl #name {
fn new() -> Self {
#name {
#init
}
}
}
impl crate::Component for #name {
fn initialize(&self, el: &crate::Element) {
}
}
};

View file

@ -1,7 +1,31 @@
#[macro_use] extern crate enterprise_compiler;
#[macro_use]
extern crate enterprise_compiler;
#[macro_use]
extern crate stdweb;
use stdweb::web::{document, Element, INonElementParentNode};
trait Component {
fn initialize(&self, element: &Element);
}
example!();
fn main() {
fn render<C: Component>(component: &C, id: impl AsRef<str>) {
let id = id.as_ref();
if let Some(el) = document().get_element_by_id(id) {
component.initialize(&el);
}
}
fn main() {
stdweb::initialize();
let todomvc = TodoMVC::new();
render(&todomvc, "");
let message = "Hello world!";
js! { console.log(@{message}); }
stdweb::event_loop();
}