Skip to content

Commit

Permalink
work on the vars and new tokenizer
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavier2p committed Sep 26, 2023
1 parent adc110c commit a693b30
Show file tree
Hide file tree
Showing 17 changed files with 227 additions and 301 deletions.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion snippets/simple-code.f90 → fragments/simple-code.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ program nearlyuseless

temperature = 98.6
cows = 9
name = " dadsfdfadsfa &
& . fsdafasdfasdd"

print *, "There are ", cows, " cows outside."
print *, "You are probably ", temperature, " right now"
print *, "You are probably ",temperature, " right now"
end program
1 change: 0 additions & 1 deletion src/builtins/mod.rs

This file was deleted.

21 changes: 12 additions & 9 deletions src/helpers/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::{Parser, Subcommand};
use std::path::Path;

/// This struct is used to parse the arguments passed to the program.
#[derive(Debug, Parser, Clone)]
#[derive(Parser, Clone)]
#[command(author, version, about)]
#[command(subcommand_required = true)]
pub struct Cli {
Expand Down Expand Up @@ -38,7 +38,7 @@ pub enum Commands {
file: String,

/// Threat `Warning` as `Error`
#[arg(long)]
#[arg(long, short = 'W')]
werror: bool,

/// Print the comment during the execution of the program
Expand All @@ -57,13 +57,14 @@ impl Cli {
}

/// This function returns the value of the `verbose` argument.
// pub fn get_verbose(&self) -> bool {
// self.verbose
// || match &self.command {
// Commands::Run { verbose, .. } => *verbose,
// Commands::Check { verbose, .. } => *verbose,
// }
// }
#[allow(dead_code)]
pub fn get_verbose(&self) -> bool {
self.verbose
|| match &self.command {
Commands::Run { verbose, .. } => *verbose,
Commands::Check { verbose, .. } => *verbose,
}
}

/// This function returns the value of the `werror` argument.
pub fn get_werror(&self) -> bool {
Expand All @@ -78,6 +79,8 @@ impl Cli {
&self.command
}

/// This function prints the `Cli` struct for debug purposes.
/// Not available in release mode.
pub fn debug(&self) {
println!("Cli {{");
println!(" command: {:?}", self.get_command());
Expand Down
16 changes: 9 additions & 7 deletions src/helpers/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ pub enum Error {
UnknownToken,
UnexpectedToken,
Critical,
TooCharacters,
}

fn error_to_string(error: Error) -> String {
fn error_to_string(error: &Error) -> &'static str {
match error {
Error::Syntax => "Syntax",
Error::NotImplemented => "NotImplemented",
Expand All @@ -22,11 +23,11 @@ fn error_to_string(error: Error) -> String {
Error::UnknownToken => "UnknownToken",
Error::UnexpectedToken => "UnexpectedToken",
Error::Critical => "Critical",
Error::TooCharacters => "TooCharacters",
}
.to_string()
}

fn get_code_number(kind: Error) -> i32 {
fn get_code_number(kind: Error) -> u8 {
match kind {
Error::Syntax => 1,
Error::NotImplemented => 2,
Expand All @@ -35,10 +36,11 @@ fn get_code_number(kind: Error) -> i32 {
Error::UnknownToken => 1,
Error::UnexpectedToken => 1,
Error::Critical => 2,
Error::TooCharacters => 1,
}
}

fn header(program: Program, kind: Error, is_warning: bool) -> String {
fn header(program: &Program, kind: &Error, is_warning: bool) -> String {
let kind_colored = if is_warning {
error_to_string(kind).yellow()
} else {
Expand All @@ -59,14 +61,14 @@ fn header(program: Program, kind: Error, is_warning: bool) -> String {
}

pub fn raise(program: &Program, kind: Error, message: String) {
let header: String = header(program.clone(), kind.clone(), false);
let header: String = header(program, &kind, false);
let code = get_code_number(kind);
eprintln!("{} [Code {}]\nDetails: {}", header, code, message);
// std::process::exit();
}

pub fn warn(program: &Program, kind: Error, message: String) {
let header: String = header(program.clone(), kind.clone(), true);
let code = get_code_number(kind);
let header: String = header(program, &kind, true);
let code: u8 = get_code_number(kind);
println!("{} [Code {}]\nDetails: {}", header, code, message);
}
10 changes: 3 additions & 7 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
//! The lexer is the first step of the compilation process. It takes the source code and converts it into tokens.
use crate::{
helpers::errors::{self, Error},
print::print_to_stdout,
program::Program,
tokens::Token,
utils::print::print_to_stdout,
variables, VERBOSE,
};
use colored::Colorize;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub fn lexer(program: &mut Program) {
Error::UnexpectedToken,
format!(
"Expected `END {}`, got `END {}`",
stack.last().unwrap().get_name().to_ascii_uppercase(),
stack.last().unwrap().debug().to_ascii_uppercase(),
line.get(index + 1)
.unwrap()
.get_value()
Expand All @@ -58,11 +58,7 @@ pub fn lexer(program: &mut Program) {
_ => errors::warn(
program,
Error::UnexpectedToken,
format!(
"Unexpected token {} `{}`",
token.get_name(),
token.get_value()
),
format!("Unexpected token {} `{}`", token.debug(), token.get_value()),
),
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ mod ast;
mod helpers;
mod lexer;
mod parser;
mod print;
mod program;
mod tokenizer;
mod tokens;
mod utils;
mod variables;
mod verbose;

Expand All @@ -34,7 +35,7 @@ use clap::Parser;
static VERBOSE: bool = true;

fn main() {
let args: cli::Cli = cli::Cli::parse();
let args = cli::Cli::parse();

if VERBOSE {
args.debug();
Expand All @@ -55,6 +56,12 @@ fn main() {
file.debug();
}

let tokenized = tokenizer::tokenizer(&file);

if VERBOSE {
tokenized.debug();
}

let mut program = parser::parser(file);

if VERBOSE {
Expand Down
186 changes: 0 additions & 186 deletions src/old/ast-old.rs

This file was deleted.

Loading

0 comments on commit a693b30

Please sign in to comment.