From 83b537bb860643ebdabc43ab47cb8645da8a2e6d Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 14 Jul 2024 13:00:51 +0100 Subject: rename: trawk -> tbsp --- src/eval.rs | 8 ++- src/main.rs | 188 ++++++++++++++---------------------------------------------- 2 files changed, 48 insertions(+), 148 deletions(-) (limited to 'src') 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 @@ -//! tree walking interpreter for trawk +//! tree walking interpreter for tbsp use crate::ast; use std::{collections::HashMap, fmt}; @@ -112,6 +112,8 @@ impl Value { fn mul(&self, other: &Self) -> Result { match (self, other) { (Self::Integer(s), Self::Integer(o)) => Ok(Self::Integer(*s * *o)), + (Self::Integer(s), Self::String(o)) => Ok(Self::String(o.repeat(*s as usize))), + (Self::String(_), Self::Integer(_)) => other.mul(self), _ => Err(Error::UndefinedBinOp( ast::BinOp::Arith(ast::ArithOp::Mul), self.ty(), @@ -656,13 +658,13 @@ impl<'a> Context<'a> { pub fn evaluate(file: &str, program: &str, language: tree_sitter::Language) -> Result { let mut parser = tree_sitter::Parser::new(); - let _ = parser.set_language(language); + let _ = parser.set_language(&language); let tree = parser.parse(file, None).unwrap(); let cursor = tree.walk(); let program = ast::Program::new().from_str(program).unwrap(); - let mut ctx = Context::new(tree_sitter_md::language()) + let mut ctx = Context::new(language) .with_input(file.to_owned()) .with_cursor(cursor) .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 @@ -/// TBSP: tree-based source processor +/// tree-based source processor #[derive(argh::FromArgs)] struct Cli { /// read the TBSP program source from a file @@ -14,42 +14,43 @@ struct Cli { file: Option, } -fn main() { - let cli: Cli = argh::from_env(); - - let program = std::fs::read_to_string(&cli.program_file).unwrap_or_else(|e| { - eprintln!( - "failed to read program-file from `{}`: {e}", - cli.program_file.display() - ); - std::process::exit(-1); - }); - - let language = match cli.language.as_str() { - "md" => tree_sitter_md::language(), - "typescript" => tree_sitter_typescript::language_typescript(), - "javascript" => tree_sitter_javascript::language(), - "python" => tree_sitter_python::language(), - "rust" => tree_sitter_rust::language(), - lang => { - eprintln!("unknown language `{lang}`"); +impl Cli { + fn program(&self) -> String { + std::fs::read_to_string(&self.program_file).unwrap_or_else(|e| { + eprintln!( + "failed to read program-file from `{}`: {e}", + self.program_file.display() + ); std::process::exit(-1); - } - }; + }) + } - let file = cli - .file - .map(std::fs::read_to_string) - .unwrap_or_else(try_consume_stdin) - .unwrap_or_else(|e| { - eprintln!("{e}"); - std::process::exit(-1) - }); + fn language(&self) -> tree_sitter::Language { + match self.language.as_str() { + "md" => tree_sitter_md::language(), + "typescript" => tree_sitter_typescript::language_typescript(), + "javascript" => tree_sitter_javascript::language(), + "python" => tree_sitter_python::language(), + "rust" => tree_sitter_rust::language(), + lang => { + eprintln!("unknown language `{lang}`"); + std::process::exit(-1); + } + } + } - trawk::evaluate(&file, &program, language).unwrap_or_else(|e| { - eprintln!("{e:?}"); - std::process::exit(-1); - }); + fn file(&self) -> String { + match self.file.as_ref() { + Some(f) => std::fs::read_to_string(f).unwrap_or_else(|e| { + eprintln!("failed to read input-file from `{}`: {e}", f.display()); + std::process::exit(-1); + }), + None => try_consume_stdin().unwrap_or_else(|e| { + eprintln!("failed to read input-file from stdin: {e}"); + std::process::exit(-1); + }), + } + } } fn try_consume_stdin() -> std::io::Result { @@ -69,114 +70,11 @@ fn try_consume_stdin() -> std::io::Result { } } -// fn main() { -// let src = r#" -// # foo1 -// -// bar -// -// ## foo1.1 -// -// bar baz -// -// # foo2 -// -// bar baz -// -// ``` -// fn main() { -// } -// ``` -// -// - foo -// - bar -// - baz -// -// "# -// .to_owned(); -// -// let program = Program::new() -// .from_str( -// r#" -// BEGIN { -// int depth = 0; -// -// print("\n"); -// print("\n"); -// } -// -// enter section { -// depth += 1; -// } -// leave section { -// depth -= 1; -// } -// -// enter atx_heading { -// print(""); -// } -// leave atx_heading { -// print("\n"); -// } -// -// enter paragraph { -// print("

"); -// } -// leave paragraph { -// print("

\n"); -// } -// -// enter list { -// print("
    "); -// } -// leave list { -// print("
\n"); -// } -// -// enter list_item { -// print("
  • "); -// } -// leave list_item { -// print("
  • \n"); -// } -// -// enter fenced_code_block { -// print("
    ");
    -//     }
    -//     leave fenced_code_block {
    -//         print("
    \n"); -// } -// -// enter inline { -// print(text(node)); -// } -// enter code_fence_content { -// print(text(node)); -// } -// -// END { -// print("\n"); -// print("\n"); -// } -// "#, -// ) -// .unwrap(); -// -// let mut parser = tree_sitter::Parser::new(); -// let _ = parser.set_language(&tree_sitter_md::language()); -// -// let tree = parser.parse(&src, None).unwrap(); -// let cursor = tree.walk(); -// -// let mut ctx = Context::new(tree_sitter_md::language()) -// .with_input(src) -// .with_cursor(cursor) -// .with_program(program) -// .unwrap(); -// -// let _ = ctx.eval(); -// } +fn main() { + let cli: Cli = argh::from_env(); + + tbsp::evaluate(&cli.file(), &cli.program(), cli.language()).unwrap_or_else(|e| { + eprintln!("{e:?}"); + std::process::exit(-1); + }); +} -- cgit v1.2.3