Skip to content

Commit 95f4593

Browse files
committed
Add markdown macro and parse/escape functions
1 parent e86119c commit 95f4593

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

internal/compiler/builtin_macros.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub fn lower_macro(
8787
}
8888
BuiltinMacroFunction::Rgb => rgb_macro(n, sub_expr.collect(), diag),
8989
BuiltinMacroFunction::Hsv => hsv_macro(n, sub_expr.collect(), diag),
90+
BuiltinMacroFunction::Markdown => markdown_macro(n, sub_expr.collect()),
9091
}
9192
}
9293

@@ -319,6 +320,30 @@ fn debug_macro(
319320
}
320321
}
321322

323+
fn markdown_macro(node: &dyn Spanned, args: Vec<(Expression, Option<NodeOrToken>)>) -> Expression {
324+
let mut string = None;
325+
for (expr, node) in args {
326+
let escaped = Expression::FunctionCall {
327+
function: BuiltinFunction::EscapeMarkdown.into(),
328+
arguments: vec![expr],
329+
source_location: Some(node.to_source_location()),
330+
};
331+
string = Some(match string {
332+
None => escaped,
333+
Some(string) => Expression::BinaryExpression {
334+
lhs: Box::new(string),
335+
op: '+',
336+
rhs: Box::new(escaped),
337+
},
338+
});
339+
}
340+
Expression::FunctionCall {
341+
function: BuiltinFunction::ParseMarkdown.into(),
342+
arguments: vec![string.unwrap_or_else(|| Expression::default_value_for_type(&Type::String))],
343+
source_location: Some(node.to_source_location()),
344+
}
345+
}
346+
322347
fn to_debug_string(
323348
expr: Expression,
324349
node: &dyn Spanned,

internal/compiler/expression_tree.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ pub enum BuiltinFunction {
114114
StopTimer,
115115
RestartTimer,
116116
OpenUrl,
117+
ParseMarkdown,
118+
EscapeMarkdown,
117119
}
118120

119121
#[derive(Debug, Clone)]
@@ -142,6 +144,8 @@ pub enum BuiltinMacroFunction {
142144
Hsv,
143145
/// transform `debug(a, b, c)` into debug `a + " " + b + " " + c`
144146
Debug,
147+
/// Markdown
148+
Markdown,
145149
}
146150

147151
macro_rules! declare_builtin_function_types {
@@ -273,7 +277,9 @@ declare_builtin_function_types!(
273277
StartTimer: (Type::ElementReference) -> Type::Void,
274278
StopTimer: (Type::ElementReference) -> Type::Void,
275279
RestartTimer: (Type::ElementReference) -> Type::Void,
276-
OpenUrl: (Type::String) -> Type::Void
280+
OpenUrl: (Type::String) -> Type::Void,
281+
EscapeMarkdown: (Type::String) -> Type::String,
282+
ParseMarkdown: (Type::String) -> Type::Void
277283
);
278284

279285
impl BuiltinFunction {
@@ -370,6 +376,7 @@ impl BuiltinFunction {
370376
BuiltinFunction::StopTimer => false,
371377
BuiltinFunction::RestartTimer => false,
372378
BuiltinFunction::OpenUrl => false,
379+
BuiltinFunction::ParseMarkdown | BuiltinFunction::EscapeMarkdown => true,
373380
}
374381
}
375382

@@ -448,6 +455,7 @@ impl BuiltinFunction {
448455
BuiltinFunction::StopTimer => false,
449456
BuiltinFunction::RestartTimer => false,
450457
BuiltinFunction::OpenUrl => false,
458+
BuiltinFunction::ParseMarkdown | BuiltinFunction::EscapeMarkdown => true,
451459
}
452460
}
453461
}

internal/compiler/llr/optim_passes/inline_expressions.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ fn builtin_function_cost(function: &BuiltinFunction) -> isize {
155155
BuiltinFunction::StopTimer => 10,
156156
BuiltinFunction::RestartTimer => 10,
157157
BuiltinFunction::OpenUrl => isize::MAX,
158+
BuiltinFunction::ParseMarkdown | BuiltinFunction::EscapeMarkdown => isize::MAX,
158159
}
159160
}
160161

0 commit comments

Comments
 (0)