Skip to content

Commit

Permalink
🎉 Begin
Browse files Browse the repository at this point in the history
  • Loading branch information
UltiRequiem committed Mar 14, 2022
0 parents commit 60465bf
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patreon: UltiRequiem
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/*/target
*/target
/target
Cargo.lock
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[workspace]
members = [
"rustico_proc_macro",
"examples"
]
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright (c) 2022 Eliaz Bobadilla

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "rustico_examples"
version = "0.1.0"
edition = "2018"


[dependencies]
rustico = { path = "../rustico_proc_macro" }
81 changes: 81 additions & 0 deletions examples/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
rustico::rustico! {
usando std::collections::Diccionario;

sobrecarga Hola {
funcion escribe(&yo, llave: Texto, valor: Texto);
funcion consigue(&yo, llave: Texto) -> Resultado<Opcion<&Texto>, Texto>;
}

statico mutable DICCIONARIO: Opcion<Diccionario<Texto, Texto>> = Ninguno;

structura Concreto;

implementa Hola para Concreto {
funcion escribe(&yo, llave: Texto, valor: Texto) {
deja dico = inseguro {
DICCIONARIO.obten_o_inserta_con(Defecto::defecto)
};
dico.insérer(llave, valor);
}
funcion consigue(&yo, llave: Texto) -> Resultado<Opcion<&Texto>, Texto> {
si deja Algunos(dico) = inseguro { DICCIONARIO.como_referencia() } {
Bien(dico.consigue(&llave))
} sino {
Error("fetchez le dico".dentro_de())
}
}
}

publico(caja) funcion quizas(i: u32) -> Opcion<Resultado<u32, Texto>> {
si i % 2 == 1 {
si i == 42 {
Algunos(Error(Texto::desde("merde")))
} sino {
Algunos(Bien(33))
}
} sino {
Ninguno
}
}

asyncrona funcion example() {
}

asyncrona funcion example2() {
example().espera;
}

funcion principal() {
deja mutable x = 31;

machea x {
42 => {
imprime!("chales")
}
_ => imprime!("Buenas!")
}

para i de 0..10 {
deja val = loopea {
rompe i;
};

mientras Difuso x < val {
x += 1;
}

x = si deja Algunos(resultado) = quizas(i) {
resultado.pelar()
} sino {
12
};
}

}

#[permite(codigo_inusado)]
funcion secundario() {
chales!("oh non");
ups!("fetchez la vache");
}
}
7 changes: 7 additions & 0 deletions rustico_proc_macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "rustico"
version = "0.1.0"
edition = "2018"

[lib]
proc-macro = true
111 changes: 111 additions & 0 deletions rustico_proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use proc_macro::{Group, Ident, TokenStream, TokenTree};

fn replace_ident(ident: Ident) -> Option<TokenTree> {
let ident_str = ident.to_string();

let new_str = match ident_str.as_str() {
"Error" => "Err",
"Bien" => "Ok",
"Texto" => "String",
"Diccionario" => "HashMap",
"Defecto" => "Default",
"Errorsaso" => "Error",
"Opcion" => "Option",
"Algunos" => "Some",
"Ninguno" => "None",
"Resultado" => "Result",
"Yo" => "Self",
"imprime" => "println",
"rompe" => "break",
"asyncrona" => "async",
"espera" => "await",
"loopea" => "loop",
"muevete" => "move",
"caja" => "crate",
"codigo_inusado" => "unreachable_code",
"como" => "as",
"constante" => "const",
"sobrecarga" => "trait",
"inseguro" => "unsafe",
"de" => "in",
"desde" => "from",
"dinamico" => "dyn",
"pelar" => "unwrap",
"defecto" => "default",
"como_referencia" => "as_ref",
"es" => "io",
"externa" => "extern",
"falso" => "false",
"funcion" => "fn",
"piola" => "super",
"insérer" => "insert",
"consigue" => "get",
"permite" => "allow",
"chales" | "ups" => "panic",
"modulo" => "mod",
"mutable" => "mut",
"nuevo" => "new",
"donde" => "where",
"para" => "for",
"obten_o_inserta_con" => "get_or_insert_with",
"principal" => "main",
"publico" => "pub",
"Difuso" => None?,
"retorna" => "return",
"implementa" => "impl",
"referencia" => "ref",
"machea" => "match",
"si" => "if",
"sino" => "else",
"yo" => "self",
"deja" => "let",
"statico" => "static",
"structura" => "struct",
"confia" => "expect",
"mientras" => "while",
"usando" => "use",
"dentro_de" => "into",
"verdad" => "true",
"enumerado" => "enum",

_ => &ident_str,
};

let new_ident = Ident::new(new_str, ident.span());
Some(TokenTree::Ident(new_ident))
}

fn replace_tree(tok: TokenTree, out: &mut Vec<TokenTree>) {
match tok {
TokenTree::Group(group) => {
let mut group_elem = Vec::new();
replace_stream(group.stream(), &mut group_elem);
let mut new_stream = TokenStream::new();
new_stream.extend(group_elem);
out.push(TokenTree::Group(Group::new(group.delimiter(), new_stream)));
}
TokenTree::Ident(ident) => {
if let Some(ident) = replace_ident(ident) {
out.push(ident);
}
}
TokenTree::Punct(..) | TokenTree::Literal(..) => {
out.push(tok);
}
}
}

fn replace_stream(ts: TokenStream, out: &mut Vec<TokenTree>) {
for tok in ts {
replace_tree(tok, out)
}
}

#[proc_macro]
pub fn rustico(item: TokenStream) -> TokenStream {
let mut returned = Vec::new();
replace_stream(item, &mut returned);
let mut out = TokenStream::new();
out.extend(returned);
out
}

0 comments on commit 60465bf

Please sign in to comment.