Skip to content

Commit

Permalink
[IR] trying to fix a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Sep 3, 2024
1 parent 84a094f commit 08d7a0d
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 21 deletions.
77 changes: 66 additions & 11 deletions src/IR/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,30 @@ pub enum TokenType {
Func(String),
}

impl TokenType {
pub(crate) fn name(&self) -> String {
match self {
TokenType::Dot => "dot",
TokenType::Const => "const",
TokenType::Comma => "comma",
TokenType::Var(_) => "var",
TokenType::Equal => "equal",
TokenType::LParam => "lparam",
TokenType::RParam => "rparam",
TokenType::LBracket => "lbracket",
TokenType::RBracket => "rbracket",
TokenType::LSquare => "lsquare",
TokenType::RSquare => "rsquare",
TokenType::Ident(_) => "ident",
TokenType::String(_) => "string",
TokenType::Int(_) => "int",
TokenType::Declare => "declare",
TokenType::Define => "define",
TokenType::Func(_) => "func",
}.to_string()
}
}

/// An ir token
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Token {
Expand All @@ -96,6 +120,8 @@ pub struct IrLexer {

loc: Loc,

no_pop: bool,

keywords: HashMap<String, TokenType>,

/// The output
Expand Down Expand Up @@ -129,11 +155,13 @@ impl IrLexer {
},

out: vec![],

no_pop: false,
}
}

fn is_at_end(&self) -> bool {
self.current >= (self.input_stream.chars().count() - 1) as u64
self.current >= (self.input_stream.chars().count()) as u64
}

fn update_loc(&mut self) {
Expand All @@ -149,25 +177,34 @@ impl IrLexer {
}

fn advance(&mut self) -> Result<char, IrError> {
if !self.no_pop {
self.current += 1;
}
self.current += 1;
let peek = self.peek();

let mut out = ' ';

if let Some(peek) = peek {
if peek == '\n' {
self.coloumn = 1;
self.coloumn = 0;
self.line_no += 1;

self.update_line_string();
} else {
self.coloumn += 1;
if !self.no_pop {
self.coloumn += 1;
}
out = peek;
}
} else {
Err(IrError::OutOfChars)?
}

if self.no_pop {
self.no_pop = false;
}

self.loc.length = self.current - self.start - 1;

Ok(out)
Expand Down Expand Up @@ -201,6 +238,7 @@ impl IrLexer {
'(' => ty = Some(TokenType::LParam),
'{' => ty = Some(TokenType::LBracket),
'[' => ty = Some(TokenType::LSquare),

')' => ty = Some(TokenType::RParam),
'}' => ty = Some(TokenType::RBracket),
']' => ty = Some(TokenType::RSquare),
Expand Down Expand Up @@ -261,6 +299,10 @@ impl IrLexer {

_ => looping = false,
}

if looping {
self.advance()?;
}
}

Ok(TokenType::Var(out))
Expand All @@ -286,6 +328,10 @@ impl IrLexer {

_ => out.push(chr),
}

if looping {
self.advance()?;
}
}

Ok(TokenType::String(out))
Expand Down Expand Up @@ -343,7 +389,7 @@ impl IrLexer {
})?
}

let chr = self.advance()?;
let chr = self.peek().unwrap();

match chr {
'0'..='9' => string.push(chr),
Expand All @@ -352,6 +398,10 @@ impl IrLexer {

_ => looping = false,
}

if looping {
self.advance()?;
}
}

let mut negate = false;
Expand Down Expand Up @@ -380,8 +430,6 @@ impl IrLexer {
fn scan_func(&mut self) -> Result<TokenType, IrError> {
let mut out = String::new();

out.push( self.peek().unwrap() );

let mut looping = true;

while looping {
Expand All @@ -392,18 +440,25 @@ impl IrLexer {
})?
}

let chr = self.advance()?;
let chr = self.peek().unwrap();

match chr {
'0'..='9' => out.push(chr),
'a'..='z' => out.push(chr),
'A'..='Z' => out.push(chr),
'_' => out.push(chr),
'0'..='9' => out.push( chr ),
'a'..='z' => out.push( chr ),
'A'..='Z' => out.push( chr ),
'@' => out.push('@'),
'_' => out.push( '_' ),

_ => looping = false,
}

if looping {
self.advance()?;
}
}

self.no_pop = true;

Ok(TokenType::Func(out))
}
}
53 changes: 50 additions & 3 deletions src/IR/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,36 @@ pub enum IrError {
loc: Loc,
/// The box of the error
err: Box<dyn Error>,
}
},

/// Expected token
ExpectedTokenButFoundAnUnexpectedOne{
/// the token which was found
found: lexer::Token,
/// the token which was expected
expected: lexer::Token
},

/// A unkown type
UnkownType(lexer::Token),
}

impl Display for IrError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
IrError::UnexpectedToken(token) => {
let mut fab = Support::Error::new("", "", token.loc.line.to_string(), token.loc.coloumn.to_string());
let mut fab = Support::Error::new("unexpected token", "", token.loc.line.to_string(), token.loc.coloumn.to_string());

fab.deactivateLocationDisplay();

fab.setCodeLine(token.loc.line_string.to_string());
fab.addWhere("unexpected token", token.loc.coloumn, token.loc.length);
let mut length = token.loc.length;

if 0 == length {
length = 1;
}

fab.addWhere("unexpected token", token.loc.coloumn, length);

fab.to_string()
},
Expand Down Expand Up @@ -122,6 +139,36 @@ impl Display for IrError {

fab.to_string()
}

IrError::ExpectedTokenButFoundAnUnexpectedOne{found, expected} => {
let mut fab = Support::Error::new("expected a specific token but found another one", "", found.loc.line.to_string(), found.loc.coloumn.to_string());

fab.deactivateLocationDisplay();

fab.setCodeLine(found.loc.line_string.to_string());

let mut length = found.loc.length;

if 0 == length {
length = 1;
}

fab.addWhere(format!("expected following token: {:?} but found {:?}", expected.typ.name(), found.typ), found.loc.coloumn, length);

fab.to_string()
}

IrError::UnkownType(typ) => {
let mut fab = Support::Error::new("unknown type", "", typ.loc.line.to_string(), typ.loc.coloumn.to_string());

fab.deactivateLocationDisplay();

fab.setCodeLine(typ.loc.line_string.to_string());
fab.addWhere("unkown type", typ.loc.coloumn, typ.loc.length);

fab.to_string()

}
})
}
}
Expand Down
Loading

0 comments on commit 08d7a0d

Please sign in to comment.