Skip to content

Commit

Permalink
parsing added for external items
Browse files Browse the repository at this point in the history
  • Loading branch information
JCBurnside committed Mar 1, 2024
1 parent c26549d commit 24a77dc
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 86 deletions.
18 changes: 13 additions & 5 deletions compiler/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ pub struct ArgDeclaration {
pub ty: Option<ResolvedType>,
}

#[derive(PartialEq,Debug)]
pub struct Abi {
pub loc : crate::Location,
pub identifier : String,
}

#[derive(PartialEq, Debug)]
pub struct ValueDeclaration {
Expand All @@ -197,7 +202,7 @@ pub struct ValueDeclaration {
pub ty: Option<ResolvedType>,
pub value: ValueType,
pub generictypes: Option<GenericsDecl>,
pub abi : Option<String>,
pub abi : Option<Abi>,
}
impl ValueDeclaration {
fn replace(&mut self, nice_name: &str, actual: &str) {
Expand Down Expand Up @@ -228,21 +233,23 @@ impl ValueDeclaration {
pub enum ValueType {
Expr(Expr),
Function(Vec<Statement>),
External,
}
impl ValueType {
fn replace(&mut self, nice_name: &str, actual: &str) {
match self {
ValueType::Expr(expr) => expr.replace(nice_name, actual),
ValueType::Function(stmnts) => stmnts
Self::Expr(expr) => expr.replace(nice_name, actual),
Self::Function(stmnts) => stmnts
.iter_mut()
.for_each(|it| it.replace(nice_name, actual)),
Self::External => (),
}
}

fn get_dependencies(&self, known_values: Vec<String>) -> HashSet<String> {
match self {
ValueType::Expr(expr) => expr.get_dependencies(known_values),
ValueType::Function(stmnts) => {
Self::Expr(expr) => expr.get_dependencies(known_values),
Self::Function(stmnts) => {
stmnts
.iter()
.fold(
Expand All @@ -257,6 +264,7 @@ impl ValueType {
)
.1
}
Self::External => HashSet::new(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/src/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl Context {
.map(|stmnt| self.assign_ids_stmnt(stmnt))
.collect(),
),
untyped_ast::ValueType::External => todo!(),
};
ast::ValueDeclaration {
loc,
Expand Down
65 changes: 28 additions & 37 deletions compiler/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ macro_rules! operators {

use itertools::Itertools;

use crate::tokens::Token;
use crate::{tokens::Token, util::ExtraIterUtils};
impl<I: Iterator<Item = char> + Clone> Lexer<Peekable<I>> {
pub fn new<II: IntoIterator<IntoIter = I>>(source: II) -> Self {
Self {
Expand Down Expand Up @@ -210,9 +210,7 @@ impl<I: Iterator<Item = char> + Clone> Lexer<Peekable<I>> {
.clone()
.take_while(|c| (c.is_numeric()) || c == &'_' || c == &'.')
.collect();
for _ in 0..inner.len() {
self.source_stream.next();
}
ExtraIterUtils::advance_by(&mut self.source_stream, inner.len());
self.curr_col += inner.len();
let inner = c.to_string() + &inner;
(Token::FloatingPoint(false, inner), (self.curr_line, start_col))
Expand Down Expand Up @@ -241,17 +239,24 @@ impl<I: Iterator<Item = char> + Clone> Lexer<Peekable<I>> {
self.curr_col += 2;
(Token::For, (self.curr_line, start_col))
}

'e' if self
.source_stream
.clone()
.take_while(ident_char)
.collect::<String>() == "xtern" =>
{
ExtraIterUtils::advance_by(&mut self.source_stream, 5);
self.curr_col+=5;
(Token::Extern, (self.curr_line, start_col))
}
't' if self
.source_stream
.clone()
.take_while(ident_char)
.collect::<String>()
== "rue" =>
{
for _ in 0..3 {
let _ = self.source_stream.next();
}
ExtraIterUtils::advance_by(&mut self.source_stream, 3);
self.curr_col += 3;
(Token::True, (self.curr_line, start_col))
}
Expand All @@ -262,9 +267,7 @@ impl<I: Iterator<Item = char> + Clone> Lexer<Peekable<I>> {
.collect::<String>()
== "alse" =>
{
for _ in 0..4 {
let _ = self.source_stream.next();
}
ExtraIterUtils::advance_by(&mut self.source_stream, 4);
self.curr_col += 4;
(Token::False, (self.curr_line, start_col))
}
Expand All @@ -275,9 +278,7 @@ impl<I: Iterator<Item = char> + Clone> Lexer<Peekable<I>> {
.collect::<String>()
== "atch" =>
{
for _ in 0..4 {
let _ = self.source_stream.next();
}
ExtraIterUtils::advance_by(&mut self.source_stream, 4);
self.curr_col += 4;
(Token::Match, (self.curr_line, start_col))
}
Expand All @@ -289,9 +290,7 @@ impl<I: Iterator<Item = char> + Clone> Lexer<Peekable<I>> {
.collect::<String>()
== "here" =>
{
for _ in 0..4 {
let _ = self.source_stream.next();
}
ExtraIterUtils::advance_by(&mut self.source_stream, 4);
self.curr_col += 4;
(Token::Where, (self.curr_line, start_col))
}
Expand All @@ -309,9 +308,7 @@ impl<I: Iterator<Item = char> + Clone> Lexer<Peekable<I>> {
.collect::<String>()
== "hen" =>
{
let _ = self.source_stream.next();
let _ = self.source_stream.next();
let _ = self.source_stream.next();
ExtraIterUtils::advance_by(&mut self.source_stream, 3);
self.curr_col += 3;
(Token::Then, (self.curr_line, start_col))
}
Expand All @@ -323,9 +320,7 @@ impl<I: Iterator<Item = char> + Clone> Lexer<Peekable<I>> {
.collect::<String>()
== "lse" =>
{
let _ = self.source_stream.next();
let _ = self.source_stream.next();
let _ = self.source_stream.next();
ExtraIterUtils::advance_by(&mut self.source_stream, 3);
self.curr_col += 3;
(Token::Else, (self.curr_line, start_col))
}
Expand All @@ -337,8 +332,7 @@ impl<I: Iterator<Item = char> + Clone> Lexer<Peekable<I>> {
.collect::<String>()
== "et" =>
{
self.source_stream.next();
self.source_stream.next();
ExtraIterUtils::advance_by(&mut self.source_stream, 2);
self.curr_col += 2;
(Token::Let, (self.curr_line, start_col))
}
Expand All @@ -350,9 +344,7 @@ impl<I: Iterator<Item = char> + Clone> Lexer<Peekable<I>> {
== "eturn" =>
{
// self.source_stream.advance_by(5).unwrap();
for _ in 0..5 {
self.source_stream.next();
}
ExtraIterUtils::advance_by(&mut self.source_stream, 5);
self.curr_col += 5;
(Token::Return, (self.curr_line, start_col))
}
Expand All @@ -363,9 +355,7 @@ impl<I: Iterator<Item = char> + Clone> Lexer<Peekable<I>> {
.collect::<String>()
== "ype" =>
{
for _ in 0..3 {
self.source_stream.next();
}
ExtraIterUtils::advance_by(&mut self.source_stream, 3);
self.curr_col += 3;
(Token::Type, (self.curr_line, start_col))
}
Expand All @@ -376,9 +366,7 @@ impl<I: Iterator<Item = char> + Clone> Lexer<Peekable<I>> {
.collect::<String>()
== "num" =>
{
for _ in 0..3 {
self.source_stream.next();
}
ExtraIterUtils::advance_by(&mut self.source_stream, 3);
self.curr_col += 3;
(Token::Enum, (self.curr_line, start_col))
}
Expand All @@ -390,9 +378,7 @@ impl<I: Iterator<Item = char> + Clone> Lexer<Peekable<I>> {
.take_while(|c| (c.is_alphanumeric()) || c == &'_')
.collect();
// self.source_stream.advance_by(,inner.len()).unwrap();
for _ in 0..inner.len() {
self.source_stream.next();
}
ExtraIterUtils::advance_by(&mut self.source_stream, inner.len());
self.curr_col += inner.len();
let inner = c.to_string() + &inner;
if inner.chars().all(|c| c.is_numeric()) {
Expand Down Expand Up @@ -684,6 +670,11 @@ let match_expr_with_block x : int32 -> int32 = match x where
[Token::ArrayClose, Token::EoF],
"array close ]"
);
assert_eq!(
TokenStream::from_source("extern").map(fst).collect_vec(),
[Token::Extern, Token::EoF],
""
)
}

#[test]
Expand Down
Loading

0 comments on commit 24a77dc

Please sign in to comment.