latest pushes

This commit is contained in:
2024-09-28 22:55:56 -04:00
parent 2ce6807484
commit eaa2609230
7 changed files with 875 additions and 36 deletions

View File

@@ -1,47 +1,20 @@
// Copyright (C) 2024 Bellande Architecture Mechanism Research Innovation Center, Ronaldson Bellande
mod interpreter;
mod lexer;
mod parser;
mod utilities;
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
use crate::interpreter::interpreter::Interpreter;
use crate::lexer::lexer::Lexer;
use crate::parser::parser::Parser;
use crate::utilities::Token;
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#[allow(dead_code)]
pub mod Interpreter {
mod interpreter;
}
pub mod Lexer {
mod lexer;
}
pub mod Parser {
mod parser;
}
pub mod utilities;
use glob::glob;
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::{self, Read, Write};
use std::os::unix::io::AsRawFd;
use std::process::{Child, Command, Stdio};
use std::sync::{Arc, Mutex};
use std::thread;
use bellande_rust_importer::{from_import, import, Importer};
use std::path::PathBuf;
use std::io::{self, BufRead, Write};
fn main() -> Result<(), String> {
let mut interpreter = Interpreter::new();
let args: Vec<String> = env::args().collect();
if args.len() > 1 {
// Execute script file
@@ -53,3 +26,72 @@ fn main() -> Result<(), String> {
}
Ok(())
}
fn execute_script(interpreter: &mut Interpreter, filename: &str) -> Result<(), String> {
let file =
File::open(filename).map_err(|e| format!("Error opening file {}: {}", filename, e))?;
let reader = io::BufReader::new(file);
let mut lines = reader.lines();
// Check for shebang
if let Some(Ok(first_line)) = lines.next() {
if !first_line.starts_with("#!") {
// If no shebang, process this line
process_line(interpreter, &first_line, 1)?;
}
}
// Process remaining lines
for (line_num, line) in lines.enumerate() {
let line = line.map_err(|e| format!("Error reading line: {}", e))?;
process_line(interpreter, &line, line_num + 2)?;
}
Ok(())
}
fn process_line(interpreter: &mut Interpreter, line: &str, line_num: usize) -> Result<(), String> {
let trimmed_line = line.trim();
if trimmed_line.is_empty() || trimmed_line.starts_with('#') {
return Ok(()); // Skip empty lines and comments
}
let lexer = Lexer::new(line.to_string());
let tokens: Vec<Token> = lexer.into_iter().collect();
let mut parser = Parser::new(tokens);
match parser.parse() {
Ok(ast) => {
if let Err(e) = interpreter.interpret(ast) {
eprintln!("Error on line {}: {}", line_num, e);
}
}
Err(e) => eprintln!("Parse error on line {}: {}", line_num, e),
}
Ok(())
}
fn run_interactive_mode(interpreter: &mut Interpreter) -> Result<(), String> {
loop {
print!("bellos> ");
io::stdout().flush().unwrap();
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
if input.trim().is_empty() {
continue;
}
let lexer = Lexer::new(input);
let tokens: Vec<Token> = lexer.into_iter().collect();
let mut parser = Parser::new(tokens);
match parser.parse() {
Ok(ast) => {
if let Err(e) = interpreter.interpret(ast) {
eprintln!("Error: {}", e);
}
}
Err(e) => eprintln!("Parse error: {}", e),
}
}
}