Skip to content

Commit c3fa31a

Browse files
committed
function call codegen and started Blossom
1 parent f0b4d0f commit c3fa31a

File tree

18 files changed

+344
-125
lines changed

18 files changed

+344
-125
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@ edition = "2024"
55

66
[dependencies]
77
clap = { version = "4.5.38", features = ["derive"] }
8+
colored = "3.0.0"
89
pretty_assertions = "1.4.1"
910

1011
[dev-dependencies]
1112
insta = { version = "1.43.1", features = ["glob"] }
13+
14+
[lints.clippy]
15+
pedantic = "warn"
16+
nursery = "warn"
17+
perf = "warn"
18+
correctness = "warn"
19+
suspicious = "warn"
20+
style = "warn"
21+
complexity = "warn"
22+
cargo = "warn"

cool_app/lib/math.lv

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Adds two integers
2+
add :: fun (~x: Int, to y: Int) -> Int: x + y
3+
4+
## Subtracts two integers
5+
subtract :: fun (~x: Int, from y: Int) -> Int: y - x
6+
7+
## Multiplies two integers
8+
multiply :: fun (~x: Int, with y: Int) -> Int: x * y
9+
10+
## Divides two integers
11+
divide :: fun (~x: Int, by y: Int) -> Int: x / y
12+
13+
## The ratio of the circumference to the diameter of a circle
14+
pi :: 3

cool_app/main.lv

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
use lib/math
2+
use num.(get_three, get_four)
3+
4+
main :: fun () -> Int: math.add(2, to: get_three())

cool_app/num.lv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
get_three :: fun () -> Int: 3

examples/test.asm

Lines changed: 0 additions & 48 deletions
This file was deleted.

examples/test.lv

Lines changed: 0 additions & 6 deletions
This file was deleted.

next_up.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
- all about function call!!
2-
- fix that idiv bug that rsahwe pointed out

src/blossom/mod.rs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// lovingly named by japi (July 24, 2025)
2+
3+
use crate::parser;
4+
use std::{error::Error, fs, path::PathBuf};
5+
6+
pub mod printer;
7+
8+
pub struct Blossom {}
9+
10+
impl Blossom {
11+
pub fn build(path: String) -> Result<(), Box<dyn Error>> {
12+
let path_buf = PathBuf::from(path);
13+
let file_tree = collect_files(path_buf)?;
14+
let lovely_files = file_tree
15+
.filter(&|name, _| name.ends_with(".lv"))
16+
.expect("Expected a directory");
17+
let parsed_lovely_files =
18+
lovely_files.try_map(&|s| parser::Parser::new(s).parse().map_err(|e| e.into()))?;
19+
20+
println!("Building your project...");
21+
22+
Ok(())
23+
}
24+
}
25+
26+
fn collect_files(path: PathBuf) -> Result<FileTreeNode<String>, Box<dyn Error>> {
27+
if !path.is_dir() {
28+
printer::error("That's not a directory you silly!");
29+
return Err("Not a directory".into());
30+
}
31+
let Ok(dir) = path.read_dir() else {
32+
printer::error("Uh for some reason we couldn't read the directory. Not really sure why.");
33+
return Err("Failed to read directory".into());
34+
};
35+
let Ok(dir) = dir.collect::<Result<Vec<_>, _>>() else {
36+
printer::error(
37+
"You know, today just really isn't your day is it. Something awful happened, and I have no idea what it was.",
38+
);
39+
return Err("Failed to read directory".into());
40+
};
41+
42+
let mut children = vec![];
43+
44+
for dir_entry in dir {
45+
if dir_entry.path().is_dir() {
46+
children.push(collect_files(dir_entry.path())?);
47+
} else {
48+
let contents = fs::read_to_string(dir_entry.path())?;
49+
let name = dir_entry.path().to_str().unwrap().to_string();
50+
children.push(FileTreeNode::File { name, contents })
51+
}
52+
}
53+
54+
Ok(FileTreeNode::Directory {
55+
children,
56+
name: path
57+
.to_str()
58+
.expect("Weird file name, it made me uncomfortable.")
59+
.into(),
60+
})
61+
}
62+
63+
enum FileTreeNode<T> {
64+
Directory {
65+
name: String,
66+
children: Vec<FileTreeNode<T>>,
67+
},
68+
File {
69+
name: String,
70+
contents: T,
71+
},
72+
}
73+
74+
impl<T> FileTreeNode<T> {
75+
fn filter<F>(&self, f: &F) -> Option<FileTreeNode<T>>
76+
where
77+
F: Fn(&String, &T) -> bool,
78+
T: Clone,
79+
{
80+
match self {
81+
FileTreeNode::Directory { children, name } => Some(FileTreeNode::Directory {
82+
name: name.clone(),
83+
children: children.iter().filter_map(|c| c.filter(f)).collect(),
84+
}),
85+
FileTreeNode::File { name, contents } => {
86+
if f(name, contents) {
87+
Some(FileTreeNode::File {
88+
name: name.clone(),
89+
contents: contents.clone(),
90+
})
91+
} else {
92+
None
93+
}
94+
}
95+
}
96+
}
97+
98+
fn map<U>(&self, f: &impl Fn(&T) -> U) -> FileTreeNode<U> {
99+
match self {
100+
Self::Directory { name, children } => FileTreeNode::Directory {
101+
name: name.clone(),
102+
children: children.iter().map(|c| FileTreeNode::map(c, f)).collect(),
103+
},
104+
Self::File { name, contents } => FileTreeNode::File {
105+
name: name.clone(),
106+
contents: f(contents),
107+
},
108+
}
109+
}
110+
111+
fn try_map<U>(
112+
&self,
113+
f: &impl Fn(&T) -> Result<U, Box<dyn Error>>,
114+
) -> Result<FileTreeNode<U>, Box<dyn Error>> {
115+
match self {
116+
Self::Directory { name, children } => Ok(FileTreeNode::Directory {
117+
name: name.clone(),
118+
children: children
119+
.iter()
120+
.map(|c| c.try_map(f))
121+
.collect::<Result<Vec<_>, _>>()?,
122+
}),
123+
Self::File { name, contents } => Ok(FileTreeNode::File {
124+
name: name.clone(),
125+
contents: f(contents)?,
126+
}),
127+
}
128+
}
129+
}

src/blossom/printer.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use colored::Colorize;
2+
3+
pub fn error(text: &str) {
4+
eprintln!("{}", text.red());
5+
}

0 commit comments

Comments
 (0)