aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/eval.rs8
-rw-r--r--src/main.rs188
2 files changed, 48 insertions, 148 deletions
diff --git a/src/eval.rs b/src/eval.rs
index e13cec3..029cffe 100644
--- a/src/eval.rs
+++ b/src/eval.rs
@@ -1,4 +1,4 @@
1//! tree walking interpreter for trawk 1//! tree walking interpreter for tbsp
2 2
3use crate::ast; 3use crate::ast;
4use std::{collections::HashMap, fmt}; 4use std::{collections::HashMap, fmt};
@@ -112,6 +112,8 @@ impl Value {
112 fn mul(&self, other: &Self) -> Result { 112 fn mul(&self, other: &Self) -> Result {
113 match (self, other) { 113 match (self, other) {
114 (Self::Integer(s), Self::Integer(o)) => Ok(Self::Integer(*s * *o)), 114 (Self::Integer(s), Self::Integer(o)) => Ok(Self::Integer(*s * *o)),
115 (Self::Integer(s), Self::String(o)) => Ok(Self::String(o.repeat(*s as usize))),
116 (Self::String(_), Self::Integer(_)) => other.mul(self),
115 _ => Err(Error::UndefinedBinOp( 117 _ => Err(Error::UndefinedBinOp(
116 ast::BinOp::Arith(ast::ArithOp::Mul), 118 ast::BinOp::Arith(ast::ArithOp::Mul),
117 self.ty(), 119 self.ty(),
@@ -656,13 +658,13 @@ impl<'a> Context<'a> {
656 658
657pub fn evaluate(file: &str, program: &str, language: tree_sitter::Language) -> Result { 659pub fn evaluate(file: &str, program: &str, language: tree_sitter::Language) -> Result {
658 let mut parser = tree_sitter::Parser::new(); 660 let mut parser = tree_sitter::Parser::new();
659 let _ = parser.set_language(language); 661 let _ = parser.set_language(&language);
660 662
661 let tree = parser.parse(file, None).unwrap(); 663 let tree = parser.parse(file, None).unwrap();
662 let cursor = tree.walk(); 664 let cursor = tree.walk();
663 665
664 let program = ast::Program::new().from_str(program).unwrap(); 666 let program = ast::Program::new().from_str(program).unwrap();
665 let mut ctx = Context::new(tree_sitter_md::language()) 667 let mut ctx = Context::new(language)
666 .with_input(file.to_owned()) 668 .with_input(file.to_owned())
667 .with_cursor(cursor) 669 .with_cursor(cursor)
668 .with_program(program)?; 670 .with_program(program)?;
diff --git a/src/main.rs b/src/main.rs
index 6196a32..ed024fc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,4 +1,4 @@
1/// TBSP: tree-based source processor 1/// tree-based source processor
2#[derive(argh::FromArgs)] 2#[derive(argh::FromArgs)]
3struct Cli { 3struct Cli {
4 /// read the TBSP program source from a file 4 /// read the TBSP program source from a file
@@ -14,42 +14,43 @@ struct Cli {
14 file: Option<std::path::PathBuf>, 14 file: Option<std::path::PathBuf>,
15} 15}
16 16
17fn main() { 17impl Cli {
18 let cli: Cli = argh::from_env(); 18 fn program(&self) -> String {
19 19 std::fs::read_to_string(&self.program_file).unwrap_or_else(|e| {
20 let program = std::fs::read_to_string(&cli.program_file).unwrap_or_else(|e| { 20 eprintln!(
21 eprintln!( 21 "failed to read program-file from `{}`: {e}",
22 "failed to read program-file from `{}`: {e}", 22 self.program_file.display()
23 cli.program_file.display() 23 );
24 );
25 std::process::exit(-1);
26 });
27
28 let language = match cli.language.as_str() {
29 "md" => tree_sitter_md::language(),
30 "typescript" => tree_sitter_typescript::language_typescript(),
31 "javascript" => tree_sitter_javascript::language(),
32 "python" => tree_sitter_python::language(),
33 "rust" => tree_sitter_rust::language(),
34 lang => {
35 eprintln!("unknown language `{lang}`");
36 std::process::exit(-1); 24 std::process::exit(-1);
37 } 25 })
38 }; 26 }
39 27
40 let file = cli 28 fn language(&self) -> tree_sitter::Language {
41 .file 29 match self.language.as_str() {
42 .map(std::fs::read_to_string) 30 "md" => tree_sitter_md::language(),
43 .unwrap_or_else(try_consume_stdin) 31 "typescript" => tree_sitter_typescript::language_typescript(),
44 .unwrap_or_else(|e| { 32 "javascript" => tree_sitter_javascript::language(),
45 eprintln!("{e}"); 33 "python" => tree_sitter_python::language(),
46 std::process::exit(-1) 34 "rust" => tree_sitter_rust::language(),
47 }); 35 lang => {
36 eprintln!("unknown language `{lang}`");
37 std::process::exit(-1);
38 }
39 }
40 }
48 41
49 trawk::evaluate(&file, &program, language).unwrap_or_else(|e| { 42 fn file(&self) -> String {
50 eprintln!("{e:?}"); 43 match self.file.as_ref() {
51 std::process::exit(-1); 44 Some(f) => std::fs::read_to_string(f).unwrap_or_else(|e| {
52 }); 45 eprintln!("failed to read input-file from `{}`: {e}", f.display());
46 std::process::exit(-1);
47 }),
48 None => try_consume_stdin().unwrap_or_else(|e| {
49 eprintln!("failed to read input-file from stdin: {e}");
50 std::process::exit(-1);
51 }),
52 }
53 }
53} 54}
54 55
55fn try_consume_stdin() -> std::io::Result<String> { 56fn try_consume_stdin() -> std::io::Result<String> {
@@ -69,114 +70,11 @@ fn try_consume_stdin() -> std::io::Result<String> {
69 } 70 }
70} 71}
71 72
72// fn main() { 73fn main() {
73// let src = r#" 74 let cli: Cli = argh::from_env();
74// # foo1 75
75// 76 tbsp::evaluate(&cli.file(), &cli.program(), cli.language()).unwrap_or_else(|e| {
76// bar 77 eprintln!("{e:?}");
77// 78 std::process::exit(-1);
78// ## foo1.1 79 });
79// 80}
80// bar baz
81//
82// # foo2
83//
84// bar baz
85//
86// ```
87// fn main() {
88// }
89// ```
90//
91// - foo
92// - bar
93// - baz
94//
95// "#
96// .to_owned();
97//
98// let program = Program::new()
99// .from_str(
100// r#"
101// BEGIN {
102// int depth = 0;
103//
104// print("<html>\n");
105// print("<body>\n");
106// }
107//
108// enter section {
109// depth += 1;
110// }
111// leave section {
112// depth -= 1;
113// }
114//
115// enter atx_heading {
116// print("<h");
117// print(depth);
118// print(">");
119// }
120// leave atx_heading {
121// print("</h");
122// print(depth);
123// print(">\n");
124// }
125//
126// enter paragraph {
127// print("<p>");
128// }
129// leave paragraph {
130// print("</p>\n");
131// }
132//
133// enter list {
134// print("<ol>");
135// }
136// leave list {
137// print("</ol>\n");
138// }
139//
140// enter list_item {
141// print("<li>");
142// }
143// leave list_item {
144// print("</li>\n");
145// }
146//
147// enter fenced_code_block {
148// print("<pre>");
149// }
150// leave fenced_code_block {
151// print("</pre>\n");
152// }
153//
154// enter inline {
155// print(text(node));
156// }
157// enter code_fence_content {
158// print(text(node));
159// }
160//
161// END {
162// print("</body>\n");
163// print("</html>\n");
164// }
165// "#,
166// )
167// .unwrap();
168//
169// let mut parser = tree_sitter::Parser::new();
170// let _ = parser.set_language(&tree_sitter_md::language());
171//
172// let tree = parser.parse(&src, None).unwrap();
173// let cursor = tree.walk();
174//
175// let mut ctx = Context::new(tree_sitter_md::language())
176// .with_input(src)
177// .with_cursor(cursor)
178// .with_program(program)
179// .unwrap();
180//
181// let _ = ctx.eval();
182// }