Skip to content

Commit 7e4dd3b

Browse files
committed
Added modules, include and import statements
1 parent 6f6501e commit 7e4dd3b

10 files changed

+542
-166
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "density_function_lang"
3-
version = "1.0.1"
3+
version = "2.0.0"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

src/compiler/ast.rs

+55-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
use std::cell::RefCell;
12
use std::fmt::{Debug, Formatter};
3+
use std::path::PathBuf;
4+
use std::rc::{Rc, Weak};
25
use crate::compiler::lexer::Token;
36

4-
#[derive(Clone)]
7+
#[derive(Clone, PartialEq)]
58
pub enum Stmt {
69
Template {
710
name: Token,
@@ -14,6 +17,18 @@ pub enum Stmt {
1417
expr: Expr,
1518
},
1619

20+
Module {
21+
name: Token,
22+
statements: Vec<Stmt>,
23+
},
24+
Include {
25+
path: Token,
26+
},
27+
Import {
28+
path: Vec<Token>,
29+
selector: Option<Vec<Token>>,
30+
},
31+
1732
Error,
1833
}
1934

@@ -28,12 +43,21 @@ impl Debug for Stmt {
2843
Stmt::Function { name, expr } =>
2944
write!(f, "function {} = {:?};", name.source(), expr),
3045

46+
Stmt::Module { name, statements } =>
47+
write!(f, "module {} {{ {} }}", name.source(), statements.iter().map(|stmt| format!("{:?}", stmt))
48+
.collect::<Vec<String>>().join(" ")),
49+
Stmt::Include { path } => write!(f, "include \"{}\";", path.source()),
50+
Stmt::Import { path, selector } => write!(f, "{}.{}",
51+
path.iter().map(|name| name.source().to_owned()).collect::<Vec<String>>().join("."),
52+
selector.as_ref().map(|names| names.iter().map(|name| name.source().to_owned()).collect::<Vec<String>>().join(", "))
53+
.map(|names| format!("{{{}}}", names)).unwrap_or_else(|| String::from("*"))),
54+
3155
Stmt::Error => write!(f, "Error;")
3256
}
3357
}
3458
}
3559

36-
#[derive(Clone)]
60+
#[derive(Clone, PartialEq)]
3761
pub enum Expr {
3862
ConstantFloat(f32),
3963
ConstantInt(i32),
@@ -103,6 +127,29 @@ impl Debug for Expr {
103127
}
104128
}
105129

130+
#[derive(Debug)]
131+
pub struct OutputFunction {
132+
pub name: String,
133+
pub path: PathBuf,
134+
pub json: JsonElement,
135+
}
136+
137+
#[derive(Debug)]
138+
pub struct Template {
139+
pub name: String, // Can be identifiers or operator names (like `+`)
140+
pub args: Vec<String>,
141+
pub expr: Expr,
142+
pub current_modules: Vec<Weak<RefCell<Module>>>,
143+
}
144+
145+
#[derive(Debug)]
146+
pub struct Module {
147+
pub name: String,
148+
pub sub_modules: Vec<Rc<RefCell<Module>>>,
149+
pub templates: Vec<Rc<RefCell<Template>>>,
150+
pub output_functions: Vec<Rc<RefCell<OutputFunction>>>,
151+
}
152+
106153
#[derive(Clone)]
107154
pub enum JsonElement {
108155
ConstantFloat(f32),
@@ -112,6 +159,9 @@ pub enum JsonElement {
112159
Object(Vec<(String, JsonElement)>),
113160
Array(Vec<JsonElement>),
114161

162+
Module(Rc<RefCell<Module>>),
163+
Template(Rc<RefCell<Template>>),
164+
115165
Error,
116166
}
117167

@@ -127,6 +177,9 @@ impl Debug for JsonElement {
127177
JsonElement::Array(elements) => write!(f, "[{}]", elements.iter()
128178
.map(|element| format!("{:?}", element)).collect::<Vec<String>>()
129179
.join(", ")),
180+
181+
JsonElement::Module(module) => write!(f, "<module {}>", &module.borrow().name),
182+
JsonElement::Template(template) => write!(f, "<template {}>", &template.borrow().name),
130183
JsonElement::Error => write!(f, "Error"),
131184
}
132185
}

0 commit comments

Comments
 (0)