Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
migrate to ctutils
Browse files Browse the repository at this point in the history
  • Loading branch information
Thepigcat76 committed Nov 30, 2023
1 parent 966292e commit a6e3c63
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ keywords = ["rust", "nexus", "scripting", "language"]
homepage = "https://muffingroup.github.io/MuffinSite/"

[dependencies]
clutils = "0.1.0"
clutils = "0.0.1"
colored = "2.0.4"
maplit = "1.0.2"
9 changes: 5 additions & 4 deletions src/lexer/lexer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use clutils::file_handler::FileHandler;

use crate::{
lexer::tokens::{Token, TokenType},
util::{FileHandler, FirstAsChar},
util::FirstAsChar,
};

/// Same as tokens.push() but reduces boilerplate
Expand All @@ -16,7 +18,6 @@ macro_rules! push_token {
/// Lexer struct containing
/// necessary info to
/// construct the parser
#[derive(Clone)]
pub struct Lexer {
pub input: FileHandler,
pub current_pos: usize,
Expand All @@ -31,7 +32,7 @@ pub struct Lexer {
impl Lexer {
/// Constructs lexer from FileHandler
pub fn new(input: FileHandler) -> Self {
let input_chars: Vec<char> = input.file_content.chars().collect();
let input_chars: Vec<char> = input.content.chars().collect();
if input_chars.len() > 0 {
return Lexer {
input: input,
Expand All @@ -50,7 +51,7 @@ impl Lexer {
Some(alt) => {
alt.chars().collect()
},
None => self.input.file_content.chars().collect(),
None => self.input.content.chars().collect(),
};
let mut tokens: Vec<Token> = Vec::new();

Expand Down
38 changes: 38 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use clutils::{file_handler::{Extension, FileHandler}, literal::Literal};
use evaluator::evaluator::Evaluator;
use lexer::lexer::Lexer;
use parser::parser::Parser;

mod builtin;
mod evaluator;
mod lexer;
mod parser;
mod util;

pub enum NexusExtensions {
NX,
}

impl Literal for NexusExtensions {
fn literal(self: &Self) -> &str {
"nx"
}
}

impl Extension for NexusExtensions {

}

pub fn run_interpreter(src: FileHandler) {
let mut lexer = Lexer::new(src);

let mut parser = match Parser::new(&mut lexer, true) {
Ok(parser) => parser,
Err(_) => todo!(),
};

let ast = parser.parse_program();

let mut evaluator = Evaluator::new();
evaluator.eval_program(ast);
}
32 changes: 5 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
mod builtin;
mod evaluator;
mod lexer;
mod parser;
mod util;

use std::env;

use lexer::lexer::Lexer;
use parser::parser::Parser;
use util::FileHandler;

use crate::evaluator::evaluator::Evaluator;
use clutils::file_handler::FileHandler;
use nexus::NexusExtensions;

// nexus run <File>

Expand All @@ -31,23 +22,10 @@ fn main() {

match first_arg.as_str() {
"run" => {
let src = FileHandler::read_file(&second_arg);
run_interpreter(src)
// TODO: allowing passing references to file handler
let src = FileHandler::new_with_extension(second_arg.clone(), Box::new(NexusExtensions::NX)).expect("Failed to handle file");
nexus::run_interpreter(src)
},
_ => todo!(),
};
}

fn run_interpreter(src: FileHandler) {
let mut lexer = Lexer::new(src);

let mut parser = match Parser::new(&mut lexer, true) {
Ok(parser) => parser,
Err(_) => todo!(),
};

let ast = parser.parse_program();

let mut evaluator = Evaluator::new();
evaluator.eval_program(ast);
}
7 changes: 5 additions & 2 deletions src/parser/parser.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use std::process;

use clutils::file_handler::FileHandler;

use crate::{
builtin::errors::{Error, *},
lexer::{
lexer::Lexer,
tokens::{Token, TokenType},
},
parser::ast::*,
util::{self, FileHandler},
util,
};

/// Parser struct containing
/// necessary info to
/// construct Evaluator
Expand Down Expand Up @@ -815,7 +818,7 @@ impl<'a> Parser<'a> {
let msg = format!(
"{} error at: {}:{}:{}",
message,
self.file_handler.file_path,
self.file_handler.full_path,
self.line_count,
self.peek_token.cur_pos + 1,
);
Expand Down
29 changes: 0 additions & 29 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ use colored::Colorize;

use crate::evaluator::object::Error;

/// Handles files and stores file path
/// for error messages
#[derive(Clone)]
pub struct FileHandler {
pub file_path: String,
pub file_content: String,
}

pub trait FirstAsChar {
/// Returns first character of a string as a char.
/// Most useful when converting string with only
Expand All @@ -35,27 +27,6 @@ impl<T> ToSome<T> for T {
}
}

impl FileHandler {
/// Constructs FileHandler from file path
pub fn read_file(path: &str) -> FileHandler {
let mut file = File::open(path).expect(format!("Failed to find file: {}", path).as_str());
let mut buffer = String::new();
let file_ending = match path.split('.').last() {
Some(ending) => ending,
None => "unknown",
};

if file_ending != "nx" {
panic!("Wrong file format. Current: {}, expected: nx", file_ending);
}

file.read_to_string(&mut buffer)
.expect("Failed to read file");

FileHandler { file_path: path.to_string(), file_content: buffer }
}
}

/// Accept input from the console
pub fn input() -> String {
io::stdout().flush().expect("Failed to flush output");
Expand Down
14 changes: 1 addition & 13 deletions test/test.nx
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
<<<<<<< HEAD
foo :: func(x) {
print(x)
}

foo(0)
=======
var x = [0, 0]

loop i in 0..10 {
print(i)
}
>>>>>>> parent of 9811e27 (rewrite enviroments)
print("Good morning!")

0 comments on commit a6e3c63

Please sign in to comment.