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>),
}
@ -137,6 +138,10 @@ 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(),
@ -146,23 +151,7 @@ pub fn example(input_tokens: proc_macro::TokenStream) -> proc_macro::TokenStream
..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()),
];
@ -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 , });
}
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();
}