Skip to content

Commit 3a6ece3

Browse files
committed
fix grammar lex
添加行号表设置 add ds module and fix clippy warnings
1 parent d0e64d2 commit 3a6ece3

28 files changed

+432
-293
lines changed

rust/derive/src/def_module.rs

+22-20
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ pub fn lex_arrow(
1111
errormsg: &str,
1212
) {
1313
match iter.next().unwrap() {
14-
proc_macro::TokenTree::Group(x, ..) => {
14+
TokenTree::Group(x, ..) => {
1515
let mut left_name = TokenStream::new();
1616
let mut right_name = TokenStream::new();
1717
let mut control_which_put = false;
1818
let mut iter = x.stream().into_iter();
1919
while let Some(i) = iter.next() {
2020
// println!("{}", i);
21-
if let proc_macro::TokenTree::Punct(x) = i {
21+
if let TokenTree::Punct(x) = i {
2222
let x = x.to_string();
2323
if x == "=" {
2424
check_next_iter(&mut iter, ">");
@@ -31,19 +31,17 @@ pub fn lex_arrow(
3131
left_name = TokenStream::new();
3232
right_name = TokenStream::new();
3333
control_which_put = false;
34-
} else if x == "]" {
35-
left_push.push(syn::parse(left_name).expect("left push break"));
36-
right_push.push(syn::parse(right_name).expect("right push break"));
37-
break;
38-
}
39-
} else {
40-
if !control_which_put {
41-
left_name.extend(std::iter::once(i));
4234
} else {
43-
right_name.extend(std::iter::once(i));
35+
panic!("{}", errormsg);
4436
}
37+
} else if !control_which_put {
38+
left_name.extend(std::iter::once(i));
39+
} else {
40+
right_name.extend(std::iter::once(i));
4541
}
4642
}
43+
left_push.push(syn::parse(left_name).expect("left push break"));
44+
right_push.push(syn::parse(right_name).expect("right push break"));
4745
}
4846
_ => {
4947
panic!("{}", errormsg);
@@ -53,7 +51,7 @@ pub fn lex_arrow(
5351

5452
fn check_next_iter(iter: &mut IntoIter, check_str: &str) {
5553
if let Some(i) = iter.next() {
56-
if let proc_macro::TokenTree::Punct(x) = i {
54+
if let TokenTree::Punct(x) = i {
5755
if x.to_string() != check_str {
5856
panic!("expected {}", check_str);
5957
}
@@ -75,12 +73,12 @@ pub fn def_impl(content: TokenStream) -> TokenStream {
7573
let mut submodules = vec![];
7674
while let Some(i) = iter.next() {
7775
match i {
78-
proc_macro::TokenTree::Ident(x) => {
76+
TokenTree::Ident(x) => {
7977
let x = x.to_string();
8078
if x == "module_name" {
8179
check_next_iter(&mut iter, "=");
8280
if let TokenTree::Ident(tmp) = iter.next().expect("name is expected") {
83-
if let Some(_) = module_ident {
81+
if module_ident.is_some() {
8482
panic!("double defined");
8583
}
8684
module_ident =
@@ -106,14 +104,14 @@ pub fn def_impl(content: TokenStream) -> TokenStream {
106104
);
107105
} else if x == "submodules" {
108106
check_next_iter(&mut iter, "=");
109-
if let proc_macro::TokenTree::Group(x, ..) = iter.next().unwrap() {
107+
if let TokenTree::Group(x, ..) = iter.next().unwrap() {
110108
// println!("{}", x);
111109
let mut iter = x.stream().into_iter();
112-
while let Some(i) = iter.next() {
113-
if let proc_macro::TokenTree::Ident(x) = i {
110+
for i in iter {
111+
if let TokenTree::Ident(x) = i {
114112
submodules
115113
.push(syn::parse_str::<syn::Ident>(&(x.to_string())).unwrap());
116-
} else if let proc_macro::TokenTree::Ident(x) = i {
114+
} else if let TokenTree::Ident(x) = i {
117115
let x = x.to_string();
118116
if x != "," {
119117
panic!("expected ,.get {}", x);
@@ -127,7 +125,7 @@ pub fn def_impl(content: TokenStream) -> TokenStream {
127125
}
128126
}
129127
}
130-
proc_macro::TokenTree::Punct(x) => {
128+
TokenTree::Punct(x) => {
131129
if x.to_string() != "," {
132130
panic!("expected ,");
133131
}
@@ -147,15 +145,19 @@ pub fn def_impl(content: TokenStream) -> TokenStream {
147145
use std::collections::hash_map::HashMap;
148146
let mut functions = HashMap::new();
149147
let mut classes = HashMap::new();
148+
let mut submodules = HashMap::new();
150149
#(
151150
functions.insert(stringify!(#right_func).to_string(), #left_func());
152151
)*
153152
#(
154153
classes.insert(stringify!(#right_class).to_string(), #left_class::export_info());
155154
)*
155+
#(
156+
submodules.insert(stringify!(#submodules).to_string(), #submodules::init());
157+
)*
156158
Stdlib::new(
157159
stringify!(#module_ident),
158-
HashMap::new(),
160+
submodules,
159161
functions,
160162
classes
161163
)

rust/derive/src/function.rs

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use core::panic;
2+
3+
use quote::ToTokens;
14
use syn::{
25
parse_str, punctuated, token::Comma, FnArg, PatType, ReturnType, Signature, Stmt, Type,
36
TypePath,
@@ -24,11 +27,11 @@ pub fn process_function_def(sig: &mut Signature) -> (Vec<Stmt>, Vec<TypePath>, T
2427
};
2528
let input_args = sig.inputs.clone();
2629
let mut new_stmts = vec![];
27-
let mut new_args: punctuated::Punctuated<FnArg, Comma> = syn::punctuated::Punctuated::new();
30+
let mut new_args: punctuated::Punctuated<FnArg, Comma> = punctuated::Punctuated::new();
2831
// 第一个参数是self
29-
if input_args.len() >= 1 {
32+
if !input_args.is_empty() {
3033
if let FnArg::Receiver(_) = &input_args[0] {
31-
new_args.push(input_args[0].clone());
34+
panic!("don't use self, use fn(DynaData) instead.")
3235
}
3336
}
3437
let mut args_type_required = vec![];
@@ -43,18 +46,25 @@ pub fn process_function_def(sig: &mut Signature) -> (Vec<Stmt>, Vec<TypePath>, T
4346
_ => unreachable!(),
4447
};
4548
// println!("argv:{:#?}", path);
46-
if path.path.segments[0].ident.to_string() == "any" {
47-
args_type_required.push(parse_str("ANY_TYPE").unwrap());
49+
if path.path.segments[0].ident == "any" {
50+
args_type_required.push(parse_str("RustClass").unwrap());
51+
new_stmts.push(
52+
parse_str::<Stmt>(&format!(
53+
"let mut {} = dydata.obj_stack.pop().unwrap();",
54+
arg_name
55+
))
56+
.unwrap(),
57+
);
4858
} else {
4959
args_type_required.push(path.clone());
60+
new_stmts.push(
61+
parse_str::<Stmt>(&format!(
62+
"let mut {} = dydata.obj_stack.pop().unwrap().downcast::<{}>().unwrap();",
63+
arg_name, path.to_token_stream()
64+
))
65+
.unwrap(),
66+
);
5067
}
51-
new_stmts.push(
52-
parse_str::<Stmt>(&format!(
53-
"let mut {} = dydata.obj_stack.pop().unwrap();",
54-
arg_name
55-
))
56-
.unwrap(),
57-
);
5868
}
5969
}
6070
}

rust/derive/src/lib.rs

+27-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod function;
1010

1111
#[proc_macro_attribute]
1212
/// 返回值一定要加上return
13-
pub fn trc_function(_: TokenStream, input: TokenStream) -> TokenStream {
13+
pub fn trc_function(attr: TokenStream, input: TokenStream) -> TokenStream {
1414
let mut input = parse_macro_input!(input as ItemFn);
1515
let (mut new_stmts, args_type_required, output) = process_function_def(&mut input.sig);
1616
let name = input.sig.ident.clone();
@@ -51,15 +51,25 @@ pub fn trc_function(_: TokenStream, input: TokenStream) -> TokenStream {
5151
let info_func_name =
5252
parse_str::<Ident>(&function::convent_to_info_func(name.to_string())).expect("name error");
5353
// println!("{}{:#?}", name.to_string(), info_function_self);
54+
let function_path: syn::Path;
55+
if let Some(ty) = attr.into_iter().next() {
56+
if ty.to_string() == "true" {
57+
function_path = parse_str(&format!("Self::{}", name)).unwrap();
58+
} else {
59+
function_path = parse_str(&name.to_string()).unwrap();
60+
}
61+
} else {
62+
function_path = parse_str(&name.to_string()).unwrap();
63+
}
5464
let rettmp = quote!(#input
5565
fn #info_func_name() -> RustFunction {
5666
use crate::base::stdlib::*;
5767
use crate::compiler::scope::TypeAllowNull;
58-
let ret_classes = vec![#(#args_type_required.clone()),*];
59-
return RustFunction::new(stringify!(#name), #name, IOType::new(ret_classes, #output));
68+
let ret_classes = vec![#(#args_type_required::export_info()),*];
69+
return RustFunction::new(stringify!(#name), #function_path, IOType::new(ret_classes, #output));
6070
}
6171
);
62-
// println!("{}", rettmp.to_token_stream());
72+
println!("{}", rettmp.to_token_stream());
6373
rettmp.into()
6474
}
6575

@@ -83,6 +93,16 @@ pub fn trc_class(_: TokenStream, input: TokenStream) -> TokenStream {
8393
// 说明是要导出的成员
8494
let varname = i.ident.clone().unwrap();
8595
let vartype = i.ty.clone();
96+
if varname
97+
.to_token_stream()
98+
.into_iter()
99+
.next()
100+
.unwrap()
101+
.to_string()
102+
.starts_with('_')
103+
{
104+
continue;
105+
}
86106
members_ident.push(varname);
87107
members_ty.push(vartype);
88108
}
@@ -93,7 +113,7 @@ pub fn trc_class(_: TokenStream, input: TokenStream) -> TokenStream {
93113
impl #name {
94114
pub fn export_info() -> RustClass {
95115
use std::collections::hash_map::HashMap;
96-
use crate::compiler::scope::{Var};
116+
use crate::compiler::scope::Var;
97117
let mut members = HashMap::new();
98118
#(
99119
members.insert(Var::new(stringify!(#members_ty), #members_ident));
@@ -144,12 +164,12 @@ pub fn trc_method(_: TokenStream, input: TokenStream) -> TokenStream {
144164
fn function_export() -> HashMap<String, RustFunction> {
145165
let mut ret = HashMap::new();
146166
#(
147-
ret.push(stringify!(#funcs).to_string(), self.#funcs());
167+
ret.insert(stringify!(#funcs).to_string(), Self::#funcs());
148168
)*
149169
ret
150170
}
151171
}
152172
);
153-
// println!("{:#?}", ret.to_string());
173+
// println!("{}", ret);
154174
ret.into()
155175
}

rust/docs/usage.md

+46-46
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@ Tip3:The form like 001 is supported
3030

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

33-
|Way|Explain|
34-
|:---|:---|
35-
|"hello world"|commom value|
36-
|'hello_world'|another method that equals to "hello world"|
37-
|"""hello world"""|this method is for code across multiple lines|
33+
| Way | Explain |
34+
|:------------------|:----------------------------------------------|
35+
| "hello world" | commom value |
36+
| 'hello_world' | another method that equals to "hello world" |
37+
| """hello world""" | this method is for code across multiple lines |
3838

3939
There are also many easape char in the string:
4040

41-
|escape char|meaning|
42-
|:---|:---|
43-
|\t|tab|
44-
|\n|new line|
45-
|\\\\|\|
46-
|\'|'|
47-
|\"|"|
48-
|\0|the tick of the end of the string|
41+
| escape char | meaning |
42+
|:------------|:----------------------------------|
43+
| \t | tab |
44+
| \n | new line |
45+
| \\\\|\| | |
46+
| \' | ' |
47+
| \" | " |
48+
| \0 | the tick of the end of the string |
4949

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

5959
Here are the operator support
6060

61-
|Operator|Explain|
62-
|:---|:---|
63-
|+|addition|
64-
|-|subtraction|
65-
|*|multiplication|
66-
|/|division|
67-
|//|divisible|
68-
|%|mod|
69-
|**|exponent|
70-
|<|less than|
71-
|>|greater than|
72-
|<=|less than or equal to|
73-
|>=|greater than or equal to|
74-
|==|equal to|
75-
|!=|not equal to|
76-
|&&|and|
77-
|\|\||or|
78-
|\||bit or|
79-
|&|bit and|
80-
|^|bit xor|
81-
|~|bit not|
82-
|<<|bit left shift|
83-
|>>|bit right shift|
84-
|!|not|
61+
| Operator | Explain |
62+
|:---------|:-------------------------|
63+
| + | addition |
64+
| - | subtraction |
65+
| * | multiplication |
66+
| / | division |
67+
| // | divisible |
68+
| % | mod |
69+
| ** | exponent |
70+
| < | less than |
71+
| > | greater than |
72+
| <= | less than or equal to |
73+
| >= | greater than or equal to |
74+
| == | equal to |
75+
| != | not equal to |
76+
| && | and |
77+
| \|\| | or |
78+
| \| | bit or |
79+
| & | bit and |
80+
| ^ | bit xor |
81+
| ~ | bit not |
82+
| << | bit left shift |
83+
| >> | bit right shift |
84+
| ! | not |
8585

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

@@ -179,15 +179,15 @@ int a:=90
179179

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

182-
|Structure|
183-
|:---|
184-
|St table|
185-
|suffix automaton|
186-
|ac automaton|
187-
|list|
188-
|forward list|
189-
|stack|
190-
|deque|
182+
| Structure |
183+
|:-----------------|
184+
| St table |
185+
| suffix automaton |
186+
| ac automaton |
187+
| list |
188+
| forward list |
189+
| stack |
190+
| deque |
191191

192192
## Function
193193

rust/examples/expr.trc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
print(1+1)
2+
print("hello")
3+
print("ppp"+1)

rust/src/base/codegen.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,19 @@ pub struct StaticData {
9393
pub inst: Vec<Inst>,
9494
pub funcs: Vec<func::Func>,
9595
pub sym_table_sz: usize,
96+
pub line_table: Vec<usize>,
97+
pub has_line_table: bool,
9698
}
9799

98100
impl StaticData {
99-
pub fn new() -> StaticData {
101+
pub fn new(has_line_table: bool) -> StaticData {
100102
Self {
101103
constpool: ConstPool::new(),
102104
inst: vec![],
103105
funcs: vec![],
104106
sym_table_sz: 0,
107+
line_table: vec![],
108+
has_line_table,
105109
}
106110
}
107111

0 commit comments

Comments
 (0)