Skip to content

Commit 85dbdc6

Browse files
committed
working on umpl support
1 parent 686fd7b commit 85dbdc6

File tree

4 files changed

+88
-12
lines changed

4 files changed

+88
-12
lines changed

git-function-history-lib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ cached = {version = "0.42.0", optional = true}
3131
gitoxide-core = "0.22.0"
3232
git-repository = { version = "0.33.0", default-features = false, features = ["max-performance-safe"] }
3333
git-features = { version = "0.26.1", features = ["zlib", "once_cell"] }
34-
umpl = "1.0.2"
34+
umpl = "1.1.0"

git-function-history-lib/src/languages/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ make_file!(UMPLFile, UMPLFunction, UMPL);
338338
mod lang_tests {
339339
// macro that auto genertes the test parse_<lang>_file_time
340340
macro_rules! make_file_time_test {
341-
($name:ident, $extname:ident, $function:ident, $filetype:ident) => {
341+
($name:ident, $extname:ident, $function:ident, $filetype:ident, $fnname:literal) => {
342342
#[test]
343343
fn $name() {
344344
let mut file = std::env::current_dir().unwrap();
@@ -347,7 +347,7 @@ mod lang_tests {
347347
let files = std::fs::read_to_string(file.clone())
348348
.expect(format!("could not read file {:?}", file).as_str());
349349
let start = std::time::Instant::now();
350-
let ok = $function::find_function_in_file(&files, "empty_test");
350+
let ok = $function::find_function_in_file(&files, $fnname);
351351
let end = std::time::Instant::now();
352352
match &ok {
353353
Ok(hist) => {
@@ -371,11 +371,13 @@ mod lang_tests {
371371
}
372372

373373
use super::*;
374-
make_file_time_test!(python_parses, py, python, PythonFile);
375-
make_file_time_test!(rust_parses, rs, rust, RustFile);
374+
make_file_time_test!(python_parses, py, python, PythonFile, "empty_test");
375+
make_file_time_test!(rust_parses, rs, rust, RustFile, "empty_test");
376376
// #[cfg(feature = "c_lang")]
377-
// make_file_time_test!(c_parses, c, c, CFile);
377+
// make_file_time_test!(c_parses, c, c, CFile, "empty_test");
378378
#[cfg(feature = "unstable")]
379-
make_file_time_test!(go_parses, go, go, GoFile);
380-
make_file_time_test!(ruby_parses, rb, ruby, RubyFile);
379+
make_file_time_test!(go_parses, go, go, GoFile, "empty_test");
380+
make_file_time_test!(ruby_parses, rb, ruby, RubyFile, "empty_test");
381+
382+
make_file_time_test!(umpl_parses, umpl, umpl, UMPLFile, "😂");
381383
}

git-function-history-lib/src/languages/umpl.rs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use umpl;
1+
use umpl::{self, parser::Thing};
22

33
use std::{error::Error, fmt};
44

@@ -10,6 +10,16 @@ pub struct UMPLFunction {
1010
pub(crate) lines: (usize, usize),
1111
pub(crate) name: String,
1212
pub(crate) body: String,
13+
pub(crate) args_count: usize,
14+
pub(crate) parents: Vec<UMPLParentFunction>,
15+
}
16+
#[derive(Debug, Clone, PartialEq, Eq)]
17+
pub struct UMPLParentFunction {
18+
pub(crate) lines: (usize, usize),
19+
pub(crate) name: String,
20+
pub(crate) top: String,
21+
pub(crate) bottom: String,
22+
pub(crate) args_count: usize,
1323
}
1424

1525
impl fmt::Display for UMPLFunction {
@@ -43,8 +53,55 @@ pub(crate) fn find_function_in_file(
4353
let tokens = lexed.scan_tokens();
4454
let mut parsed = umpl::parser::Parser::new(tokens);
4555
let ast = parsed.parse();
46-
for node in ast {}
47-
Err("")?
56+
let res = find_function_recurse(name, ast, &vec![]);
57+
if res.len() > 0 {
58+
return Ok(res);
59+
}
60+
Err("no function found")?
61+
}
62+
63+
fn find_function_recurse(name: &str, ast: Vec<Thing>, current: &Vec<UMPLParentFunction>) -> Vec<(UMPLFunction)> {
64+
let mut results = Vec::new();
65+
for node in ast {
66+
match node {
67+
Thing::Function(fns) => {
68+
if fns.name.to_string() == name {
69+
let new_fn = UMPLFunction {
70+
lines: (fns.line as usize, fns.end_line as usize),
71+
name: fns.name.to_string(),
72+
// TODO: get the function body
73+
body: String::new(),
74+
args_count: fns.num_arguments as usize,
75+
parents: current.clone()
76+
};
77+
results.push(new_fn);
78+
} else {
79+
let mut new_current = current.clone();
80+
// turn into a parent function
81+
let pfn = UMPLParentFunction {
82+
lines: (fns.line as usize, fns.end_line as usize),
83+
name: fns.name.to_string(),
84+
// TODO: get the top and bottom lines
85+
top: String::new(),
86+
bottom: String::new(),
87+
args_count: fns.num_arguments as usize
88+
};
89+
new_current.push(pfn);
90+
results.append(&mut find_function_recurse(name, fns.body, &new_current));
91+
}
92+
}
93+
Thing::LoopStatement(loops) => {
94+
results.append(&mut find_function_recurse(name, loops.body, current));
95+
}
96+
Thing::IfStatement(ifs) => {
97+
results.append(&mut find_function_recurse(name, ifs.body_true, current));
98+
results.append(&mut find_function_recurse(name, ifs.body_false, current));
99+
}
100+
_ => {}
101+
}
102+
}
103+
results
104+
48105
}
49106

50107
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -54,4 +111,4 @@ impl UMPLFilter {
54111
pub fn matches(&self, function: &UMPLFunction) -> bool {
55112
false
56113
}
57-
}
114+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
potato 😀 2 ⧼ ! to create a custom function first we use the potato keyword to signify that we want to create a custom function.
2+
! then we add the name of the function and the number of arguments that the function will take.
3+
(`HELLO`)> ! prints HELLO
4+
($1)> ! to acces function args we use $1 for the first args and so on
5+
create z with ((plus 0xA.9 $1 $2))< ! new variable z with the value of the sum of the first two arguments and 169 (0xA9 in hex)
6+
(z)> ! prints z
7+
potato 😂 1 ⧼ ! to create a custom function first we use the potato keyword to signify that we want to create a custom function.
8+
! then we add the name of the function and the number of arguments that the function will take.
9+
(`BYE`)> ! prints HELLO
10+
create z with ((plus 0xFF $1))< ! new variable z with the value of the sum of the first two arguments and 169 (0xA9 in hex)
11+
return z ! at the end we return z notice there is no colon b/c were returning something
12+
13+
create z with ((new 😂 z))< ! to call custom function we must use the new keyword followed by the function name seperated argumentsaa
14+
return z ! at the end we return z notice there is no colon b/c were returning something
15+
16+
((new 😀 1 0xA))> ! to call custom function we must use the new keyword followed by the function name seperated arguments
17+
! prints HELLO and on a new line 1 and on a new line 180

0 commit comments

Comments
 (0)