Skip to content

Commit

Permalink
fix grammar lex
Browse files Browse the repository at this point in the history
添加行号表设置

add ds module and fix clippy warnings
  • Loading branch information
limuy2022 committed Feb 5, 2024
1 parent d0e64d2 commit 3a6ece3
Show file tree
Hide file tree
Showing 28 changed files with 432 additions and 293 deletions.
42 changes: 22 additions & 20 deletions rust/derive/src/def_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ pub fn lex_arrow(
errormsg: &str,
) {
match iter.next().unwrap() {
proc_macro::TokenTree::Group(x, ..) => {
TokenTree::Group(x, ..) => {
let mut left_name = TokenStream::new();
let mut right_name = TokenStream::new();
let mut control_which_put = false;
let mut iter = x.stream().into_iter();
while let Some(i) = iter.next() {
// println!("{}", i);
if let proc_macro::TokenTree::Punct(x) = i {
if let TokenTree::Punct(x) = i {
let x = x.to_string();
if x == "=" {
check_next_iter(&mut iter, ">");
Expand All @@ -31,19 +31,17 @@ pub fn lex_arrow(
left_name = TokenStream::new();
right_name = TokenStream::new();
control_which_put = false;
} else if x == "]" {
left_push.push(syn::parse(left_name).expect("left push break"));
right_push.push(syn::parse(right_name).expect("right push break"));
break;
}
} else {
if !control_which_put {
left_name.extend(std::iter::once(i));
} else {
right_name.extend(std::iter::once(i));
panic!("{}", errormsg);
}
} else if !control_which_put {
left_name.extend(std::iter::once(i));
} else {
right_name.extend(std::iter::once(i));
}
}
left_push.push(syn::parse(left_name).expect("left push break"));
right_push.push(syn::parse(right_name).expect("right push break"));
}
_ => {
panic!("{}", errormsg);
Expand All @@ -53,7 +51,7 @@ pub fn lex_arrow(

fn check_next_iter(iter: &mut IntoIter, check_str: &str) {
if let Some(i) = iter.next() {
if let proc_macro::TokenTree::Punct(x) = i {
if let TokenTree::Punct(x) = i {
if x.to_string() != check_str {
panic!("expected {}", check_str);
}
Expand All @@ -75,12 +73,12 @@ pub fn def_impl(content: TokenStream) -> TokenStream {
let mut submodules = vec![];
while let Some(i) = iter.next() {
match i {
proc_macro::TokenTree::Ident(x) => {
TokenTree::Ident(x) => {
let x = x.to_string();
if x == "module_name" {
check_next_iter(&mut iter, "=");
if let TokenTree::Ident(tmp) = iter.next().expect("name is expected") {
if let Some(_) = module_ident {
if module_ident.is_some() {
panic!("double defined");
}
module_ident =
Expand All @@ -106,14 +104,14 @@ pub fn def_impl(content: TokenStream) -> TokenStream {
);
} else if x == "submodules" {
check_next_iter(&mut iter, "=");
if let proc_macro::TokenTree::Group(x, ..) = iter.next().unwrap() {
if let TokenTree::Group(x, ..) = iter.next().unwrap() {
// println!("{}", x);
let mut iter = x.stream().into_iter();
while let Some(i) = iter.next() {
if let proc_macro::TokenTree::Ident(x) = i {
for i in iter {
if let TokenTree::Ident(x) = i {
submodules
.push(syn::parse_str::<syn::Ident>(&(x.to_string())).unwrap());
} else if let proc_macro::TokenTree::Ident(x) = i {
} else if let TokenTree::Ident(x) = i {
let x = x.to_string();
if x != "," {
panic!("expected ,.get {}", x);
Expand All @@ -127,7 +125,7 @@ pub fn def_impl(content: TokenStream) -> TokenStream {
}
}
}
proc_macro::TokenTree::Punct(x) => {
TokenTree::Punct(x) => {
if x.to_string() != "," {
panic!("expected ,");
}
Expand All @@ -147,15 +145,19 @@ pub fn def_impl(content: TokenStream) -> TokenStream {
use std::collections::hash_map::HashMap;
let mut functions = HashMap::new();
let mut classes = HashMap::new();
let mut submodules = HashMap::new();
#(
functions.insert(stringify!(#right_func).to_string(), #left_func());
)*
#(
classes.insert(stringify!(#right_class).to_string(), #left_class::export_info());
)*
#(
submodules.insert(stringify!(#submodules).to_string(), #submodules::init());
)*
Stdlib::new(
stringify!(#module_ident),
HashMap::new(),
submodules,
functions,
classes
)
Expand Down
34 changes: 22 additions & 12 deletions rust/derive/src/function.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use core::panic;

use quote::ToTokens;
use syn::{
parse_str, punctuated, token::Comma, FnArg, PatType, ReturnType, Signature, Stmt, Type,
TypePath,
Expand All @@ -24,11 +27,11 @@ pub fn process_function_def(sig: &mut Signature) -> (Vec<Stmt>, Vec<TypePath>, T
};
let input_args = sig.inputs.clone();
let mut new_stmts = vec![];
let mut new_args: punctuated::Punctuated<FnArg, Comma> = syn::punctuated::Punctuated::new();
let mut new_args: punctuated::Punctuated<FnArg, Comma> = punctuated::Punctuated::new();
// 第一个参数是self
if input_args.len() >= 1 {
if !input_args.is_empty() {
if let FnArg::Receiver(_) = &input_args[0] {
new_args.push(input_args[0].clone());
panic!("don't use self, use fn(DynaData) instead.")
}
}
let mut args_type_required = vec![];
Expand All @@ -43,18 +46,25 @@ pub fn process_function_def(sig: &mut Signature) -> (Vec<Stmt>, Vec<TypePath>, T
_ => unreachable!(),
};
// println!("argv:{:#?}", path);
if path.path.segments[0].ident.to_string() == "any" {
args_type_required.push(parse_str("ANY_TYPE").unwrap());
if path.path.segments[0].ident == "any" {
args_type_required.push(parse_str("RustClass").unwrap());
new_stmts.push(
parse_str::<Stmt>(&format!(
"let mut {} = dydata.obj_stack.pop().unwrap();",
arg_name
))
.unwrap(),
);
} else {
args_type_required.push(path.clone());
new_stmts.push(
parse_str::<Stmt>(&format!(
"let mut {} = dydata.obj_stack.pop().unwrap().downcast::<{}>().unwrap();",
arg_name, path.to_token_stream()
))
.unwrap(),
);
}
new_stmts.push(
parse_str::<Stmt>(&format!(
"let mut {} = dydata.obj_stack.pop().unwrap();",
arg_name
))
.unwrap(),
);
}
}
}
Expand Down
34 changes: 27 additions & 7 deletions rust/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod function;

#[proc_macro_attribute]
/// 返回值一定要加上return
pub fn trc_function(_: TokenStream, input: TokenStream) -> TokenStream {
pub fn trc_function(attr: TokenStream, input: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(input as ItemFn);
let (mut new_stmts, args_type_required, output) = process_function_def(&mut input.sig);
let name = input.sig.ident.clone();
Expand Down Expand Up @@ -51,15 +51,25 @@ pub fn trc_function(_: TokenStream, input: TokenStream) -> TokenStream {
let info_func_name =
parse_str::<Ident>(&function::convent_to_info_func(name.to_string())).expect("name error");
// println!("{}{:#?}", name.to_string(), info_function_self);
let function_path: syn::Path;
if let Some(ty) = attr.into_iter().next() {
if ty.to_string() == "true" {
function_path = parse_str(&format!("Self::{}", name)).unwrap();
} else {
function_path = parse_str(&name.to_string()).unwrap();
}
} else {
function_path = parse_str(&name.to_string()).unwrap();
}
let rettmp = quote!(#input
fn #info_func_name() -> RustFunction {
use crate::base::stdlib::*;
use crate::compiler::scope::TypeAllowNull;
let ret_classes = vec![#(#args_type_required.clone()),*];
return RustFunction::new(stringify!(#name), #name, IOType::new(ret_classes, #output));
let ret_classes = vec![#(#args_type_required::export_info()),*];
return RustFunction::new(stringify!(#name), #function_path, IOType::new(ret_classes, #output));
}
);
// println!("{}", rettmp.to_token_stream());
println!("{}", rettmp.to_token_stream());
rettmp.into()
}

Expand All @@ -83,6 +93,16 @@ pub fn trc_class(_: TokenStream, input: TokenStream) -> TokenStream {
// 说明是要导出的成员
let varname = i.ident.clone().unwrap();
let vartype = i.ty.clone();
if varname
.to_token_stream()
.into_iter()
.next()
.unwrap()
.to_string()
.starts_with('_')
{
continue;
}
members_ident.push(varname);
members_ty.push(vartype);
}
Expand All @@ -93,7 +113,7 @@ pub fn trc_class(_: TokenStream, input: TokenStream) -> TokenStream {
impl #name {
pub fn export_info() -> RustClass {
use std::collections::hash_map::HashMap;
use crate::compiler::scope::{Var};
use crate::compiler::scope::Var;
let mut members = HashMap::new();
#(
members.insert(Var::new(stringify!(#members_ty), #members_ident));
Expand Down Expand Up @@ -144,12 +164,12 @@ pub fn trc_method(_: TokenStream, input: TokenStream) -> TokenStream {
fn function_export() -> HashMap<String, RustFunction> {
let mut ret = HashMap::new();
#(
ret.push(stringify!(#funcs).to_string(), self.#funcs());
ret.insert(stringify!(#funcs).to_string(), Self::#funcs());
)*
ret
}
}
);
// println!("{:#?}", ret.to_string());
// println!("{}", ret);
ret.into()
}
92 changes: 46 additions & 46 deletions rust/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ Tip3:The form like 001 is supported

The next is the string value. To make you to write strings more easily:

|Way|Explain|
|:---|:---|
|"hello world"|commom value|
|'hello_world'|another method that equals to "hello world"|
|"""hello world"""|this method is for code across multiple lines|
| Way | Explain |
|:------------------|:----------------------------------------------|
| "hello world" | commom value |
| 'hello_world' | another method that equals to "hello world" |
| """hello world""" | this method is for code across multiple lines |

There are also many easape char in the string:

|escape char|meaning|
|:---|:---|
|\t|tab|
|\n|new line|
|\\\\|\|
|\'|'|
|\"|"|
|\0|the tick of the end of the string|
| escape char | meaning |
|:------------|:----------------------------------|
| \t | tab |
| \n | new line |
| \\\\|\| | |
| \' | ' |
| \" | " |
| \0 | the tick of the end of the string |

If you add ```r``` or ```R``` in front of the string.Trc will treat it as a raw string.
Yes.These rules are from Python.I love its grammar rules
Expand All @@ -58,30 +58,30 @@ the operators for basic types of trc is like others language.

Here are the operator support

|Operator|Explain|
|:---|:---|
|+|addition|
|-|subtraction|
|*|multiplication|
|/|division|
|//|divisible|
|%|mod|
|**|exponent|
|<|less than|
|>|greater than|
|<=|less than or equal to|
|>=|greater than or equal to|
|==|equal to|
|!=|not equal to|
|&&|and|
|\|\||or|
|\||bit or|
|&|bit and|
|^|bit xor|
|~|bit not|
|<<|bit left shift|
|>>|bit right shift|
|!|not|
| Operator | Explain |
|:---------|:-------------------------|
| + | addition |
| - | subtraction |
| * | multiplication |
| / | division |
| // | divisible |
| % | mod |
| ** | exponent |
| < | less than |
| > | greater than |
| <= | less than or equal to |
| >= | greater than or equal to |
| == | equal to |
| != | not equal to |
| && | and |
| \|\| | or |
| \| | bit or |
| & | bit and |
| ^ | bit xor |
| ~ | bit not |
| << | bit left shift |
| >> | bit right shift |
| ! | not |

Obviously,operators like ```+=``` is supported,too.

Expand Down Expand Up @@ -179,15 +179,15 @@ int a:=90

Std lib provide many kinds of data structures for Trc.Here is the list:

|Structure|
|:---|
|St table|
|suffix automaton|
|ac automaton|
|list|
|forward list|
|stack|
|deque|
| Structure |
|:-----------------|
| St table |
| suffix automaton |
| ac automaton |
| list |
| forward list |
| stack |
| deque |

## Function

Expand Down
3 changes: 3 additions & 0 deletions rust/examples/expr.trc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
print(1+1)
print("hello")
print("ppp"+1)
6 changes: 5 additions & 1 deletion rust/src/base/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,19 @@ pub struct StaticData {
pub inst: Vec<Inst>,
pub funcs: Vec<func::Func>,
pub sym_table_sz: usize,
pub line_table: Vec<usize>,
pub has_line_table: bool,
}

impl StaticData {
pub fn new() -> StaticData {
pub fn new(has_line_table: bool) -> StaticData {
Self {
constpool: ConstPool::new(),
inst: vec![],
funcs: vec![],
sym_table_sz: 0,
line_table: vec![],
has_line_table,
}
}

Expand Down
Loading

0 comments on commit 3a6ece3

Please sign in to comment.