Skip to content

Commit

Permalink
[IR] finishing parser (for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Sep 7, 2024
1 parent 3db3802 commit 5205534
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 22 deletions.
23 changes: 18 additions & 5 deletions src/IR/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ pub enum TokenType {

/// @func_name
Func(String),

/// block:
Block(String),
}

impl TokenType {
Expand All @@ -95,6 +98,7 @@ impl TokenType {
TokenType::Define => "define",
TokenType::Func(_) => "func",
TokenType::TripleDot => "...",
TokenType::Block(_) => "block",
}.to_string()
}
}
Expand Down Expand Up @@ -155,7 +159,7 @@ impl IrLexer {

lines: lines,

coloumn: 1,
coloumn: 0,
start: 0,
current: 0,

Expand Down Expand Up @@ -221,7 +225,7 @@ impl IrLexer {
}

if self.current != self.start {
self.loc.length = self.current - self.start - 1;
self.loc.length = self.current - self.start;
} else {
self.loc.length = 1;
}
Expand Down Expand Up @@ -341,8 +345,6 @@ impl IrLexer {
_ => looping = false,
}

println!("{}", chr);

if looping {
self.advance()?;
}
Expand Down Expand Up @@ -386,6 +388,7 @@ impl IrLexer {
let mut out = String::new();

let mut looping = true;
let mut block = false;

while looping {
if self.is_at_end() {
Expand All @@ -403,6 +406,10 @@ impl IrLexer {
'A'..='Z' => out.push(chr),
'_' => out.push(chr),

':' => {
looping = false;
block = true;
}

_ => looping = false,
};
Expand All @@ -412,7 +419,13 @@ impl IrLexer {
}
}

self.no_pop = true;
if !block {
self.no_pop = true;
}

if block {
return Ok(TokenType::Block(out));
}

if let Some(keyword) = self.keywords.get(&out) {
Ok(keyword.clone())
Expand Down
115 changes: 99 additions & 16 deletions src/IR/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::{HashMap, VecDeque};

use crate::prelude::Ir;
use crate::Obj::Linkage;
use crate::IR::{self, ir, Const, Type, TypeMetadata, Var};
use crate::IR::{ir, Const, FnTy, Function, Type, TypeMetadata, Var};

use super::lexer::{Loc, Token, TokenType};
use super::IrError;
Expand Down Expand Up @@ -36,7 +36,7 @@ pub enum IrStmt {
Func{
name: String,
ret: TypeMetadata,
args: HashMap<String, TypeMetadata>,
args: (HashMap<String, TypeMetadata>, /*unlim args*/bool),
body: HashMap<String, IrBlock>,
scope: Linkage,
},
Expand Down Expand Up @@ -115,13 +115,21 @@ impl IrParser {

self.input.pop_front();

let mut unlim = false;

loop {
let current = self.current_token()?;

if TokenType::RParam == current.typ {
break;
}

if TokenType::TripleDot == current.typ {
self.input.pop_front();
unlim = true;
break;
}

let var_type = self.parse_type()?;
self.input.pop_front();

Expand All @@ -139,15 +147,18 @@ impl IrParser {
};

args.insert(var_name, var_type );

self.input.pop_front();
}

self.expect(TokenType::RParam)?;
self.input.pop_front(); // the closing param )

Ok(IrStmt::Func {
name: name,
body: HashMap::new(),
scope: Linkage::Extern,
args: args,
args: (args, unlim),
ret: ret,
})
}
Expand Down Expand Up @@ -220,10 +231,12 @@ impl IrParser {
body.insert( name, block );
}

self.input.pop_front(); // }

Ok(IrStmt::Func {
name: name,
body: body,
args: args,
args: (args, false),
scope: Linkage::Extern,
ret: ret,
})
Expand Down Expand Up @@ -289,34 +302,29 @@ impl IrParser {
}

fn parse_block(&mut self) -> Result<(String, IrBlock), IrError> {
self.expect(TokenType::Ident(String::new()))?;
self.expect(TokenType::Block(String::new()))?;

let name;
let loc;
let curr_token = self.current_token()?;
if let TokenType::Ident(ident) = &curr_token.typ {
if let TokenType::Block(ident) = &curr_token.typ {
name = ident.to_string();
loc = curr_token.loc.clone();
} else { unreachable!() }

self.input.pop_front();

self.expect(TokenType::Dot)?;
self.input.pop_front();

println!("{:?}", self.input);

let mut body = vec![];


loop {
let curr = self.current_token()?;

if TokenType::RParam == curr.typ {
if TokenType::RBracket == curr.typ {
break;
}

if let TokenType::Ident(_) = curr.typ {
if let TokenType::Block(_) = curr.typ {
break;
}

Expand Down Expand Up @@ -364,7 +372,9 @@ impl IrParser {
"ret" => self.parse_ret()?,
_ => Err(IrError::UnkownInstrinc{loc: curr.loc.clone(), found: instrinc })?,
}
} else { todo!("error handling") }
} else {
Err(IrError::UnexpectedToken(curr.clone()))?
}
};

let loc = curr.loc;
Expand All @@ -377,7 +387,30 @@ impl IrParser {
}

fn parse_ret(&mut self) -> Result<Box<dyn Ir>, IrError> {
todo!()
self.input.pop_front(); // ret

let out_ty = self.parse_type()?;
self.input.pop_front();

let curr = self.current_token()?;

let out: Result<Box<dyn Ir>, IrError> = if let TokenType::Int(numeral) = &curr.typ {
Ok(ir::Return::new(Type::from_int(out_ty, *numeral)))
} else if let TokenType::Var(var) = &curr.typ {
Ok(ir::Return::new(Var {
name: var.to_owned(),
ty: out_ty,
}))
} else {
Err(IrError::UndeterminedTokenSequence {
loc: curr.loc.clone(),
expected: "ints, vars - for valid return nodes".to_owned(),
})
};

self.input.pop_front();

out
}

fn parse_const_assing(&mut self, var: String, ty: TypeMetadata) -> Result<Box<dyn Ir>, IrError> {
Expand Down Expand Up @@ -410,7 +443,57 @@ impl IrParser {
}

fn parse_call(&mut self, var: String) -> Result<Box<dyn Ir>, IrError> {
todo!()
self.input.pop_front(); // call

let func_ty = self.parse_type()?;
self.input.pop_front();

self.expect(TokenType::Ident(String::from("func name")))?;

let target = &self.current_token()?;

let target = if let TokenType::Ident(ident) = &target.typ {
ident.to_owned()
} else { unreachable!() };

self.input.pop_front(); // function name

let out = Var {
name: format!("{var}"),
ty: func_ty
};

let mut args = vec![];

loop {
let ty = if let TokenType::Ident(ty) = &self.current_token()?.typ {
if let Some(ty) = TypeMetadata::parse(ty.to_owned()) {
ty
} else {
break
}
} else { break; };

self.input.pop_front();

let var = self.current_token()?;

if let TokenType::Var(name) = &var.typ {
args.push(Var {
name: name.to_string(),
ty: ty
});
}

self.input.pop_front();
}

Ok(ir::Call::new(Function {
ty: FnTy(vec![], func_ty),
name: target,
linkage: Linkage::External,
blocks: VecDeque::new(),
}, args, out))
}

fn parse_data_array(&mut self) -> Result<Vec<u8>, IrError> {
Expand Down
2 changes: 1 addition & 1 deletion src/Support/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Error {
pub fn addWhere<T: ToString + Clone + Display + Into<String>>(&mut self, msg: T, col: u64, size: u64) {
let msg = msg.to_string();
let offset = format!(" {} | ", self.line).chars().count() as u64;
self.fmtLines.push(format!("{}{} {}", String::new().pad_to_len((offset + col + 1) as isize), "^".repeat(size as usize).red(), msg.gray()));
self.fmtLines.push(format!("{}{} {}", String::new().pad_to_len((offset + col) as isize), "^".repeat(size as usize).red(), msg.gray()));
}

/// Prints the error to stderr
Expand Down

0 comments on commit 5205534

Please sign in to comment.